5aliases = ["/docs/learn/hoon/hoon-tutorial/lists/"]
10A **list** is a type of noun that you'll frequently encounter when reading and writing Hoon. A list can be thought of as an ordered arrangement of zero or more elements terminated by a `~` (null).
12So a list can be either null or non-null. When the list contains only `~` and no items, it's the null list. Most lists are, however, non-null lists, which have items preceding the `~`. Non-null lists, called _lests_, are cells in which the head is the first list item, and the tail is the rest of the list. The tail is itself a list, and if such a list is also non-null, the head of this sub-list is the second item in the greater list, and so on. To illustrate, let's look at a list `[1 2 3 4 ~]` with the cell-delineating brackets left in:
16It's easy to see where the heads are and where the nesting tails are. The head of the above list is the atom `1` and the tail is the list `[2 [3 [4 ~]]]`, (or `[2 3 4 ~]`). Recall that whenever cell brackets are omitted so that visually there appears to be more than two child nouns, it is implicitly understood that the right-most nouns constitute a cell.
18To make a list, let's cast nouns to the `(list @)` ("list of atoms") type.
24> `(list @)`[1 2 3 4 5 ~]
27> `(list @)`[1 [2 [3 [4 [5 ~]]]]]
30> `(list @)`~[1 2 3 4 5]
34Notice how the last Dojo command has a different construction, with the `~` in front of the bracketed items. This is just another way of writing the same thing; `~[1 2]` is semantically identical to `[1 2 ~]`.
36## An aside about Casting
38The use of casts in this example is helpful to explain to the Hoon compiler exactly what it is we mean with these data structures. In this case, we are telling it they are all lists of atoms. Get in the habit of casting your data structures, as it will not only help anyone reading your code, but it will help you in hunting down bugs in your code.
40Let's make a list whose items are of the `@t` string type:
43> `(list @t)`['Urbit' 'will' 'rescue' 'the' 'internet' ~]
44<|Urbit will rescue the internet|>
47The head of a list has the face `i` and the tail has the face `t`. (For the sake of neatness, these faces aren't shown by the Hoon pretty printer.) To use the `i` and `t` faces of a list, you must first prove that the list is non-null by using the conditional family of runes, `?`:
50> =>(b=`(list @)`[1 2 3 4 5 ~] i.b)
54> =>(b=`(list @)`[1 2 3 4 5 ~] ?~(b ~ i.b))
57> =>(b=`(list @)`[1 2 3 4 5 ~] ?~(b ~ t.b))
61It's important to note that performing tests like `?~ mylist` will actually transform `mylist` into a `lest`, a non-null list. Because `lest` is a different type than `list`, performing such tests can come back to bite you later in non-obvious ways when you try to use some standard library functions meant for `list`s.
63You can construct lists of any type. `(list @)` indicates a list of atoms, `(list ^)` indicates a list of cells, `(list [@ ?])` indicates a list of cells whose head is an atom and whose tail is a flag, etc.
67While a list can be of any type, there are some special types of lists that are built into Hoon. The tape is the most common example.
69Hoon has two kinds of strings: cords and tapes. Cords are atoms with aura `@t`, and they're pretty-printed between `''` marks.
762.037.307.443.564.446.887.986.503.990.470.772
79A tape is a list of `@tD` atoms (i.e., ASCII characters).
85> `(list @)`"this is a tape"
86~[116 104 105 115 32 105 115 32 97 32 116 97 112 101]
89You can also use the words `cord` and `tape` for casting:
92> `tape`"this is a tape"
95> `cord`'this is a cord'
99## List Functions in the Hoon Standard Library
101Lists are a commonly used data structure. Accordingly, there are several functions in the [Hoon standard library](/docs/reference/library/) intended to make lists easier to use.
103For a complete list of these functions, check out the standard library reference for lists [here](/docs/reference/library/2b/). Here we'll look at a few of these functions.
107The `flop` function takes a list and returns it in reverse order:
116> (flop (flop "Hello!"))
120#### `flop` Exercise 1.5a
122Without 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.
126The `sort` function uses the 'quicksort' algorithm to sort a list. It takes a list to sort and a gate that serves as a comparator. For example, if you want to sort the list `~[37 62 49 921 123]` from least to greatest, you would pass that list along with the `lth` gate (for 'less than'):
129> (sort ~[37 62 49 921 123] lth)
133To sort the list from greatest to least, use the `gth` gate ('greater than') as the basis of comparison instead:
136> (sort ~[37 62 49 921 123] gth)
140You can sort letters this way as well:
143> (sort ~['a' 'f' 'e' 'k' 'j'] lth)
147The function passed to `sort` must produce a `flag`, i.e., `?`.
151The `weld` function takes two lists of the same type and concatenates them:
154> (weld ~[1 2 3] ~[4 5 6])
157> (weld "Happy " "Birthday!")
161#### `weld` Exercise 1.5b
163Without 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.
167The `snag` function takes an atom `n` and a list, and returns the `n`th item of the list, where `0` is the first item:
170> (snag 0 `(list @)`~[11 22 33 44])
173> (snag 1 `(list @)`~[11 22 33 44])
176> (snag 3 `(list @)`~[11 22 33 44])
189Note: there is currently a type system issue that causes some of these functions to fail when passed a list `b` after some type inference has been performed on `b`. For an illustration of the bug, let's set `b` to be a `(list @)` of `~[11 22 33 44]` in the dojo:
192> =b `(list @)`~[11 22 33 44]
198Now let's use `?~` to prove that `b` isn't null, and then try to `snag` it:
205The problem is that `snag` is expecting a raw list, not a list that is known to be non-null.
207You can cast `b` to `(list)` to work around this:
210> ?~(b ~ (snag 0 `(list)`b))
214#### `snag` Exercise 1.5c
216Without using `snag`, write a gate that returns the `n`th item of a list. There is a solution at the bottom of this lesson.
220The `oust` function takes a pair of atoms `[a=@ b=@]` and a list, and returns the list with `b` items removed, starting at item `a`:
223> (oust [0 1] `(list @)`~[11 22 33 44])
226> (oust [0 2] `(list @)`~[11 22 33 44])
229> (oust [1 2] `(list @)`~[11 22 33 44])
232> (oust [2 2] "Hello!")
238The `lent` function takes a list and returns the number of items in it:
241> (lent ~[11 22 33 44])
248#### `lent` Exercise 1.5d
250Without 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.
254The `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`:
257> (roll `(list @)`~[11 22 33 44 55] add)
260> (roll `(list @)`~[11 22 33 44 55] mul)
266The `turn` function takes a list and a gate, and returns a list of the products of applying each item of the input list to the gate. For example, to add `1` to each item in a list of atoms:
269> (turn `(list @)`~[11 22 33 44] |=(a=@ +(a)))
273Or to double each item in a list of atoms:
276> (turn `(list @)`~[11 22 33 44] |=(a=@ (mul 2 a)))
280`turn` is Hoon's version of Haskell's `map`.
282We can rewrite the Caesar cipher program using `turn`:
291?: &((gte c 'A') (lte c 'Z'))
295?: &((gte c 'a') (lte c 'z'))
302## Using `hoon.hoon` to Learn Hoon
304The 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/)
306## Additional Exercises
309Without entering these expressions into the dojo, what are the products of the following expressions?
314> (lent ~[~[1 2] ~[1 2 3] ~[2 3 4]])
317> (lent ~[1 2 (weld ~[1 2 3] ~[4 5 6])])
321First, bind these faces.
323> =b ~['moon' 'planet' 'star' 'galaxy']
326Then determine whether the following dojo expressions are valid, and if so, what they evaluate to.
337> (add (lent b) (lent c))
341Write 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.
361> +flop ~[11 22 33 44]
369|= [a=(list @) b=(list @)]
378> +weld [~[1 2 3] ~[3 4 5 6]]
396> +snag [0 ~[11 22 33 44]]
399> +snag [2 ~[11 22 33 44]]
432> (lent ~[~[1 2] ~[1 2 3] ~[2 3 4]])
434> (lent ~[1 2 (weld ~[1 2 3] ~[4 5 6])])
444<|moon planet star galaxy moon planet star galaxy|>
449This will not run because `weld` expects the elements of both lists to be of the same type.
453This 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`.
455> (add (lent b) (lent c))
458We see here the correct way to find the sum of the length of two lists of unknown type.
473> +palindrome "racecar"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list