sample-code: Add some sample Conlang programs :D

This commit is contained in:
John 2023-10-30 00:23:43 -05:00
parent c1c834701a
commit c3e02d21ad
2 changed files with 35 additions and 0 deletions

15
sample-code/fibonacci.cl Normal file
View File

@ -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
}
}

20
sample-code/fizzbuzz.cl Normal file
View File

@ -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
})
}
}