Re: recursion

Liste des GroupesRevenir à cl forth 
Sujet : Re: recursion
De : ruvim.pinka (at) *nospam* gmail.com (Ruvim)
Groupes : comp.lang.forth
Date : 16. Jul 2024, 08:55:29
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v7591i$15jt2$2@dont-email.me>
References : 1 2 3 4 5 6 7
User-Agent : Mozilla Thunderbird
On 2024-07-15 23:37, Ruvim wrote:
On 2024-07-15 18:45, Marc Olschok wrote:
On Mon, 15 Jul 2024 15:29:17 Anton Ertl wrote:
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.
  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.
  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.
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).
In the former approach (via variants of "recursive") you can get bugs that seem very strange at the first glance.
--
Ruvim

Date Sujet#  Auteur
6 Jul 24 * exercise in double number arithmetic69Krishna Myneni
6 Jul 24 +* Re: exercise in double number arithmetic16Ahmed
7 Jul 24 i`* Re: exercise in double number arithmetic15Krishna Myneni
7 Jul 24 i +* Re: exercise in double number arithmetic13Ahmed
7 Jul 24 i i`* Re: exercise in double number arithmetic12Krishna Myneni
7 Jul 24 i i +* Re: exercise in double number arithmetic10Gerry Jackson
8 Jul 24 i i i`* Re: exercise in double number arithmetic9Krishna Myneni
10 Jul 24 i i i `* Re: exercise in double number arithmetic8Gerry Jackson
10 Jul 24 i i i  `* Re: exercise in double number arithmetic7dxf
10 Jul 24 i i i   +- Re: exercise in double number arithmetic1Krishna Myneni
10 Jul 24 i i i   `* Re: exercise in double number arithmetic5dxf
11 Jul 24 i i i    `* Re: exercise in double number arithmetic4Krishna Myneni
11 Jul 24 i i i     `* Re: exercise in double number arithmetic3minforth
12 Jul 24 i i i      `* Re: exercise in double number arithmetic2Krishna Myneni
12 Jul 24 i i i       `- Re: exercise in double number arithmetic1minforth
7 Jul 24 i i `- Re: exercise in double number arithmetic1Ahmed
7 Jul 24 i `- Re: exercise in double number arithmetic1minforth
6 Jul 24 +* Re: exercise in double number arithmetic3Ahmed
7 Jul 24 i`* Re: exercise in double number arithmetic2Krishna Myneni
7 Jul 24 i `- Re: exercise in double number arithmetic1Ahmed
7 Jul 24 +* Re: exercise in double number arithmetic14mhx
7 Jul 24 i`* Re: exercise in double number arithmetic13Ahmed
7 Jul 24 i +- Re: exercise in double number arithmetic1mhx
7 Jul 24 i +* Re: exercise in double number arithmetic2minforth
7 Jul 24 i i`- Re: exercise in double number arithmetic1Ahmed
7 Jul 24 i `* Re: exercise in double number arithmetic9Ahmed
7 Jul 24 i  `* Re: exercise in double number arithmetic8Ahmed
7 Jul 24 i   `* Re: exercise in double number arithmetic7mhx
7 Jul 24 i    +- Re: exercise in double number arithmetic1Ahmed
8 Jul 24 i    `* Re: exercise in double number arithmetic5Krishna Myneni
8 Jul 24 i     `* Re: exercise in double number arithmetic4Ahmed
8 Jul 24 i      +- Re: exercise in double number arithmetic1minforth
8 Jul 24 i      `* Re: exercise in double number arithmetic2Krishna Myneni
8 Jul 24 i       `- Re: exercise in double number arithmetic1Ahmed
14 Jul 24 `* Re: exercise in double number arithmetic35Marc Olschok
14 Jul 24  +- Re: exercise in double number arithmetic1Marc Olschok
15 Jul 24  +* Re: exercise in double number arithmetic12Krishna Myneni
15 Jul 24  i+* Re: exercise in double number arithmetic8minforth
15 Jul 24  ii`* Re: exercise in double number arithmetic7minforth
15 Jul 24  ii `* Re: exercise in double number arithmetic6Ahmed
15 Jul 24  ii  `* Re: exercise in double number arithmetic5minforth
15 Jul 24  ii   +- Re: exercise in double number arithmetic1minforth
15 Jul 24  ii   +- Re: exercise in double number arithmetic1Ahmed
15 Jul 24  ii   `* Re: exercise in double number arithmetic2albert
15 Jul 24  ii    `- Re: exercise in double number arithmetic1minforth
15 Jul 24  i+- Re: exercise in double number arithmetic1Anton Ertl
15 Jul 24  i+- Re: exercise in double number arithmetic1Gerry Jackson
31 Jul 24  i`- Re: exercise in double number arithmetic1Marc Olschok
15 Jul 24  `* Re: exercise in double number arithmetic21minforth
15 Jul 24   `* recursion (was: exercise in double number arithmetic)20Anton Ertl
15 Jul 24    +* Re: recursion10Marc Olschok
15 Jul 24    i`* Re: recursion9Ruvim
15 Jul 24    i +* Re: recursion5Gerry Jackson
15 Jul 24    i i+* Re: recursion2Gerry Jackson
16 Jul 24    i ii`- Re: recursion1Gerry Jackson
16 Jul 24    i i`* Re: recursion2Ruvim
16 Jul 24    i i `- Re: recursion1dxf
16 Jul 24    i +- Re: recursion1mhx
16 Jul 24    i `* Re: recursion2Ruvim
16 Jul 24    i  `- Re: recursion1Ruvim
16 Jul 24    `* Re: recursion9sjack
16 Jul 24     +- Re: recursion1sjack
16 Jul 24     +* Re: recursion4minforth
16 Jul 24     i`* Re: recursion3sjack
16 Jul 24     i `* Re: recursion2minforth
16 Jul 24     i  `- Re: recursion1mhx
17 Jul 24     `* Re: recursion3dxf
22 Jul 24      `* Re: recursion2Stephen Pelc
22 Jul 24       `- Re: recursion1Anton Ertl

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal