From 1f3049300fdbdf4480681eb3c5d7803bef454b73 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Mon, 25 Aug 2025 12:29:54 -0400 Subject: add slow factorial example more blinken! --- site/samples/slo-fac.fs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 site/samples/slo-fac.fs (limited to 'site/samples') 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 -- cgit v1.3