fizzbuzz
examples/fizzbuzz.omg
examples/fizzbuzz.omg33 lines
1{- FizzBuzz, as a program.
2
3 omegyu run examples/fizzbuzz.omg -- 15
4
5 The program is a value of type `IO unit`: a description of what to do,
6 which `omegyu run` carries out.
7-}
8
9let Console = std:IO
10
11let fizzbuzz
12 : Natural -> Text
13 = \n ->
14 let fizz = Natural/isZero (Natural/mod n 3)
15 let buzz = Natural/isZero (Natural/mod n 5)
16 in if fizz && buzz
17 then "FizzBuzz"
18 else if fizz
19 then "Fizz"
20 else if buzz
21 then "Buzz"
22 else Natural/show n
23
24-- The first argument, if it is a number; 15 otherwise.
25let limit
26 : List Text -> Natural
27 = \arguments ->
28 std:Optional.default 15 (std:Optional.andThen (std:List.head arguments) Natural/parse)
29
30in do {
31 arguments <- Console.args;
32 Console.times (limit arguments) (\(k : Natural) -> Console.putStrLn (fizzbuzz (k + 1)))
33 }