Show Recursion Show episode artwork

EPISODE · Mar 15, 2021 · 2H 8M

Show Recursion Show

from Coding Blocks · host Allen Underwood, Michael Outlaw, Joe Zack

We dig into recursion and learn that Michael is the weirdo, Joe gives a subtle jab, and Allen doesn't play well with others while we dig into recursion. This episode's show notes can be found at https://www.codingblocks.net/episode154, for those that might be reading this via their podcast player. Sponsors Datadog –  Sign up today for a free 14 day trial and get a free Datadog t-shirt after creating your first dashboard. News Thank you all for the reviews: iTunes: ripco55, Jla115 Audible: _onetea, Marnu, Ian Here I Go Again On My Own What is Recursion? Recursion is a method of solving a problem by breaking the problem down into smaller instances of the same problem. A simple "close enough" definition: Functions that call themselves Simple example: fib(n) { n Recursion pros: Elegant solutions that read well for certain types of problems, particularly with unbounded data. Work great with dynamic data structures, like trees, graphs, linked lists. Recursion cons: Tricky to write. Generally perform worse than iterative solutions. Runs the risk of stack overflow errors. Recursion is often used for sorting algorithms. How Functions Roughly Work in Programming Languages Programming languages generally have the notion of a "call stack". A stack is a data structure designed for LIFO. The call stack is a specialized stack that is common in most languages Any time you call a function, a "frame" is added to the stack. The frame is a bucket of memory with (roughly) space allocated for the input arguments, local variables, and a return address. Note: "value types" will have their values duplicated in the stack and reference types contain a pointer. When a method "returns", it's frame is popped off of the stack, deallocating the memory, and the instructions from the previous function resume where it left off. When the last frame is popped off of the call stack, the program is complete. The stack size is limited. In C#, the size is 1MB for 32-bit processes and 4MB for 64-bit processes. You can change these values but it's not recommended! When the stack tries to exceed it's size limitations, BOOM! … stack overflow exception! How big is a frame? Roughly, add up your arguments (values + references), your local variables, and add an address. Ignoring some implementation details and compiler optimizations, a function that adds two 32b numbers together is going to be roughly 96b on the stack: 32 * 2 + return address. You may be tempted to "optimize" your code by condensing arguments and inlining code rather than breaking out functions… don't do this! These are the very definition of micro optimizations. Your compiler/interpreter does a lot of the work already and this is probably not your bottleneck by a longshot. Use a profiler! Not all languages are stack based though: Stackless Python (kinda), Haskell (graph reduction), Assembly (jmp), Stackless C (essentially inlines your functions, has limitations) The Four Memory Segments source: Quora How Recursive Functions Work The stack doesn't care about what the return address is. When a function calls any other function, a frame is added to the stack. To keep things simple, suppose for a Fibonacci sequence function, the frame requires 64b, 32b for the argument and 32b for the return address. Every Fibonacci call, aside from 0 or 1, adds 2 frames to the stack. So for the 100th number we will allocate .6kb (1002 * 32). And remember, we only have 1mb for everything. You can actually solve Fibonacci iteratively, skipping the backtracking. Fibonacci is often given as an example of recursion for two reasons: It's relatively easy to explain the algorithm, and It shows the danger of this approach. What is Tail Recursion? The recursive Fibonacci algorithm discussed so far relies on backtracking, i.e. getting to the end of our data before starting to wind back. If we can re-write the program such that the last operation, the one in "tail position" is the ONLY recursive call, then we no longer need the frames, because they are essentially just pass a through. A smart compiler can see that there are no operations left to perform after the next frame returns and collapse it. The compiler will first remove the last frame before adding the new one. This means we no longer have to allocate 1002 extra frames on the stack and instead just 1 frame. A common approach to rewriting these types of problems involves adding an "accumulator" that essentially holds the state of the operation and then passing that on to the next function. The important thing here, is that the your ONE AND ONLY recursive call must be the LAST operation … all by itself. Joe's (Un)Official Recursion Tips Start with the end. Do it by hand. Practice, practice, practice. Joe Recursion Joe's Motivational Script   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #!/usr/bin/env python3 import sys   def getname(arg):     print(f"{arg}")       if arg == 'Joe':         getname('Recursion')     else:         getname('Joe')     return   if __name__ == "__main__":     sys.setrecursionlimit(500)     print(f"Who is awesome?")       try:         getname('Joe')     except:         print("You got this!")   Recap Recursion is a powerful tool in programming. It can roughly be defined as a function that calls itself. It's great for dynamic/unbounded data structures like graphs, trees, or linked lists. Recursive functions can be memory intensive and since the call stack is limited, it is easy to overflow. Tail call optimization is a technique and compiler trick that mitigates the call stack problem, but it requires language support and that your recursive call be the last operation in your function. FAANG-ish interviews love recursive problems, and they love to push you on the memory. Resources We Like Recursion (computer science) (Wikipedia) Dynamic Programming (LeetCode) Grokking Dynamic Programming Patterns for Coding Interviews (educative.io) Boxing and Unboxing in .NET (episode 2) IDA EBP variable offset (Stack Exchange) What is the difference between the stack and the heap? (Quora) Data Structures – Arrays and Array-ish (episode 95) Function Calls, Part 3 (Frame Pointer and Local Variables) (codeguru.com) How to implement the Fibonacci sequence in Python (educative.io) Tail Recursion for Fibonacci (GeeksforGeeks.org) Recursion (GeeksforGeeks.org) Structure and Interpretation of Computer Programs (MIT) Tail Recursion Explained – Computerphile (YouTube) !!Con 2019- Tail Call Optimization: The Musical!! by Anjana Vakil & Natalia Margolis (YouTube) Tip of the Week How to take good care of your feet (JeanCoutu.com) Be sure to add labels to your Kubernetes objects so you can later use them as your selector. (kubernetes.io) Example: kubectl get pods --selector=app=nginx Security Now!, Episode 808 (twit.tv, grc.com) AdGuard Home (adguard.com) Match CNAME records against the blocklists (GitHub)

