* Mads Martin Jørgensen mmj@suse.de [011219 12:50]:
- Stefan Reinauer stepan@openbios.net [Dec 19. 2001 01:56]:
-- Ok : FC RECURSE DUP 1 > IF DUP 1 - FC * ELSE DROP 1 ENDIF ; 42 FC .
:-) Maybe our project founder would elaborate on that one? Not all of us speak Forth that good (yet :)
:-) Actually it should have been a little forth word definition that calculates faculty of the given number recursively. But there were 2 mistakes in it. The correct version would be: Ok : fc recursive dup 1 > if dup 1 - fc * then ; 42 fc . (with Ok being the Open Firmware prompt)
The program can be written in a more readable way:
: FC RECURSIVE ( n -- n! | 1 ) \ This is the word definition including \ the stack diagram. DUP 1 > \ safe n and compare it to 1 IF \ if (n>1) DUP 1 - FC * \ fc(n) = fc(n-1) * n ELSE DROP 1 \ If n=1 or below (n! not defined for n<1) \ bail out of recursion. THEN ;
"42 fc ." then pushes 42 to the stack, calculates faculty and prints it.
Best regards, Stefan Reinauer