summaryrefslogtreecommitdiff
path: root/ack.kpl
diff options
context:
space:
mode:
Diffstat (limited to 'ack.kpl')
-rw-r--r--ack.kpl29
1 files changed, 29 insertions, 0 deletions
diff --git a/ack.kpl b/ack.kpl
new file mode 100644
index 0000000..80a3a74
--- /dev/null
+++ b/ack.kpl
@@ -0,0 +1,29 @@
+
+// Ackermann function
+
+`export ack : Fn[m; n] $ (
+ ? !=(`type m; `type n) {
+ `exit "m and n must be the same type and above zero"
+ }
+ ? {
+ m = 0 { n + 1 }
+ &(m > 0; n = 0) { ack `call_sync (m - 1; 1) }
+ &(m > 0; n > 0) { ack `call_sync (m - 1; ack `call_sync (m; n - 1)) }
+ { 0 }
+ }
+)
+
+`export ack_string : Fn[m; n] $ (
+ String $ ("ack("; m; " "; n; ") = "; ack `call_sync (m; n); "\n")
+)
+
+`is_main Fn $ (
+ ( args ) : `use "sys"
+ ? 4 != `length args {
+ `return `error String $ (
+ "Got: "; " " `join args; "\n"
+ "Usage: "; args `get 0; " "; args `get 1; " <m> <n>\n"
+ )
+ }
+ `print ack_string `call_sync (I64 $ args `get -2; I64 $ args `get -1)
+)