1 / 8

Understanding Mutual Recursion and Higher Order Functions in Programming

This section explores the concept of mutual recursion, where two functions call each other. It explains how mutual recursion can be implemented and offers alternatives through higher-order functions. Additionally, it touches on the importance of modules and invariants in program organization, ensuring operations are performed in a specific order and sensitive information is managed properly. The significance of signatures for clarity and maintenance in functional programming practice is also highlighted, with examples provided for better comprehension.

angelo
Download Presentation

Understanding Mutual Recursion and Higher Order Functions in 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. Section 4 CSE 341 – Patrick Larson

  2. Mutual Recursion What if we need function f to call g, and function g to call f?

  3. Mutual Recursion Does this work? 1fun f x = 5 fun g y = 2 ... 6 ... 3 g y 7 f x 4... 8 ...

  4. Mutual Recursion We can employ higher order functions for a work around. fun earlier (f,x) = … f y … … fun later x = … earlier(later,y)

  5. Mutual Recursion But ML gives us special syntax for this 1fun f x = 5 and g y = 2 ... 6 ... 3 g y 7 f x 4 ... 8 ...

  6. Modules Remember that signatures are good both for organization and management and for maintaining invariants

  7. Modules - Invariants • Ordering of operations • e.g. insert, then query • Data kept in good state • e.g. fractions in lowest terms • Policies followed • e.g. don't allow shipping request without purchase order • Sensitive information hidden • e.g. force authentication for api use

  8. Currying and Higher Order Functions Questions?

More Related