Hello,
It seems that the recursive call to choose is too eager.
I had a problem with your version (which seems to be using a pre lazy-seq
version) where nthrest still existed (?).
But I rewrote something along the lines of what you did, and had first the
same problem.
Wrapping the recursive call with lazy-seq seemed to solve it.
Here is my final (apparently) working version :
(defn rests [coll]
"returns a lazy seq of coll, (rest coll), (rest (rest coll)) ..."
(lazy-seq
(when-let [s (seq coll)]
(cons s (rests (rest s))))))
(defn choose [v n]
(cond
(zero? n)
[]
(= 1 n)
(map vector v)
:else
(mapcat (fn [sub]
(map #(concat [(first sub)] %)
(lazy-seq (choose (rest sub) (dec n)))))
(rests v))))
(count (choose [1 2 3 4 5 6] 3))
(take 10 (choose (iterate inc 0) 500))
HTH,
--
Laurent
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---