@@ -1,6 +1,6 @@
2−title = "1.4 Lists"
2+title = "1.5 Lists"
4template = "doc.html"
4template = "doc.html"
5aliases = ["/docs/learn/hoon/hoon-tutorial/lists/"]
5aliases = ["/docs/learn/hoon/hoon-tutorial/lists/"]
@@ -113,7 +113,7 @@ The `flop` function takes a list and returns it in reverse order:
116−#### `flop` Exercise
116+#### `flop` Exercise 1.5a
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.
@@ -144,7 +144,7 @@ The function passed to `sort` must produce a `flag`, i.e., `?`.
145### `weld`
145### `weld`
147−The `weld` function takes two lists and concatenates them:
147+The `weld` function takes two lists of the same type and concatenates them:
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!"
157+#### `weld` Exercise 1.5b
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.
157### `snag`
161### `snag`
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:
206−#### `snag` exercise
210+#### `snag` Exercise 1.5c
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.
@@ -237,6 +241,10 @@ The `lent` function takes a list and returns the number of items in it:
244+#### `lent` Exercise 1.5d
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.
240### `roll`
248### `roll`
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
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/)
302+## Additional Exercises
305+Without entering these expressions into the dojo, what are the products of the following expressions?
307+> (lent ~[1 2 3 4 5])
310+> (lent ~[~[1 2] ~[1 2 3] ~[2 3 4]])
313+> (lent ~[1 2 (weld ~[1 2 3] ~[4 5 6])])
317+First, bind these faces.
319+> =b ~['moon' 'planet' 'star' 'galaxy']
322+Then determine whether the following dojo expressions are valid, and if so, what they evaluate to.
333+> (add (lent b) (lent c))
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.
294## Exercise Solutions
340## Exercise Solutions
296−#### `flop`
342+#### `flop` 1.5a
299:: flop.hoon
345:: flop.hoon
@@ -312,7 +358,24 @@ Run in dojo:
312~[44 33 22 11]
358~[44 33 22 11]
365+|= [a=(list @) b=(list @)]
374+> +weld [~[1 2 3] ~[3 4 5 6]]
315−#### `snag`
378+#### `snag` 1.5c
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]]
426+> (lent ~[1 2 3 4 5])
428+> (lent ~[~[1 2] ~[1 2 3] ~[2 3 4]])
430+> (lent ~[1 2 (weld ~[1 2 3] ~[4 5 6])])
440+<|moon planet star galaxy moon planet star galaxy|>
445+This will not run because `weld` expects the elements of both lists to be of the same type.
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`.
451+> (add (lent b) (lent c))
454+We see here the correct way to find the sum of the length of two lists of unknown type.
467+> +palindrome "urbit"
469+> +palindrome "racecar"