Question

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)
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top