In a Forth definition recursion is achieved by the word RECURSE. It
doesn't seem to be possible in standard Forth to achieve recursion in a single definition by calling the word's name. However it can be done by
using two definitions with the same name and exploiting Forth's
visibility rules during compilation of the recursive definition. A new recursive definition can be defined to hide the implementation details.
The two simplest ways to implement recurse by name seem to be:
synonym foo recurse
and
: foo postpone recurse ; immediate
Each followed by the recursive definition such as:
: foo ?dup if dup >r 1- foo r> . then ;
10 foo \ displays 1 2 3 4 5 6 7 8 9 10
Both solutions at first sight look a bit strange and it is better to
hide the detail behind a new defining word such as RECURSIVE:
Definitions of RECURSIVE: for each solution are:
Using SYNONYM we need to build a string to evaluate e.g.
: recursive: >in @ parse-name 2dup
s" recurse" <# holds holds s" synonym " holds #> evaluate
>in ! :
;
EXECUTE-PARSING can be used to simplify the definition slightly.
Using POSTPONE RECURSE
: recursive: >in @
: [: postpone recurse ;] compile, postpone ; immediate
>in ! :
;
The example definition of foo above gives the same result.
recursive: foo ?dup if dup >r 1- foo r> . then ;
10 foo
Both definitions display 1 2 3 4 5 6 7 8 9 10
Gerry Jackson <do-not-use@swldwa.uk> wrote:Of course this should be
In a Forth definition recursion is achieved by the word RECURSE. It
doesn't seem to be possible in standard Forth to achieve recursion in a
single definition by calling the word's name. However it can be done by
using two definitions with the same name and exploiting Forth's
visibility rules during compilation of the recursive definition. A new
recursive definition can be defined to hide the implementation details.
The two simplest ways to implement recurse by name seem to be:
synonym foo recurse
and
: foo postpone recurse ; immediate
Each followed by the recursive definition such as:
: foo ?dup if dup >r 1- foo r> . then ;
10 foo \ displays 1 2 3 4 5 6 7 8 9 10
Both solutions at first sight look a bit strange and it is better to
hide the detail behind a new defining word such as RECURSIVE:
Definitions of RECURSIVE: for each solution are:
Using SYNONYM we need to build a string to evaluate e.g.
: recursive: >in @ parse-name 2dup
s" recurse" <# holds holds s" synonym " holds #> evaluate
>in ! :
;
EXECUTE-PARSING can be used to simplify the definition slightly.
Using POSTPONE RECURSE
: recursive: >in @
: [: postpone recurse ;] compile, postpone ; immediate
>in ! :
;
The example definition of foo above gives the same result.
recursive: foo ?dup if dup >r 1- foo r> . then ;
10 foo
Both definitions display 1 2 3 4 5 6 7 8 9 10
I use the following (forward, resolve)
WANT F:
:F GCD ;
:R GCD OVER MOD DUP IF SWAP GCD THEN DROP ;
Implementation is trivial, if you have not complicate your Forth.
Both create a dea with name "GCD". Most code serve the purpose to
make one entry invisible, and repress the "ISN;T UNQUE" message.
It is usable for mutually recursive calls.
:F A ;
:F B ;
:R A ... A ... B ... A ... ;
:R B ... A ... B ... B ... ;
\ Without ironcladding and whatnot:
:R >IN @ >R NAME FOUND R> >IN !
LATEST >DFA @ SWAP >DFA ! \ Patch the high level code in the forward dea
;
:F is just an alias for : .
Groetjes Albert
--
The glass is half empty. There is no such thing as a free world.
This is the first day of the end of your life.
If you can't beat them, ... too bad.
In a Forth definition recursion is achieved by the word RECURSE. It
doesn't seem to be possible in standard Forth to achieve recursion in a single definition by calling the word's name. However it can be done by using two definitions with the same name and exploiting Forth's
visibility rules during compilation of the recursive definition. A new recursive definition can be defined to hide the implementation details.
The two simplest ways to implement recurse by name seem to be:
synonym foo recurse
and
: foo-a postpone recurse ; immediate
Each followed by the recursive definition such as:
: foo ?dup if dup >r 1- foo r> . then-a ;
10 foo-a \ displays 1 2 3 4 5 6 7 8 9 10
Both solutions at first sight look a bit strange and it is better to
hide the detail behind a new defining word such as RECURSIVE:
Definitions of RECURSIVE: for each solution are:
Using SYNONYM we need to build a string to evaluate e.g.
: recursive: >in @ parse-name 2dup
-a-a s"-a recurse" <# holds holds s" synonym " holds #> evaluate
-a-a >in ! :
;
EXECUTE-PARSING can be used to simplify the definition slightly.
Using POSTPONE RECURSE
: recursive:-a >in @
-a-a-a-a : [: postpone recurse ;] compile, postpone ; immediate
-a-a-a-a >in ! :
;
The example definition of foo above gives the same result.
recursive: foo ?dup if dup >r 1- foo r> . then-a ;
10 foo
Both definitions display-a-a 1 2 3 4 5 6 7 8 9 10
On 24-07-2026 13:27, Gerry Jackson wrote:
4tH creates a symbol table entry the moment it executes ":". Hence,
after that the compiler will find that entry and use it. So:
: foo dup if dup >r 1- foo r> . ;then drop ;
Indeed executes as follows:
pp4th -x ntoc.4th
1 2 3 4 5 6 7 8 9 10
RECURSE required some additional thought, but it works as well. It all compiles to the very same code:
-a Addr| Opcode-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a Operand-a-a Argument
-a-a-a-a 0| branch-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a 11-a-a foo
-a-a-a-a 1| dup-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a 0
-a-a-a-a 2| 0branch-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a 9
-a-a-a-a 3| dup-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a 0
-a-a-a-a 4| >r-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a 0
-a-a-a-a 5| +literal-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a -1
-a-a-a-a 6| call-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a 0-a-a foo
-a-a-a-a 7| r>-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a 0
-a-a-a-a 8| .-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a 0
-a-a-a-a 9| exit-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a 0
-a-a-a 10| drop-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a 0
-a-a-a 11| exit-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a 0
Hans Bezemer
In a Forth definition recursion is achieved by the word RECURSE. It
doesn't seem to be possible in standard Forth to achieve recursion in
a single definition by calling the word's name. However it can be done
by using two definitions with the same name and exploiting Forth's
visibility rules during compilation of the recursive definition. A new
recursive definition can be defined to hide the implementation details.
The two simplest ways to implement recurse by name seem to be:
synonym foo recurse
and
: foo-a postpone recurse ; immediate
Each followed by the recursive definition such as:
: foo ?dup if dup >r 1- foo r> . then-a ;
10 foo-a \ displays 1 2 3 4 5 6 7 8 9 10
Both solutions at first sight look a bit strange and it is better to
hide the detail behind a new defining word such as RECURSIVE:
Definitions of RECURSIVE: for each solution are:
Using SYNONYM we need to build a string to evaluate e.g.
: recursive: >in @ parse-name 2dup
-a-a-a s"-a recurse" <# holds holds s" synonym " holds #> evaluate
-a-a-a >in ! :
;
EXECUTE-PARSING can be used to simplify the definition slightly.
Using POSTPONE RECURSE
: recursive:-a >in @
-a-a-a-a-a : [: postpone recurse ;] compile, postpone ; immediate
-a-a-a-a-a >in ! :
;
The example definition of foo above gives the same result.
recursive: foo ?dup if dup >r 1- foo r> . then-a ;
10 foo
Both definitions display-a-a 1 2 3 4 5 6 7 8 9 10
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 74 |
| Nodes: | 6 (1 / 5) |
| Uptime: | 51:11:01 |
| Calls: | 1,101 |
| Calls today: | 1 |
| Files: | 1,339 |
| Messages: | 276,012 |