on master help sign in
mini exercises for 1.2-1.6 (#692)
1f012c6poprox
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
84~zod
84~zod
85```
85```
86
86
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).
88
88
89Cell literals can be written in Hoon using `[ ]`. Cell literals are complex, because other expressions are put inside the square brackets. Examples:
89Cell literals can be written in Hoon using `[ ]`. Cell literals are complex, because other expressions are put inside the square brackets. Examples:
90
90
@@ -124,7 +124,7 @@ Let's start with the base case: a single limb. A limb expression is a trivial w
124- `add`
124- `add`
125- `mul`
125- `mul`
126
126
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).)
128
128
129Wing expressions with multiple limbs are complex expressions. Examples:
129Wing expressions with multiple limbs are complex expressions. Examples:
130
130
@@ -141,7 +141,7 @@ Wing expressions with multiple limbs are complex expressions. Examples:
141
141
142Hoon 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`.
142Hoon 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`.
143
143
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).)
145
145
146### Rune Expressions
146### Rune Expressions
147
147
tutorials/hoon/nouns.md+75
@@ -454,6 +454,32 @@ Without using the dojo, for each fragment below provide the address relative to
454
454
455That's it for our basic introduction to nouns. Hit `ctrl-d` to exit your ship, or else move on to the next lesson.
455That's it for our basic introduction to nouns. Hit `ctrl-d` to exit your ship, or else move on to the next lesson.
456
456
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+
457## Exercise Answers
483## Exercise Answers
458
484
459### 1.2a
485### 1.2a
@@ -479,3 +505,52 @@ That's it for our basic introduction to nouns. Hit `ctrl-d` to exit your ship,
479+ +14
505+ +14
480+ +15
506+ +15
481+ +31
507+ +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
11
11
12For 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.
12For 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.
13
13
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:
15
15
16```
16```
17> +1:[[11 22] 33]
17> +1:[[11 22] 33]
@@ -58,7 +58,7 @@ Let's create a subject with some computations:
58
58
59## Limbs of the Subject
59## Limbs of the Subject
60
60
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]]]`:
62
62
63```
63```
64 [[4 5] [6 [14 15]]]
64 [[4 5] [6 [14 15]]]
@@ -79,7 +79,7 @@ Sometimes a programmer simply wants to produce a value from the subject. In oth
79
79
80### Address-based Limb Expressions
80### Address-based Limb Expressions
81
81
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.
83
83
84In 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.
84In 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.
85
85
@@ -176,6 +176,27 @@ You can think of this sort of lark series -- e.g., `+>-<` -- as indicating a bin
176 *Head* Tail
176 *Head* Tail
177```
177```
178
178
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+
179#### `&` and `|` operators
200#### `&` and `|` operators
180
201
181`&n` returns the `n`th item of a list that has at least `n + 1` items. `|n` returns everything after `&n`.
202`&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:
223['third' %pair]
244['third' %pair]
224```
245```
225
246
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)`.
227
248
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`.
229
250
230`|n` returns the rest of the list after `&n`:
251`|n` returns the rest of the list after `&n`:
231
252
@@ -243,7 +264,7 @@ For example, let `n` be 4. What is `&4`? `&3` is equivalent to `+14`. `(14 x
243~
264~
244```
265```
245
266
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)`.
247
268
248### Other Limb Expressions: Names
269### Other Limb Expressions: Names
249
270
@@ -654,3 +675,52 @@ You can also use more complex wings and modify their values as well:
654
675
655At 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.
676At 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.
656
677
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+```