【问题标题】:How is this the correct representation of the given nested structure [SICP]? What am I missing?这是给定嵌套结构 [SICP] 的正确表示形式吗?我错过了什么?
【发布时间】:2020-08-14 05:41:21
【问题描述】:

SICPSection 2.2.2 (Hierarchical Structures)第二句:作者说((1 2) 3 4)(三个元素的列表)可以由(cons (list 1 2) (list 3 4))构造。

我认为(当然是错误的)它将构造((1 2) (3 4))(两个元素),因为:

  1. 34 将包含在嵌套的 list 中,而不是在顶级 cons 中,并且
  2. cons 构造一对item,pair表示2个元素而不是3个。

我在这里有什么不明白的地方?

【问题讨论】:

    标签: list nested scheme sicp


    【解决方案1】:

    列表是一串对,以cdr 为空列表的对结束。

    (list 3 4)是两对,相当于

                     (cons 3 (cons 4 '()))
    

    所以(cons (list 1 2) (list 3 4))是3对,相当于

    (cons (list 1 2) (cons 3 (cons 4 '())))
    

    一般来说,如果你有一个列表old-list,你可以在前面创建一个新元素的新列表:

    (cons new-element old-list)
    

    如果你写了,你会得到你所期望的

    (list (list 1 2) (list 3 4))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      • 1970-01-01
      • 2020-03-26
      • 2017-03-04
      • 1970-01-01
      相关资源
      最近更新 更多