on master help sign in
change order of 1.4 and 1.5, mini exercises for lists (#689)

* reverses order of lessons 1.4 and 1.5 since 1.4 actually depended on 1.5 but not vice versa * weld exercise * lent exercise * fix exercise numbers * exercisest 1.5e,f * exercise 1.5g

commit175c53c0c3c6078f667785e4a4a625ceb035852c authorpoprox <51337059+syzygyzer@users.noreply.github.com> dateWed 30 Oct 2019, 14:12 (7 years ago) parent23d6dfa
4 files changed+152−15
tutorials/hoon/fibonacci.md+2−2 tutorials/hoon/gates.md+4−4 tutorials/hoon/lists.md+144−7 tutorials/hoon/recursion.md+2−2
tutorials/hoon/fibonacci.md+2−2
@@ -1,6 +1,6 @@
11+++
2−title = "1.4.1 Walkthrough: Fibonacci Sequence"
3−weight = 7
2+title = "1.5.1 Walkthrough: Fibonacci Sequence"
3+weight = 9
44template = "doc.html"
55aliases = ["/docs/learn/hoon/hoon-tutorial/fibonacci/"]
66+++
tutorials/hoon/gates.md+4−4
@@ -1,6 +1,6 @@
11+++
2−title = "1.5 Gates (Hoon Functions)"
3−weight = 8
2+title = "1.4 Gates (Hoon Functions)"
3+weight = 6
44template = "doc.html"
55aliases = ["/docs/learn/hoon/hoon-tutorial/gates/"]
66+++
@@ -166,7 +166,7 @@ Let's look at the context of `inc`:
166166
167167This 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.
168168
169−### Exercise 1.5.1a
169+### Exercise 1.4.1a
170170
171171Write 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.
172172
@@ -256,7 +256,7 @@ Before finishing the lesson let's unbind `ten`:
256256> =ten
257257```
258258
259−## Exercise 1.5a Solution
259+## Exercise 1.4.1a Solution
260260
261261Write 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.
262262
tutorials/hoon/lists.md+144−7
@@ -1,6 +1,6 @@
11+++
2−title = "1.4 Lists"
3−weight = 6
2+title = "1.5 Lists"
3+weight = 8
44template = "doc.html"
55aliases = ["/docs/learn/hoon/hoon-tutorial/lists/"]
66+++
@@ -113,7 +113,7 @@ The `flop` function takes a list and returns it in reverse order:
113113"Hello!"
114114```
115115
116−#### `flop` Exercise
116+#### `flop` Exercise 1.5a
117117
118118Without 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.
119119
@@ -144,7 +144,7 @@ The function passed to `sort` must produce a `flag`, i.e., `?`.
144144
145145### `weld`
146146
147−The `weld` function takes two lists and concatenates them:
147+The `weld` function takes two lists of the same type and concatenates them:
148148
149149```
150150> (weld ~[1 2 3] ~[4 5 6])
@@ -154,6 +154,10 @@ The `weld` function takes two lists and concatenates them:
154154"Happy Birthday!"
155155```
156156
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+
157161### `snag`
158162
159163The `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:
20320711
204208```
205209
206−#### `snag` exercise
210+#### `snag` Exercise 1.5c
207211
208212Without using `snag`, write a gate that returns the `n`th item of a list. There is a solution at the bottom of this lesson.
209213
@@ -237,6 +241,10 @@ The `lent` function takes a list and returns the number of items in it:
2372416
238242```
239243
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+
240248### `roll`
241249
242250The `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
291299
292300The 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/)
293301
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+
294340## Exercise Solutions
295341
296−#### `flop`
342+#### `flop` 1.5a
297343
298344```hoon
299345:: flop.hoon
@@ -312,7 +358,24 @@ Run in dojo:
312358~[44 33 22 11]
313359```
314360
315−#### `snag`
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+
378+#### `snag` 1.5c
316379
317380```hoon
318381:: snag.hoon
@@ -332,3 +395,77 @@ Run in dojo:
332395> +snag [2 ~[11 22 33 44]]
33339633
334397```
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 @@
11+++
2−title = "1.5.1 Walkthrough: Recursion"
3−weight = 9
2+title = "1.4.1 Walkthrough: Recursion"
3+weight = 7
44template = "doc.html"
55aliases = ["/docs/learn/hoon/hoon-tutorial/recursion/"]
66+++