aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2024-04-19 20:21:56 +0100
committerJuan J. Martinez <jjm@usebox.net>2024-04-19 20:25:26 +0100
commit044360219f2e4e0b5b5a95cb7ede00753340d61d (patch)
tree4c2341a6e4941b3c3b70ca9941699e8f84353901 /examples
downloadfunco-044360219f2e4e0b5b5a95cb7ede00753340d61d.tar.gz
funco-044360219f2e4e0b5b5a95cb7ede00753340d61d.zip
Initial import
Diffstat (limited to 'examples')
-rw-r--r--examples/fact.fco14
-rw-r--r--examples/fib.fco13
-rw-r--r--examples/for.fco11
3 files changed, 38 insertions, 0 deletions
diff --git a/examples/fact.fco b/examples/fact.fco
new file mode 100644
index 0000000..6eccca9
--- /dev/null
+++ b/examples/fact.fco
@@ -0,0 +1,14 @@
+# Factorial of a number
+def fact(n acc)
+ if =(n 1)
+ acc
+ else
+ # tagging a tail call with "@" so it can be optimized
+ @fact(-(n 1) *(acc n))
+ end
+end
+
+def main()
+ display("Running fact(50)...")
+ display(fact(50. 1))
+end
diff --git a/examples/fib.fco b/examples/fib.fco
new file mode 100644
index 0000000..d459341
--- /dev/null
+++ b/examples/fib.fco
@@ -0,0 +1,13 @@
+# fibonacci number
+def fib(n)
+ if <(n 2)
+ n
+ else
+ +(fib(- (n 1)) fib(- (n 2)))
+ end
+end
+
+def main()
+ display("Running fib(20)...")
+ display(fib(20))
+end
diff --git a/examples/for.fco b/examples/for.fco
new file mode 100644
index 0000000..9710fe2
--- /dev/null
+++ b/examples/for.fco
@@ -0,0 +1,11 @@
+# recursive for loop
+def for(n fn)
+ if >(n 0)
+ fn(n)
+ @for(-(n 1) fn)
+ end
+end
+
+def main()
+ for(10000 display)
+end