by spuz · 17y ago · 0 comments
The second solution will run indefinitely:
(defn e2 [limit]
(filter #(and (< % limit)
(zero? (mod % 2)))
fibs))
Using a predicate in the filter function of "< limit and mod 2 == 0" does not work as the fibs sequence is evaluated even after the terms in the sequence become greater than the limit (all it means is that every term above the limit is filtered out). Replacing the limit check with take-while would fix this.The third solution is also broken, it works fine for small values of n but enter a large number such as is given in the problem and you quickly run out of stack. Replacing the recursive call with recur will enable tail-call optimisation and give your stack a lot more breathing room.
I recommend the clojure-euler wiki for more reliable examples of good clojure code :)
So, yes its a learning process. I'm updating the post. Thanks again for your comments.
The short version is that it's a limitation in the JVM currently that he's hoping is removed some day.