why my code is wrong in accumulate function using and operator

430 Views Asked by At

My code is like this

(define (accumulate op initial sequence)
  (if (null? sequence)
      initial
      (op (car sequence)
          (accumulate op initial (cdr sequence)))))

(define seq '(#t #t #t #t))

(accumulate and #t seq))

I use ikarus, the error message is

Unhandled exception
 Condition components:
   1. &who: and
   2. &message: "invalid syntax"
   3. &syntax:
       form: and
       subform: #f
   4. &trace: #<syntax and>

Question is:

and can not be used as op in accumulate function?

If I modify the code above like this, then it works.

(accumulate (lambda (x y) (and x y)) #t seq)
1

There are 1 best solutions below

0
On BEST ANSWER

and is not a procedure, it's syntax or a macro. It needs to be syntax because it doesn't evaluate all its arguments, it evaluates arguments left to right until it encounters #f.