cons() 的意外输出

发布于 2024-09-14 11:25:07 字数 434 浏览 7 评论 0 原文

我来自命令式背景,但这些天尝试使用 LISP (Common LISP)

我读了 这里 关于缺点

(缺点 x L):

给定一个 LISP 对象 x 和一个列表 L,计算 (cons x L) 创建一个包含 x 的列表,后跟 L 中的元素。

当我故意不使用列表作为第二个参数时,即当我使用

(cons ' a 'a) 我预计会出现错误,但是哇哦!我得到了(A.A)

我错过了什么?(A . A) 是什么?

I am from an imperative background but these days trying my hands on LISP (Common LISP)

I read here about cons that

(cons x L):

Given a LISP object x and a list L, evaluating (cons x L) creates a list containing x followed by the elements in L.

When I intentionally did not use a list as the second argument i.e when I used

(cons 'a 'a) I expected an error but whoa! I got (A . A).

What have I missed and what is (A . A)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半﹌身腐败 2024-09-21 11:25:07

Cons 构造一个“cons cell”。这首先与列表无关。缺点单元格是一对两个值。 cons 单元以书面形式用“点对”表示,例如 (A . B),其中包含两个值 'A'B< /代码>。

cons 单元中的两个位置称为“car”和“cdr”。您可以将这样的 cons 单元可视化为二等分的块:

  car   cdr
+-----+-----+
|  A  |  B  |
+-----+-----+

在 Lisp 中,值也可以是对其他内容的引用,例如另一个 cons 单元:

+-----+-----+       +-----+-----+
|  A  |   --------> |  B  |  C  |
+-----+-----+       +-----+-----+

这将以“点对”形式表示为 (A . (B.C))。您可以像这样继续:

+-----+-----+       +-----+-----+       +-----+-----+
|  A  |   --------> |  B  |   --------> |  C  |  D  |
+-----+-----+       +-----+-----+       +-----+-----+

这是(A . (B . (C . D)))。正如您所看到的,在这样的结构中,值始终位于 cons 单元的 car 中,cdr 指向结构的其余部分。最后一个值是一个例外,它位于最后一个cdr 中。不过,我们不需要这个异常:Lisp 中有一个特殊的值 NIL,它表示“无”。通过将 NIL 放入最后一个 cdr,您就拥有了一个方便的哨兵值,并且所有您的值都在 car 中>s:

+-----+-----+       +-----+-----+       +-----+-----+       +-----+-----+
|  A  |   --------> |  B  |   --------> |  C  |   --------> |  D  | NIL |
+-----+-----+       +-----+-----+       +-----+-----+       +-----+-----+

这就是 Lisp 中列表的构建方式。由于 (A . (B . (C . (D . NIL)))) 有点笨拙,因此也可以简单地表示为 (ABCD)NIL也称为空列表();这些是同一事物的可互换符号。

现在您可以明白为什么 (cons x list) 返回另一个列表了。 Cons 只是用 car 中的 x 和对 cdr 中的 list 的引用构造另一个 cons 单元

+-----+-----+
|  X  |   --------> list
+-----+-----+

如果 list(AB),则计算结果为:

+-----+-----+       +-----+-----+       +-----+-----+
|  X  |   --------> |  A  |   --------> |  B  | NIL |
+-----+-----+       +-----+-----+       +-----+-----+

