Finalize stack draft writeup
All checks were successful
continuous-integration/drone/push Build is passing
BIN
content/blog/stack_recursion/if_999_1_part1.png
Normal file
After Width: | Height: | Size: 30 KiB |
BIN
content/blog/stack_recursion/if_999_1_part2.png
Normal file
After Width: | Height: | Size: 34 KiB |
BIN
content/blog/stack_recursion/if_999_1_part3.png
Normal file
After Width: | Height: | Size: 36 KiB |
|
@ -133,16 +133,16 @@ However, to stick by convention 4, we pretend that the stack is empty, and that
|
|||
trying to manipulate it will result in an error. So, we can start by imagining
|
||||
an empty stack, with an integer \\(x\\) on top:
|
||||
|
||||
{{< todo >}}Stack with x on top{{< /todo >}}
|
||||
{{< stack_image "if_999_1_part1.png" >}}
|
||||
|
||||
Then, \\(\\text{PushI} \\; 0\\) will push 0 onto the stack:
|
||||
|
||||
{{< todo >}}Stack with x then 0{{< /todo >}}
|
||||
{{< stack_image "if_999_1_part2.png" >}}
|
||||
|
||||
\\(\\text{Slide} \\; 1\\) will then remove the 1 element after the top element: \\(x\\).
|
||||
We end up with the following stack:
|
||||
|
||||
{{< todo >}}Stack with 0{{< /todo >}}
|
||||
{{< stack_image "slide_example_part3.png" >}}
|
||||
|
||||
The function has finished running, and we maintain convention 3: the function's
|
||||
return value is in place of its argument on the stack.
|
||||
|
@ -195,20 +195,20 @@ Eq
|
|||
Let's walk through this. We start with only the arguments
|
||||
on the stack:
|
||||
|
||||
{{< todo >}}image of stack of factorial call{{< /todo >}}
|
||||
{{< stack_image "recursion_all_part1.png" >}}
|
||||
|
||||
Then, \\(\\text{Offset} \\; 0\\) duplicates the first argument
|
||||
(the number):
|
||||
|
||||
{{< todo >}}image of stack of factorial call with number duped{{< /todo >}}
|
||||
{{< stack_image "recursion_all_part2.png" >}}
|
||||
|
||||
Next, 0 is pushed onto the stack:
|
||||
|
||||
{{< todo >}}image of stack of factorial call with number duped, and zero{{< /todo >}}
|
||||
{{< stack_image "recursion_all_part3.png" >}}
|
||||
|
||||
Finally, \\(\\text{Eq}\\) performs the equality check:
|
||||
|
||||
{{< todo >}}image of stack of factorial call with boolean{{< /todo >}}
|
||||
{{< stack_image "recursion_all_part4.png" >}}
|
||||
|
||||
Great! Now, it's time to branch. What happens if "true" is on top of
|
||||
the stack? In that case, we no longer need any more information.
|
||||
|
@ -222,11 +222,11 @@ Slide 2
|
|||
|
||||
As before, we push the desired answer onto the stack:
|
||||
|
||||
{{< todo >}}image of stack of factorial call with 1 on the stack{{< /todo >}}
|
||||
{{< stack_image "recursion_base_part2.png" >}}
|
||||
|
||||
Then, to follow convention 3, we must get rid of the arguments. We do this by using \\(\\text{Slide}\\):
|
||||
|
||||
{{< todo >}}image of stack of factorial call with only 1 on the stack{{< /todo >}}
|
||||
{{< stack_image "recursion_base_part3.png" >}}
|
||||
|
||||
Great! The \\(\\textbf{if}\\) branch is now done, and we're left with the correct answer on the stack.
|
||||
Excellent!
|
||||
|
@ -241,7 +241,7 @@ Offset 1
|
|||
|
||||
The result is as follows:
|
||||
|
||||
{{< todo >}}image of stack of factorial call with extra function on top{{< /todo >}}
|
||||
{{< stack_image "recursion_rec_part1.png" >}}
|
||||
|
||||
Next, we must compute \\(n-1\\). This is pretty standard stuff:
|
||||
|
||||
|
@ -254,11 +254,11 @@ Add
|
|||
Why these three instructions? Well, with the function now on the top of the stack, the number argument is somewhat
|
||||
buried, and thus, we need to use \\(\\text{Offset} \\; 1\\) to get to it:
|
||||
|
||||
{{< todo >}}image of stack of factorial call with extra function and number on top{{< /todo >}}
|
||||
{{< stack_image "recursion_rec_part2.png" >}}
|
||||
|
||||
Then, we push a negative number, and add it to to the number on top. We end up with:
|
||||
|
||||
{{< todo >}}image of stack of factorial call with extra function and number-1 on top{{< /todo >}}
|
||||
{{< stack_image "recursion_rec_part3.png" >}}
|
||||
|
||||
Finally, we have our arguments in order as per convention 2. To follow convention 1, we must
|
||||
now push the function onto the top of the stack:
|
||||
|
@ -269,8 +269,7 @@ Offset 1
|
|||
|
||||
The stack is now as follows:
|
||||
|
||||
{{< todo >}}image of stack of factorial call with extra function and number-1
|
||||
and extra function on top{{< /todo >}}
|
||||
{{< stack_image "recursion_rec_part4.png" >}}
|
||||
|
||||
Good! With the preparations for the function call now complete, we take
|
||||
the leap:
|
||||
|
@ -286,7 +285,7 @@ will be removed from the stack and replaced with the result of the function
|
|||
as per convention 2. The rest of the stack will remain untouched as
|
||||
per convention 4. We thus expect the stack to look as follows:
|
||||
|
||||
{{< todo >}}image of stack of factorial call with with (n-1)! on top{{< /todo >}}
|
||||
{{< stack_image "recursion_rec_part5.png" >}}
|
||||
|
||||
We're almost there! What's left is to perform the multiplication (we're
|
||||
safe to destroy the argument now, since we will not be needing it after
|
||||
|
@ -300,12 +299,12 @@ Slide 1
|
|||
The multiplication leaves us with \\(n(n-1)! = n!\\) on top of the stack,
|
||||
and the function argument below it:
|
||||
|
||||
{{< todo >}}image of stack of factorial call with with n! on top{{< /todo >}}
|
||||
{{< stack_image "recursion_rec_part6.png" >}}
|
||||
|
||||
We then use \\(\\text{Slide}\\) so that only the factorial is on the
|
||||
stack, satisfying convention 3:
|
||||
|
||||
{{< todo >}}image of stack of factorial call with with n! on top{{< /todo >}}
|
||||
{{< stack_image "recursion_rec_part7.png" >}}
|
||||
|
||||
That's it! We have successfully executed the recursive case. The whole
|
||||
function is now as follows:
|
BIN
content/blog/stack_recursion/recursion_all_part1.png
Normal file
After Width: | Height: | Size: 35 KiB |
BIN
content/blog/stack_recursion/recursion_all_part2.png
Normal file
After Width: | Height: | Size: 38 KiB |
BIN
content/blog/stack_recursion/recursion_all_part3.png
Normal file
After Width: | Height: | Size: 41 KiB |
BIN
content/blog/stack_recursion/recursion_all_part4.png
Normal file
After Width: | Height: | Size: 58 KiB |
BIN
content/blog/stack_recursion/recursion_base_part1.png
Normal file
After Width: | Height: | Size: 36 KiB |
BIN
content/blog/stack_recursion/recursion_base_part2.png
Normal file
After Width: | Height: | Size: 38 KiB |
BIN
content/blog/stack_recursion/recursion_base_part3.png
Normal file
After Width: | Height: | Size: 26 KiB |
BIN
content/blog/stack_recursion/recursion_rec_part1.png
Normal file
After Width: | Height: | Size: 42 KiB |
BIN
content/blog/stack_recursion/recursion_rec_part2.png
Normal file
After Width: | Height: | Size: 45 KiB |
BIN
content/blog/stack_recursion/recursion_rec_part3.png
Normal file
After Width: | Height: | Size: 45 KiB |
BIN
content/blog/stack_recursion/recursion_rec_part4.png
Normal file
After Width: | Height: | Size: 52 KiB |
BIN
content/blog/stack_recursion/recursion_rec_part5.png
Normal file
After Width: | Height: | Size: 41 KiB |
BIN
content/blog/stack_recursion/recursion_rec_part6.png
Normal file
After Width: | Height: | Size: 35 KiB |
BIN
content/blog/stack_recursion/recursion_rec_part7.png
Normal file
After Width: | Height: | Size: 28 KiB |
BIN
content/blog/stack_recursion/slide_example_part3.png
Normal file
After Width: | Height: | Size: 30 KiB |
1
layouts/shortcodes/stack_image.html
Normal file
|
@ -0,0 +1 @@
|
|||
<img src="{{ .Get 0 }}" style="max-height: 20rem; margin: auto; display: block;">
|