@@ -11,26 +11,23 @@ Here's everyone's favorite whiteboard example, FizzBuzz. Each
11line is commented with its number:
11line is commented with its number:
14−:gate end/atom :: 1
14+|= end/atom :: 1
15−:var count 1 :: 2
15+=/ count 1 :: 2
17−:cast (list tape) :: 4
17+^- (list tape) :: 4
18−:if =(end count) :: 5
18+?: =(end count) :: 5
21− :if =(0 (mod count 15)) :: 8
21+ ?: =(0 (mod count 15)) :: 8
22− "FizzBuzz" :: 9
22+ "FizzBuzz" :: 9
23− :if =(0 (mod count 5)) :: 10
23+ ?: =(0 (mod count 5)) :: 10
24− "Fizz" :: 11
24+ "Fizz" :: 11
25− :if =(0 (mod count 3)) :: 12
25+ ?: =(0 (mod count 3)) :: 12
26− "Buzz" :: 13
26+ "Buzz" :: 13
27− (pave :wrap(count)) :: 14
27+ (pave !>(count)) :: 14
28−:moar(count (add 1 count)) :: 15
28+$(count (add 1 count)) :: 15
31−Can you understand this code by just looking at it? Maybe.
32−But let's explain it anyway.
34Hoon is a functional language. FizzBuzz is usually defined as a
31Hoon is a functional language. FizzBuzz is usually defined as a
35side effect, but officially Hoon has no side effects. This code
32side effect, but officially Hoon has no side effects. This code
36will *return* a list of strings, not *print* a list of strings.
33will *return* a list of strings, not *print* a list of strings.
@@ -41,8 +38,8 @@ The code produces a list in which each item is either a number
41(as a string), "Fizz", "Buzz", or "Fizzbuzz". The value `~` is
38(as a string), "Fizz", "Buzz", or "Fizzbuzz". The value `~` is
42"null" and indicates the end of the list.
39"null" and indicates the end of the list.
44−Line 1 defines a function of one argument, `end`, an unsigned
41+Line 1 uses `|=` to define a function of one argument, `end`,
45−integer.
42+which is of the type `atom` (i.e. an unsigned integer).
47Line 2 declares a variable, `count`, with initial value `1`.
44Line 2 declares a variable, `count`, with initial value `1`.
@@ -50,24 +47,28 @@ Line 3 begins a loop.
51Line 4 defines the product of the loop as a list of `tape`s.
48Line 4 defines the product of the loop as a list of `tape`s.
52(A `tape` is a list of characters, one of Hoon's two string
49(A `tape` is a list of characters, one of Hoon's two string
53−types.)
50+types.) The `^-`
51+*[rune](https://urbit.org/docs/about/glossary#rune)* is used for
52+defining the type the function is to return.
54+Line 5 uses the `?:` rune, which is a conditional. This line
55−Line 5 checks if `count` equals `end`. If so, line 6 (and the
55+checks whether `count` equals `end`. If so, line 6 (and the
56−whole loop) returns the value `~`. If not, line 7 produces an
56+whole loop) returns the value `~`. The list is finished. If not,
57−ordered pair. (This pair will be: (a) the next item in the list,
57+line 7 produces an ordered pair. (This pair will be: (a) the next
58−and (b) the rest of the list.)
58+item in the list, determined by lines 8-14; and (b) the rest of
59+the list, determined by line 15.)
60(a) The head of the pair is: (i) "FizzBuzz", or "Fizz", or
61(a) The head of the pair is: (i) "FizzBuzz", or "Fizz", or
61"Buzz", if the respective tests hit; otherwise, it's
62"Buzz", if the respective tests hit; otherwise, it's
62(ii) the number in question, `count`. In case (ii), line 14
63(ii) the number in question, `count`. In case (ii), line 14
63−makes `count` into a `tape`, first by using `:wrap` to produce a
64+makes `count` into a `tape`, first by using `!>` to produce a
64pair: runtime type of `count`, and `count` itself. `pave` is a
65pair: runtime type of `count`, and `count` itself. `pave` is a
65Hoon function that converts the type-value pair to a `tape`
66Hoon function that converts the type-value pair to a `tape`
66(like C `sprintf()`).
67(like C `sprintf()`).
68(b) The tail of the pair—the rest of the output list—is
69(b) The tail of the pair—the rest of the output list—is
69−produced at line 15, which repeats the loop (like Clojure
70+produced at line 15, which uses `$()` to repeat the loop (like
70−`recur`), with `count` incremented.
71+Clojure `recur`), with `count` incremented.
72You can test this code by mounting your home desk with
73You can test this code by mounting your home desk with
73`|mount %` from the dojo; saving the above code as
74`|mount %` from the dojo; saving the above code as