2013-12-14 29 views

回答

7

你可以做到這一點內建partition功能:

> (partition 2 1 '(1 2 3 4 5)) 
((1 2) (2 3) (3 4) (4 5)) 

但是,當人們問「所有對」他們usially意味着「所有組合」。如果您需要所有組合 - 請使用math.combinatorics

+0

正確的,我制定了標題真的很對。我更好地重新配置它。感謝你的回答。 – fifigyuri

0

像這樣:

(defn pairs [coll] 
    (map list coll (next coll))) 

(pairs '(1 2 3 4 5)) 
;;=> ((1 2) (2 3) (3 4) (4 5)) 
+0

謝謝,這也是一個有用的方法。 – fifigyuri

+0

不客氣。 – omiel

0

combinatorics一個更通用的解決方案給所有對(但我知道這是不是你的例子做什麼)。

Clojure=> (combinations '(a b c) 2) 
((a b) (a c) (b c)) 
Clojure=> (combinations '(a b c d e) 3) 
((a b c) (a b d) (a b e) (a c d) (a c e) (a d e) (b c d) (b c e) (b d e) (c d e)) 
+0

有點不同,但也有用,知道,謝謝 – fifigyuri

相關問題