理解 SICP 中的树 - 练习 2.24
understanding the tree in SICP - exercise 2.24
来自SICP
Exercise 2.24: Suppose we evaluate the expression (list 1 (list 2 (list 3 4))). Give the result printed by the interpreter, the corresponding box-and-pointer structure, and the interpretation of this as a tree (as in Figure 2.6).
问题是我的眼睛坏了,所以我既看不到盒子和指针图,也看不到图2.6。所以现在我只能猜测这个列表应该像树一样基于:
Another way to think of sequences whose elements are sequences is as trees. The elements of the sequence are the branches of the tree, and elements that are themselves sequences are subtrees.
请检查我的树解释。这只是我的想象。我非常有信心这是正确的,但无法确认,因为我找到的所有练习答案都是图片,而我的屏幕 reader 无法读取它们。
(list 1 (list 2 (list 3 4))) - 我认为这是树本身,或者根节点。这棵树有两个分支或 children.
第一个分支 (1) 是叶节点,所以我们在树的这一边完成了。
第二个分支(list 2(list 3 4)是另一棵树。
现在我们关注子树(list 2(list 3 4)。它有两个 children/branches.
第一个分支是叶节点 (2),所以我们到这里就完成了。
第二个分支是另一棵树(清单 3 4)。
现在我们关注子树(列表 3 4)。它有两个 children 分支。
它们都是叶节点,所以我们完成了。
这是正确的吗?我对这棵树的理解正确吗?
你的解释是正确的。解释器打印出来的结果是(1 (2 (3 4)))
,也就是你描述的那棵树
Lisp 中真正的 list-building 原语是 cons
。 (list 1 2 3)
形式的求值结果列表与 (cons 1 (cons 2 (cons 3 '())))
的求值结果相同,也可以写成 '(1 2 3 . ())
,或全点形式 [=16] =].
所有这些将被解释器打印为(1 2 3)
:
(list 1 2 3) '(1 2 3) ; (1 2 3)
(cons 1 (list 2 3)) '(1 . (2 3)) ; (1 2 3)
(cons 1 (cons 2 (list 3))) '(1 . (2 . (3))) ; (1 2 3)
(cons 1 (cons 2 (cons 3 '()))) '(1 . (2 . (3 . ()))) ; (1 2 3)
从树的角度来看,(1 2 3)
有三个分支 - 都是叶子节点:1、2 和 3。另一方面,(1 (2 3))
有两个分支 - 叶子和两个叶节点的树。并且((1 2) 3)
也有两个分支——一个有两个叶子分支的树,还有一个叶子分支。
作为 boxes-and-pointers 结构,点代表缺点单元(即框),每个单元都有两个槽或指针 - car
(点左侧)和 cdr
(点右边)。
因此,评估 (list 1 (list 2 3))
的结果打印为 (1 (2 3))
,也是由调用 (cons 1 (cons (cons 2 (cons 3 '())) '()))
构造的;所以作为 box-and-pointer 结构,它实际上是 '(1 . ( (2 . (3 . ())) . () ))
。它的 car
是 1
,它的 cdr
是盒子 ( (2 . (3 . ())) . () )
其 cdr
是 ()
和它的 car
- 盒子(2 . (3 . ()))
;等等
最后一个方框 cdr
中带有 ()
的列表被称为 "proper lists"。任何其他的都称为 "improper lists",例如
'(1 2 . 3)
= '(1 . (2 . 3))
= (cons 1 (cons 2 3))
来自SICP
Exercise 2.24: Suppose we evaluate the expression (list 1 (list 2 (list 3 4))). Give the result printed by the interpreter, the corresponding box-and-pointer structure, and the interpretation of this as a tree (as in Figure 2.6).
问题是我的眼睛坏了,所以我既看不到盒子和指针图,也看不到图2.6。所以现在我只能猜测这个列表应该像树一样基于:
Another way to think of sequences whose elements are sequences is as trees. The elements of the sequence are the branches of the tree, and elements that are themselves sequences are subtrees.
请检查我的树解释。这只是我的想象。我非常有信心这是正确的,但无法确认,因为我找到的所有练习答案都是图片,而我的屏幕 reader 无法读取它们。
(list 1 (list 2 (list 3 4))) - 我认为这是树本身,或者根节点。这棵树有两个分支或 children.
第一个分支 (1) 是叶节点,所以我们在树的这一边完成了。
第二个分支(list 2(list 3 4)是另一棵树。
现在我们关注子树(list 2(list 3 4)。它有两个 children/branches.
第一个分支是叶节点 (2),所以我们到这里就完成了。
第二个分支是另一棵树(清单 3 4)。
现在我们关注子树(列表 3 4)。它有两个 children 分支。
它们都是叶节点,所以我们完成了。
这是正确的吗?我对这棵树的理解正确吗?
你的解释是正确的。解释器打印出来的结果是(1 (2 (3 4)))
,也就是你描述的那棵树
Lisp 中真正的 list-building 原语是 cons
。 (list 1 2 3)
形式的求值结果列表与 (cons 1 (cons 2 (cons 3 '())))
的求值结果相同,也可以写成 '(1 2 3 . ())
,或全点形式 [=16] =].
所有这些将被解释器打印为(1 2 3)
:
(list 1 2 3) '(1 2 3) ; (1 2 3)
(cons 1 (list 2 3)) '(1 . (2 3)) ; (1 2 3)
(cons 1 (cons 2 (list 3))) '(1 . (2 . (3))) ; (1 2 3)
(cons 1 (cons 2 (cons 3 '()))) '(1 . (2 . (3 . ()))) ; (1 2 3)
从树的角度来看,(1 2 3)
有三个分支 - 都是叶子节点:1、2 和 3。另一方面,(1 (2 3))
有两个分支 - 叶子和两个叶节点的树。并且((1 2) 3)
也有两个分支——一个有两个叶子分支的树,还有一个叶子分支。
作为 boxes-and-pointers 结构,点代表缺点单元(即框),每个单元都有两个槽或指针 - car
(点左侧)和 cdr
(点右边)。
因此,评估 (list 1 (list 2 3))
的结果打印为 (1 (2 3))
,也是由调用 (cons 1 (cons (cons 2 (cons 3 '())) '()))
构造的;所以作为 box-and-pointer 结构,它实际上是 '(1 . ( (2 . (3 . ())) . () ))
。它的 car
是 1
,它的 cdr
是盒子 ( (2 . (3 . ())) . () )
其 cdr
是 ()
和它的 car
- 盒子(2 . (3 . ()))
;等等
最后一个方框 cdr
中带有 ()
的列表被称为 "proper lists"。任何其他的都称为 "improper lists",例如
'(1 2 . 3)
= '(1 . (2 . 3))
= (cons 1 (cons 2 3))