[erlang-questions] Modeling Recursion into Tail Recursion
ok@REDACTED
ok@REDACTED
Sun Mar 15 09:28:46 CET 2015
> On Sat, Mar 14, 2015 at 07:57:22AM -0700, Harit Himanshu wrote:
>> *Question*
>> 2. Can we turn every recursive function into tail-recursive? If no,
>> how
>> to identify?
> As for the second question I'm not 100% sure I'm remembering this
> correctly but using continuation-passing-style you could express every
> recursive function with tail-recursion.
Yes you can but it's not USEFUL to do so.
Put very briefly, CPS style means passing an extra argument
to each function, called a "continuation". This is a function
that expresses "what to do next".
Basically, it *is* the recursion stack you would otherwise have
had, disguised as a function.
There are plenty of good reasons to do this, such as situations
where a function needs more than one continuation, or you want to
simulate long-distance return, or you want a function that can
return more than once, e.g., to do backtracking.
But using CPS in order to use tail recursion is like cutting
slots in nails so you can use a screwdriver on them.
More information about the erlang-questions
mailing list