Skip to content

Instantly share code, notes, and snippets.

@ordnungswidrig
Created December 14, 2010 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ordnungswidrig/741207 to your computer and use it in GitHub Desktop.
Save ordnungswidrig/741207 to your computer and use it in GitHub Desktop.
Permutations filtered by predicat
;;; eager permutations
(defn pop-nth [coll i]
[(nth coll i) (concat (take i coll) (drop (inc i) coll))])
(defn perms [items]
(letfn [(p [previous rest]
(if (empty? rest)
[previous]
(mapcat (fn [i]
(let [[n r] (pop-nth rest i)]
(p (concat previous [n]) r)))
(range (count rest)))))]
(p [] items)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment