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 2026-07-24 15:27, Gerry Jackson wrote:
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
Elegant.
Note: that approach does not work if the compilation word list is absent
in the search order, or the new word is accidentally shadowed by another.
Another issue is that it does not help in case of indirect mutual recursion.
Therefore, as a portable module, I would implement that using a
Recognizer API.
Usage might look like this:
forward( foo bar baz ){
} \ the lexical scope where `foo` `bar` `baz` are visible.
--
Ruvim
Recursion by name means changing all the references when the name changes. ANS notes RECURSE was originally called MYSELF - a clever name if ever
there was. Perhaps they ought to have stayed with that.
Recursion by name means changing all the references when the name changes.
forward foo okfoo<<< \ not previously defined
see foofoo<<< \ resolves foo in the colon definition
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 76 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 53:59:14 |
| Calls: | 1,265 |
| Calls today: | 1 |
| Files: | 1,362 |
| D/L today: |
3 files (2,684K bytes) |
| Messages: | 285,448 |