about summary refs log tree commit diff
path: root/examples/gcd.ccl
diff options
context:
space:
mode:
authorNakidai <nakidai@disroot.org>2025-03-26 20:22:14 +0300
committerNakidai <nakidai@disroot.org>2025-03-26 20:22:14 +0300
commit6fa64679e24b2bef4aee49657df1b2f411b18392 (patch)
tree9a0a34ec741e24dc8ee80231c71fa5baaed02f78 /examples/gcd.ccl
parentec7a100bd2b528ebc467993fc289bdbbe97dc57e (diff)
download3cl-6fa64679e24b2bef4aee49657df1b2f411b18392.tar.gz
3cl-6fa64679e24b2bef4aee49657df1b2f411b18392.zip
Add examples as tests
Diffstat (limited to 'examples/gcd.ccl')
-rw-r--r--examples/gcd.ccl25
1 files changed, 25 insertions, 0 deletions
diff --git a/examples/gcd.ccl b/examples/gcd.ccl
new file mode 100644
index 0000000..2f0a0d5
--- /dev/null
+++ b/examples/gcd.ccl
@@ -0,0 +1,25 @@
+/ Euclid gcd
+/ Last 2 numbers on the stack are arguments
+/ Result is passed through the stack too
+F {
+    / Parse arguments into variables
+    &a=a
+    &b=b
+
+    / If a == b then exit with it
+    $ab?#;
+
+    b[-] &c=c   / a - b
+    &f^+=f c (  / if a - b > 0 (or a > b)
+        $c $b
+    ^=f#) f (   / else
+        $b a[-] $a
+    #)
+
+    / Call ourselves but
+    / with simpler case
+    @F
+}
+
+/ gcd(4, 5)
+^++++ ^+++++ @F