From 044360219f2e4e0b5b5a95cb7ede00753340d61d Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Fri, 19 Apr 2024 20:21:56 +0100 Subject: Initial import --- examples/fact.fco | 14 ++++++++++++++ examples/fib.fco | 13 +++++++++++++ examples/for.fco | 11 +++++++++++ 3 files changed, 38 insertions(+) create mode 100644 examples/fact.fco create mode 100644 examples/fib.fco create mode 100644 examples/for.fco (limited to 'examples') 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 -- cgit v1.2.3