1 / 7

Understanding Tail Recursion and Pattern Matching in Functional Programming

Learn about tail recursion in functional programming, including its definition, how to implement it, and its benefits for memory efficiency. Explore the concept of pattern matching over data types in SML, avoiding empty list exceptions, and the power of breaking apart data structures. Discover the principles of space efficiency with loops using recursive functions.

dudu
Download Presentation

Understanding Tail Recursion and Pattern Matching in Functional Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 341 Section 3 HW1 Debrief, Tail Recursion, More Pattern-Matching Winter 2019

  2. Learning Objectives ● More tail recursion (QC, ~15 min) ○ What is and isn’t tail recursive? ○ How can we make functions tail recursive? ○ When can’t we be tail recursive? ● Pattern-matching over expression trees ● Function patterns(?)

  3. Key Concepts Review • Custom datatypes • all of (records), one of (variants) • Pattern matching • Powerful way to break apart data • Tail recursion • Space efficiency of loops with recursive functions

  4. Tail Recursion What is it? Briefly: if a function will immediately return after making a call, we can reuse the stack space of the current function.

  5. Tail Recursion Quickcheck! (6 minutes, ungraded) Speak with a neighbor if you’d like.

  6. Tail Recursion Was length tail recursive? Was all_positive tail recursive? Why tail recursion? Let’s look at more examples of tail recursion

  7. Pattern Matching • We can pattern match over datatypes • Beware “non-exhaustive matching” • Pattern matching can avoid “empty list” exceptions! • Most functions pattern match over a single argument • SML has special syntax for this common case! • Use is a matter of taste • Live demo

More Related