缺点目前表现如下:
(cons '(1 2) '(3))
;=> ((1 2) 3)
Run Code Online (Sandbox Code Playgroud)
我想实现:
(magic-cons '(1 2) '(3))
;=> (1 2 3)
Run Code Online (Sandbox Code Playgroud)
我找不到这方面的资源但这似乎很简单,我觉得应该有一个内置函数.
或者我只是不知道用于描述这种情况的写字.无论哪种方式,请告诉我.谢谢!
编辑:请不要回答"flatten":P ie
(flatten (cons '(1 2) '(3)))
Run Code Online (Sandbox Code Playgroud)
sku*_*uro 10
你必须使用concat
:
clojure.core/concat
([] [x] [x y] [x y & zs])
Returns a lazy seq representing the concatenation of the elements in the supplied colls.
Run Code Online (Sandbox Code Playgroud)
样品用途:
user> (concat '(1 2) '(3))
(1 2 3)
Run Code Online (Sandbox Code Playgroud)
小智 5
我相信你正在寻找concat(想想"连接列表"):
[Concat]返回一个lazy seq,表示所提供的colls中元素的串联.
在这种情况下,用法将是:
(concat '(1 2) '(3))
Run Code Online (Sandbox Code Playgroud)
请注意,与(许多)其他LISP方言不同,Clojure会concat
产生一个懒惰的序列.请参阅如何在Clojure中将惰性序列转换为非惰性序列?关于如何"强制"一个序列(这可能会或可能没有用/需要,取决于更大的背景,但重要的是要记住).
快乐的编码.
另一种选择是“进入”。
into 总是返回第一个参数的类型,不像 concat 总是返回一个列表。
=> (into [2 4] '(1 2 3))
[2 4 1 2 3]
(into '(2 4) '(1 2 3))
(3 2 1 2 4)
Run Code Online (Sandbox Code Playgroud)