Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Doesn't loop/recur only help in directly self-recursive calls, as opposed to mutual recursion or tail calls to function arguments?


That's correct. It should be possible to have an explicit guarantee of TCO while maintaining the ability to write things that aren't trivially reducible to loops...


Which is +- what Scala allows with an annotation.


No, scala's @tailrec annotation only guarantees that calls that can trivially be compiled to loops in fact are. You can't do better on a standard JVM without jumping through hoops (or, rather, trampolines).


Right. In Clojure you'd use an explicit trampoline for stack-free mutual recursion.


Mutual TCO is really uncommon for what it's worth. Of you need it that bad a trampoline should suffice


Direct mutual recursion is somewhat rare (but useful for encoding state machines, for instance). However, using continuation passing style (and tail calls to a continuation argument) to prevent stack overflow is a common idiom in some functional languages (e.g. when mapping over a binary tree).


Erlang uses mutual TCO everywhere. All actors/processes are state machines with the function they're in being their current state. To switch states, you just tail-call a new function (which doesn't have to be from the same module you were in.)




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

Search: