Liste des Groupes | Revenir à cl forth |
On 7/14/24 16:43, Marc Olschok wrote:That's why the simplest way to achieve recursion by name is:On Sat, 06 Jul 2024 22:20:45 Krishna Myneni wrote:...THank you for the recursive version. It's nice to have both looping and recursive examples.How many different ways can you choose 42 distinct objects, 21 at a>
time? This is "n choose k" or the binomial coefficent.
Yes, M*/ comes in handy for C(n,0) = 1 , C(n+1,k+1) = C(n,k)*n/k
>
42 21 binom d.
gives 538257874440
>
where
>
: 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 ;
>
Gforth SEE nicely replaced the original 'recurse' with 'binom' for
better readability.
>
There's a reason why RECURSE (or equivalent) is preferable to having the name of the word in the output of SEE in Forth. This is because it is possible to have an earlier definition with the same name and to call it from within the definition e.g.
: binom ... ;
: binom ... binom ... ;
In the later definition of BINOM Forth requires the call to BINOM be the earlier definition if it exists in the search order. If it does not exist in the search order, then I believe the standard would require an error to occur.
Les messages affichés proviennent d'usenet.