on master help sign in
Merge branch 'cleanorg' into HEAD
commit8bfd1a5339d9647ff330101d425811a9a507e227 authorPhilip C Monk <philip.monk@tlon.io> dateFri 22 Apr 2016, 13:12 (11 years ago) parent96049c50d9fe15
1 file changed+166
docs/programming/hoon/exercises.md+166
docs/programming/hoon/exercises.mdnew file+166
@@ -0,0 +1,166 @@
1+---
2+sort: 14
3+---
4+
5+# Exercises
6+
7+The best way to cement this knowledge in your mind is to do
8+exercises. These are roughly in order of ascending difficulty,
9+and they are intended to form a sort of primer to Hoon.
10+
11+## Friends of FizzBuzz
12+
13+1. Consider the following implementation of FizzBuzz, which is
14+ equivalent to the one in the [Demo](demo) section.
15+
16+ ```
17+ |= end/atom
18+ =+ count=1
19+ |-
20+ ^- (list tape)
21+ ?: =(end count)
22+ ~
23+ :-
24+ ?: =(0 (mod count 15))
25+ "FizzBuzz"
26+ ?: =(0 (mod count 5))
27+ "Fizz"
28+ ?: =(0 (mod count 3))
29+ "Buzz"
30+ (text !>(count))
31+ $(count (add 1 count))
32+ ```
33+
34+ Modify this to produce "Fuzz" when the number is divisible by
35+ 7, "Fuzz" when divisible by 21, "FuzzFizz" when divisble by 35,
36+ and "FuzzFizzBuzz" when divisble by 105.
37+
38+1. Modify FizzBuzz from the previous exercise to take both a
39+ start and an end number.
40+
41+1. Modify FizzBuzz from the previous exercise to count down
42+ instead of up.
43+
44+## Math
45+
46+1. Write a function that produces a list of the first `n`
47+ triangular numbers. The `n`th triangular number is the sum
48+ of the first `n` positive integers.
49+
50+1. Using the program from the previous exercise, write a
51+ function that produces the first `n` tetrahedral numbers.
52+ The `n`th tetrahedral number is the sum of the first `n`
53+ triangular numbers.
54+
55+## Conway's Game of Life
56+
57+Conway's Game of Life is a zero-player game played on an `n`x`n`
58+grid of spaces, each of which is either "alive" or "dead". Every
59+iteration, some or all of the spaces change from alive to dead or
60+from dead to alive.
61+
62+Specifically, for each iteration, each dead space becomes alive
63+if and only if it is adjacent to exactly three live spaces. Each
64+live remains alive if and only if it is adjacent to either two or
65+three live spaces. In this context, "adjacent to" includes the
66+eight spaces that share either a side or a corner with the space.
67+Thus, in the following map, the center space (dead) is adjacent
68+to exactly three live spaces, so it will be alive in the next
69+iteration. `#` signifies a live space and `.` signifies a dead
70+space.
71+
72+ ..#
73+ ...
74+ ##.
75+
76+
77+1. Write a function that takes nine loobean inputs, `nw`, `nc`,
78+ `ne`, `cw`, `cc`, `ce`, `sw`, `sc`, and `se`. Produce 'yes'
79+ if the center space will be alive in the next generation and
80+ 'no' if it will be dead.
81+
82+1. Write a function that takes an integer `n` and produces an
83+ `n` by `n` grid of loobeans, (hereafter, "board"),
84+ initialized to 'no'. This grid should be represented as a
85+ list of `n` lists of length `n`.
86+
87+1. Write a function that takes a board, a row index, and a
88+ column index, and produces a new board, identical to the old
89+ except that the state of the space referred to by the row and
90+ column indices has been toggled.
91+
92+1. Use the following function to pretty-print the board. For
93+ bonus points, try to follow what it's doing.
94+
95+ ```
96+ :: XX test
97+ |= board/(list (list ?))
98+ ^- tang
99+ %+ turn board
100+ |= row/(list ?)
101+ :- %leaf
102+ ^- tape
103+ %+ turn row
104+ |= space/?
105+ ?: space
106+ '# '
107+ '. '
108+ ```
109+
110+ If you run this function with a board, you'll get a
111+ pretty-printable object (a `tang`), but it won't be
112+ pretty-printed. When running the generator from the dojo,
113+ run `&tang +generator arguments` to mark the result as a
114+ `tang`, which will tell the dojo to intelligently
115+ pretty-print it.
116+
117+1. Using the functions created in the previous exercises, create
118+ a 10 by 10 board, and flip the states of the spaces at
119+ coordinates (counting from the top left, indexed from 0)
120+ `(2,1)`, `(3,2)`, `(1,3)`, `(2,3)`, and `(3,3)`. Use the
121+ pretty printer to print out the board. Your output should be
122+ the following:
123+
124+ ```
125+ . . . . . . . . . .
126+ . . # . . . . . . .
127+ . . . # . . . . . .
128+ . # # # . . . . . .
129+ . . . . . . . . . .
130+ . . . . . . . . . .
131+ . . . . . . . . . .
132+ . . . . . . . . . .
133+ . . . . . . . . . .
134+ . . . . . . . . . .
135+ ```
136+
137+1. Write a function that iterates every space on the board.
138+ Print out the new board. It should look like this:
139+
140+ ```
141+ . . . . . . . . . .
142+ . . . . . . . . . .
143+ . # . # . . . . . .
144+ . . # # . . . . . .
145+ . . # . . . . . . .
146+ . . . . . . . . . .
147+ . . . . . . . . . .
148+ . . . . . . . . . .
149+ . . . . . . . . . .
150+ . . . . . . . . . .
151+ ```
152+
153+ > Note that the spaces on the edge don't have the full nine
154+ > neighbors. For now, assume that they're always dead.
155+
156+1. Write a function that runs the above function `n` times on
157+ the board. Observe the board as the game progresses.
158+ Congratulations, you've written Conway's Game of Life in
159+ Hoon!
160+
161+1. One common way to handle the edge of the board is to pretend
162+ that it "wraps around", forming a torus. In other words, say
163+ that the spaces on the left edge of the map are adjacent to
164+ the ones on the right edge, and the spaces on the top edge
165+ are adjacent to the spaces on the bottom edge. Pay special
166+ attention to the corners.