With thanks to Alexander Lent, Nick Mooney, Spencer Pearson

Slides:



Advertisements
Similar presentations
Higher-order functions in ML
Advertisements

A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 7 Functions Taking/Returning Functions Dan Grossman Fall 2011.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
CSE 341 Lecture 6 exceptions; higher order functions; map, filter, reduce Ullman 5.2, 5.4 slides created by Marty Stepp
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 박성우.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Interpreters and Higher-Order Functions CSE 413 Autumn 2008 Credit: CSE341 notes by Dan Grossman.
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
CSE 341 : Programming Languages Lecture 9 Lexical Scope, Closures Zach Tatlock Spring 2014.
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 7 First-Class Functions
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
With thanks to Nick Mooney & Spencer Pearson
CSE 341 Section 4 Nick Mooney Spring 2017.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 7 First-Class Functions
Nicholas Shahan Spring 2016
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2016.
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 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
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 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Higher Order Functions
CSE 341 Section 3 Nick Mooney Spring 2017.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
CSE-321 Programming Languages Introduction to Functional Programming
CSE 3302 Programming Languages
CSE341: Programming Languages Lecture 7 First-Class Functions
Announcements Quiz 5 HW6 due October 23
With thanks to Nick Mooney, Spencer Pearson & Alexander Lent
CSE-321 Programming Languages Introduction to Functional Programming
CSE341: Programming Languages Lecture 7 First-Class Functions
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
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 Alexander Lent, Nick Mooney, Spencer Pearson CSE 341 Section 4 Winter 2018 With thanks to Alexander Lent, Nick Mooney, Spencer Pearson

CSE 341: Programming Languages Today’s Agenda Standard-Library Docs More Currying and Higher Order Functions Mutual Recursion Winter 2018 CSE 341: Programming Languages

Standard Basis Documentation Online Documentation http://www.standardml.org/Basis/index.html http://www.smlnj.org/doc/smlnj-lib/Manual/toc.html Helpful Subset Top-Level http://www.standardml.org/Basis/top-level-chapter.html List http://www.standardml.org/Basis/list.html ListPair http://www.standardml.org/Basis/list-pair.html Real http://www.standardml.org/Basis/real.html String http://www.standardml.org/Basis/string.html

Higher-Order Functions Review A function that returns a function or takes a function as an argument. Canonical Examples map : ('a -> 'b) * 'a list -> 'b list Applies a function to every element of a list and return a list of the resulting values. Example: map (fn x => x*3, [1,2,3]) === [3,6,9] filter : ('a -> bool) * 'a list -> 'a list Returns the list of elements from the original list that, when a predicate function is applied, result in true. Example: filter (fn x => x>2, [~5,3,2,5]) === [3,5] Note: List.map and List.filter are similarly defined in SML but use currying.

Higher-Order Functions Review foldl: (f: 'a*'b->'b) (acc: 'b) (l: 'a list) -> 'b f(ln, f( …, (f(l2, f(l1, acc)))) Apply function to the current element and the accumulator as soon as possible foldr: (f: 'a*'b->'b) (acc: 'b) (l: 'a list) -> 'b f(l1, f(l2, f( …, f(ln, acc)))) Wait until the rest of the list has been evaluated and then apply function to the current element and result from rest of the list We’ve written foldl in lecture, write foldr Winter 2018 CSE 341: Programming Languages

Broader Idea Functions are Awesome! SML functions can be passed around like any other value. They can be passed as function arguments, returned, and even stored in data structures or variables. Functions like map are very pervasive in functional languages. A function like map can even be written for other data structures such as trees. piecewise(~3.0) ~3.0;

Currying and High Order Functions Some functions from standard library: List.map List.filter List.foldl List.foldr Write our own higher order functions Alternating 0 and 1 Winter 2018 CSE 341: Programming Languages

CSE 341: Programming Languages 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  Winter 2018 CSE 341: Programming Languages

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”.) Winter 2018 CSE 341: Programming Languages

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 Winter 2018 CSE 341: Programming Languages