Lisp教程列表中的列表

时间:2019-06-10 08:59:45

标签: lisp elisp

我正在阅读Programming in Emacs Lisp

Here is another list, this time with a list inside of it:

     '(this list has (a list inside of it))

我对嵌套列表感到困惑,为什么它没有前缀引用为

 '(this list has  '(a list inside of it))

如果没有前缀`,为什么不将a解析为函数?

2 个答案:

答案 0 :(得分:4)

's-expression(quote s-expression)的缩写: s表达式中的任何内容都被视为基准,并且不进行评估。

所以

'(this list has (a list inside of it))

的缩写:

(quote (this list has (a list inside of it)))

包含以下列表:

(this list has (a list inside of it))

这是整个quote表单的值,因为未评估。

通过编写以下代码很容易验证这一点:

'(this list has '(a list inside of it))

如果进行了评估,它将产生以下列表作为值:

(this list has (quote (a list inside of it)))

答案 1 :(得分:2)

这是Lisp的轻微困难之一:列表是数据,也可以是程序。如果希望列表成为Lisp程序中的数据,则需要用引号引起来。

这样的列表:人们可以使用READ读取它们

(this list has (a list inside of it))
(this list has no list inside of it)
(+ 1 2)
(1 2 +)
(1 + 2)
(quote (this list has (a list inside of it)))
(quote (this list has (quote (a quote list inside of it))))
(quote quote)

有效的Lisp表单:可以使用EVAL对其进行评估

(+ 1 2)
Evaluates to -> 3

(quote (+ 1 2))
Evaluates to -> (+ 1 2)

(quote (this list has (a list inside of it)))
Evaluates to -> (this list has (a list inside of it))

(quote quote)
Evaluates to -> quote

这也是有效的Lisp表单:

(quote (this list has (quote (a quoted list inside of it))))

其评估结果为:

(this list has (quote (a quoted list inside of it)))