为什么”和”不能在Racket-lang内进行累加运算?

why “and” can not be operation within accumulate in Racket-lang?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#lang racket

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

(define (all-is-true? items)
  (accumulate and
              true
              items))

(all-is-true? (list true true true))

输出为:

1
 and: bad syntax in: and

我找不到为什么"和"过程不能成为累加操作的原因。


and是宏。在此示例中,您可以通过编写以下代码来修复代码:

1
2
3
4
(define (all-is-true? items)
  (accumulate (lambda (a b) (and a b))
              true
              items))

不能将宏作为参数传递。它们只能作为应用程序的汽车出现。