Hi,
I just wrote a very small combinatorics utility for myself. Choose. It
is given a vector of elements, followed by a number that indicates how
many elements to "choose" out of the vector. It returns all possible
combinations of choosing <num> elements out of <v> while retaining the
correct order.
I hit a snag though, I'm getting lots of OutOfMemory errors because of
the use of mapcat, which doesn't seem to be lazy. Can I have some help
writing this into a lazy function?
Thanks a lot
-Patrick
(defn choose [v num]
(if (zero? num)
[]
(mapcat (fn [i]
(let [n (nth v i)]
(map #(conj % n) (choose (nthrest v (inc i))
(dec num)))))
(range (count v)))))
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---