(cons x '(ab)) 的计算结果至(xab)

列表只是 cons 单元的一种非常常见的用途。实际上,您还可以从 cons 单元、循环列表或任何有向图构造任意树。

Cons constructs a "cons cell". This has nothing to do with lists at first. A cons cell is a pair of two values. A cons cell is represented in written form by a "dotted pair", e.g. (A . B), which holds the two values 'A and 'B.

The two places in a cons cell are called "car" and "cdr". You can visualize such a cons cell as a bisected block:

  car   cdr
+-----+-----+
|  A  |  B  |
+-----+-----+

In Lisp, a value can also be a reference to something else, for example, another cons cell:

+-----+-----+       +-----+-----+
|  A  |   --------> |  B  |  C  |
+-----+-----+       +-----+-----+

This would be represented in "dotted pair" form as (A . (B . C)). You can continue like this:

+-----+-----+       +-----+-----+       +-----+-----+
|  A  |   --------> |  B  |   --------> |  C  |  D  |
+-----+-----+       +-----+-----+       +-----+-----+

This is (A . (B . (C . D))). As you can see, in such a structure, the values are always in the car of a cons cell, and the cdr points to the rest of the structure. An exception is the last value, which is in the last cdr. We do not need this exception, though: there is a special value NIL in Lisp, which denotes "nothing". By putting NIL into the last cdr, you have a handy sentinel value, and all your values are in the cars:

+-----+-----+       +-----+-----+       +-----+-----+       +-----+-----+
|  A  |   --------> |  B  |   --------> |  C  |   --------> |  D  | NIL |
+-----+-----+       +-----+-----+       +-----+-----+       +-----+-----+

This is how a list is constructed in Lisp. Since (A . (B . (C . (D . NIL)))) is a bit unwieldy, it can also be represented simply as (A B C D). NIL is also called the empty list (); these are exchangable notations for the same thing.

Now you can see why (cons x list) returns another list. Cons simply constructs another cons cell with x in the car and a reference to list in the cdr:

+-----+-----+
|  X  |   --------> list
+-----+-----+

and if list is (A B), it works out as:

+-----+-----+       +-----+-----+       +-----+-----+
|  X  |   --------> |  A  |   --------> |  B  | NIL |
+-----+-----+       +-----+-----+       +-----+-----+

So, (cons x '(a b)) evaluates to (x a b).

Lists are just one very common use of cons cells. You can also construct arbitrary trees from cons cells, or circular lists, or any directed graph, actually.

送舟行 2024-09-21 11:25:07

'a 是一个 Lisp 原子,(A . A) 是一个称为 cons 单元 或“点对”。由于您没有在 (cons x L) 中传递参数 L 的列表,因此您返回了一个单元格。

'a is a lisp atom and (A . A) is a degenerate list called a cons cell or "dotted pair". Since you didn't pass a list for argument L in (cons x L) you got back a cell.

若水般的淡然安静女子 2024-09-21 11:25:07

(CONS x L)

给定 x 和 L,CONS 返回一个新的 cons 单元,其中 x 作为该单元的 CAR,L 作为该单元的 CDR。

列表是 cons 单元的链接链。

CL-USER 141 > (sdraw '(a b c))

[*|*]--->[*|*]--->[*|*]---> NIL
  |        |        |
  v        v        v
  A        B        C

CL-USER 142 > (sdraw (cons 'foo '(a b c)))

[*|*]--->[*|*]--->[*|*]--->[*|*]---> NIL
  |        |        |        |
  v        v        v        v
 FOO       A        B        C

如果 CONS 获取两个符号作为参数,则如下所示:

CL-USER 143 > (sdraw (cons 'foo 'bar))

[*|*]---> BAR
  |
  v
 FOO

(CONS x L)

Given x and L, CONS returns a new cons cell with x as the CAR of that cell and L as the CDR of that cell.

Lists are linked chains of cons cells.

CL-USER 141 > (sdraw '(a b c))

[*|*]--->[*|*]--->[*|*]---> NIL
  |        |        |
  v        v        v
  A        B        C

CL-USER 142 > (sdraw (cons 'foo '(a b c)))

[*|*]--->[*|*]--->[*|*]--->[*|*]---> NIL
  |        |        |        |
  v        v        v        v
 FOO       A        B        C

If CONS gets two symbols as an argument it looks like this:

CL-USER 143 > (sdraw (cons 'foo 'bar))

[*|*]---> BAR
  |
  v
 FOO
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文