Hacker News new | past | comments | ask | show | jobs | submit login

You use recur in clojure for tail optimization. Don't think the jvm has tail optimization support currently.



To add to this, Clojure also has "trampoline" for TCO in mutually recursive functions.


Is recur kind of workaround? Any performance penalty?

Please bear with me as I'm not familiar with Lisp family languages. I'm interested in ML languages. There is a project to port OCaml to JVM, but I don't see any sign of progress, so I thought it may be something on JVM to hold them back.


I actually like the 'recur' better than implicit tail-call optimization. It does what it says on the tin. If you don't use recur, you get unoptimized recursion, and more importantly: where you can't make a function tail-recursive, you can't use 'recur', because it is a compiler error.


Recur relies on AST rewriting to turn self-recursion into a loop. The JVM doesn't play a part in this. My impression is that this is 50/50 language design (recur throws errors on non-tail calls) and compromise for the JVM as a platform.

OCaml on the JVM would have the same problems, if not worse. Eliminating tail recursion is essential in the ML family, and doing that while sanely supporting Java interop seems to be difficult.

I don't really keep up with it much, but I remember proposals to give some bytecode operations that would help here in the next Java version. Not sure if any of them made it past that stage.


Recur is essentially just a loop - it doesn't use the stack at all.


Exactly, it doesn't. Only self-recursion with recur is tail optimized by the JVM


recur isn't tail-optimized by the JVM. It's converted by the Clojure compiler into a loop.


True, my bad




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: