为什么使用 cons 创建一对两个列表会生成一个列表和两个元素?
我开始学习Scheme,主要是为了好玩,而且因为我以前从未使用过函数式语言。我选择Scheme是因为我想长期阅读SICP。
不管怎样,我目前正在学习列表,在此之前我学习了 cons、car 和 cdr。有一个例子创建了一个带有 cons 的列表列表,如下所示:
(cons (list 1 2) (list 3 4))
结果列表是 ((1 2) 3 4),这对我来说没有意义,我希望 ((1 2)(3 4) )作为结果(由两个列表组成的列表)。为什么它会这样?我意识到如果我要使用汽车,我会得到(1 2),而 cdr 我会得到(3 4),因为 cdr 总是返回“其余”,但我不明白为什么没有列出该列表两个列表?
I've started learning Scheme, for fun mostly, and because I've never used a functional language before. I chose Scheme because I wanted to read SICP for a long time.
Anyway, I'm currently learning about lists, and before that I learned about cons, car and cdr. And there's an example that creates a list of lists with cons, like this :
(cons (list 1 2) (list 3 4))
The resulting list is ((1 2) 3 4), which doesn't make sense to me, I would expect ((1 2)(3 4)) to be the result (a list made out of two lists). Why does it behave like that? I realize that if I were to use car, I would get (1 2), and cdr I'd get (3 4) becaue cdr always returns "the rest", but I don't understand why the list isn't made of two lists?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您会得到一个列表,其中
(1 2)
作为第一个元素(汽车),(3 4)
作为其余元素(cdr),因为 cons 的第一个参数是列表的第一个元素和第二个参数是包含其余项目的列表。这与列表的结构非常相似:(适当的)列表的每个节点都包含一个元素和一个包含所有其他元素的列表。
cons
创建一个这样的节点。如果 cons 的第二个参数将成为列表的第二个元素,那么如何创建具有三个参数的列表?您必须将
cons
设为变量,此时它只是list
的另一个名称。如果要创建列表的列表,请使用
(list (list 1 2) (list 3 4))
。You get a list with
(1 2)
as the first element (the car) and(3 4)
as the rest (the cdr) because the first argument to cons is the first element of the list and the second argument is a list containing the remaining items.This closely resembles the structure of a list: each node of a (proper) list contains an element and a list containing all other element.
cons
creates one such node.If the second argument to
cons
would become the second element of the list, how would you create a list with three arguments? You'd have to makecons
variardic at which point, it'd just be another name forlist
.If you want to create a list of lists use
(list (list 1 2) (list 3 4))
.相同
与 Which 结果
也可以写为
is the same as
Which results in
which can also be written as
内部结构的图形表示可以帮助我们可视化问题。
这将会有更多帮助:
你看到这个模式了吗?上面是列表
(X 3 4)
。这就是(cons AB)
仅将car
部分绘制为单独列表的原因,而不是cdr
的原因。A graphic representation of the inner structures can help us to visualize the problem.
And this will help some more:
Do you see the pattern? The above is the list
(X 3 4)
. That's the reason(cons A B)
draws only thecar
part as a separate list and not thecdr
.由于 cons-cell 不是两个元素的列表,因此两者经常被混淆。如果
(a . b)
是 cons 单元格,则(a . (b . ()))
是两个元素的列表。任何列表(特别是空列表)都是一个 cons 单元,其car
字段包含第一个元素,其cdr
字段包含包含其余元素的列表。因此,列表只是一个二叉树,其最右边的叶子是特殊常量()
或nil
,具体取决于您的方言。这就是为什么
(cons 0 '(1 2 3))
计算结果为(0 1 2 3)
而不是(0 (1 2 3))
code>:我们创建一个cons
单元,其car
为0
,其cdr
为(1 2 3)
,所以,一个列表(0 1 2 3)
。Because a cons-cell is not a list of two elements, the two are often confused. If
(a . b)
is a cons cell, then(a . (b . ()))
is a list of two elements. Any list - save the empty list specifically - is a cons cell whosecar
field contains the first element and whosecdr
field contains the list which contains the remaining elements. A list is thus simply a binary tree whose right-most leaf is the special constant()
ornil
depending on your dialect.Which is why
(cons 0 '(1 2 3))
evaluates to(0 1 2 3)
and not(0 (1 2 3))
: we create acons
cell whosecar
is0
, and whosecdr
is(1 2 3)
, so, a list(0 1 2 3)
.这是一个非常简单的解释:
我们知道这两个条件必须为真:
满足这个条件的唯一方法是 x = ((1 2) 3 4)。
_________________________________________________________________________________________________________
x = ((1 2)(3 4))
不正确,因为:看到为
(cdr x)
创建了一个额外的括号/列表代码>.x = ((1 2)(3 4))
实际上是一个列表的列表:Here's a really simple explanation:
we know that these two conditions must be true:
the only way to satisfy this is
x = ((1 2) 3 4)
.________________________________________________________________________________________________________
x = ((1 2)(3 4))
is incorrect because:See there is an extra bracket/list created for
(cdr x)
.x = ((1 2)(3 4))
is actually a list of lists: