on master help sign in
Changing demo.md from keywords to runes
commit5c09b06dafac89cf209e9134d6793286b29fccb1 authorJoshua Reagan <joshuareagan@users.noreply.github.com> dateThu 5 Oct 2017, 02:27 (9 years ago) parent19bc98f
1 file changed+29−28
docs/hoon/demo.md+29−28
docs/hoon/demo.md+29−28
@@ -11,26 +11,23 @@ Here's everyone's favorite whiteboard example, FizzBuzz. Each
1111line is commented with its number:
1212
1313```
14−:gate end/atom :: 1
15−:var count 1 :: 2
16−:loop :: 3
17−:cast (list tape) :: 4
18−:if =(end count) :: 5
19− ~ :: 6
20−:cons :: 7
21− :if =(0 (mod count 15)) :: 8
22− "FizzBuzz" :: 9
23− :if =(0 (mod count 5)) :: 10
24− "Fizz" :: 11
25− :if =(0 (mod count 3)) :: 12
26− "Buzz" :: 13
27− (pave :wrap(count)) :: 14
28−:moar(count (add 1 count)) :: 15
14+|= end/atom :: 1
15+=/ count 1 :: 2
16+|- :: 3
17+^- (list tape) :: 4
18+?: =(end count) :: 5
19+ ~ :: 6
20+:- :: 7
21+ ?: =(0 (mod count 15)) :: 8
22+ "FizzBuzz" :: 9
23+ ?: =(0 (mod count 5)) :: 10
24+ "Fizz" :: 11
25+ ?: =(0 (mod count 3)) :: 12
26+ "Buzz" :: 13
27+ (pave !>(count)) :: 14
28+$(count (add 1 count)) :: 15
2929```
3030
31−Can you understand this code by just looking at it? Maybe.
32−But let's explain it anyway.
33−
3431Hoon is a functional language. FizzBuzz is usually defined as a
3532side effect, but officially Hoon has no side effects. This code
3633will *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
4138(as a string), "Fizz", "Buzz", or "Fizzbuzz". The value `~` is
4239"null" and indicates the end of the list.
4340
44−Line 1 defines a function of one argument, `end`, an unsigned
45−integer.
41+Line 1 uses `|=` to define a function of one argument, `end`,
42+which is of the type `atom` (i.e. an unsigned integer).
4643
4744Line 2 declares a variable, `count`, with initial value `1`.
4845
@@ -50,24 +47,28 @@ Line 3 begins a loop.
5047
5148Line 4 defines the product of the loop as a list of `tape`s.
5249(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.
5453
55−Line 5 checks if `count` equals `end`. If so, line 6 (and the
56−whole loop) returns the value `~`. If not, line 7 produces an
57−ordered pair. (This pair will be: (a) the next item in the list,
58−and (b) the rest of the list.)
54+Line 5 uses the `?:` rune, which is a conditional. This line
55+checks whether `count` equals `end`. If so, line 6 (and the
56+whole loop) returns the value `~`. The list is finished. If not,
57+line 7 produces an ordered pair. (This pair will be: (a) the next
58+item in the list, determined by lines 8-14; and (b) the rest of
59+the list, determined by line 15.)
5960
6061(a) The head of the pair is: (i) "FizzBuzz", or "Fizz", or
6162"Buzz", if the respective tests hit; otherwise, it's
6263(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
6465pair: runtime type of `count`, and `count` itself. `pave` is a
6566Hoon function that converts the type-value pair to a `tape`
6667(like C `sprintf()`).
6768
6869(b) The tail of the pair&mdash;the rest of the output list&mdash;is
69−produced at line 15, which repeats the loop (like Clojure
70−`recur`), with `count` incremented.
70+produced at line 15, which uses `$()` to repeat the loop (like
71+Clojure `recur`), with `count` incremented.
7172
7273You can test this code by mounting your home desk with
7374`|mount %` from the dojo; saving the above code as