aboutsummaryrefslogtreecommitdiff
path: root/buzzard/demo4.th
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-06-10 23:18:31 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-06-10 23:18:31 +0200
commitda312e375eb0a0758a4dd72e287d3aba86c04d99 (patch)
treeaa7d3631273b516e7f10ef40a53a5edd21a81b7e /buzzard/demo4.th
parente4d7ac43458f9f96a15041d35feddecca20ddbba (diff)
Add FIRST & THIRD almost FORTH
Diffstat (limited to 'buzzard/demo4.th')
-rw-r--r--buzzard/demo4.th30
1 files changed, 30 insertions, 0 deletions
diff --git a/buzzard/demo4.th b/buzzard/demo4.th
new file mode 100644
index 0000000..3f9a76d
--- /dev/null
+++ b/buzzard/demo4.th
@@ -0,0 +1,30 @@
+( compute factorial recursively )
+( take x as input, return x! and x as output )
+
+: fact-help
+
+ dup if
+ 1 - ( leave x-1 on top )
+ fact-help ( leave x-1, [x-1]! )
+ 1 + ( leave x, [x-1]!, x )
+ swap over swap ( leave [x-1]!, x, x )
+ * ( into x!, x )
+ swap ( into x, x! )
+ else
+ 1 swap
+ then
+;
+
+: fact
+
+ fact-help
+ drop
+
+;
+
+: demo4
+ " 4 factorial is: " 4 fact . cr
+ " 6 factorial is: " 6 fact . cr
+;
+
+demo4