给定一个集合,我想遍历集合中的所有对。例

(all-pairs seq)

(all-pairs '(a b c d)) => ([a b] [a c] [a d] [b c] [b d] [c d]))


这是我的主意

(defn all-pairs [coll]
  (for [ [idx elmt] (indexed coll)
         other-elmt (subvec coll (inc idx))]
     (vector elmt other-elm)))


但这并不习惯

最佳答案

怎么样:

(use 'clojure.contrib.combinatorics)
(vec (map vec (combinations '(a b c d) 2)))

08-27 06:50