on master help sign in
Merge pull request #580 from urbit/conditionals-change
21c76ebDavid Kerschner
learn/hoon/hoon-tutorial/conditionals.md+21−12
@@ -19,7 +19,7 @@ In this lesson, we will write a generator that takes an integer and checks if it
19%.n
19%.n
20```
20```
21
21
22−On the very first line, with `:- %say` we are beginning to create a generator of the `%say` variety. The result of a `%say` generator is a cell with a head of `%say` and tail that is a gate, itself producing a `cask`, a pair of a `mark` and some data. For more information about `%say` generators, see the [Generators](../generators) documentation.
22+On the very first line, with `:- %say` we are beginning to create a generator of the `%say` variety. The result of a `%say` generator is a cell with a head of `%say` and tail that is a gate, itself producing a `cask`, a pair of a `mark` and some data. It's not important for understanding conditionals; this is just template code. For more information about `%say` generators, see the [Generators](../generators) documentation.
23
23
24```
24```
25|= [* [n=@ud ~] ~]
25|= [* [n=@ud ~] ~]
@@ -33,23 +33,17 @@ The code above builds a gate. The gate's first argument is a cell provided by Do
33
33
34This code is the third line of the `%say` "boilerplate," and it produces a `cask` with the head of `%noun`. We could use any `mark` here, but `%noun` is the most generic type, able to fit any data.
34This code is the third line of the `%say` "boilerplate," and it produces a `cask` with the head of `%noun`. We could use any `mark` here, but `%noun` is the most generic type, able to fit any data.
35
35
36−Below we'll examine the the series of `?` runes used.
36+But now let's get into the conditionals themselves. Below we'll examine the series of `?` runes used.
37
37
38```
38```
39?: ?& =(0 (mod n 2))
39?: ?& =(0 (mod n 2))
40```
40```
41
41
42−`?:` is the simplest "wut" rune. It takes three expressions: a boolean test, a yes-branch, and a no-branch. The yes-branch is executed when the test evaluates to `%.y` and the no-branch is executed when the test evaluates to `%.n`. Here the yes-branch is `%.y` and the no-branch is `%.n`. The boolean test is the more complicated portion. First we're going to use `?&` to combine two expressions with a logical "and" operation.
42+`?:` (pronounced "wut-col") 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.
43
43
44−The first expression is:
44+In 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.
45
45
46−```
47−=(0 (mod n 2))
48−```
49−
50−which simply asks if `n` is even or not.
51−
52−The second expression is:
46+The second child is:
53
47
54```
48```
55 ?& (gte n 1)
49 ?& (gte n 1)
@@ -57,6 +51,21 @@ The second expression is:
57 ==
51 ==
58```
52```
59
53
60−Again we use the "and" operator on two expressions with `?&` the first is just checking if n is greater than or equal to 1. The second we have artificially use the `?!` rune to demonstrate its use. `?!` is a logical NOT operation. We use that on the result of asking if n is greater than 100 to effectively ask if it is less than or equal to 100.
54+Its first child is another `?&` rune. This second "and" rune checks for the truth of the following two expressions: `(gte n 1)`, meaning "n is greater than or equal to one"; and `?! (gth n 100),` which means "n is not greater than 100".
55+
56+`?!` ("wut-zup") is the logical "not" operator, which inverts the truth value of its single child. We would normally use simpler code here to do the same thing: `(lte n 100)`, without the `?!` rune. We're just using this rune artificially to demonstrate its use to the reader. The `==` runes close the `?&` runes, since `?&` runes can take an unlimited number of children.
57+
58+So the two `?&` runes that we used in this code together check the that all three of these different expressions evaluate to true: `=(0 (mod n 2))`, `(gte n 1)`, and `?! (gth n 100)`. If all of these expressions evaluate to true, our original `?:` expression branches to the `%.y` we wrote on the second-to-last line; if one or more evaluates to false, it branches to the `%.n` on the final line. Try to visualize how this works by looking through the program and examining the indentation.
59+
60+### Other Runes
61+
62+We only went into three "wut" runes in our walkthrough, but there are many others. Here are a few more examples:
63+
64+`?@` takes three children. It branches on whether its first child is an atom.
65+`?^` takes three children. It branches on whether its first child is a cell.
66+`?~` takes three children. It branches on whether its first child is null.
67+`?|` takes an indefinite number of children. It's the logical "or" operator. It checks if at least one of its children is true.
68+
69+To see an exhaustive list of "wut" runes, check out the [reference documentation on conditionals](/docs/reference/hoon-expressions/rune/wut/).
61
70
62### [Next Up: Reading -- Lists](../lists)
71### [Next Up: Reading -- Lists](../lists)