Pergunta

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)
Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top