diff options
Diffstat (limited to 'site')
| -rw-r--r-- | site/samples/slo-fac.fs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/site/samples/slo-fac.fs b/site/samples/slo-fac.fs new file mode 100644 index 0000000..0ba50ca --- /dev/null +++ b/site/samples/slo-fac.fs @@ -0,0 +1,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 |
