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))
(defn combinations
"All the unique ways of taking n different elements from items"
[items n]
(let [v-items (vec (reverse items))]
(if (zero? n) (list ())
(let [cnt (count items)]
(cond (> n cnt) nil
(= n cnt) (list (seq items))
:else
(map #(map v-items %) (index-combinations n cnt)))))))
Comments top
No comments for combinations. Log in to add a comment.