6 ms·
Ha thanks for your comments spuz. Not sure what happened on #2 that I ended up posting broken code. For number #3, I don't think recur will work without changin
by grokcode 17y ago
Ha thanks for your comments spuz. Not sure what happened on #2 that I ended up posting broken code. For number #3, I don't think recur will work without changing the algorithm a bit. There are 2 recursive calls depending on the conditional, and recur needs to be last for it to compile. Also in my defense it works on my machine ;)
So, yes its a learning process. I'm updating the post. Thanks again for your comments.
- spuz 17y agoI tried your solution to #3 on my old windows laptop and trying to find the prime factors for 600851475143 gave a stack overflow. However, replacing the recursive calls with recur produced the correct result. I think this is because only one of the two branches that recurse is ever executed so tail-call optimisation is still possible in this case. If it was called twice (as in some implementations of the Fibonacci sequence) I'm guessing it would fail.
- grokcode 17y agoSure makes sense. Any idea why clojure doesn't do automatic tail call optimization, and instead depends on recur?
- cjenkins 17y agoThe first reply at http://groups.google.com/group/clojure/browse_thread/thread/3a2da6a71a2213be http://groups.google.com/group/clojure/browse_thread/thread/... goes into pretty good detail from the language designer. The short version is that it's a limitation in the JVM currently that he's hoping is removed some day.
- deleted 17y ago[deleted]