on master help sign in
split ex
8b79c89Galen Wolfe-Pauly
docs/programming/hoon/exercises.md−157
@@ -7,160 +7,3 @@ sort: 14
7The best way to cement this knowledge in your mind is to do
7The best way to cement this knowledge in your mind is to do
8exercises. These are roughly in order of ascending difficulty,
8exercises. These are roughly in order of ascending difficulty,
9and they are intended to form a sort of primer to Hoon.
9and 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.
docs/programming/hoon/exercises/fizzbuzz.mdnew file+37
@@ -0,0 +1,37 @@
1+---
2+sort: 1
3+comments: true
4+---
5+
6+# Friends of FizzBuzz
7+
8+1. Consider the following implementation of FizzBuzz, which is
9+ equivalent to the one in the [Demo](demo) section.
10+
11+ ```
12+ |= end/atom
13+ =+ count=1
14+ |-
15+ ^- (list tape)
16+ ?: =(end count)
17+ ~
18+ :-
19+ ?: =(0 (mod count 15))
20+ "FizzBuzz"
21+ ?: =(0 (mod count 5))
22+ "Fizz"
23+ ?: =(0 (mod count 3))
24+ "Buzz"
25+ (text !>(count))
26+ $(count (add 1 count))
27+ ```
28+
29+ Modify this to produce "Fuzz" when the number is divisible by
30+ 7, "Fuzz" when divisible by 21, "FuzzFizz" when divisble by 35,
31+ and "FuzzFizzBuzz" when divisble by 105.
32+
33+1. Modify FizzBuzz from the previous exercise to take both a
34+ start and an end number.
35+
36+1. Modify FizzBuzz from the previous exercise to count down
37+ instead of up.
docs/programming/hoon/exercises/life.mdnew file+117
@@ -0,0 +1,117 @@
1+---
2+sort: 3
3+comments: true
4+---
5+
6+# Conway's Game of Life
7+
8+Conway's Game of Life is a zero-player game played on an `n`x`n`
9+grid of spaces, each of which is either "alive" or "dead". Every
10+iteration, some or all of the spaces change from alive to dead or
11+from dead to alive.
12+
13+Specifically, for each iteration, each dead space becomes alive
14+if and only if it is adjacent to exactly three live spaces. Each
15+live remains alive if and only if it is adjacent to either two or
16+three live spaces. In this context, "adjacent to" includes the
17+eight spaces that share either a side or a corner with the space.
18+Thus, in the following map, the center space (dead) is adjacent
19+to exactly three live spaces, so it will be alive in the next
20+iteration. `#` signifies a live space and `.` signifies a dead
21+space.
22+
23+ ..#
24+ ...
25+ ##.
26+
27+
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.
32+
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`.
37+
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.
42+
43+1. Use the following function to pretty-print the board. For
44+ bonus points, try to follow what it's doing.
45+
46+ ```
47+ :: XX test
48+ |= board/(list (list ?))
49+ ^- tang
50+ %+ turn board
51+ |= row/(list ?)
52+ :- %leaf
53+ ^- tape
54+ %+ turn row
55+ |= space/?
56+ ?: space
57+ '# '
58+ '. '
59+ ```
60+
61+ If you run this function with a board, you'll get a
62+ pretty-printable object (a `tang`), but it won't be
63+ pretty-printed. When running the generator from the dojo,
64+ run `&tang +generator arguments` to mark the result as a
65+ `tang`, which will tell the dojo to intelligently
66+ pretty-print it.
67+
68+1. Using the functions created in the previous exercises, create
69+ a 10 by 10 board, and flip the states of the spaces at
70+ coordinates (counting from the top left, indexed from 0)
71+ `(2,1)`, `(3,2)`, `(1,3)`, `(2,3)`, and `(3,3)`. Use the
72+ pretty printer to print out the board. Your output should be
73+ the following:
74+
75+ ```
76+ . . . . . . . . . .
77+ . . # . . . . . . .
78+ . . . # . . . . . .
79+ . # # # . . . . . .
80+ . . . . . . . . . .
81+ . . . . . . . . . .
82+ . . . . . . . . . .
83+ . . . . . . . . . .
84+ . . . . . . . . . .
85+ . . . . . . . . . .
86+ ```
87+
88+1. Write a function that iterates every space on the board.
89+ Print out the new board. It should look like this:
90+
91+ ```
92+ . . . . . . . . . .
93+ . . . . . . . . . .
94+ . # . # . . . . . .
95+ . . # # . . . . . .
96+ . . # . . . . . . .
97+ . . . . . . . . . .
98+ . . . . . . . . . .
99+ . . . . . . . . . .
100+ . . . . . . . . . .
101+ . . . . . . . . . .
102+ ```
103+
104+ > Note that the spaces on the edge don't have the full nine
105+ > neighbors. For now, assume that they're always dead.
106+
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!
111+
112+1. One common way to handle the edge of the board is to pretend
113+ that it "wraps around", forming a torus. In other words, say
114+ that the spaces on the left edge of the map are adjacent to
115+ 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.
docs/programming/hoon/exercises/math.mdnew file+15
@@ -0,0 +1,15 @@
1+---
2+sort: 2
3+comments: true
4+---
5+
6+# Math
7+
8+1. Write a function that produces a list of the first `n`
9+ triangular numbers. The `n`th triangular number is the sum
10+ of the first `n` positive integers.
11+
12+1. Using the program from the previous exercise, write a
13+ function that produces the first `n` tetrahedral numbers.
14+ The `n`th tetrahedral number is the sum of the first `n`
15+ triangular numbers.