Math demos
fibonacci : Int -> Int = \n.
(if (lt n 2) n (add (fibonacci (sub n 1)) (fibonacci (sub n 2))))
factorial : Int -> Int = \n.
(if (eq n 0) 1 (mul n (factorial (sub n 1))))
-- Greatest common divisor, tail recursive.
gcd : Int -> Int -> Int = \a. \b.
(if (eq b 0) a (gcd b (mod a b)))
isPrime : Int -> Bool = \n.
let divides = \d. (eq (mod n d) 0) in
let check = \d. (if (gt (mul d d) n) True (if (divides d) False (check (add d 1)))) in
(and (gt n 1) (check 2))
(println (concat "fibonacci 10 = " (show (fibonacci 10))))
(println (concat "factorial 10 = " (show (factorial 10))))
(println (concat "gcd 1071 462 = " (show (gcd 1071 462))))
(println (concat "primes below 30: " (show (filter isPrime (range 0 30)))))
Output
fibonacci 10 = 55
factorial 10 = 3628800
gcd 1071 462 = 21
primes below 30: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Run it yourself from the repository root: lbd run examples/math_demos.lbd