common lisp cons 从两个符号创建一个列表,clojure cons 需要一个 seq 到 cons 上?

发布于 2024-09-09 12:36:17 字数 334 浏览 2 评论 0原文

(免责声明 - 我知道 Clojure 中 Seqs 的重要性)

在 common lisp 中,cons 函数可用于将两个符号组合成一个列表:

(def s 'x)
(def l 'y)
(cons s l)

在 clojure 中 - 你只能将 cons 放到序列上 - cons 尚未扩展到使用两个符号。所以你必须写:

(def s 'x)
(def l 'y)
(cons s '(l))

Clojure 中是否有更高级别的模式来解释 Common LISP 和 Clojure 之间的差异?

(Disclaimer - I'm aware of the significance of Seqs in Clojure)

In common lisp the cons function can be used to combine two symbols into a list:

(def s 'x)
(def l 'y)
(cons s l)

In clojure - you can only cons onto a sequence - cons hasn't been extended to work with two symbols. So you have to write:

(def s 'x)
(def l 'y)
(cons s '(l))

Is there a higher level pattern in Clojure that explains this difference between Common LISP and Clojure?

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

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

发布评论

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

评论(5

篱下浅笙歌 2024-09-16 12:36:26

当你

> (cons 'a 'b)

在 common lisp 中说时,你得到的不是一个列表,而是一个点对:(a . b),而结果

> (cons 'a (cons 'b nil))

是点对 (a . ( b . nil))< /代码>。

在第一个列表中,它的 cdr() 不是一个列表,因为它在这里是 b 而不是 nil,这使得它成为一个不正确的列表。正确的列表必须以 nil 终止。因此,诸如 mapcar() 之类的高阶函数将无法工作,但我们保存了一个 cons-cell。我猜 Clojure 的设计者删除了这个功能,因为它可能会引起混乱。

When you say

> (cons 'a 'b)

in common lisp you dont get a list but a dotted pair: (a . b), whereas the result of

> (cons 'a (cons 'b nil))

is the dotted pair (a . ( b . nil)).

In the first list the cdr() of that is not a list, since it is here b and not nil, making it an improper list. Proper lists must be terminated by nil. Therefore higher order functions like mapcar() and friends won't work, but we save a cons-cell. I guess the designers of Clojure removed this feature because of the confusion it could cause.

泪冰清 2024-09-16 12:36:25

在 Clojure 中,首选使用二元素向量:[:a :b]。在底层,这样的小向量被实现为 Java 数组,并且非常简单和快速。

(cons :a '(:b)) (或 (cons :a (cons :b nil)))的简写是 list:<代码>(列表:a:b)。

In Clojure the use of a two-element vector is preferred: [:a :b]. Under the hood such small vectors are implemented as Java arrays and are extremely simple and fast.

A short hand for (cons :a '(:b)) (or (cons :a (cons :b nil))) is list: (list :a :b).

梦巷 2024-09-16 12:36:24

Lisp 列表只是使用 cons 单元的常见方法(请参阅 Rainer 的描述)。 Clojure 最好被视为没有 cons 单元(尽管类似的东西可能隐藏在幕后)。 Clojure cons 是一个用词不当,它实际上应该被命名为 prepend

A Lisp list is just a common way of using cons cells (see Rainer's description). Clojure is best seen as not having cons cells (although something similar might hide under the hood). The Clojure cons is a misnomer, it should actually just be named prepend.

机场等船 2024-09-16 12:36:23

在 Common Lisp 中,CONS 创建一个所谓的 CONS 单元,它类似于具有两个槽的记录:“car”和“cdr”。

您可以将任何东西放入 cons 单元的这两个插槽中。

Cons 单元格用于构建列表。但是我们可以用 cons 单元创建各种数据结构:树、图、各种类型的专用列表……

Lisp 的实现经过高度优化,可以提供非常高效的 cons 单元。

In Common Lisp CONS creates a so-called CONS cell, which is similar to a record with two slots: the 'car' and the 'cdr'.

You can put ANYTHING into those two slots of a cons cell.

Cons cells are used to build lists. But one can create all kinds of data structures with cons cells: trees, graphs, various types of specialized lists, ...

The implementations of Lisp are highly optimized to provide very efficient cons cells.

鹤舞 2024-09-16 12:36:22

在 Clojure 中,与传统 Lisp 不同,列表不是主要数据结构。数据结构可以实现 ISeq 接口 - 这是给定数据结构的另一种视图 - 允许相同的函数访问每个接口中的元素。 (列表已经实现了此功能。seq? 检查某些内容是否实现了ISeq.(seq? '(1 2)), (seq? [1 2])) Clojure 只是表现不同(有充分的理由) ,因为当使用 cons 时,由 a( 构造的序列(实际上是 clojure.lang.Cons 类型)返回 seq b)。 (a 是 arg 1 和 b arg 2)显然,符号没有也不能实现 ISeq

Clojure.org/sequences

Rich Hickey 的序列截屏/演讲 但是,请注意 rest 已更改,它以前的行为现在位于 next 中,并且 lazy-cons 已被 lazy-seqcons 取代。

clojure.lang.RT

In Clojure, unlike traditional Lisps, lists are not the primary data structures. The data structures can implement the ISeq interface - which is another view of the data structure it's given - allowing the same functions to access elements in each. (Lists already implement this. seq? checks whether something implements ISeq.(seq? '(1 2)), (seq? [1 2])) Clojure simply acts differently (with good reason), in that when cons is used, a sequence (it's actually of type clojure.lang.Cons) constructed of a and (seq b) is returned. (a being arg 1 and b arg 2) Obviously, symbols don't and can't implement ISeq.

Clojure.org/sequences

Sequences screencast/talk by Rich Hickey However, note that rest has changed, and it's previous behaviour is now in next, and that lazy-cons has been replaced by lazy-seq and cons.

clojure.lang.RT

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