Hi All,
I've been trying to learn clojure lately by solving the project euler
problems with it, and I think the distinct function is broken (or it
doesn't quite work as I assume it would).
here is the code i'm running
(defn pow [nbr pwr]
(if (< pwr 2)
nbr
(* nbr (pow nbr (dec pwr)))))
(count (sort (distinct (apply concat (map (fn [i] (map (fn [j] (pow i
j)) (range 2 101))) (range 2 101))))))
for which the result shows 9188, but should be 9183.
I wrote my own distinct function which gives the correct result but
runs a LOT slower
(defn in? [lst n]
(if (nil? lst)
false
(if (= (first lst) n)
true
(in? (rest lst) n))))
(defn unique [lst]
(loop [l lst n (list)]
(if (nil? l)
(sort n)
(if (in? n (first l))
(recur (rest l) n)
(recur (rest l) (cons (first l) n))))))
i'm using revision 1185.
is this a bug or am i doing something wrong?
thanks
-Tristan
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---