[net.lang.forth] Mutual recursion in Forth

oster@ucblapis.BERKELEY.EDU (David Phillip Oster) (03/12/86)

My favorite version of recursion in forth is the way it is done in the
Laxen & Perry Forth-83s.  I'm going to show you how you write a collection
of mutually recursive routines, (which, for me, is much more command
practise than writing a single routine that calls it self recursively.)

DEFER Routine1
DEFER Routine2
DEFER Routine3
: (Routine1) ... do something one ... ;
: (Routine2) .... do sonething two ... ;
: (Routine3) ... do something 3 ... ;
' (Routine1) IS Routine1
' (Routine2) IS Routine2
' (Route3) IS Routine3

The makes the the un-parenthesized named synonyms for the parenthisized names.
Since all the un-parenthesized names are declared before any of the
parenthesized ones I can use all 3 un-parenthesized names in the
definitions of any of the parenthesized names.

Lastly, here is some simple code for DEFER and IS (leaving aside the
issues of user variables and state smartness that are handled by the real
Laxen & Perry code.)
: DEFER CREATE ' , DOES> @ EXECUTE ;
: IS ' >PFA ! ;		\ remember, in F83, "'" returns the CFA!

--- David Phillip Oster
-----------------------  ``What do you look like when you aren't visualizing?''
Arpa: oster@lapis.berkeley.edu
Uucp: ucbvax!ucblapis!oster

liberte@uiucdcsb.CS.UIUC.EDU (03/15/86)

I implemented indirect recursion in Forth once upon a time.
I used a Forward defining word for those routines which would be
used before being fully declared.  At execution time, the forwarded
word would dynamically look up its name in the dictionary for the
most recent version of itself and then replace the reference to the
forward definition to a reference to the most recent version.  Thereafter,
the most recent version is used directly.  

For the "married" functions from "Godel, Escher, Bach", p 137 (assuming
direct recursion is also allowed):

forward m
: f dup 1 - f m - ;
: m dup 1 - m f - ;


Dan LaLiberte
liberte@b.cs.uiuc.edu
liberte@uiuc.csnet
ihnp4!uiucdcs!liberte