With thanks to Nick Mooney, Spencer Pearson & Alexander Lent

Slides:



Advertisements
Similar presentations
Higher-order functions in ML
Advertisements

CSE341: Programming Languages Lecture 9 Function-Closure Idioms Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 12 Modules Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 7 Functions Taking/Returning Functions Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
Programming LanguagesSection 41 Programming Languages Section 4. SML Remaining Topics Xiaojuan Cai Spring 2015.
CSE 341 : Programming Languages Lecture 12 ML Modules Zach Tatlock Spring 2014.
CSE 341 : Programming Languages Lecture 8 First Class Functions Zach Tatlock Spring 2014.
Recursion Higher Order Functions CSCE 314 Spring 2016.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
CSE 341 Lecture 8 curried functions Ullman 5.5 slides created by Marty Stepp
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
CSE341: Programming Languages Lecture 9 Function-Closure Idioms Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 10 ML Modules
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 10 ML Modules
With thanks to Nick Mooney & Spencer Pearson
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
CSE 341 Section 4 Nick Mooney Spring 2017.
CSE341: Programming Languages Lecture 7 First-Class Functions
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 11 Type Inference
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE 341 Section 2 Winter 2018 Adapted from slides by Nick Mooney, Nicholas Shahan, Patrick Larson, and Dan Grossman.
CSE341: Programming Languages Lecture 10 ML Modules
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE 341: Programming Languages Section 1
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 10 ML Modules
Spencer Pearson Spring 2017
CSE 341 Section 2 Nick Mooney Spring 2017
CS 457/557: Functional Languages Folds
CSE 341 Section 3 Nick Mooney Spring 2017.
341 midterm review Xinrong Zhao Autumn 2018.
CSE-321 Programming Languages Introduction to Functional Programming
CSE 341 Section 4.
CSE341: Programming Languages Lecture 10 ML Modules
CSE341: Programming Languages Lecture 7 First-Class Functions
Announcements Quiz 5 HW6 due October 23
CSE 373 Optional Section Led by Yuanwei, Luyi Apr
CSE-321 Programming Languages Introduction to Functional Programming
Section 4 CSE 341 – Konstantin Weitz.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 11 Type Inference
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2017.
With thanks to Alexander Lent, Nick Mooney, Spencer Pearson
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 10 ML Modules
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 10 ML Modules
CSE341: Programming Languages Lecture 11 Type Inference
Higher-Order Functions and Closures
Review Previously in: Lots of language features: functions, lists, records, tuples, variants, pattern matching Today: No new language features New idioms.
Presentation transcript:

With thanks to Nick Mooney, Spencer Pearson & Alexander Lent CSE 341 Section 4 Ethan Shea Autumn 2017 With thanks to Nick Mooney, Spencer Pearson & Alexander Lent

Today’s Agenda Mutual Recursion Module System Example Namespace Organization Preserving Invariants Practice with Currying and High Order Functions

Mutual Recursion What if we need function f to call g, and function g to call f? This is a common idiom fun earlier x = ... later x fun later x = earlier x Unfortunately this does not work 

Mutual Recursion Workaround We can use higher order functions to get this working It works, but there has got to be a better way! fun earlier f x = ... f x fun later x = earlier later x We know “earlier” will want to recurse on another function, but at the time of definition, we don’t know what function this will be We can “solve” this by allowing it to take a function at runtime But remember when I told you that “and” meant something else and to ignore it…? (We can’t easily do this with both taking a function, because we have to do what’s known as “tying the knot”.)

Mutual Recursion with and SML has a keyword for that Works with mutually recursive datatype bindings too fun earlier x = ... later x and later x = earlier x

Module System Good for organizing code, and managing namespaces (useful, relevant) Good for maintaining invariants (interesting) Good for data hiding (useful)

Interesting Examples of 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 We’ll see more on this in class.

Currying and High Order Functions Some examples: List.map List.filter List.foldl

Currying fun plus x y = x + y fun plus x = fn y => x + y plus : int -> (int -> int) plus : int -> int -> int plus 5

Foldr vs Foldl List.foldl (fn (x, acc) => x+acc) 0 xs List.foldr (fn (x, acc) => x+acc) 0 xs 0 [1, 2, 3, 4, 5] [1, 2, 3, 4, 5] 0