3 ms·
Manual conversion to a loop also destroys the same information, so it’s a wash either way on that front.
by pja 10y ago
Manual conversion to a loop also destroys the same information, so it’s a wash either way on that front.
- 4ad 10y agoYou can't convert arbitrary tail calls to a loop. You can only convert recursive tail calls to a loop. The information loss associated with non-recursive tail calls is great. It's true that recursive tail calls lose less information, but it is not true that it's equivalent to the information lost in a loop. In a loop you always have access to {head, current, tail} while in a tail call you might (subject to algorithm and compiler optimization) only have access to {current, head|tail}, depending if the loop goes forwards or backwards.
- spc476 10y agoBut I've found that non-recursive tail calls are great in handling state machines. I wrote a TFTP client and server in Lua [1] and the code was both much cleaner and more reusable between both client and server. Granted, Lua does proper TCO (as long as you properly code the return statement) and the lack of stack trace wasn't much of an issue there. These rules are mostly geared towards C, which doesn't guarantee TCO at all. [1] Partly as way to learn Lua and TCO, and as a way to manage Cisco router config files in a version control system.