为什么我的代码在使用和运算符的累积函数中是错误的

时间:2013-12-20 08:49:50

标签: scheme accumulate

我的代码就像这样

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

我使用ikarus,错误信息是

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

问题是:

累积功能中,

不能用作操作?

如果我像这样修改上面的代码,那么它就可以了。

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

1 个答案:

答案 0 :(得分:1)

and不是程序,它是语法或宏。它需要是语法,因为它不会评估它的所有参数,它会在遇到#f之前从左到右计算参数。

相关问题