Higher-Order Functions and Closures

Slides:



Advertisements
Similar presentations
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Advertisements

CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
Mark Hennessy Dept. Computer Science NUI Maynooth 1 CaML continued CS 351 Programming Paradigms Dept. Computer Science NUI Maynooth.
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
CMSC 330: Organization of Programming Languages Functional Programming with OCaml.
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.
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 11 Type Inference
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Zach Tatlock Winter 2018.
Important Concepts from Clojure
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2013.
Important Concepts from Clojure
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
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
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman 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
CSE 341 Section 7 Winter 2018 Adapted from slides by Eric Mullen, Nicholas Shahan, Dan Grossman, and Tam Dang.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
13.3 day 2 Higher- Order Partial Derivatives
13.3 day 2 Higher- Order Partial Derivatives
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE S. Tanimoto Explicit Function Application
Explicit Application of Procedures, and Explicit Evaluation
CSE 341 Section 3 Nick Mooney Spring 2017.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
341 midterm review Xinrong Zhao Autumn 2018.
CSE 3302 Programming Languages
Recursion, Higher-order-functions
CSE341: Programming Languages Lecture 7 First-Class Functions
Important Concepts from Clojure
Predicates and Quantifiers
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 11 Type Inference
Nicholas Shahan Spring 2016
CSE 341 Lecture 11 b closures; scoping rules
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 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 Section 1
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 7 First-Class Functions
HW1 Debrief, Tail Recursion, More Pattern-Matching Winter 2019
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 11 Type Inference
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2019.
Presentation transcript:

Higher-Order Functions and Closures CSE 341 Section 4 Higher-Order Functions and Closures Winter 2019

Learning Objectives The “Value Restriction” Higher-Order Functions (QC, ~35 min) Understand higher order functions and their expressiveness Become familiar with anonymous functions Currying and partial application (~5 min)

Type Inference How does type inference work? A good answer is outside the scope of this class. For weird enough cases, this is a topic of active research. How does type inference work?

Type Inference How does type inference work in SML? Still mostly outside the scope of this class We’ll talk about it on Monday Today, we’ll go over an SML-specific quirk How does type inference work in SML?

The Value Restriction Let’s hop into Emacs

Key Concepts Review Higher-order functions Pass functions around like any data Closures: functions capture references to their environment Lexical scoping: variables are captured at time of creation Higher-order function idioms: foldl, map, etc. Polymorphic functions Functions that are generic over the type of arguments

Higher-order functions Functions are no different from any program data. An extremely powerful feature! The “defining feature” of functional programming.* * debatable

Higher-order functions QuickCheck time! (~5 minutes, ungraded) Speak with a friend if you like

Higher-order functions What is the type of fold? In what order does fold process its elements? Is there the one true type for a fold function? Why/Why not?

Higher-Order Functions Let’s look at an association list representation of a map and some operations (Emacs)

Association Lists k1 v1 k2 v2 k3 v3 ...

Closure-Based Representation The function (map!) returned by add captures: the inserted key (k) the inserted value (v) the original map (m)

Closure-Based Representation k1 fn => ... fn => ... k3 k2 fn => ... m v1 v3 m’’ m’ v2 ... Does this look familiar?

Closure-Based Representation k1 fn => ... fn => ... k3 k2 fn => ... m v1 v3 m’’ m’ v2 ... k1 v1 k2 v2 k3 v3 ...

Benefits of this representation Remove is O(1) Map is O(1) (kinda!) Only ends up transforming values accessed later (emacs) Although the result can be more expensive computationally (why?)