on master help sign in
More examples
ecaaf93Philip C Monk
docs/hoon/exercises/data-structures.md+41−2
@@ -3,7 +3,7 @@ sort: 4
3comments: true
3comments: true
4---
4---
5
5
6−# Sets, Maps, and Queues
6+# Sets and Maps
7
7
8Sets are constructed with `++silt`. A set is a non-ordered
8Sets are constructed with `++silt`. A set is a non-ordered
9collection of unique elements.
9collection of unique elements.
@@ -17,6 +17,45 @@ is of type `(set @t)` since each element is a `@t`.
17
17
18The `++in` core contains library functions for dealing with sets.
18The `++in` core contains library functions for dealing with sets.
19For example, `++has:in` checks whether an element is in the set.
19For example, `++has:in` checks whether an element is in the set.
20−Thus, `(~(has in s) 'arvo')` returns yes.
20+Thus, `(~(has in s) 'arvo')` returns yes. To complete these
21+exercises, you'll need to refer to the [library reference for
22+`++in`](/docs/hoon/library/2h).
21
23
24+1. Create another set, `l`, with members `'c'`, `'hoon'`,
25+ `'javacript'`, `'python'`.
26+
27+1. At the dojo, take the union of `s` and `l`. Delete
28+ `'javascript'` from the result, and add `'coffescript'`.
29+
30+1. Create an alphabetized list of the members of the set created
31+ in the previous exercise.
32+
33+1. Take the Cartesian product of `s` and `l`. The Cartesian
34+ product of two sets is the set of all cells `[a b]` where
35+ `a` is in `s` and `b` is in `l`.
36+
37+The `++by` core is to maps as `++in` is to sets. Refer to its
38+[library reference](/docs/hoon/library/2i). Note that since a
39+map is basically a set of pairs (although not every set of pairs
40+is a map), most functions in `++in` have analogues in `++by`.
41+
42+The constructor for maps is `++malt`.
43+
44+```
45+(malt `(list {@t @ud})`[['kaylee' 3] ['wash' 2] ['mal' 4] ['summer' 1] ~])
46+```
47+
48+Let this map be `m`.
49+
50+1. Create another map, `s`, with `'spring'` mapped to `1`,
51+ `'summer'` mapped to `2`, `'autumn'` mapped to `'3'`, and
52+ `'winter'` mapped to `4`.
53+
54+1. In `m`, change the value at `'mal'` to 5 and add `'cobb'`
55+ with value 4.
56+
57+1. Take the union of `m` and `s`, then get the value at the key
58+ `summer`. Try taking the union of `s` and `m` (i.e. flip the
59+ order) and see if it makes a difference. Try intersecting
60+ them in both orders.
22
61