about summary refs log tree commit diff
path: root/examples/gcd.ccl
diff options
context:
space:
mode:
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