on master help sign in
more examples
commitcd02e96b08cf941dc54dde48122bc1aed668021c authorPhilip C Monk <philip.monk@tlon.io> dateFri 22 Apr 2016, 20:28 (11 years ago) parent01ac191
4 files changed+98−23
docs/programming/hoon/exercises/data-structures.md+22 docs/programming/hoon/exercises/life.md+26−23 docs/programming/hoon/exercises/lists.md+39 docs/programming/hoon/exercises/math.md+11
docs/programming/hoon/exercises/data-structures.mdnew file+22
@@ -0,0 +1,22 @@
1+---
2+sort: 4
3+comments: true
4+---
5+
6+# Sets, Maps, and Queues
7+
8+Sets are constructed with `++silt`. A set is a non-ordered
9+collection of unique elements.
10+
11+```
12+(silt `(list @t)`['urbit' 'arvo' 'hoon' 'arvo' ~])
13+```
14+
15+We'll refer to this particular set as `s` in the exercises. It
16+is of type `(set @t)` since each element is a `@t`.
17+
18+The `++in` core contains library functions for dealing with sets.
19+For example, `++has:in` checks whether an element is in the set.
20+Thus, `(~(has in s) 'arvo')` returns yes.
21+
22+
docs/programming/hoon/exercises/life.md+26−23
@@ -1,5 +1,5 @@
11---
2−sort: 3
2+sort: 5
33comments: true
44---
55
@@ -25,23 +25,23 @@ space.
2525 ##.
2626
2727
28−1. Write a function that takes nine loobean inputs, `nw`, `nc`,
29− `ne`, `cw`, `cc`, `ce`, `sw`, `sc`, and `se`. Produce 'yes'
30− if the center space will be alive in the next generation and
31− 'no' if it will be dead.
28+1. Write a function `next-generation` that takes nine loobean
29+ inputs, `nw`, `nc`, `ne`, `cw`, `cc`, `ce`, `sw`, `sc`, and
30+ `se`. Produce 'yes' if the center space will be alive in the
31+ next generation and 'no' if it will be dead.
3232
33−1. Write a function that takes an integer `n` and produces an
34− `n` by `n` grid of loobeans, (hereafter, "board"),
35− initialized to 'no'. This grid should be represented as a
36− list of `n` lists of length `n`.
33+1. Write a function `create-board` that takes an integer `n` and
34+ produces an `n` by `n` grid of loobeans, (hereafter,
35+ "board"), initialized to 'no'. This grid should be
36+ represented as a list of `n` lists of length `n`.
3737
38−1. Write a function that takes a board, a row index, and a
39− column index, and produces a new board, identical to the old
40− except that the state of the space referred to by the row and
41− column indices has been toggled.
38+1. Write a function `toggle-space` that takes a board, a row
39+ index, and a column index, and produces a new board,
40+ identical to the old except that the state of the space
41+ referred to by the row and column indices has been toggled.
4242
43−1. Use the following function to pretty-print the board. For
44− bonus points, try to follow what it's doing.
43+1. Use the following function `print-board` to pretty-print the
44+ board. For bonus points, try to follow what it's doing.
4545
4646 ```
4747 :: XX test
@@ -85,8 +85,8 @@ space.
8585 . . . . . . . . . .
8686 ```
8787
88−1. Write a function that iterates every space on the board.
89− Print out the new board. It should look like this:
88+1. Write a function `step` that iterates every space on the
89+ board. Print out the new board. It should look like this:
9090
9191 ```
9292 . . . . . . . . . .
@@ -104,14 +104,17 @@ space.
104104 > Note that the spaces on the edge don't have the full nine
105105 > neighbors. For now, assume that they're always dead.
106106
107−1. Write a function that runs the above function `n` times on
108− the board. Observe the board as the game progresses.
109− Congratulations, you've written Conway's Game of Life in
110− Hoon!
107+1. Write a function `life` that runs the above function `n`
108+ times on the board. Observe the board as the game
109+ progresses. Congratulations, you've written Conway's Game of
110+ Life in Hoon!
111111
1121121. One common way to handle the edge of the board is to pretend
113113 that it "wraps around", forming a torus. In other words, say
114114 that the spaces on the left edge of the map are adjacent to
115115 the ones on the right edge, and the spaces on the top edge
116− are adjacent to the spaces on the bottom edge. Pay special
117− attention to the corners.
116+ are adjacent to the spaces on the bottom edge. Modify `step`
117+ to act this way. Pay special attention to the corners.
118+
119+1. Modify `create-board` to use `++og` to randomly initialize
120+ the board with live or dead spaces.
docs/programming/hoon/exercises/lists.mdnew file+39
@@ -0,0 +1,39 @@
1+---
2+sort: 3
3+comments: true
4+---
5+
6+# Lists
7+
8+In the following exercises, let `l` be `` `(list @t)`['u' 'r' 'b'
9+'i' 't' ~]``
10+
11+1. A list is a null-terminated tuple of values. For example,
12+ `[1 2 3 ~]` is a list of three integers, and
13+ `['a' 'b' 'c' ~]` is a list of three cords. Try casting
14+ `['a' 'b' 'c' ~]` to `(list @t)` and to `(list @)`. Lists
15+ are parameterized with the type of the values.
16+
17+1. `++flop` takes a list and produces its reverse. Use it to
18+ reverse `l`.
19+
20+1. Repeat the above exercise without using `++flop`.
21+
22+1. `++sort` takes a list and a comparison function to sort a
23+ list. The function must take two elements of the list and
24+ produce true if the first element is "less than" the other.
25+ Sort `l` alphabetically. Sort it reverse alphabetically.
26+
27+1. `++turn` takes a list and a function and applies the function
28+ to each element in the list, forming a new list of the
29+ results. This is called "map" in many functional languages.
30+ The function must accept an element of the list. Use the
31+ fact that subtracting 32 from a lowercase letter gives the
32+ equivalent uppercase letter (i.e. `(sub 'a' 'A')` is 32) to
33+ change every character in `l` to uppercase.
34+
35+1. `++welp` concatentates two lists. Concatenate `l` with the
36+ reverse of `l`.
37+
38+1. `++snag` takes a number `n` and a list and produces the
39+ element at index `n`. Pull out the `'i'` in `l`.
docs/programming/hoon/exercises/math.md+11
@@ -13,3 +13,14 @@ comments: true
1313 function that produces the first `n` tetrahedral numbers.
1414 The `n`th tetrahedral number is the sum of the first `n`
1515 triangular numbers.
16+
17+1. The Fibonacci numbers are defined on the natural numbers as
18+ follows:
19+
20+ ```
21+ fib(0) = 0
22+ fib(1) = 1
23+ fib(n) = fib(n-1) + fib(n-2)
24+ ```
25+
26+ Write a function that computes the `nth` Fibonacci number.