diff options
| author | Brian Cully <bjc@spork.org> | 2025-08-25 12:29:54 -0400 |
|---|---|---|
| committer | Brian Cully <bjc@spork.org> | 2025-08-25 12:36:03 -0400 |
| commit | 1f3049300fdbdf4480681eb3c5d7803bef454b73 (patch) | |
| tree | b31c41a249b14442492b594b2529107f1533135e | |
| parent | 8275403e955e8998c61ecaa07d70949e3bd53a66 (diff) | |
| download | automathon-1f3049300fdbdf4480681eb3c5d7803bef454b73.tar.gz automathon-1f3049300fdbdf4480681eb3c5d7803bef454b73.zip | |
add slow factorial example
more blinken!
| -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 |
