“缺点"如何显示在Lisp工作? [英] How does "Cons" work in Lisp?

查看:123
本文介绍了“缺点"如何显示在Lisp工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Lisp,但是我没有Lisp编程经验.在学习的一部分中,我遇到了以下示例:

I was studying Lisp and I am not experienced in Lisp programming. In a part of my studies I encountered the below examples:

> (cons ‘a ‘(a b))  ----> (A A B)
> (cons ‘(a b) ‘a)  ----> ((A B).A)

我想知道为什么当我们有(cons'a'(ab))时,响应是(AAB),为什么当我们稍加更改并放入在(ab)之后的'a ,响应是一个像((AB).A)这样的虚线列表?第一行和第二行有什么区别?这些代码的背后是什么?

I was wondering why when we have (cons ‘a ‘(a b)) the response is (A A B) and why when we change it a little and put the 'a after (a b), the response is a dotted list like ((A B).A)? What is the difference between the first code line and the second one? What is going on behind these codes?

推荐答案

如果您将它们视为 cons-单元格.

简而言之,一个cons单元格由确切地两个值组成.正常的表示法是使用点,例如:

In short, a cons cell consists of exactly two values. The normal notation for this is to use the dot, e.g.:

(cons 'a 'b) ==> (A . B)

但是由于列表在LISP中经常使用,因此更好的表示法是删除点. 通过使第二个元素为新的cons单元格(最后一个结束符为终止符,通常为nil,或Common Lisp中的'())来创建列表.所以这两个是相等的:

But since lists are used so often in LISP, a better notation is to drop the dot. Lists are made by having the second element be a new cons cell, with the last ending a terminator (usually nil, or '() in Common Lisp). So these two are equal:

(cons 'a (cons 'b '())) ==> (A B)
(list 'a 'b) ==> (A B)

因此,(cons 'a 'b)创建一个单元格[a,b],而(list 'a 'b)将创建[a, [b, nil]].注意cons单元格中编码列表的约定:它们以内部nil结尾.

So (cons 'a 'b) creates a cell [a,b], and (list 'a 'b) will create [a, [b, nil]]. Notice the convention for encoding lists in cons cells: They terminate with an inner nil.

现在,如果将'a限制在最后一个列表上,则将创建一个包含[[a, [b, nil]], a]的新cons单元格.因为这不是一个适当的"列表,即它不是以nil终止的,所以写出来的方法是使用点:(cons '(a b) 'a) ==> ((a b) . a).

Now, if you cons 'a onto the last list, you create a new cons cell containing [[a, [b, nil]], a]. As this is not a "proper" list, i.e. it's not terminated with a nil, the way to write it out is to use the dot: (cons '(a b) 'a) ==> ((a b) . a).

如果未打印点,则该列表必须是结构为[[a, [b, nil]], [a, nil]]的列表.

If the dot wasn't printed, it would have to have been a list with the structure [[a, [b, nil]], [a, nil]].

您的示例

当您执行(cons 'a '(a b))时,它将带有符号'a和列表'(a b),并将它们放入新的cons单元格中.因此,这将由[a, [a, [b, nil]]]组成.由于这很自然地以内部nil结尾,因此其书写时没有点.

When you do (cons 'a '(a b)) it will take the symbol 'a and the list '(a b) and put them in a new cons cell. So this will consist of [a, [a, [b, nil]]]. Since this naturally ends with an inner nil, it's written without dots.

关于(cons '(a b) 'a),现在您将获得[[a, [b, nil]], a]. not 不会以内部nil结尾,因此将使用点符号.

As for (cons '(a b) 'a), now you'll get [[a, [b, nil]], a]. This does not terminate with an inner nil, and therefore the dot notation will be used.

我们可以使用cons使最后一个示例以内部nil结尾吗?是的,如果我们这样做

Can we use cons to make the last example end with an inner nil? Yes, if we do

(cons '(a b) (cons 'a '())) ==> ((A B) A)

最后,

(list '(a b) 'a))

等同于

(cons (cons (cons 'a (cons 'b '())) (cons 'a '())))

这篇关于“缺点"如何显示在Lisp工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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