就像标题说这个列表抛出一个错误
and: bad syntax in: and
但是如何写它,需要和在该列表中?
最好
除了在 s-expr 的开头之外,您不能使用and
,因为它似乎是一个保留字。
所以除此之外,在其他位置,你只能用quote
使用它,否则它会给出错误的语法错误。
让它出现在列表末尾的唯一可能性 - 因此 - 是:
`(test ,'and)
;; '(test and)
此外,Racket 在求值之前会进行语法检查(检查变量是否绑定(。(而不是像Common Lisp那样评估表达式时(。
(if '() whatever 3) ;; `whatever` being a variable not defined before.
;; common-lisp: 3
;; racket: whatever: unbound identifier in module in: whatever
在Common Lisp中,由于它是一个Lisp-2(函数和变量之间的不同命名空间(,你甚至可以创建/保留一个名为and
的变量:
;; CL: a variable with the name 'and'
(defvar and 3)
(list 1 and)
;; => (1 3)
但是,在 Common Lisp 中,不允许重新定义函数and
:
;; CL: redefine function 'and' - doesn't work:
(defun and (&rest args) (apply #'or args))
;; *** - DEFUN/DEFMACRO: AND is a special operator and may not be redefined.
在 Racket 中,不可能将 sth 绑定到像and
这样的保留字。
因此,由于 Racket 是 Lisp-1,因此不允许对and
进行任何重新定义(既不能作为变量名,也不能作为函数名(,并且由于 Racket 在评估 s-expr 之前对绑定变量进行语法检查 - 无论是特殊形式/宏还是函数 -and
在 s 表达式开头以外的任何其他位置都不能在没有quote
/'
的情况下出现在 Racket 中。
构造列表时,需要引用每个元素。
Welcome to Racket v6.12.
> (list 'test 'and)
'(test and)