On Thu, 2005-10-20 at 15:22 -0400, Sean 'Captain Napalm' Conner wrote:
Also, modern languages tend to convert
tail-recursion into loops. For
instanace, in Erlang [1]:
I was referring more to the typical case of recursion where arbitrary
amounts of data are being allocated on the stack (usually with no formal
analysis by the programmer, because most programmers are not capable nor
would time pressures permit most to be that serious). The other issue is
sheer complexity in cases where things are moderately complex. In a 10
or 15 line self-contained function, and you know you aren't going to
blow the stack, recursion may make sense. My experience is that those
situations just don't come up in practice often.
Mildly on-topic, Chuck Moore's current version of Forth (ColorForth)
actually gets rid of looping constructs altogether and gives you tail
recursion in its place.
4IM takes some cues from ColorForth, and is easier to read,
: SPACES DUP IF BL EMIT 1- SPACES ; THEN DROP .
4IM compiles the sequence SPACES ; as a jump to SPACES when it sees ;.
In this example this results in jumping back to the beginning of the
definition. The rule for tail recursion is simple:
If ; or . follows a word, a jump to this word is performed instead of
calling it.
This rule also works with EXECUTE and defered words.
The difference between a jump and a call is that a call pushes a return
address on the return stack when a jump does not. Without this feature,
invoking SPACES with a large argument would result in a return stack
overflow."
Note that . is the word terminator in 4IM. Semicolon is EXIT.
-- John.