Pattern / Racket: Bend with Boolean

(foldr + 0 '(1 2 3 4)) 

returns 10 what i expect but

 (foldr and false '(true true false)) 

gives an error

 and: expected an open parenthesis before and, but found none 

foldr accepts a function (which takes two parameters, since I have one list), and the base register, and list (s). I expect my second line of code will return true if the list has more than zero, and all of them are true, and that’s how I thought it would work. But apparently not.

+4
source share
2 answers

And this is a special form that will not work with foldr , which expects a procedure as a second argument. Try this instead:

 (foldr (lambda (xy) (and xy)) #t '(#t #t #f)) ;#t as base case 

Another alternative for this particular case would be to use andmap :

 (andmap identity '(#t #t #f)) 
+7
source

In a system, a schema, and , as a rule, are defined as a macro or a special form, and not as a procedure, therefore it does not work:

 #;1> + #<procedure C_plus> #;2> and Error: unbound variable: and 
0
source

Source: https://habr.com/ru/post/1401276/


All Articles