(cons'a(cons'b'c))和(cons'a'(b.c))之间的Lisp差异 [英] Lisp difference between (cons 'a (cons 'b 'c)) and (cons 'a '(b.c))

查看:106
本文介绍了(cons'a(cons'b'c))和(cons'a'(b.c))之间的Lisp差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之间有什么区别

(cons 'a (cons 'b 'c)) ;; (A B . C)

(cons 'a '(b.c)) ;; (A B.C)

我需要使用cons创建以下列表((a.b).c),以便尝试理解."的含义.代表.

I need to create the following list ((a.b).c) using cons so i'm trying to understand what that "." represents.

L.E.:我有以下(cons (cons 'a 'b) 'c),但它生成的是((A . B) . C)而不是((A.B).C)(请注意多余的空格)

L.E.: I have the following (cons (cons 'a 'b) 'c) but it produces ((A . B) . C) and not ((A.B).C) (Note the extra spaces)

推荐答案

空格用于分隔列表标记. A.B是单个令牌. (A.B)是具有单个元素的列表. (A . B)是一个cons单元格,其中A作为汽车,B作为cdr.

Spaces are used to separate list tokens. A.B is a single token. (A.B) is a list with a single element. (A . B) is a cons cell with A as car and B as cdr.

缺点单元格是一对事物"(对象).在您的情况下,这些都是符号,它们分别称为AB等.例如,此类单元格的打印表示形式是(A . B).这称为点符号".第一个元素称为汽车",第二个元素称为"cdr".

A cons cell is a pair of "things" (objects). In your case, these things are symbols, and they are named A, B, etc.. The printed representation of such a cell is (A . B), for example. This is called "dot notation". The first element is called "car", the second "cdr".

函数cons创建此类单元格.因此,(cons 'a 'b)生成单元格(A . B). 请注意,名称始终在内部使用大写字母.

The function cons creates such a cell. (cons 'a 'b) thus produces the cell (A . B). Note that names are always upcased internally.

这很可能是您的老师想要的,因此((A . B) . C)是正确的输出,而您的代码是正确的答案.这是一个单元格,汽车指向另一个单元格,而cdr包含C.另一个单元格是汽车包含A和cdr B的单元格.

This is most likely what your teacher wanted, so ((A . B) . C) is the correct output, and your code the right answer. This is a cell where the car points to another cell, and the cdr contains C. That other cell is a cell where the car contains A and the cdr B.

顺便说一句,列表是这样的cons单元格的线性链,因此汽车始终保持一个值,而cdr指向列表的其余部分.最后一个cdr指向无处(在Lisp中称为NIL).在点表示法中,列表是例如(A . (B . (C . NIL))).由于列表很重要,因此可以将它们写得更短:(A B C).如果最后一个CDR的值不是NIL,则以点表示法显示,例如(A . (B . (C . D))))可以写为(A B C . D).

By the way, a list is a linear chain of such cons cells, such that the car always holds a value and the cdr points to the rest of the list. The last cdr points nowhere (which is called NIL in Lisp). In dot notation, a list is e.g. (A . (B . (C . NIL))). Since lists are important, they can be written shorter like this: (A B C). If the last CDR has a value instead of NIL, it is shown in dot notation, e.g. (A . (B . (C . D)))) can be written as (A B C . D).

这篇关于(cons'a(cons'b'c))和(cons'a'(b.c))之间的Lisp差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