on master help sign in
change order of 1.4 and 1.5, mini exercises for lists (#689)
175c53cpoprox
tutorials/hoon/fibonacci.md+2−2
@@ -1,6 +1,6 @@
1+++
1+++
2−title = "1.4.1 Walkthrough: Fibonacci Sequence"
2+title = "1.5.1 Walkthrough: Fibonacci Sequence"
3−weight = 7
3+weight = 9
4template = "doc.html"
4template = "doc.html"
5aliases = ["/docs/learn/hoon/hoon-tutorial/fibonacci/"]
5aliases = ["/docs/learn/hoon/hoon-tutorial/fibonacci/"]
6+++
6+++
tutorials/hoon/gates.md+4−4
@@ -1,6 +1,6 @@
1+++
1+++
2−title = "1.5 Gates (Hoon Functions)"
2+title = "1.4 Gates (Hoon Functions)"
3−weight = 8
3+weight = 6
4template = "doc.html"
4template = "doc.html"
5aliases = ["/docs/learn/hoon/hoon-tutorial/gates/"]
5aliases = ["/docs/learn/hoon/hoon-tutorial/gates/"]
6+++
6+++
@@ -166,7 +166,7 @@ Let's look at the context of `inc`:
166
166
167This is exactly the default dojo subject, from before we put `inc` into the subject. The `|=` expression defines the context as whatever the subject is. This guarantees that the context has all the information it needs to have for the `$` arm to work correctly.
167This is exactly the default dojo subject, from before we put `inc` into the subject. The `|=` expression defines the context as whatever the subject is. This guarantees that the context has all the information it needs to have for the `$` arm to work correctly.
168
168
169−### Exercise 1.5.1a
169+### Exercise 1.4.1a
170
170
171Write a gate that takes an atom, `a=@`, and which returns double the value of `a`. Bind this gate to `double` and test it in the dojo. A solution is given at the end of this lesson.
171Write a gate that takes an atom, `a=@`, and which returns double the value of `a`. Bind this gate to `double` and test it in the dojo. A solution is given at the end of this lesson.
172
172
@@ -256,7 +256,7 @@ Before finishing the lesson let's unbind `ten`:
256> =ten
256> =ten
257```
257```
258
258
259−## Exercise 1.5a Solution
259+## Exercise 1.4.1a Solution
260
260
261Write a gate that takes an atom, `a=@`, and which returns double the value of `a`. Bind this gate to `double` and test it in the dojo.
261Write a gate that takes an atom, `a=@`, and which returns double the value of `a`. Bind this gate to `double` and test it in the dojo.
262
262
tutorials/hoon/lists.md+144−7
@@ -1,6 +1,6 @@
1+++
1+++
2−title = "1.4 Lists"
2+title = "1.5 Lists"
3−weight = 6
3+weight = 8
4template = "doc.html"
4template = "doc.html"
5aliases = ["/docs/learn/hoon/hoon-tutorial/lists/"]
5aliases = ["/docs/learn/hoon/hoon-tutorial/lists/"]
6+++
6+++
@@ -113,7 +113,7 @@ The `flop` function takes a list and returns it in reverse order:
113"Hello!"
113"Hello!"
114```
114```
115
115
116−#### `flop` Exercise
116+#### `flop` Exercise 1.5a
117
117
118Without using `flop`, write a gate that takes a `(list @)` and returns it in reverse order. There is a solution at the bottom of this lesson.
118Without using `flop`, write a gate that takes a `(list @)` and returns it in reverse order. There is a solution at the bottom of this lesson.
119
119
@@ -144,7 +144,7 @@ The function passed to `sort` must produce a `flag`, i.e., `?`.
144
144
145### `weld`
145### `weld`
146
146
147−The `weld` function takes two lists and concatenates them:
147+The `weld` function takes two lists of the same type and concatenates them:
148
148
149```
149```
150> (weld ~[1 2 3] ~[4 5 6])
150> (weld ~[1 2 3] ~[4 5 6])
@@ -154,6 +154,10 @@ The `weld` function takes two lists and concatenates them:
154"Happy Birthday!"
154"Happy Birthday!"
155```
155```
156
156
157+#### `weld` Exercise 1.5b
158+
159+Without using `weld`, write a gate that takes a `[(list @) (list @)]` who product is the concatenation of these two lists. There is a solution at the bottom of this lesson.
160+
157### `snag`
161### `snag`
158
162
159The `snag` function takes an atom `n` and a list, and returns the `n`th item of the list, where `0` is the first item:
163The `snag` function takes an atom `n` and a list, and returns the `n`th item of the list, where `0` is the first item:
@@ -203,7 +207,7 @@ You can cast `b` to `(list)` to work around this:
20311
20711
204```
208```
205
209
206−#### `snag` exercise
210+#### `snag` Exercise 1.5c
207
211
208Without using `snag`, write a gate that returns the `n`th item of a list. There is a solution at the bottom of this lesson.
212Without using `snag`, write a gate that returns the `n`th item of a list. There is a solution at the bottom of this lesson.
209
213
@@ -237,6 +241,10 @@ The `lent` function takes a list and returns the number of items in it:
2376
2416
238```
242```
239
243
244+#### `lent` Exercise 1.5d
245+
246+Without using `lent`, write a gate that takes a list and returns the number of item in it. There is a solution at the bottom of this lesson.
247+
240### `roll`
248### `roll`
241
249
242The `roll` function takes a list and a gate, and accumulates a value of the list items using that gate. For example, if you want to add or multiply all the items in a list of atoms, you would use `roll`:
250The `roll` function takes a list and a gate, and accumulates a value of the list items using that gate. For example, if you want to add or multiply all the items in a list of atoms, you would use `roll`:
@@ -291,9 +299,47 @@ c
291
299
292The Hoon standard library and compiler are written in Hoon. At this point, you know enough Hoon to be able to explore the standard library portions of `hoon.hoon` and find more functions relevant to lists. [Look around in section `2b` (and elsewhere)](/docs/reference/library/2b/)
300The Hoon standard library and compiler are written in Hoon. At this point, you know enough Hoon to be able to explore the standard library portions of `hoon.hoon` and find more functions relevant to lists. [Look around in section `2b` (and elsewhere)](/docs/reference/library/2b/)
293
301
302+## Additional Exercises
303+
304+#### Exercise 1.5e
305+Without entering these expressions into the dojo, what are the products of the following expressions?
306+```
307+> (lent ~[1 2 3 4 5])
308+```
309+```
310+> (lent ~[~[1 2] ~[1 2 3] ~[2 3 4]])
311+```
312+```
313+> (lent ~[1 2 (weld ~[1 2 3] ~[4 5 6])])
314+```
315+
316+#### Exercise 1.5f
317+First, bind these faces.
318+```
319+> =b ~['moon' 'planet' 'star' 'galaxy']
320+> =c ~[1 2 3]
321+```
322+Then determine whether the following dojo expressions are valid, and if so, what they evaluate to.
323+```
324+> (weld b b)
325+```
326+```
327+> (weld b c)
328+```
329+```
330+> (lent (weld b c))
331+```
332+```
333+> (add (lent b) (lent c))
334+```
335+
336+#### Exercise 1.5g
337+Write a gate that takes in a list `a` and returns `%.y` if `a` is a palindrome and `%.n` otherwise. You may make use of the `flop` function.
338+
339+
294## Exercise Solutions
340## Exercise Solutions
295
341
296−#### `flop`
342+#### `flop` 1.5a
297
343
298```hoon
344```hoon
299:: flop.hoon
345:: flop.hoon
@@ -312,7 +358,24 @@ Run in dojo:
312~[44 33 22 11]
358~[44 33 22 11]
313```
359```
314
360
361+#### `weld` 1.5b
362+```hoon
363+:: weld.hoon
364+::
365+|= [a=(list @) b=(list @)]
366+|- ^- (list @)
367+?~ a b
368+[i.a $(a t.a)]
369+```
370+
371+Run in dojo:
372+
373+```
374+> +weld [~[1 2 3] ~[3 4 5 6]]
375+~[1 2 3 3 4 5 6]
376+```
377+
315−#### `snag`
378+#### `snag` 1.5c
316
379
317```hoon
380```hoon
318:: snag.hoon
381:: snag.hoon
@@ -332,3 +395,77 @@ Run in dojo:
332> +snag [2 ~[11 22 33 44]]
395> +snag [2 ~[11 22 33 44]]
33333
39633
334```
397```
398+
399+#### `lent` 1.5d
400+
401+```hoon
402+:: lent.hoon
403+::
404+|= a=(list)
405+^- @
406+=/ b=@ 0
407+|-
408+?~ a b
409+$(a t.a, b +(b))
410+```
411+
412+Run in dojo:
413+
414+```
415+> +lent ~[1 2 3 4 5]
416+5
417+> +lent "asdf"
418+4
419+```
420+
421+#### 1.5e
422+
423+Run in dojo:
424+
425+```
426+> (lent ~[1 2 3 4 5])
427+5
428+> (lent ~[~[1 2] ~[1 2 3] ~[2 3 4]])
429+3
430+> (lent ~[1 2 (weld ~[1 2 3] ~[4 5 6])])
431+3
432+```
433+
434+#### 1.5f
435+
436+Run in dojo:
437+
438+```
439+> (weld b b)
440+<|moon planet star galaxy moon planet star galaxy|>
441+```
442+```
443+> (weld b c)
444+```
445+This will not run because `weld` expects the elements of both lists to be of the same type.
446+```
447+> (lent (weld b c))
448+```
449+This also fails for the same reason, but it is important to note that in some languages that are more lazily evaluated, such an expression would still work since it would only look at the length of `b` and `c` and not worry about what the elements were. In that case, it would return `7`.
450+```
451+> (add (lent b) (lent c))
452+7
453+```
454+We see here the correct way to find the sum of the length of two lists of unknown type.
455+
456+#### 1.5g
457+```hoon
458+:: palindrome.hoon
459+::
460+|= a=(list)
461+=(a (flop a))
462+```
463+
464+Run in dojo:
465+
466+```
467+> +palindrome "urbit"
468+%.n
469+> +palindrome "racecar"
470+%.y
471+```
tutorials/hoon/recursion.md+2−2
@@ -1,6 +1,6 @@
1+++
1+++
2−title = "1.5.1 Walkthrough: Recursion"
2+title = "1.4.1 Walkthrough: Recursion"
3−weight = 9
3+weight = 7
4template = "doc.html"
4template = "doc.html"
5aliases = ["/docs/learn/hoon/hoon-tutorial/recursion/"]
5aliases = ["/docs/learn/hoon/hoon-tutorial/recursion/"]
6+++
6+++