guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bug in and-map and or-map? What am I doing wrong?


From: Neil Jerram
Subject: Re: Bug in and-map and or-map? What am I doing wrong?
Date: 06 May 2001 10:38:03 +0100
User-agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7

>>>>> "Alejandro" == Alejandro Forero Cuervo <address@hidden> writes:

    Alejandro> I just discovered the `and-map' and `or-map'
    Alejandro> procedures, [...]

    Alejandro> Shouldn't they (`and-map' and `or-map') accept any
    Alejandro> number of lists (instead of just one list), [...]

    Alejandro> Am I getting something wrong, or is this really a bug?

It's not a bug, as there is no specification for and-map and or-map;
they are simply two utility procedures defined in boot-9.scm.
However, the behaviour that you describe is more useful, and back
compatible, so it's worth considering whether the and-map and or-map
procedures in boot-9.scm should be enhanced.

Here are two extended definitions that do what you want:

(define (and-map-multilist f . lsts)
  (if (apply eq? (map length lsts))
      (let loop ((result #t)
                 (ls lsts))
        (and result
             (or (and (null? (car ls))
                      result)
                 (loop (apply f (map car ls)) (map cdr ls)))))
      (error "Lists have different lengths")))

(define (or-map-multilist f . lsts)
  (if (apply eq? (map length lsts))
      (let loop ((result #f)
                 (ls lsts))
        (or result
            (and (not (null? (car ls)))
                 (loop (apply f (map car ls)) (map cdr ls)))))
      (error "Lists have different lengths")))

    Alejandro> So, now, if you really care about an and (or an or) of
    Alejandro> the elements in the list, you do:

    Alejandro> -=> (and-map equal? (list 1 2 3) (list 3 2 1) (list 2 2
    Alejandro> 2)) standard input::: In expression (and-map equal?
    Alejandro> (list 1 2 ...) ...): standard input::: Wrong number of
    Alejandro> arguments to #<procedure and-map (f lst)> ABORT:
    Alejandro> (wrong-number-of-args)

guile> (and-map-multilist equal? (list 1 2 3) (list 3 2 1) (list 2 2 2))
($ 1) => #f
guile> (or-map-multilist equal? (list 1 2 3) (list 3 2 1) (list 2 2 2))
($ 2) => #t

        Neil




reply via email to

[Prev in Thread] Current Thread [Next in Thread]