相关疑难解决方法(0)

Clojure:cons(seq)vs. conj(list)

我知道cons返回一个seq并conj返回一个集合.我也知道conj将项目"添加"到集合的最佳末端,并cons始终将项目"添加"到前面.这个例子说明了这两点:

user=> (conj [1 2 3] 4) //returns a collection
[1 2 3 4]
user=> (cons 4 [1 2 3]) //returns a seq
(4 1 2 3)
Run Code Online (Sandbox Code Playgroud)

对于矢量,地图和集合,这些差异对我来说很有意义.但是,对于列表,它们似乎相同.

user=> (conj (list 3 2 1) 4) //returns a list
(4 3 2 1)
user=> (cons 4 (list 3 2 1)) //returns a seq
(4 3 2 1)
Run Code Online (Sandbox Code Playgroud)

是否有任何使用列表的示例,其中conjvs cons表现出不同的行为,或者它们是否真的可以互换?换句话说,有一个例子,列表和seq不能等效使用吗?

collections clojure sequence

96
推荐指数
3
解决办法
2万
查看次数

Clojure中序列和集合之间的区别是什么

我是一名Java程序员,也是Clojure的新手.从不同的地方,我看到序列和集合在不同的情况下使用.但是,我不知道他们之间究竟有什么区别.

举个例子:

1)在Clojure的序列文档中:

The Seq interface
(first coll)
  Returns the first item in the collection. 
  Calls seq on its argument. If coll is nil, returns nil.
(rest coll)
  Returns a sequence of the items after the first. Calls seq on its argument. 
  If there are no more items, returns a logical sequence for which seq returns nil.
(cons item seq)
  Returns a new seq where item is the first element and seq is the rest.
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,在描述Seq接口时,前两个函数(first/rest)使用coll,这似乎表明这是一个集合,而 …

collections abstraction clojure

31
推荐指数
5
解决办法
5785
查看次数

标签 统计

clojure ×2

collections ×2

abstraction ×1

sequence ×1