By Example
Learn Loon through short, complete programs you can run immediately.
Hello World
The simplest Loon program. Square brackets for every call, double quotes for strings.
hello.loon
[fn main []
[println "Hello, world!"]]
[main]Fibonacci
Pattern matching with match and => makes recursive definitions concise. The wildcard _ catches everything else.
fib.loon
[fn fib [n]
[match n
0 0
1 1
_ [+ [fib [- n 1]]
[fib [- n 2]]]]]
[println [fib 10]] ; 55FizzBuzz
Pipe a range through map and print each result. Nested if replaces cond — branches are checked top to bottom.
fizzbuzz.loon
[pipe [range 1 101]
[map [fn [n]
[if [= 0 [% n 15]] "FizzBuzz"
[if [= 0 [% n 3]] "Fizz"
[if [= 0 [% n 5]] "Buzz"
[str n]]]]]]
[each println]]Word Counter
Build a frequency map by folding over a list of words. Maps use {:key value} syntax and get with a default.
words.loon
[let words #["the" "cat" "sat" "on" "the" "mat"]]
[let counts [fold words {} [fn [acc w]
[assoc acc w [+ 1 [get acc w 0]]]]]]
[println counts]Data Pipeline
Chain filter, map, and sort-by with pipe. Each step passes its result to the next.
pipeline.loon
[let people #[
{:name "Alice" :age 32}
{:name "Bob" :age 17}
{:name "Carol" :age 25}]]
[pipe people
[filter [fn [p] [>= [get p :age 0] 18]]]
[map [fn [p] [get p :name "?"]]]
[sort]
[each println]]Effect Handling
Declare an effect with effect, perform it inside functions, and handle it at the call site. Effects replace exceptions and dependency injection.
effects.loon
[effect Ask
[prompt [q : Str] : Str]]
[fn greet []
[let name [Ask.prompt "Name?"]]
[println [str "Hello, " name "!"]]]
[handle [greet]
[Ask.prompt q] "World"]Testing
The test form defines inline tests. Run them with loon test — no separate test files needed.
math.loon
[fn square [x] [* x x]]
[test "square of 0" []
[assert-eq [square 0] 0]]
[test "square of 5" []
[assert-eq [square 5] 25]]
[test "square of negative" []
[assert-eq [square -3] 9]]