on master help sign in
section on nesting runes, lego block diagrams (#717)
788df14poprox
tutorials/hoon/hoon-syntax.md+29
@@ -190,6 +190,35 @@ This is the preferred way to write an expression on a single line. The rune its
190
190
191Nearly all rune expressions can be written in either form, but there are exceptions. `|%` and `|_` expressions, for example, can only be written in tall form. (Those are a bit too complicated to fit comfortably on one line anyway.)
191Nearly all rune expressions can be written in either form, but there are exceptions. `|%` and `|_` expressions, for example, can only be written in tall form. (Those are a bit too complicated to fit comfortably on one line anyway.)
192
192
193+#### Nesting Runes
194+
195+Since runes take a fixed number of children, one can visualize how Hoon expressions are built by thinking of each rune being followed by a series of boxes to be filled - one for each of its children. Let us illustrate this with the `:-` rune.
196+
197+![](https://media.urbit.org/docs/hoon-syntax/cell1.png)
198+
199+Here we have drawn the `:-` rune followed by a box for each of its two children. We can fill these boxes with either a value or an additional rune. The following figure corresponds to the Hoon expression `:- 2 3`.
200+
201+![](https://media.urbit.org/docs/hoon-syntax/cell2.png)
202+
203+This, of course, evaluates to the cell `[2 3]`. This next figure corresponds to the Hoon expression `:- :- 2 3 4`.
204+
205+![](https://media.urbit.org/docs/hoon-syntax/cell3.png)
206+
207+This evaluates to `[[2 3] 4]`, and we can think of the second `:-` as being "nested" inside of the first `:-`.
208+
209+What Hoon expression does the following figure correspond to, and what does it evaluate to?
210+
211+![](https://media.urbit.org/docs/hoon-syntax/cell4.png)
212+
213+Right. This represents the Hoon expression `:- 2 :- 3 4`,
214+and evaluates to `[2 [3 4]]`. Remember, though, that if you input this into dojo it will print as `[2 3 4]`.
215+
216+Thinking in terms of these "LEGO block" diagrams, as well as the more literal binary tree diagrams utilized in [Lesson 1.2](@/docs/tutorials/hoon/nouns.md), can be a helpful learning and debugging tactic.
217+
218+##### Exercise 1.3.a.
219+
220+Draw binary trees corresponding to the above figures. Determine a one-to-one correspondence between linearly ordered nested boxes (i.e. what is depicted in the above figures) and binary trees. You will need to make use of the convention `[a [b c]] = [a b c]`.
221+
193#### Irregular Forms
222#### Irregular Forms
194
223
195Some runes are used so frequently that they have irregular counterparts that are easier to write and which mean precisely the same thing. Irregular rune syntax is hence a form of [syntactic sugar](https://en.wikipedia.org/wiki/Syntactic_sugar). All irregular rune syntax is flat. (It follows from this that all tall form expressions are regular.)
224Some runes are used so frequently that they have irregular counterparts that are easier to write and which mean precisely the same thing. Irregular rune syntax is hence a form of [syntactic sugar](https://en.wikipedia.org/wiki/Syntactic_sugar). All irregular rune syntax is flat. (It follows from this that all tall form expressions are regular.)