Created
December 14, 2010 22:14
-
-
Save ordnungswidrig/741207 to your computer and use it in GitHub Desktop.
Permutations filtered by predicat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; 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