on master help sign in
finishing idiomaticization
c9a4518syzygyzer
tutorials/hoon/conditionals.md+49−6
@@ -12,9 +12,10 @@ In this lesson, we will write a generator that takes an integer and checks if it
12|= [* [n=@ud ~] ~]
12|= [* [n=@ud ~] ~]
13:- %noun
13:- %noun
14^- ?
14^- ?
15+?&
15−?& =(0 (mod n 2))
16+ =(0 (mod n 2))
16 (gte n 1)
17 (gte n 1)
17− (lte n 1000)
18+ (lte n 100)
18==
19==
19```
20```
20
21
@@ -35,14 +36,41 @@ This code is the third line of the `%say` "boilerplate," and it produces a `cask
35```hoon
36```hoon
36^- ?
37^- ?
37```
38```
38−This line casts the output as a `flag`, which is a type whose values are `%.y` and `%.n` representing "yes" and "no".
39+This line casts the output as a `flag`, which is a type whose values are `%.y` and `%.n` representing "yes" and "no". These behave as boolean values.
39
40
40−Let's get into the conditionals themselves. Below we'll examine the series of `?` runes used.
41+Let's look at the conditional.
41
42
42```hoon
43```hoon
43−?& =(0 (mod n 2))
44+?&
44```
45```
45
46
47+`?&` (pronounced "wut-pam") takes in a list of Hoon expressions terminated by `==` that evaluate to a `flag` and returns the logical "AND" of these `flag`s. Most runes take a fixed number of children, but the handful that do not (such as `?&`) end their list of children with a terminating rune. In our context, that means that if the product of each of the children of `?&` is `%.y`, then the product of the entire `?&` expression is `%.y` as well. Otherwise, the product of the conditional `?&` is `%.n`.
48+
49+The first child of `?&` is the following.
50+
51+```hoon
52+=(0 (mod n 2))
53+```
54+
55+This checks to see if `0` is equal to `(mod n 2)` and returns a `flag`. In other words, it produces `%.y` if `n` is even and `%.n` if `n` is odd.
56+
57+Next we have:
58+```hoon
59+(gte n 1)
60+```
61+This utilizes the standard library function `gte` which stands for "greater than or equal to". `(gte a b)` returns `%.y` if `a` is greater than or equal to `b`, and `%.n` otherwise.
62+
63+```hoon
64+(lte n 100)
65+```
66+`lte` is the standard library function for "less than or equal to". `(lte a b)` returns `%.y` if `a` is less than `b`, and `%.n` otherwise.
67+
68+```hoon
69+==
70+```
71+This terminates the list of children of `?&`.
72+
73+
46`?:` (pronounced "wut-pam") is the simplest "wut" rune. It takes three children, also called sub-expressions. The first child is a boolean test, which means that it looks for a `%.y` ("yes") or a `%.n` ("no."). The second child is a yes-branch, which is what we arrive at if the aforementioned boolean test evaluates to `%.y`. The third child is a no-branch, so we arrive at it if instead the boolean test evaluates to `%.n`. These branches can contain any sort of Hoon expression, including further conditional expressions, as we will see.
74`?:` (pronounced "wut-pam") is the simplest "wut" rune. It takes three children, also called sub-expressions. The first child is a boolean test, which means that it looks for a `%.y` ("yes") or a `%.n` ("no."). The second child is a yes-branch, which is what we arrive at if the aforementioned boolean test evaluates to `%.y`. The third child is a no-branch, so we arrive at it if instead the boolean test evaluates to `%.n`. These branches can contain any sort of Hoon expression, including further conditional expressions, as we will see.
47
75
48In our case, the first child of `?:` is `?& =(0 (mod n 2))`. It itself has another conditional rune, `?&` ("wut-pam"), which performs the logical "and" operation on its two children, making sure that both of them are true. The first child of our `?&` rune is `=(0 (mod n 2))`, which simply asks if `n` is even or not.
76In our case, the first child of `?:` is `?& =(0 (mod n 2))`. It itself has another conditional rune, `?&` ("wut-pam"), which performs the logical "and" operation on its two children, making sure that both of them are true. The first child of our `?&` rune is `=(0 (mod n 2))`, which simply asks if `n` is even or not.
@@ -63,7 +91,22 @@ So the two `?&` runes that we used in this code together check the that all thre
63
91
64### Other Runes
92### Other Runes
65
93
66−We only went into three "wut" runes in our walkthrough, but there are many others. Here are a few more examples:
94+We only utilized one "wut" rune in our walkthrough, but there are many others. Here are a few more examples:
95+
96+- `?:` (pronounced "wut-pam") is the simplest "wut" rune. It takes three children, also called sub-expressions. The first child is a boolean test, so it looks for a `%.y` or a `%.n`. The second child is a yes-branch, which is what we arrive at if the aforementioned boolean test evaluates to `%.y`. The third child is a no-branch, which we arrive at if the boolean test evaluates to `%.n`. These branches can contain any sort of Hoon expression, including further conditional expressions. Instead of the `?&` expression in our `%say` generator above, we could have written
97+
98+```hoon
99+?: ?&
100+ =(0 (mod n 2))
101+ (gte n 1)
102+ (lte n 100)
103+ ==
104+ %.y
105+%.n
106+```
107+Of course, doing so would be needlessly obfuscating - we mention this only to illustrate that these two Hoon expressions have the same product.
108+
109+- `?!` ("wut-zup") is the logical "NOT" operator, which inverts the truth value of its single child. Instead of `(lte n 100)` in our `%say` generator above, we could have written `?! (gth n 100)`. Again, this would be bad practice, we only present this as an example.
67
110
68- `?@` takes three children. It branches on whether its first child is an atom.
111- `?@` takes three children. It branches on whether its first child is an atom.
69
112