Episode metadata supplied by the publisher feed · Published Mar 15, 2021

NOW PLAYING

Show Recursion Show

0:00 2:08:12

No transcript for this episode yet

We transcribe on demand. Request one and we'll notify you when it's ready — usually under 10 minutes.

API Intersection Stoplight Building a successful API requires more than just coding. It starts with collaborative design, focuses on creating a great developer experience, and ends with getting your company on board, maintaining consistency, and maximizing your API’s profitability.In the API Intersection, you’ll learn from experienced API practitioners who transformed their organizations, and get tangible advice to build quality APIs with collaborative API-first design.Jason Harmon brings over a decade of industry-recognized REST API experience to discuss topics around API design, governance, identity/auth versioning, and more.They’ll answer listener questions, and discuss best practices on API design (definition, modeling, grammar), Governance (multi-team design, reviewing new API’s), Platform Transformation (culture, internal education, versioning) and more.They’ll also chat with experienced API practitioners from a wide array of industries to draw out practical takeaways and insights you can use.H Unshamed & Unchained: Carving Space For Self-Healing & Habit Transformation Danny Poelman Welcome to "Unshamed & Unchained: Carving Space For Self-Healing & Habit Transformation", the podcast where we break the chains of shame and societal expectations to create a safe space for self-healing, habit transformation, and personal growth. Hosted by a seasoned life coach, Danny Poelman DDS, with years of hands-on experience, this podcast is your guide to reclaiming your voice, embracing your story, and living life on your terms.In each episode, we dive deep into the topics that matter most to you—whether it's:-breaking free from unwanted habits like pornography-excessive people-pleasing-healing from past trauma-recovering from narcissistic abuse or religious/relational trauma-anxiety/depression-money mindset blocks-overcoming limiting beliefsWe’re not afraid to talk about the things that are often considered taboo, because we believe that through honest, unfiltered conversations, real transformation happens.You’ll hear real Khanyisa Keke TV Khanyisa Keke On Khanyisa Keke TV, developers can learn and improve their Android for Kotlin Development skills. On this podcast, programmers can learn Android for Kotlin coding from scratch, improve their existing programming skills, get tips, be kept up to date with all the latest happenings and get access to free resources. Powered by Firstory Hosting The Triathlon Mental Performance Podcast Neil Edge This podcast is for you if you are a Triathlete that is interested in learning about tools and strategies to overcome challenges and to utilize the power of your mind to race faster.I'm an experienced Triathlon Mental Performance Coach working with both Age Groupers and Pros.Episodes will cover the following and more.How to improve your mental toughnessRemoving the possibility of panic attacks in open water Removing the fear of fast descents on your bike -Removing mental blocks to improve your race times Completely remove performance anxiety (you don't have to just cope with it)4 weeks to race day - Strategies to  arrive at your a-race feeling calm and confident, with race day mental strategiesI will also talk about specific tools that you can use to ensure that you race faster.<b

Frequently Asked Questions

How long is this episode of Coding Blocks?

This episode is 2 hours and 8 minutes long.

When was this Coding Blocks episode published?

This episode was published on March 15, 2021.

What is this episode about?

We dig into recursion and learn that Michael is the weirdo, Joe gives a subtle jab, and Allen doesn't play well with others while we dig into recursion. This episode's show notes can be found at https://www.codingblocks.net/episode154, for those...

Can I download this Coding Blocks episode?

Yes, you can download this episode by clicking the download button on the episode player, or subscribe to the podcast in your preferred podcast app for automatic downloads.
URL copied to clipboard!