3 ms·
You use recur in clojure for tail optimization. Don't think the jvm has tail optimization support currently.
by sbochins 16y ago
You use recur in clojure for tail optimization. Don't think the jvm has tail optimization support currently.
- dikbrouwer 16y agoExactly, it doesn't. Only self-recursion with recur is tail optimized by the JVM
- deleted 16y ago[deleted]
- oconnor0 16y agorecur isn't tail-optimized by the JVM. It's converted by the Clojure compiler into a loop.
- dikbrouwer 16y agoTrue, my bad
- bitsai 16y agoTo add to this, Clojure also has "trampoline" for TCO in mutually recursive functions.
- cygwin98 16y agoIs 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.
- dminor 16y agoRecur is essentially just a loop - it doesn't use the stack at all.
- awj 16y agoRecur 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.
- jcromartie 16y agoI 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.