summaryrefslogtreecommitdiffstats
path: root/site/samples/slo-fac.fs
blob: 1de4dc83ea25166b44ac235bfba777ec611089c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
\ calculate factorial by creating a stack of all numbers between n and
\ 1, then multiply every element of that stack together.
\
\ this uses only tail-position call/if, so the call stack never grows
\ past the initial function call.
\
\ this is not meant to be efficient, just exercise parts of the system
\ and be something nice to look at with blinken

: count-to ( n -- n n-1 ... 1 )
    dup
    1 > if
        dup 1 - count-to
    then
;

: stack-mul ( n-x ... n x -- v )
    dup
    1 > if
        rot rot * swap 1 - stack-mul
    else
        drop
    then
;

5 count-to
5 stack-mul
drop