Liste des Groupes | Revenir à cl forth |
On 2024-07-15 18:45, Marc Olschok wrote:One more advantage of this approach is that it works as expected when the compilation word list is absent in the search order. Because the reference in a definition is resolved once the forwarded word name is placed in the same compilation word list (regardless of the search order).On Mon, 15 Jul 2024 15:29:17 Anton Ertl wrote:I think, the word "recursive" is confusing. It looks like an ordinary word, but behaves like an immediate word that changes the current word list, and it does not add any behavior to the current definition.minforth@gmx.net (minforth) writes:>On Mon, 15 Jul 2024 10:41:54 +0000, albert@spenarnc.xs4all.nl wrote:>So I prefer:>
>
:F binom ;
>
:R binom ( n1 n2 -- nd ) \ n k --> C(n,k)
dup 0=
IF 2drop 1 s>d
ELSE 2dup 1- swap 1- swap binom 2swap m*/
THEN ;
>
In my efforts to Make A Lisp (a github https://github.com/kanaka/mal )
I discovered that using recurse is an ugly cludge that present
a lot of problems in refactoring code, if not prevent it.
Forward and resolve definitions is the more sane method, cf. c.
It is hardly more complicated.
IIRC gforth has RECURSIVE to avoid duplicating definitions.
Yes. So for a direct recursion like this one you can write
>
: binom ( n1 n2 -- nd ) recursive \ n k --> C(n,k)
dup 0=
IF 2drop 1 s>d
ELSE 2dup 1- swap 1- swap binom 2swap m*/
THEN ;
Oh that is nice, I did not know about that. And it also avoids the
source paste problem that Albert noted.
Some better variants:
: binom [recursive] ... binom ... ;
recursive
: binom ... binom ... ;
rec: binom ... binom ... ;
An even more better and more general approach:
: binom ... forward binom ... ;
: binom ... forward:binom ... ;
The later one can also be used as:
: foo ... ['] forward:foo ... ;
An advantage of this approach is that it is more general than the "recursive" word, and you also don't have to repeat the definition name.
Les messages affichés proviennent d'usenet.