on master help sign in
edits fib.md
commit5bb92b70529cf95aa10d359ab25da8b1af05d808 authorRobert <robert@tlon.io> dateFri 17 May 2019, 14:42 (7 years ago) parent766993d
1 file changed+10−9
learn/hoon/workbook/fib.md+10−9
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`.
22
33```
44|= n=@ud
@@ -14,32 +14,33 @@ Write a generator that produce a list of Fibonacci numbers up to n.
1414==
1515```
1616
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.
1818
1919```
2020%- flop
2121```
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).
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+
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).
2326
2427```
2528=+ [i=0 p=0 q=1 r=*(list @ud)]
2629```
2730
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)`.
3132
3233```
3334|- ^+ r
3435```
3536
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`.
3738
3839```
3940?: =(i n) r
4041```
4142
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.
4344
4445```
4546%= $
@@ -50,4 +51,4 @@ Here is our terminating case, if `i` and `n` are equal we produce `r` as the res
5051==
5152```
5253
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.