aboutsummaryrefslogtreecommitdiff
path: root/examples/fib_tc.fco
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2024-06-18 18:57:14 +0100
committerJuan J. Martinez <jjm@usebox.net>2024-06-18 18:57:14 +0100
commit88781ff50d9c43b79192448254e086af75cfdbd6 (patch)
treed0b565fbf4e896c4a925ee6bdc6cc6d5262980d9 /examples/fib_tc.fco
parent1607b9d3d2a3b511b7914828b4ad6533b1767317 (diff)
downloadfunco-88781ff50d9c43b79192448254e086af75cfdbd6.tar.gz
funco-88781ff50d9c43b79192448254e086af75cfdbd6.zip
Fibonacci number with tail call optimizationHEADmain
Diffstat (limited to 'examples/fib_tc.fco')
-rw-r--r--examples/fib_tc.fco19
1 files changed, 19 insertions, 0 deletions
diff --git a/examples/fib_tc.fco b/examples/fib_tc.fco
new file mode 100644
index 0000000..b231751
--- /dev/null
+++ b/examples/fib_tc.fco
@@ -0,0 +1,19 @@
+# fibonacci number with tail call optimization
+def fib(n)
+ def fibrec(n acc1 acc2)
+ if =(n 0)
+ acc1
+ elif =(n 1)
+ acc2
+ else
+ @fibrec(-(n 1) acc2 +(acc1 acc2))
+ end
+ end
+
+ fibrec(90 0 1)
+end
+
+def main()
+ display("Running fib(90)...")
+ display(fib(90))
+end