将列表附加到列表序列

noa*_*hlz 5 clojure

我有一系列列表:

(def s '((1 2) (3 4) (5 6)))
Run Code Online (Sandbox Code Playgroud)

我想将另一个列表附加到该序列的尾部,即

(concat-list s '(7 8))
=> '((1 2) (3 4) (5 6) (7 8))
Run Code Online (Sandbox Code Playgroud)

各种(显然)不起作用的方法:

(cons '((1 2)) '(3 4))
=> (((1 2)) 3 4)

(conj '(3 4) '((1 2)))
=> (((1 2)) 3 4)

(concat '((1 2)) '(3 4))
=> ((1 2) 3 4)

;; close, but wrong order...
(conj '((1 2)) '(3 4))
=> ((3 4) (1 2))

;; Note: vectors work - do I really have to convert entire 
;; structure from lists to vectors and back again?
(conj [[1 2]] [3 4])
=> [[1 2] [3 4]]
Run Code Online (Sandbox Code Playgroud)

有哪些可能的实现concat-list,或者是否存在执行此操作的库函数?

Dog*_*ert 2

可能有更好的解决方案,但这是一种方法:

user=> s
((1 2) (3 4) (5 6))
user=> s2
(7 8)
user=> (concat s (cons s2 '()))
((1 2) (3 4) (5 6) (7 8))
Run Code Online (Sandbox Code Playgroud)

  • 由于 `(cons s2 '())` 等于 `(list s2)`,因此解决方案最好写为 `(concat s (list s2))` (3认同)