diff --git a/content/blog/stack_recursion/if_999_1_part1.png b/content/blog/stack_recursion/if_999_1_part1.png new file mode 100644 index 0000000..748ee5e Binary files /dev/null and b/content/blog/stack_recursion/if_999_1_part1.png differ diff --git a/content/blog/stack_recursion/if_999_1_part2.png b/content/blog/stack_recursion/if_999_1_part2.png new file mode 100644 index 0000000..d9c9f8c Binary files /dev/null and b/content/blog/stack_recursion/if_999_1_part2.png differ diff --git a/content/blog/stack_recursion/if_999_1_part3.png b/content/blog/stack_recursion/if_999_1_part3.png new file mode 100644 index 0000000..05d0508 Binary files /dev/null and b/content/blog/stack_recursion/if_999_1_part3.png differ diff --git a/content/blog/stack_recursion.md b/content/blog/stack_recursion/index.md similarity index 90% rename from content/blog/stack_recursion.md rename to content/blog/stack_recursion/index.md index 27f7335..64cc102 100644 --- a/content/blog/stack_recursion.md +++ b/content/blog/stack_recursion/index.md @@ -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: diff --git a/content/blog/stack_recursion/recursion_all_part1.png b/content/blog/stack_recursion/recursion_all_part1.png new file mode 100644 index 0000000..eb42044 Binary files /dev/null and b/content/blog/stack_recursion/recursion_all_part1.png differ diff --git a/content/blog/stack_recursion/recursion_all_part2.png b/content/blog/stack_recursion/recursion_all_part2.png new file mode 100644 index 0000000..7df389e Binary files /dev/null and b/content/blog/stack_recursion/recursion_all_part2.png differ diff --git a/content/blog/stack_recursion/recursion_all_part3.png b/content/blog/stack_recursion/recursion_all_part3.png new file mode 100644 index 0000000..8058286 Binary files /dev/null and b/content/blog/stack_recursion/recursion_all_part3.png differ diff --git a/content/blog/stack_recursion/recursion_all_part4.png b/content/blog/stack_recursion/recursion_all_part4.png new file mode 100644 index 0000000..2a4c973 Binary files /dev/null and b/content/blog/stack_recursion/recursion_all_part4.png differ diff --git a/content/blog/stack_recursion/recursion_base_part1.png b/content/blog/stack_recursion/recursion_base_part1.png new file mode 100644 index 0000000..33070f0 Binary files /dev/null and b/content/blog/stack_recursion/recursion_base_part1.png differ diff --git a/content/blog/stack_recursion/recursion_base_part2.png b/content/blog/stack_recursion/recursion_base_part2.png new file mode 100644 index 0000000..79410b3 Binary files /dev/null and b/content/blog/stack_recursion/recursion_base_part2.png differ diff --git a/content/blog/stack_recursion/recursion_base_part3.png b/content/blog/stack_recursion/recursion_base_part3.png new file mode 100644 index 0000000..fc95fb1 Binary files /dev/null and b/content/blog/stack_recursion/recursion_base_part3.png differ diff --git a/content/blog/stack_recursion/recursion_rec_part1.png b/content/blog/stack_recursion/recursion_rec_part1.png new file mode 100644 index 0000000..0a2bfe7 Binary files /dev/null and b/content/blog/stack_recursion/recursion_rec_part1.png differ diff --git a/content/blog/stack_recursion/recursion_rec_part2.png b/content/blog/stack_recursion/recursion_rec_part2.png new file mode 100644 index 0000000..d65ec2d Binary files /dev/null and b/content/blog/stack_recursion/recursion_rec_part2.png differ diff --git a/content/blog/stack_recursion/recursion_rec_part3.png b/content/blog/stack_recursion/recursion_rec_part3.png new file mode 100644 index 0000000..1f10994 Binary files /dev/null and b/content/blog/stack_recursion/recursion_rec_part3.png differ diff --git a/content/blog/stack_recursion/recursion_rec_part4.png b/content/blog/stack_recursion/recursion_rec_part4.png new file mode 100644 index 0000000..0b33e3f Binary files /dev/null and b/content/blog/stack_recursion/recursion_rec_part4.png differ diff --git a/content/blog/stack_recursion/recursion_rec_part5.png b/content/blog/stack_recursion/recursion_rec_part5.png new file mode 100644 index 0000000..7e3e4ee Binary files /dev/null and b/content/blog/stack_recursion/recursion_rec_part5.png differ diff --git a/content/blog/stack_recursion/recursion_rec_part6.png b/content/blog/stack_recursion/recursion_rec_part6.png new file mode 100644 index 0000000..e1b80f0 Binary files /dev/null and b/content/blog/stack_recursion/recursion_rec_part6.png differ diff --git a/content/blog/stack_recursion/recursion_rec_part7.png b/content/blog/stack_recursion/recursion_rec_part7.png new file mode 100644 index 0000000..251fec4 Binary files /dev/null and b/content/blog/stack_recursion/recursion_rec_part7.png differ diff --git a/content/blog/stack_recursion/slide_example_part3.png b/content/blog/stack_recursion/slide_example_part3.png new file mode 100644 index 0000000..3a85cff Binary files /dev/null and b/content/blog/stack_recursion/slide_example_part3.png differ diff --git a/layouts/shortcodes/stack_image.html b/layouts/shortcodes/stack_image.html new file mode 100644 index 0000000..2daf599 --- /dev/null +++ b/layouts/shortcodes/stack_image.html @@ -0,0 +1 @@ +