on master help sign in
Merge pull request #256 from joshuareagan/patch-4

Simplify the Hoon demo

commitab438a71b033a716ba3239a6f5e85c9f064ac504 authorKeaton Dunsford <keaton.dunsford@gmail.com> dateThu 10 May 2018, 12:00 (8 years ago) parent3b3e7f51a73671
1 file changed+43−48
docs/hoon/demo.md+43−48
docs/hoon/demo.md+43−48
@@ -7,12 +7,13 @@ title: Demo
77
88# Demo
99
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
1112line is commented with its number:
1213
1314```
14−|= end=@ud :: 1
15−=/ count=@ud 1 :: 2
15+|= end=@ :: 1
16+=/ count=@ 1 :: 2
1617|- :: 3
1718^- (list tape) :: 4
1819?: =(end count) :: 5
@@ -24,65 +25,59 @@ line is commented with its number:
2425 "Fizz" :: 11
2526 ?: =(0 (mod count 3)) :: 12
2627 "Buzz" :: 13
27− (scow %ud count) :: 14
28+ <count> :: 14
2829$(count (add 1 count)) :: 15
2930```
3031
31−Hoon is a functional language. FizzBuzz is usually defined as a
32−side effect, but officially Hoon has no side effects. Accordingly,
33−this code will *produce* a list of strings, not *print* a list of
34−strings. (Of course, if you run this code from the Urbit
35−[_Dojo_](../../using/shell) (shell) you will be able to see the
36−product.)
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
35+has no side effects. Accordingly, the code above *produces* a
36+list of strings; it doesn't print them. (If you run it from the
37+Urbit shell, [_Dojo_](../../using/shell), you will of course be
38+able to see the product.)
3739
38−The code produces a list in which each item is either a number
39−(as a string), "Fizz", "Buzz", or "Fizzbuzz". The value `~` is
40−"null" and indicates the end of the list.
40+The code produces a list in which each item is either a number,
41+"Fizz", "Buzz", or "Fizzbuzz". The value `~` is "null" and indicates
42+the end of the list.
4143
42−Line 1 uses the _[rune](../../about/glossary#rune)_ `|=` (pronounced
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−type `@ud`, an unsigned decimal number.
44+Line 1 uses the _[rune](../../about/glossary#rune)_ `|=` to define
45+a function that takes one input, `end`. The input value must be of
46+the type `@`, an atom (i.e., an unsigned integer).
4647
47−Line 2 uses the rune `=/` (pronounced "tis-fas") to declare a variable,
48−`count` of the unsigned decimal number type, with initial value `1`.
48+Line 2 uses `=/` to declare a variable, `count`, as an atom with an
49+initial value of `1`.
4950
50−Line 3 uses the rune `|-` ("bar-hep") to begin a loop.
51+Line 3 uses `|-` to indicate the starting point of a loop.
5152
5253Line 4 defines the product of the loop as a list of `tape`s.
5354(A `tape` is a list of characters, one of Hoon's two string
54−types.) The rune `^-` ("ket-hep") is used for defining
55−the type of value the function must produce.
55+types.) The rune `^-` is used for defining the type of value the
56+function is required to produce.
5657
57−Line 5 uses the rune `?:` ("wut-col"), which is your classic "if, then,
58−else" conditional. The "if" clause uses the irregular form of the rune
59−`.=` ("dot-tis"), one of the fundamental [Nock](../../nock/definition)
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.
58+Line 5 uses the rune `?:` which is an ordinary "if-then-else"
59+conditional. If `=(end count)` evaluates as true, then line 6 is
60+evaluated next. Otherwise line 7 and on is evaluated.
6461
65−If the values of `count` and `end` are equal, line 6 (and the whole loop)
66−returns the value `~`. The list is finished.
62+If the values of `count` and `end` are equal then line 6 produces the
63+value `~`, indicating the end of the list. The program is finished.
6764
68−If not, line 7 uses the rune `:-` ("col-hep") to produce a 'cell', which
69−is an ordered pair of Hoon values. This pair will be: (a) the next item
70−in the list, determined by more `?:` conditionals in lines 8-14; and (b)
71−the rest of the list, determined by line 15.
65+If not, line 7 uses the rune `:-` to produce an
66+[ordered pair](https://en.wikipedia.org/wiki/Ordered_pair) of values.
67+The pair created will be `[a b]`: (a) the product of lines 8-14; and
68+(b) the product of line 15.
7269
73−(a) The head of the pair is: (i) "FizzBuzz", or "Fizz", or
74−"Buzz", if the respective tests hit; otherwise, it's
70+(a) The first value of the pair is: (i) "FizzBuzz", or "Fizz", or
71+"Buzz", if the respective conditional tests hit; otherwise, it's
7572(ii) the number in question, `count`. In case (ii), line 14
76−uses the Hoon standard library function `++scow` to take the value of
77−`count`, of type `@ud`, and pretty-print it as a `tape`.
73+uses the `< >` symbols to convert `count` from an `@` to a `tape`.
7874
79−(b) The tail of the pair&mdash;the rest of the output list&mdash;is
80−produced at line 15, which uses the irregular `$()` syntax for the rune
81−`%=` ("cen-tis") to repeat the loop set above (with the `|-` in line 3)
82−with `count` incremented.
75+(b) The second value of the pair -- i.e., the rest of the output
76+list -- is produced by line 15. This line uses `$()` to loop back
77+to the `|-` in line 3, but with `count` increased in value by `1`.
8378
84−You can test this code by mounting your home desk with
85−`|mount %` from the Dojo; saving the above code as
86−`home/gen/fizzbuzz.hoon` in your Urbit
87−[pier](../../about/glossary#pier); then typing
88−`+fizzbuzz 100` in the dojo.
79+You can test this code for yourself by (i) entering `|mount %` in the
80+Dojo to mount your home desk, if you haven't already done so; (ii)
81+saving the above code as `home/gen/fizzbuzz.hoon` in your Urbit's
82+[pier](../../about/glossary#pier); then (iii) entering `+fizzbuzz 100`
83+in the dojo.