on 492ae2f help sign in
58 lines 1.9 kB history raw
1---
2sort: 1
3next: true
4title: Demo
5---
7# Demo
9Here's everyone's favorite whiteboard example, FizzBuzz. Each
10line is commented with its number:
12```
13:gate end/atom :: 1
14:var count 1 :: 2
15:loop :: 3
16:if =(end count) :: 4
17 ~ :: 5
18:cons :: 6
19 :if =(0 (mod count 15)) :: 7
20 "FizzBuzz" :: 8
21 :if =(0 (mod count 5)) :: 9
22 "Fizz" :: 10
23 :if =(0 (mod count 3)) :: 11
24 "Buzz" :: 12
25 (text :wrap(count)) :: 13
26:moar(count (add 1 count)) :: 14
27```
29Can you understand this code by just looking at it? Hopefully,
30but let's explain it anyway.
32Hoon is a functional language. FizzBuzz is usually defined as a
33side effect, but Hoon has no side effects. This code will
34*make* a list of strings, not *print* a list of strings.
36Line 1 defines a function of one argument, `end`, an unsigned
37integer.
39Line 2 declares a variable, `count`, with initial value `1`.
41Line 3 begins a loop.
43Line 4 checks if `count` equals `end`. If so, line 5 (and the
44whole loop) produces the value `~`, null. If not, line 6 begins
45an ordered pair.
47The head of the pair is "FizzBuzz", or "Fizz", or "Buzz", if the
48respective tests hit. If not, line 13 wraps a runtime type
49around `count`, then prints the type-value pair to a string (like
50C `sprintf()`). This string is the first item in the output list.
52The tail of the pair is line 14, which repeats the loop (like
53Clojure `recur`), with `count` incremented. This list of strings
54is the rest of the output list.
56You can test this code by mounting your home desk with `|mount %home`;
57copying it into `home/gen/fizzbuzz.hoon` in your Urbit pier; then
58typing `+fizzbuzz 100` in the Urbit dojo (shell).