on f3ca521 help sign in
336 lines 8.6 kB history raw
1+++
2title = "1.4 Lists"
3weight = 6
4template = "doc.html"
5aliases = ["/docs/learn/hoon/hoon-tutorial/lists/"]
6+++
8# 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:
14`[1 [2 [3 [4 ~]]]]`
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.
20```
21> `(list @)`~
24> `(list @)`[1 2 3 4 5 ~]
25~[1 2 3 4 5]
27> `(list @)`[1 [2 [3 [4 [5 ~]]]]]
28~[1 2 3 4 5]
30> `(list @)`~[1 2 3 4 5]
31~[1 2 3 4 5]
32```
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 ~]`.
36Let's make a list whose items are of the `@t` string type:
38```
39> `(list @t)`['Urbit' 'will' 'rescue' 'the' 'internet' ~]
40<|Urbit will rescue the internet|>
41```
43The 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, `?`:
45```
46> =>(b=`(list @)`[1 2 3 4 5 ~] i.b)
47-find.i.b
48find-fork-d
50> =>(b=`(list @)`[1 2 3 4 5 ~] ?~(b ~ i.b))
53> =>(b=`(list @)`[1 2 3 4 5 ~] ?~(b ~ t.b))
54~[2 3 4 5]
55```
57It'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.
59You 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.
61## Tapes
63While 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.
65Hoon has two kinds of strings: cords and tapes. Cords are atoms with aura `@t`, and they're pretty-printed between `''` marks.
67```
68> 'this is a cord'
69'this is a cord'
71> `@`'this is a cord'
722.037.307.443.564.446.887.986.503.990.470.772
73```
75A tape is a list of `@tD` atoms (i.e., ASCII characters).
77```
78> "this is a tape"
79"this is a tape"
81> `(list @)`"this is a tape"
82~[116 104 105 115 32 105 115 32 97 32 116 97 112 101]
83```
85You can also use the words `cord` and `tape` for casting:
87```
88> `tape`"this is a tape"
89"this is a tape"
91> `cord`'this is a cord'
92'this is a cord'
93```
95## List Functions in the Hoon Standard Library
97Lists 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.
99For 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.
101### `flop`
103The `flop` function takes a list and returns it in reverse order:
105```
106> (flop ~[11 22 33])
107~[33 22 11]
109> (flop "Hello!")
110"!olleH"
112> (flop (flop "Hello!"))
113"Hello!"
114```
116#### `flop` Exercise
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.
120### `sort`
122The `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'):
124```
125> (sort ~[37 62 49 921 123] lth)
126~[37 49 62 123 921]
127```
129To sort the list from greatest to least, use the `gth` gate ('greater than') as the basis of comparison instead:
131```
132> (sort ~[37 62 49 921 123] gth)
133~[921 123 62 49 37]
134```
136You can sort letters this way as well:
138```
139> (sort ~['a' 'f' 'e' 'k' 'j'] lth)
140<|a e f j k|>
141```
143The function passed to `sort` must produce a `flag`, i.e., `?`.
145### `weld`
147The `weld` function takes two lists and concatenates them:
149```
150> (weld ~[1 2 3] ~[4 5 6])
151~[1 2 3 4 5 6]
153> (weld "Happy " "Birthday!")
154"Happy Birthday!"
155```
157### `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:
161```
162> (snag 0 `(list @)`~[11 22 33 44])
16311
165> (snag 1 `(list @)`~[11 22 33 44])
16622
168> (snag 3 `(list @)`~[11 22 33 44])
16944
171> (snag 3 "Hello!")
172'l'
174> (snag 1 "Hello!")
175'e'
177> (snag 5 "Hello!")
178'!'
179```
181Note: 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:
183```
184> =b `(list @)`~[11 22 33 44]
186> b
187~[11 22 33 44]
188```
190Now let's use `?~` to prove that `b` isn't null, and then try to `snag` it:
192```
193> ?~(b ~ (snag 0 b))
194nest-fail
195```
197The problem is that `snag` is expecting a raw list, not a list that is known to be non-null.
199You can cast `b` to `(list)` to work around this:
201```
202> ?~(b ~ (snag 0 `(list)`b))
20311
204```
206#### `snag` exercise
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.
210### `oust`
212The `oust` function takes a pair of atoms `[a=@ b=@]` and a list, and returns the list with `b` items removed, starting at item `a`:
214```
215> (oust [0 1] `(list @)`~[11 22 33 44])
216~[22 33 44]
218> (oust [0 2] `(list @)`~[11 22 33 44])
219~[33 44]
221> (oust [1 2] `(list @)`~[11 22 33 44])
222~[11 44]
224> (oust [2 2] "Hello!")
225"Heo!"
226```
228### `lent`
230The `lent` function takes a list and returns the number of items in it:
232```
233> (lent ~[11 22 33 44])
236> (lent "Hello!")
238```
240### `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`:
244```
245> (roll `(list @)`~[11 22 33 44 55] add)
246165
248> (roll `(list @)`~[11 22 33 44 55] mul)
24919.326.120
250```
252### `turn`
254The `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:
256```
257> (turn `(list @)`~[11 22 33 44] |=(a=@ +(a)))
258~[12 23 34 45]
259```
261Or to double each item in a list of atoms:
263```
264> (turn `(list @)`~[11 22 33 44] |=(a=@ (mul 2 a)))
265~[22 44 66 88]
266```
268`turn` is Hoon's version of Haskell's `map`.
270We can rewrite the Caesar cipher program using `turn`:
272```hoon
273|= [a=@ b=tape]
274^- tape
275?: (gth a 25)
276 $(a (sub a 26))
277%+ turn b
278|= c=@tD
279?: &((gte c 'A') (lte c 'Z'))
280 =. c (add c a)
281 ?. (gth c 'Z') c
282 (sub c 26)
283?: &((gte c 'a') (lte c 'z'))
284 =. c (add c a)
285 ?. (gth c 'z') c
286 (sub c 26)
288```
290## Using `hoon.hoon` to Learn Hoon
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/)
294## Exercise Solutions
296#### `flop`
298```hoon
299:: flop.hoon
300::
301|= a=(list @)
302=| b=(list @)
303|- ^- (list @)
304?~ a b
305$(b [i.a b], a t.a)
306```
308Run in dojo:
310```
311> +flop ~[11 22 33 44]
312~[44 33 22 11]
313```
315#### `snag`
317```hoon
318:: snag.hoon
319::
320|= [a=@ b=(list @)]
321?~ b !!
322?: =(0 a) i.b
323$(a (dec a), b t.b)
324```
326Run in dojo:
328```
329> +snag [0 ~[11 22 33 44]]
33011
332> +snag [2 ~[11 22 33 44]]
33333
334```
336### [Next Up: Walkthrough -- Fibonacci Sequence](../fibonacci)