I'm trying to make a function which gives the n! permutations of a
vector of n things. Here is my first attempt :
(defn permute
"Gives the n! permuations of the input vector of things"
[v]
(if (= 1 (count v)) (list [(v 0)])
(loop [i 0 perm '()]
(if (= i (count v))
perm
(let [s-v (into [] (concat (subvec v 0 i) (subvec v (inc i))))
perm-s-v (permute s-v)
new-perms (map #(conj % (v i)) perm-s-v)]
(recur (inc i) (into perm new-perms)))))))
1:160 user=> (permute [1 2 3])
([2 1 3] [1 2 3] [3 1 2] [1 3 2] [3 2 1] [2 3 1])
How can I write this in a better fashion? In particular how do I make
this lazy?
Thnx
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en