[erlang-questions] Modeling Recursion into Tail Recursion

Harit Himanshu harit.subscriptions@REDACTED
Tue Mar 24 15:37:33 CET 2015


Thank you so much for your valuable answers.

On Sun, Mar 15, 2015 at 1:28 AM, <ok@REDACTED> wrote:

> > 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.
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150324/711f7787/attachment.htm>


More information about the erlang-questions mailing list