Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply logical and

I have an expression: (map some-proc some-list) which evaluates to, say, '(#f #f #f).

I want to check whether all booleans in this list are true. However, (and '(#f #f #f)) returns '(#f #f #f), while I heed #f.

Now, if I (apply and '(#f #f #f)), I get the error: and: bad syntax in: and in the DrRacket environment. That is confusing because the Racket Reference gives the example of (apply + '(1 2 3)) which seems to be identical to my problem.

What am I doing wrong and how I get my #f out of '(#f #f #f)?

like image 622
skobls Avatar asked Nov 30 '25 16:11

skobls


2 Answers

you can use andmap for this:

> (andmap (lambda (x) x) '(#f #f #f))
#f

The problem stems from the fact that and is not a procedure, but a macro, in order to avoid the evaluation of all of its arguments.

It would work in Lazy Racket, though, where and is a procedure.

like image 179
uselpa Avatar answered Dec 02 '25 06:12

uselpa


Just another way of doing it:

(foldr (lambda(x y) (and x y)) #t '(#f #f #f))

or I will rewrite uselpa's solution (andmap):

(andmap identity '(#f #f #f))
like image 29
ceth Avatar answered Dec 02 '25 05:12

ceth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!