문제

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)
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top