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