From c3e02d21ad28731bb372fa83f57772bc24c9839a Mon Sep 17 00:00:00 2001 From: John Date: Mon, 30 Oct 2023 00:23:43 -0500 Subject: [PATCH] sample-code: Add some sample Conlang programs :D --- sample-code/fibonacci.cl | 15 +++++++++++++++ sample-code/fizzbuzz.cl | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 sample-code/fibonacci.cl create mode 100644 sample-code/fizzbuzz.cl diff --git a/sample-code/fibonacci.cl b/sample-code/fibonacci.cl new file mode 100644 index 0000000..ef36508 --- /dev/null +++ b/sample-code/fibonacci.cl @@ -0,0 +1,15 @@ +// Calculate Fibonacci numbers + +fn main() -> i128 { + print("fib(10):"); + fib(10) +} + +/// Implements the classic recursive definition of fib() +fn fib(a: i128) -> i128 { + if a > 1 { + fib(a - 1) + fib(a - 2) + } else { + 1 + } +} diff --git a/sample-code/fizzbuzz.cl b/sample-code/fizzbuzz.cl new file mode 100644 index 0000000..333b76a --- /dev/null +++ b/sample-code/fizzbuzz.cl @@ -0,0 +1,20 @@ +// FizzBuzz, using the unstable variadic-`print` builtin + +fn main() { + fizz_buzz(10, 20) +} + +// Outputs FizzBuzz for numbers between `start` and `end`, inclusive +fn fizz_buzz(start: i128, end: i128) { + for x in start..=end { + print(if x % 15 == 0 { + "FizzBuzz" + } else if 0 == x % 3 { + "Fizz" + } else if x % 5 == 0 { + "Buzz" + } else { + x + }) + } +}