Factorial in Forth
Example for versions
gforth 0.7.0
This example uses recursive factorial definition. Forth is a stack-oriented language, so all commands (words) perform stack manipulations. Thus, dup
duplicates the topmost element of the stack, a constant pushes itself on the top of the stack, >
compares second-topmost element to the topmost element and pushes the result, -
subtracts topmost element from second-topmost, etc.
: fac recursive
dup 1 > IF
dup 1 - fac *
else
drop 1
endif ;
: lp
swap 1 + swap
do
i . i ." ! = " i fac . cr
loop ;
16 0 lp
Comments
]]>blog comments powered by Disqus
]]>