3 ms·
Doesn't loop/recur only help in directly self-recursive calls, as opposed to mutual recursion or tail calls to function arguments?
by kvb 14y ago
Doesn't loop/recur only help in directly self-recursive calls, as opposed to mutual recursion or tail calls to function arguments?
- anonymoushn 14y agoThat'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...
- dewitt 14y agoRight. In Clojure you'd use an explicit trampoline for stack-free mutual recursion.
- andrewvc 14y agoMutual TCO is really uncommon for what it's worth. Of you need it that bad a trampoline should suffice
- kvb 14y agoDirect 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).
- derefr 14y agoErlang 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.)