【问题标题】:the list inside a list of lisp tutoriallisp教程列表中的列表
【发布时间】:2019-06-10 08:59:45
【问题描述】:

我正在阅读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解析为函数?

【问题讨论】:

  • 了解'(意思是(quote ...))并不是制作列表的简写,这一点非常重要。这是一种导致 lisp 返回的形式,未计算,由 lisp 阅读器创建的对象。我建议您在学习过程中的某个阶段阅读有关 lisp 执行的不同 readeval 阶段的信息。一旦你理解了区别,quote 就会更有意义。

标签: lisp elisp


【解决方案1】:

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

【讨论】:

    【解决方案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)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-04
      • 2023-04-08
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多