Your permutations is being called again for similar inputs..
Try using this and see that similar inputs like (2 3) are coming again for
(permutations [1 2 3 4])
(defn permutations [s]
(prn s) ;; This will print input
(lazy-seq
(if (seq (rest s))
(apply concat (for [x s]
(map #(cons x %) (permutations (remove #{x} s)))))
[s])))
Using the following solves that problem : It seems to be also not taking
much memory :
(defn insert-at-every-pos [x v]
(loop [s x
e []
r []]
(if (empty? s)
(conj r (into [v] x))
(recur (pop s) (conj e (peek s)) (conj r (into (conj s v) e))))))
(defn permutations [s]
(prn s)
(lazy-seq
(if (seq (rest s))
(mapcat (fn [p] (insert-at-every-pos p (peek s))) (permutations (pop
s)))
[s])))
(filter nil? (permutations (vec (range 11))))
--
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
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.