One can simulate it in C with gotos. Instead of recursively calling
the function, one puts a jump back to the start of the function.
Naturally one needs to write the function with this in mind.
Interpreters which have a recursive eval layer in some kind of REPL
should do this. For example the consequent and alternative statements
in an "if" statement may be considered in tail position. Those
statements might simply call eval again, or they might optimise the
tail recursion.
I don't know if Python's interpreter does this or not.
It is difficult to have a C compiler automatically detect tail
recursion and have it correctly and efficiently optimise for it,
probably because of pointers I suppose, as with everything else that
is broken about C.
Bill.
> --
> You received this message because you are subscribed to the Google Groups "sage-flame" group.
> To post to this group, send email to sage-...@googlegroups.com.
> To unsubscribe from this group, send email to sage-flame+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sage-flame?hl=en.
>
>
http://code.activestate.com/recipes/474088/
The first thing that popped into my head was to do something with the
call stack. At least googling that came up with a hit. But I didn't
look in detail to see what it does, except for the plain english
description at the start.
You won't too much of that kind of fear on this list.
>
> I'd still be interested in seeing a decorator in the style of
> http://groups.google.ca/group/sage-flame/msg/252d9f5e568b2d92 (based
> what seems to be called a "trampoline") to flatten call-stacks for
> mainly tail-recursive routines, mainly out of curiosity to see how
> hard it is.
>
Bill.
Bill.
No. GCC essentially compiles C to a form of Lisp first, then to
assembly language.
I think it is more common than one might expect, especially given that
many compilers compile to C or their interpreters are written in C
(not that the Lisp stage is still present in the latter case).
>
>> In Python you have a much more elaborate program that has to do a non-trivial parsing task, instead of READ's parsing task, which is fairly easy.
>
> It is my impression that it is mainly this non-trivial parsing task
> that willing users of Python appreciate. An important selling point of
> Python is that the source code is so easy to read.
Actually decent Lisp and Scheme compilers do go via an AST. Many toy
interpreters and compilers don't do this. But the good ones do. Python
would be much harder to parse to an AST though, of course.
>
> [yes, the AST is accessible via a standard module that comes with
> Python. I was a little disappointed to see that it doesn't come with a
> pretty-printer. So AST-level transformations are definitely possible.
> However, the kind of non-local transformation required to transform a
> tail recursion into a loop is made harder due to the absence of
> labelled break and continues, or gotos. The presence of exception
> handling and "finally"s (unwind-protect in CL if I'm not mistaken)
> need a little care too.]
>
I was being serious, but maybe to your taste it isn't a Lisp. That's
fine, of course.
Bill.