Calling `and` on a list of arguments

Viewed 105

Is there a way to call and or or to a list of arguments? Like

(and (list #t #t #f))

?

I tried writing (apply and (list #t #t #f)) but that didn't work.

3 Answers

Try ormap and andmap

(ormap values (list #t #t #f))
(andmap values (list #t #t #f))

where values is essentially an identity function in this context.

The reason (apply and (list #t #t #f)) doesn't work is that and is not a function. It's a macro, which is necessary because (and #f (error 'foo)) should short-circuit (evaluate to #f). If it were a function, it would cause the error.

You could use any? and all? from the relation library (disclosure: I'm the author):

(require relation)

(any? (list #t #t #f)) ;=> #t
(all? (list #t #t #f)) ;=> #f

To build on a previous answer. A poor man's way of doing it would be.

 > (ormap (lambda (x) (not (false? x))) (list #t #t #f))
 ## #t

 > (andmap (lambda (x) (not (false? x))) (list #t #t #f))
 ## #f
Related