On 14 Feb 2010, at 13:20, ka wrote:

> 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?


Check out the source for clojure.contrib.combinatorics:

  http://richhickey.github.com/clojure-contrib/combinatorics-api.html

-Steve

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to