about summary refs log tree commit diff
path: root/examples/fac.ccl
diff options
context:
space:
mode:
authorNakidai <nakidai@disroot.org>2025-03-31 17:22:19 +0300
committerNakidai <nakidai@disroot.org>2025-03-31 17:22:19 +0300
commit445784a629c99336d47b31ea9540bd95bbb97dc1 (patch)
tree119e91b0fb0813244a7d3968a7cc405e722a2979 /examples/fac.ccl
parentd0ea12857007f9d6d48df7e87b8dcdff4dd8a75e (diff)
download3cl-445784a629c99336d47b31ea9540bd95bbb97dc1.tar.gz
3cl-445784a629c99336d47b31ea9540bd95bbb97dc1.zip
Add 2 more examples
- Number parser
- Factorial (2 ways to implement)
Diffstat (limited to 'examples/fac.ccl')
-rw-r--r--examples/fac.ccl33
1 files changed, 33 insertions, 0 deletions
diff --git a/examples/fac.ccl b/examples/fac.ccl
new file mode 100644
index 0000000..2d53d79
--- /dev/null
+++ b/examples/fac.ccl
@@ -0,0 +1,33 @@
+^=z / z = 0
+F/actorial / n -- nth-factorial
+{
+    z?+#; / n == 0 ? return 1
+
+    =a$a$a / dup n
+    - @F=a / a = F(n - 1)
+
+    =b / b = n
+
+    / push a * b
+    b[$a]
+    $b-=b
+    b[*]
+}
+
+I/terative factorial / n -- nth-fctorial
+{
+    z?+#;   / n == 0 ? return 1
+
+    =c$c    / c = n - 1
+    $c-=c   / --c
+
+    c (                     / while c > 0
+        $c                  /   push c
+        =a=ba[$b]$a-=aa[*]  /   multiply 2 toppest nums
+        $c-=c               /   --c
+    )
+}
+
+^++++++
+^++++++ @F
+^++++++ @I