on master help sign in
Simplify the Hoon demo
7c26111Joshua Reagan
docs/hoon/demo.md+43−48
@@ -7,12 +7,13 @@ title: Demo
7
7
8# Demo
8# Demo
9
9
10−Here's everyone's favorite whiteboard example, FizzBuzz. Each
10+Here's everyone's favorite whiteboard example,
11+[FizzBuzz](https://en.wikipedia.org/wiki/Fizz_buzz). Each
11line is commented with its number:
12line is commented with its number:
12
13
13```
14```
14−|= end=@ud :: 1
15+|= end=@ :: 1
15−=/ count=@ud 1 :: 2
16+=/ count=@ 1 :: 2
16|- :: 3
17|- :: 3
17^- (list tape) :: 4
18^- (list tape) :: 4
18?: =(end count) :: 5
19?: =(end count) :: 5
@@ -24,65 +25,59 @@ line is commented with its number:
24 "Fizz" :: 11
25 "Fizz" :: 11
25 ?: =(0 (mod count 3)) :: 12
26 ?: =(0 (mod count 3)) :: 12
26 "Buzz" :: 13
27 "Buzz" :: 13
27− (scow %ud count) :: 14
28+ <count> :: 14
28$(count (add 1 count)) :: 15
29$(count (add 1 count)) :: 15
29```
30```
30
31
31−Hoon is a functional language. FizzBuzz is usually defined as a
32+When FizzBuzz is written in non-functional languages the
33+output is generated as a side effect of the program, e.g., with
34+`printf()`. Hoon is a functional language so technically it
32−side effect, but officially Hoon has no side effects. Accordingly,
35+has no side effects. Accordingly, the code above *returns* a
33−this code will *produce* a list of strings, not *print* a list of
36+list of strings, it doesn't print them. (If you run it from the
34−strings. (Of course, if you run this code from the Urbit
35−[_Dojo_](../../using/shell) (shell) you will be able to see the
37+Urbit shell, [_Dojo_](../../using/shell), you will of course be
36−product.)
38+able to see the product.)
37
39
38−The code produces a list in which each item is either a number
40+The code produces a list in which each item is either a number,
39−(as a string), "Fizz", "Buzz", or "Fizzbuzz". The value `~` is
41+"Fizz", "Buzz", or "Fizzbuzz". The value `~` is "null" and indicates
40−"null" and indicates the end of the list.
42+the end of the list.
41
43
42−Line 1 uses the _[rune](../../about/glossary#rune)_ `|=` (pronounced
44+Line 1 uses the _[rune](../../about/glossary#rune)_ `|=` to define
43−["bar-tis"](../syntax/#-glyphs-and-characters)) to define a function
44−that takes one argument, labelled `end`. The argument must be of the
45+a function that takes one input, `end`. The input value must be of
45−type `@ud`, an unsigned decimal number.
46+the type `@`, an atom (i.e., an unsigned integer).
46
47
47−Line 2 uses the rune `=/` (pronounced "tis-fas") to declare a variable,
48+Line 2 uses `=/` to declare a variable, `count`, as an atom with an
48−`count` of the unsigned decimal number type, with initial value `1`.
49+initial value of `1`.
49
50
50−Line 3 uses the rune `|-` ("bar-hep") to begin a loop.
51+Line 3 uses `|-` to indicate the starting point of a loop.
51
52
52Line 4 defines the product of the loop as a list of `tape`s.
53Line 4 defines the product of the loop as a list of `tape`s.
53(A `tape` is a list of characters, one of Hoon's two string
54(A `tape` is a list of characters, one of Hoon's two string
54−types.) The rune `^-` ("ket-hep") is used for defining
55+types.) The rune `^-` is used for defining the type of value the
55−the type of value the function must produce.
56+function is required to produce.
56
57
57−Line 5 uses the rune `?:` ("wut-col"), which is your classic "if, then,
58+Line 5 uses the rune `?:` which is an ordinary "if-then-else"
58−else" conditional. The "if" clause uses the irregular form of the rune
59+conditional. If `=(end count)` evaluates as true, then line 6 is
59−`.=` ("dot-tis"), one of the fundamental [Nock](../../nock/definition)
60+evaluated next. Otherwise line 7 and on is evaluated.
60−instructions, to perform an equality test on the values of `count` and
61−`end`. This will produce either `%.y` (yes, the number 0) or `%.n`
62−(no, 1) and consequently compute the "then" or "else" clause
63−respectively.
64
61
65−If the values of `count` and `end` are equal, line 6 (and the whole loop)
62+If the values of `count` and `end` are equal then line 6 produces the
66−returns the value `~`. The list is finished.
63+value `~`, indicating the end of the list. The program is finished.
67
64
68−If not, line 7 uses the rune `:-` ("col-hep") to produce a 'cell', which
65+If not, line 7 uses the rune `:-` to produce an
69−is an ordered pair of Hoon values. This pair will be: (a) the next item
66+[ordered pair](https://en.wikipedia.org/wiki/Ordered_pair) of values.
70−in the list, determined by more `?:` conditionals in lines 8-14; and (b)
67+The pair created will be `[a b]`: (a) the product of lines 8-14; and
71−the rest of the list, determined by line 15.
68+(b) the product of line 15.
72
69
73−(a) The head of the pair is: (i) "FizzBuzz", or "Fizz", or
70+(a) The first value of the pair is: (i) "FizzBuzz", or "Fizz", or
74−"Buzz", if the respective tests hit; otherwise, it's
71+"Buzz", if the respective conditional tests hit; otherwise, it's
75(ii) the number in question, `count`. In case (ii), line 14
72(ii) the number in question, `count`. In case (ii), line 14
76−uses the Hoon standard library function `++scow` to take the value of
73+uses the `< >` symbols to convert `count` from an `@` to a `tape`.
77−`count`, of type `@ud`, and pretty-print it as a `tape`.
78
74
79−(b) The tail of the pair&mdash;the rest of the output list&mdash;is
75+(b) The second value of the pair&mdash;i.e., the rest of the output
80−produced at line 15, which uses the irregular `$()` syntax for the rune
76+list&mdash;is produced by line 15. This line uses `$()` to loop back
81−`%=` ("cen-tis") to repeat the loop set above (with the `|-` in line 3)
77+to the `|-` in line 3, but with `count` increased in value by `1`.
82−with `count` incremented.
83
78
84−You can test this code by mounting your home desk with
79+You can test this code for yourself by (i) entering `|mount %` in the
85−`|mount %` from the Dojo; saving the above code as
80+Dojo to mount your home desk, if you haven't already done so; (ii)
86−`home/gen/fizzbuzz.hoon` in your Urbit
81+saving the above code as `home/gen/fizzbuzz.hoon` in your Urbit's
87−[pier](../../about/glossary#pier); then typing
82+[pier](../../about/glossary#pier); then (iii) entering `+fizzbuzz 100`
88−`+fizzbuzz 100` in the dojo.
83+in the dojo.