10Here's everyone's favorite whiteboard example, FizzBuzz. Each
11line is commented with its number:
21 :if =(0 (mod count 15)) :: 8
23 :if =(0 (mod count 5)) :: 10
25 :if =(0 (mod count 3)) :: 12
27 (pave :wrap(count)) :: 14
28:moar(count (add 1 count)) :: 15
31Can you understand this code by just looking at it? Maybe.
32But let's explain it anyway.
34Hoon is a functional language. FizzBuzz is usually defined as a
35side effect, but Hoon has no side effects. This code will
36*make* a list of strings, not *print* a list of strings.
38Line 1 defines a function of one argument, `end`, an unsigned
41Line 2 declares a variable, `count`, with initial value `1`.
45Line 4 casts the product of the loop to a list of `tape`
46(string as a character list).
48Line 5 checks if `count` equals `end`. If so, line 6 (and the
49whole loop) produces the value `~`, null, the empty list. If
50not, line 7 begins an ordered pair.
52The head of the pair is "FizzBuzz", or "Fizz", or "Buzz", if the
53respective tests hit. If not, line 14 wraps a runtime type
54around `count`, then prints the type-value pair to a string (like
55C `sprintf()`). This string is the first item in the output list.
57The tail of the pair is line 15, which repeats the loop (like
58Clojure `recur`), with `count` incremented. This list of strings
59is the rest of the output list.
61You can test this code by mounting your home desk with `|mount %`;
62copying it into `home/gen/fizzbuzz.hoon` in your Urbit pier; then
63typing `+fizzbuzz 100` in the Urbit dojo (shell).
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list