kerwin xu
kerwin xu

Reputation: 99

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

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 ?

Upvotes: 2

Views: 299

Answers (2)

alinsoar
alinsoar

Reputation: 15793

(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.

Upvotes: 1

Martin Půda
Martin Půda

Reputation: 7568

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

Upvotes: 4

Related Questions