on master help sign in
edits fib.md
5bb92b7Robert
learn/hoon/workbook/fib.md+10−9
@@ -1,4 +1,4 @@
1−Write a generator that produce a list of Fibonacci numbers up to n.
1+In this example, we will write a generator that produces a list of Fibonacci numbers up to `n`.
2
2
3```
3```
4|= n=@ud
4|= n=@ud
@@ -14,32 +14,33 @@ Write a generator that produce a list of Fibonacci numbers up to n.
14==
14==
15```
15```
16
16
17−First we need to produce a gate which takes an `@ud` so we know when to stop producing the list.
17+On the first line, we use `|=` to produce a gate which takes an `@ud` that's stored in the face `n`. This `n` sample that we give our gate determines how long our Fibonacci sequence is. When the list that our program builds has `n` members, the program ends.
18
18
19```
19```
20%- flop
20%- flop
21```
21```
22+
23+In the line above, we use `flop` on the output of the rest of the program. `flop` is a standard library function to reverse a list.
24+
22−Next we're going to call `flop` on the output of the rest of the program. `flop` is a standard library function to reverse a list. Why are we using this instead of producing the list in the order we want it in the first place? With lists adding an element to the end is an expensive operation that gets more expensive the longer the list is. Adding an element to the front however is cheap. In Big O Notation, adding to the end of a list is O(n) and the front is O(1).
25+Why are we using this instead of just producing the list in the order we want it in the first place? Because with lists, adding an element to the end is a computationally expensive operation that gets more expensive the longer the list is, due to the fact that you need to traverse to the end of the tree. Adding an element to the front, however, is cheap. [In Big-O Notation](https://en.wikipedia.org/wiki/Big_O_notation), adding to the end of a list is O(n) and the front is O(1).
23
26
24```
27```
25=+ [i=0 p=0 q=1 r=*(list @ud)]
28=+ [i=0 p=0 q=1 r=*(list @ud)]
26```
29```
27
30
28−We are going to need several values throughout the execution of this task so we set them all up here. It's worth noticing that because the subject is searched head first, ignoring faces, we can define a number of faces at once by putting them in a tuple like this.
29−
30−The only one of these that is interesting is `r` We're using `*` to bunt, or get the default value for, the mold `(list @ud)`.
31+Here we declare several values that we are going to use the execution of our program. The only one of these that is noteworthy is `r`: we're using `*` to bunt, or get the default value for, the mold `(list @ud)`.
31
32
32```
33```
33|- ^+ r
34|- ^+ r
34```
35```
35
36
36−Next we define the trap that will contain the majority of our program. We also use `^+` to cast the output by example. This will result in the output being the same type as `r`, a list of `@ud`.
37+With this code, we define `|-`, the _trap_ that acts as a recursion point and contains the rest of our program. We also use `^+` to cast the output by example. This will result in the output being the same type as `r`, a list of `@ud`.
37
38
38```
39```
39?: =(i n) r
40?: =(i n) r
40```
41```
41
42
42−Here is our terminating case, if `i` and `n` are equal we produce `r` as the result.
43+In the code above, `?:` checks whether the first child, `=(i n)`, our terminating case, is true or false. If it is true, it branches to `r` and the program ends. If it is false, the program continues to the code below.
43
44
44```
45```
45%= $
46%= $
@@ -50,4 +51,4 @@ Here is our terminating case, if `i` and `n` are equal we produce `r` as the res
50==
51==
51```
52```
52
53
53−The final expression in our program calls the `$` arm of the trap we are in with some changes. We increment `i`, set `p` to be `q` and `q` becomes the sum of `q` and `q`. `r` becomes a cell of `q` and whatever `r` was previously. This is the list that will get flopped to produce the result at the end of the computation.
54+The final expression in our program calls the `$` arm of the trap we are but makes some changes: we increment `i`, set `p` to be `q` and `q` becomes the sum of `p` and `q`. `r` becomes a cell of `q` and whatever `r` was previously. The list built from this is the one that will get `flop`ped to produce the result at the end of the computation.