Re: using MISC (post 1987 Forth hardware) opcodes
- From: Marc Olschok <invalid_@xxxxxxxxxxxx>
- Date: Sat, 15 Jul 2006 16:36:57 +0000 (UTC)
Adam Warner <usenet@xxxxxxxxxxxxxxxxx> wrote:
On Wed, 12 Jul 2006 09:50:45 -0700, Jeff Fox wrote:
You have to understand the conceptual foundation about how Forth code
should be constructed and factored. And people do get confused about how
code can have short words and lots of : and ; and also use a minimal
amount of the stacks!
Again, you can only use a bounded amount of stack with tail recursion or
the translation of a recursive definition into an iterative form.
A tail recursive Forth word will call RECURSE as the last thing it does
before exiting the word. A Forth compiler can translate this call into a
jump to the start of the word.
A tail recursive definition is typically harder to write than a non-tail
recursive definition.
At least for primitive recursive functions a doubt that.
Here's an example I came up with within the
limitations of my Forth knowledge: Write recursive and tail recursive
words to naively sum the integers from 1 to the first element on the stack.
Perhaps this is just not a good enough example to support your claim.
Or it may depend on the particular Forth dialect used.
In Retroforth I found
: trsum ( s n -- s' ) 0; tuck + swap 1- trsum ;
quite intuitive.
Instead of "recurse" it uses the definitions name, which I find more
readable anyway.
The other nice feature is 0; :
it leaves the current definition if the
if the TOS is 0 it consumes the TOS and leaves the current word,
if the TOS is nonzero then nothing is done (in particular the TOS
is not consumed). Hence you do not need IF...THEN constructs and
the cleanup afterward.
[...]
If your Forth does not have decent sized stacks then you cannot
effectively run recursive definitions, you cannot prototype using
recursive definitions and you will be inhibited from even thinking
recursively. You'll be forced into premature optimisation, writing harder
code for a less expressive Forth.
I have never experienced such a problem.
Marc
.
- Follow-Ups:
- Re: using MISC (post 1987 Forth hardware) opcodes
- From: jmdrake_98
- Re: using MISC (post 1987 Forth hardware) opcodes
- From: Adam Warner
- Re: using MISC (post 1987 Forth hardware) opcodes
- References:
- using MISC (post 1987 Forth hardware) opcodes
- From: Jeff Fox
- Re: using MISC (post 1987 Forth hardware) opcodes
- From: Bernd Paysan
- Re: using MISC (post 1987 Forth hardware) opcodes
- From: Jeff Fox
- Re: using MISC (post 1987 Forth hardware) opcodes
- From: Adam Warner
- Re: using MISC (post 1987 Forth hardware) opcodes
- From: Jeff Fox
- Re: using MISC (post 1987 Forth hardware) opcodes
- From: Adam Warner
- using MISC (post 1987 Forth hardware) opcodes
- Prev by Date: Re: What languages do you regularly use besides forth?
- Next by Date: Re: using MISC (post 1987 Forth hardware) opcodes
- Previous by thread: Re: using MISC (post 1987 Forth hardware) opcodes
- Next by thread: Re: using MISC (post 1987 Forth hardware) opcodes
- Index(es):