on master help sign in
adds conditionals content
commit0f82da4b6862ca872fd19ee983727cb3d6d83adf authorRobert <robert@tlon.io> dateFri 26 Jul 2019, 15:43 (7 years ago) parent6a5b687
1 file changed+6−6
learn/hoon/hoon-tutorial/conditionals.md+6−6
learn/hoon/hoon-tutorial/conditionals.md+6−6
@@ -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. It's not important for understanding; this is just template code. 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.
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−But now let's get into the conditionals themselves. 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.
3737
3838```
3939?: ?& =(0 (mod n 2))
4040```
4141
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`.
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.
4343
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:
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 expression that our `?&` rune uses is:
4545
4646```
4747=(0 (mod n 2))
@@ -59,9 +59,9 @@ The second expression is:
5959
6060Its 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".
6161
62−`?!` ("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: `(lth n 100)`, without the `?!` rune. We're just using this rune artificially to demonstrate its use to the reader.
62+`?!` ("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.
6363
64−The `==` runes close the `?&` runes, since `?&` runes can take an unlimited number of children.
64+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.
6565
6666### Other Runes
6767