what does mean "scheme and: bad syntax in: and"

Viewed 107

I am getting the following error "and: bad syntax in: and

#lang racket

(define fold 
    (lambda (fn lst)
        (if (null? (cdr lst))
            (car lst)  
            (fn (car lst) (fold fn (cdr lst)))
        )
    )
)

(define none-diff?
    (lambda (lst num) 
        (if (even? num) 
            (fold and (map even? lst))
            (fold and (map odd?  lst))
        )
    )
)

I try foldr in szScheme , the same error , I do not know why ?

  1. (fold + (list 1 2 3)) : ok
  2. (fold and (map (even? (list 1 2 3)))) : error

"and" and "+" are function , but ?

2 Answers

And isn't function, but macro. And macros can't be used as argument for higher-order functions (for example map, apply or foldl variants).

In this case, you can use andmap:

> (andmap even? (list 2 4 6))
#t
> (andmap even? (list 1 2 3))
#f
(fold and
      (map (even? (list 1 2 3)))) 
  • fold almost sure requires 3 arguments
  • and is not a function, but a special form
  • you pass map a single argument
  • a list cannot be even.
Related