on master help sign in
mini exercises for 1.2-1.6 (#692)

* fixed lesson reference * draw diagram exercise * more detail in solution * fix lesson references * fixed solution * a space * lark exercise * tree to noun exercise * limb wing and lark exercise

commit1f012c6073003127a0c823ea72374c8560e7551e authorpoprox <51337059+syzygyzer@users.noreply.github.com> dateThu 31 Oct 2019, 15:55 (7 years ago) parent175c53c
3 files changed+154−9
tutorials/hoon/hoon-syntax.md+3−3 tutorials/hoon/nouns.md+75 tutorials/hoon/the-subject-and-its-legs.md+76−6
tutorials/hoon/hoon-syntax.md+3−3
@@ -84,7 +84,7 @@ We start with atom literals. Each of these is a basic expression of Hoon that e
8484~zod
8585```
8686
87−Recall from [lesson 1.2](@/docs/tutorials/hoon/nouns.md) that even though atoms are unsigned integers, they can be pretty-printed in different ways. The way an atom is to be represented depends on its aura. The literal syntax for each of the hard-coded auras will be explained further in [lesson 2.4](@/docs/tutorials/hoon/atoms-auras-and-simple-cell-types.md).
87+Recall from [lesson 1.2](@/docs/tutorials/hoon/nouns.md) that even though atoms are unsigned integers, they can be pretty-printed in different ways. The way an atom is to be represented depends on its aura. The literal syntax for each of the hard-coded auras will be explained further in [lesson 2.1](@/docs/tutorials/hoon/atoms-auras-and-simple-cell-types.md).
8888
8989Cell literals can be written in Hoon using `[ ]`. Cell literals are complex, because other expressions are put inside the square brackets. Examples:
9090
@@ -124,7 +124,7 @@ Let's start with the base case: a single limb. A limb expression is a trivial w
124124- `add`
125125- `mul`
126126
127−As a special limb we also have `$`. This is the name of the arm in special one-armed cores called "gates". (We'll cover the role of `$` in [lesson 1.5](@/docs/tutorials/hoon/gates.md).)
127+As a special limb we also have `$`. This is the name of the arm in special one-armed cores called "gates". (We'll cover the role of `$` in [lesson 1.4](@/docs/tutorials/hoon/gates.md).)
128128
129129Wing expressions with multiple limbs are complex expressions. Examples:
130130
@@ -141,7 +141,7 @@ Wing expressions with multiple limbs are complex expressions. Examples:
141141
142142Hoon is a statically typed language. You'll learn more about the type system later in the chapter. For now, just know that Hoon's type system uses special symbols to indicate certain fundamental types: `~` (null), `*` (noun), `@` (atom), `^` (cell), and `?` (flag). Each of these symbols can be used as a stand-alone expression of Hoon. In the case of `@` there may be a series of letters following it, to indicate an atom aura; e.g., `@s`, `@rs`, `@tas`, and `@tD`.
143143
144−They may also be put in brackets to indicate compound types, e.g., `[@ ^]`, `[@ud @sb]`, `[[? *] ^]`. (Technically these expressions don't _always_ indicate compound types. In certain contexts they're interpreted in a different way. We'll address this variation of meaning in [lesson 2.6](@/docs/tutorials/hoon/structures-and-complex-types.md).)
144+They may also be put in brackets to indicate compound types, e.g., `[@ ^]`, `[@ud @sb]`, `[[? *] ^]`. (Technically these expressions don't _always_ indicate compound types. In certain contexts they're interpreted in a different way. We'll address this variation of meaning in [lesson 2.3](@/docs/tutorials/hoon/structures-and-complex-types.md).)
145145
146146### Rune Expressions
147147
tutorials/hoon/nouns.md+75
@@ -454,6 +454,32 @@ Without using the dojo, for each fragment below provide the address relative to
454454
455455That's it for our basic introduction to nouns. Hit `ctrl-d` to exit your ship, or else move on to the next lesson.
456456
457+### Exercise 1.2d
458+
459+Draw out a binary tree diagram for each of the following nouns. Try to do this without using the dojo to simplify the expressions.
460+
461+```
462+1. [[2 3] 4 [5 6 7]]
463+2. [[[5 6 7] 8 9] 10 11 [12 13]]
464+```
465+
466+### Exercise 1.2e
467+
468+Write the following binary tree as a noun.
469+
470+```
471+ .
472+ / \
473+ / \
474+ . .
475+ / \ / \
476+ . 3 4 .
477+ / \ / \
478+1 2 . 7
479+ / \
480+ 5 6
481+```
482+
457483## Exercise Answers
458484
459485### 1.2a
@@ -479,3 +505,52 @@ That's it for our basic introduction to nouns. Hit `ctrl-d` to exit your ship,
479505+ +14
480506+ +15
481507+ +31
508+
509+### 1.2d
510+
511+1. Dojo simplifies the expression
512+```
513+> [[2 3] 4 [5 6 7]]
514+[[2 3] 4 5 6 7]
515+```
516+which has the following tree:
517+```
518+ .
519+ / \
520+ / \
521+ . .
522+ / \ / \
523+ 2 3 4 .
524+ / \
525+ 5 .
526+ / \
527+ 6 7
528+```
529+
530+2. Dojo simplifies the expression
531+
532+```
533+> [[[5 6 7] 8 9] 10 11 [12 13]]
534+[[[5 6 7] 8 9] 10 11 12 13]
535+```
536+which has the following tree:
537+```
538+ .
539+ /\
540+ / \
541+ / \
542+ . .
543+ / \ / \
544+ / . 10 .
545+ / / \ / \
546+ . 8 9 11 .
547+ / \ / \
548+5 . 12 13
549+ / \
550+ 6 7
551+```
552+
553+### 1.2e
554+```
555+[[[1 2] 3] 4 [5 6] 7]
556+```
tutorials/hoon/the-subject-and-its-legs.md+76−6
@@ -11,7 +11,7 @@ Hoon isn't an object-oriented programming language; it's a "subject-oriented" pr
1111
1212For now we can say three things about the subject: (1) every Hoon expression is evaluated relative to some subject; (2) roughly, the subject defines the environment in which a Hoon expression is evaluated; and (3) the subject is a noun.
1313
14−In fact, you already created some subjects in lesson 1.2 when you used `+` to return a fragment of a noun:
14+In fact, you already learned about the noun address system in [Lesson 1.2](../nouns.md) when you used `+` to return a fragment of a noun:
1515
1616```
1717> +1:[[11 22] 33]
@@ -58,7 +58,7 @@ Let's create a subject with some computations:
5858
5959## Limbs of the Subject
6060
61−The subject is a noun, just like any other piece of Hoon data. In the last lesson we discussed how any noun can be understood as a binary tree. E.g., `[[4 5] [6 [14 15]]]`:
61+The subject is a noun, just like any other piece of Hoon data. In Lesson 1.2 we discussed how any noun can be understood as a binary tree. E.g., `[[4 5] [6 [14 15]]]`:
6262
6363```
6464 [[4 5] [6 [14 15]]]
@@ -79,7 +79,7 @@ Sometimes a programmer simply wants to produce a value from the subject. In oth
7979
8080### Address-based Limb Expressions
8181
82−A limb expression is an expression of Hoon that resolves to a limb of the subject. An address-based limb expression evaluates to a limb of the subject based on its noun address. (You learned about the noun address system in the last lesson.)
82+A limb expression is an expression of Hoon that resolves to a limb of the subject. An address-based limb expression evaluates to a limb of the subject based on its noun address.
8383
8484In the following you'll learn the various limb expressions available in Hoon, as well as how they work when they resolve to legs. First we'll explain the limb expressions that return a leg according to subject address.
8585
@@ -176,6 +176,27 @@ You can think of this sort of lark series -- e.g., `+>-<` -- as indicating a bin
176176 *Head* Tail
177177```
178178
179+#### Exercise 1.6a
180+1. Use a lark expression to obtain the value 6 in the following noun represented by a binary tree:
181+```
182+ .
183+ /\
184+ / \
185+ / \
186+ . .
187+ / \ / \
188+ / . 10 .
189+ / / \ / \
190+ . 8 9 11 .
191+ / \ / \
192+5 . 12 13
193+ / \
194+ 6 7
195+```
196+2. Use a lark expression to obtain the value 9 in the following noun: `[[5 6] 7 [[8 9 10] 3] 2] 1]`.
197+
198+Solutions to these exercises may be found at the bottom of this lesson.
199+
179200#### `&` and `|` operators
180201
181202`&n` returns the `n`th item of a list that has at least `n + 1` items. `|n` returns everything after `&n`.
@@ -223,9 +244,9 @@ The list items can themselves be cells:
223244['third' %pair]
224245```
225246
226−We can give an alternate, recursive definition of `&n` for all positive integers `n`. In the base case, `&1` is equivalent to `+2`. For the generating case, assume that `&(n - 1)` is equivalent to `+k`. Then `&n` is equivalent to `+((k x 2) + 2)`.
247+We can give an alternate, recursive definition of `&n` for all positive integers `n`. In the base case, `&1` is equivalent to `+2`. For the generating case, assume that `&(n - 1)` is equivalent to `+k`. Then `&n` is equivalent to `+((k × 2) + 2)`.
227248
228−For example, let `n` be 4. What is `&4`? `&3` is equivalent to `+14`. `(14 x 2) + 2` is `30`, so `&4` is equivalent to `+30`.
249+For example, let `n` be 4. What is `&4`? `&3` is equivalent to `+14`. `(14 × 2) + 2` is `30`, so `&4` is equivalent to `+30`.
229250
230251`|n` returns the rest of the list after `&n`:
231252
@@ -243,7 +264,7 @@ For example, let `n` be 4. What is `&4`? `&3` is equivalent to `+14`. `(14 x
243264~
244265```
245266
246−As with `&n`, we can characterize `|n` recursively. In the base case, `|1` is `+3`. In the generating case, assume that `|(n - 1)` is equivalent to `+k`. Then `|n` is equivalent to `+((k x 2) + 1)`.
267+As with `&n`, we can characterize `|n` recursively. In the base case, `|1` is `+3`. In the generating case, assume that `|(n - 1)` is equivalent to `+k`. Then `|n` is equivalent to `+((k × 2) + 1)`.
247268
248269### Other Limb Expressions: Names
249270
@@ -654,3 +675,52 @@ You can also use more complex wings and modify their values as well:
654675
655676At this point you should have at least a rough idea of what the subject is, and a fair understanding of how to get legs of the subject using wings. But legs are only one kind of limb -- we'll talk about arms in the next lesson.
656677
678+#### Exercise 1.6b
679+Enter the following into dojo:
680+```
681+=a [[[b=%bweh a=%.y c=8] b="no" c="false"] 9]
682+```
683+Test your knowledge from this lesson by evaluating the following expressions and then checking your answer in the dojo or see the solutions below.
684+
685+1. `> b:a(a [b=%skrt a="four"])`
686+2. `> ^b:a(a [b=%skrt a="four"])`
687+3. `> ^^b:a(a [b=%skrt a="four"])`
688+4. `> b.a:a(a [b=%skrt a="four"])`
689+5. `> a.a:a(a [b=%skrt a="four"])`
690+6. `+.a:a(a [b=%skrt a="four"])`
691+7. `a:+.a:a(a [b=%skrt a="four"])`
692+8. `a(a a)`
693+9. `b:-<.a(a a)`
694+10. How many times does the atom `9` appear in `a(a a(a a))`?
695+
696+## Exercise Solutions
697+
698+### Exercise 1.6a
699+1.
700+```
701+> =b [[[5 6 7] 8 9] 10 11 12 13]
702+> ->-:b
703+8
704+```
705+2.
706+```
707+> =c [[5 6] 7 [[8 9 10] 3] 2]
708+> +>-<+<:c
709+9
710+```
711+
712+### Exercise 1.6b
713+1. `%bweh`
714+2. `"no"`
715+3. Error: `ford: %slim failed:`
716+4. `%skrt`
717+5. `"four"`
718+6. `a="four"` - Note that this is different from the above!
719+7. `"four"`
720+8. `[[[b=%bweh a=[[[b=%bweh a=%.y c=8] b="no" c="false"] 9] c=8] b="no" c="false"]9]`
721+9. `%bweh`
722+10. `9` appears 3 times:
723+```
724+> a(a a(a a))
725+[[[ b=%bweh a [[[b=%bweh a=[[[b=%bweh a=%.y c=8] b="no" c="false"] 9] c=8] b="no" c="false"] 9] c=8] b="no" c="false"] 9]
726+```