Question

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

returns 10 which is what I expect, but

(foldr and false '(true true false))

gives me the error

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

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

Was it helpful?

Solution

And is a special form and won't work with foldr, which is expecting a procedure as its second argument. Try this instead:

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

Another alternative for this particular case would be using andmap:

(andmap identity '(#t #t #f))

OTHER TIPS

In a Scheme system, and is typically defined as a macro or special form and not as a procedure, which is why it doesn't work:

#;1> +
#<procedure C_plus>
#;2> and

Error: unbound variable: and
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top