on master help sign in
updates conditionals.md
commitd5508cbca888a8a12aa11cf63c607bf03c5e5cff authorRobert <robert@tlon.io> dateThu 25 Jul 2019, 17:58 (7 years ago) parentc00b16f
1 file changed+20−5
learn/hoon/hoon-tutorial/conditionals.md+20−5
learn/hoon/hoon-tutorial/conditionals.md+20−5
@@ -19,7 +19,7 @@ In this lesson, we will write a generator that takes an integer and checks if it
1919%.n
2020```
2121
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; this is just template code. For more information about `%say` generators, see the [Generators](../generators) documentation.
2323
2424```
2525|= [* [n=@ud ~] ~]
@@ -33,15 +33,15 @@ The code above builds a gate. The gate's first argument is a cell provided by Do
3333
3434This 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.
3535
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 the series of `?` runes used.
3737
3838```
3939?: ?& =(0 (mod n 2))
4040```
4141
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`.
4343
44−The first expression is:
44+In our case, the first child is `?& =(0 (mod n 2))`. It itself has another conditional rune, `?&` ("wut-pam"), which combines two expressions with a logical "and" operation, making sure that both of them are true. The first expression that our `?&` rune uses is:
4545
4646```
4747=(0 (mod n 2))
@@ -57,6 +57,21 @@ The second expression is:
5757 ==
5858```
5959
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.
60+Its first sub-expression 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".
61+
62+`?!` ("wut-zup") is the logical "not" operator, which inverts the truth value of its single sub-expression. We would normally use simpler code here to do the same thing: `(lth n 100)`, without the `?!` rune. We're just using this rune artificially to demonstrate its use to the reader.
63+
64+The `==` runes close the `?&` runes, since `?&` runes can take an unlimited number of sub-expressions.
65+
66+### Other Runes
67+
68+We only went into three "wut" runes in our walkthrough, but there are many others. Here are a few more examples:
69+
70+`?@` takes three children. It branches on whether its first child is an atom.
71+`?^` takes three children. It branches on whether its first child is a cell.
72+`?~` takes three children. It branches on whether its first child is null.
73+`?|` takes an indefinite number of children. is the logical "or." It checks if at least one of its children is true.
74+
75+To see an exhaustive list of "wut" runes, check out the [reference documentation on conditionals](/docs/reference/hoon-expressions/rune/wut/).
6176
6277### [Next Up: Reading -- Lists](../lists)