Skip to content

Instantly share code, notes, and snippets.

@trhura
Created December 26, 2013 09:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trhura/8131492 to your computer and use it in GitHub Desktop.
Save trhura/8131492 to your computer and use it in GitHub Desktop.
Combinations generator in Clojure
(defn combinations [lst k]
(letfn [(combinator [x xs]
(if (= (count x) k)
[x]
(when (not (empty? xs))
(concat (combinator (concat x [(first xs)]) (rest xs))
(combinator x (rest xs))))))]
(combinator nil lst)))
;; user> (combinations (range 1 6) 2)
;; ((1 2) (1 3) (1 4) (1 5) (2 3) (2 4) (2 5) (3 4) (3 5) (4 5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment