common lisp cons 从两个符号创建一个列表,clojure cons 需要一个 seq 到 cons 上?
(免责声明 - 我知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当你
在 common lisp 中说时,你得到的不是一个列表,而是一个点对:
(a . b)
,而结果是点对
(a . ( b . nil))< /代码>。
在第一个列表中,它的
cdr()
不是一个列表,因为它在这里是b
而不是nil
,这使得它成为一个不正确的列表。正确的列表必须以nil
终止。因此,诸如mapcar()
之类的高阶函数将无法工作,但我们保存了一个 cons-cell。我猜 Clojure 的设计者删除了这个功能,因为它可能会引起混乱。When you say
in common lisp you dont get a list but a dotted pair:
(a . b)
, whereas the result ofis the dotted pair
(a . ( b . nil))
.In the first list the
cdr()
of that is not a list, since it is hereb
and notnil
, making it an improper list. Proper lists must be terminated bynil
. Therefore higher order functions likemapcar()
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.在 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))
) islist
:(list :a :b)
.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 namedprepend
.在 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.
在 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-seq
和cons
取代。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 whencons
is used, a sequence (it's actually of typeclojure.lang.Cons
) constructed ofa
and(seq b)
is returned. (a
being arg 1 andb
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 innext
, and thatlazy-cons
has been replaced bylazy-seq
andcons
.clojure.lang.RT