CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions.

Slides:



Advertisements
Similar presentations
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Advertisements

CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Map and Fold Building Powerful Abstractions. Hello. I’m Zach, one of Sorin’s students.
CSE341: Programming Languages Lecture 6 Tail Recursion, Accumulators, Exceptions Dan Grossman Fall 2011.
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.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Fall 2011.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture: Datatypes and Recursion.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Functional Design and Programming Lecture 4: Sorting.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions, Polymorphism.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
CSE 341 : Programming Languages Lecture 6 Fancy Patterns, Exceptions, Tail Recursion Zach Tatlock Spring 2014.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Programovací jazyky F# a OCaml Chapter 5. Hiding recursion using function-as-values.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
CMSC 330: Organization of Programming Languages Functional Programming with OCaml.
CMSC 330: Organization of Programming Languages Maps and Folds Anonymous Functions.
Function Definition by Cases and Recursion Lecture 2, Programmeringsteknik del A.
Kyung-Goo Doh Hanyang University - ERICAComputer Science & Engineering Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
CS5205Haskell1 CS5205: Foundation in Programming Languages Basics of Functional Programming.
CSE 341 : Programming Languages Lecture 8 First Class Functions Zach Tatlock Spring 2014.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
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.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
10/1/20161 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
CSE341: Programming Languages Lecture 7 First-Class Functions
ML: a quasi-functional language with strong typing
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2017.
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Closures and Streams cs784(Prasad) L11Clos
CMSC 330: Organization of Programming Languages
Programming Languages and Compilers (CS 421)
PROGRAMMING IN HASKELL
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
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2013.
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 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Zach Tatlock Winter 2018.
CSE 3302 Programming Languages
Madhusudan Parthasarathy
CSE341: Programming Languages Lecture 7 First-Class Functions
CSCE 314: Programming Languages Dr. Dylan Shell
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Autumn 2017.
Review Previously in: Lots of language features: functions, lists, records, tuples, variants, pattern matching Today: No new language features New idioms.
Presentation transcript:

CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions

Today’s Plan A little more practice with recursion –Base Pattern-> Base Expression –Induction Pattern-> Induction Expression Higher-Order Functions –or, why “take” and “return” functions ?

Recursion A way of life A different way to view computation –Solutions for bigger problems –From solutions for sub-problems Why know about it ? 1. Often far simpler, cleaner than loops –But not always… 2. Forces you to factor code into reusable units –Only way to “reuse” loop is via cut-paste

Example : Factorial let rec fac n = if n=0 then 1 else n * fac (n-1);; Base Expression Inductive Expression Induction Condition

Example : Clone let rec clone x n = if n=0 then [] else x::(clone x (n-1));; Base Expression Inductive Expression Induction Condition

Example : interval let rec interval i j = if i > j then [] else i::(interval (i+1) j); Base Expression Inductive Expression Induction Condition

Example : List Append let rec append l1 l2 = match l1 with [] -> l2 | h::t -> h::(append t l2)) ;; Roll our Base Expression Inductive Expression Base “pattern” Ind. “pattern”

Example : List Maximum let max x y = if x > y then x else y let listMax l = let rec helper cur l = match l with [] -> cur | h::t -> helper (max cur h) t in helper 0 l ;; Find maximum element in +ve int list … in a more ML-ish way Base Expression Inductive Expression Base pattern Ind. pattern

“last thing” function does is a recursive call Tail Recursion NOT TR let rec fac n = if n=0 then 1 else n * fac (n-1);; bad because height of stack = O(n)

“last thing” function does is a recursive call Tail Recursion NOT TR let rec fac n = if n=0 then 1 else n * fac (n-1);; bad because height of stack = O(n)

“last thing” function does is a recursive call Tail Recursive Factorial let rec fac n =

News PA3 is up –Due 4/22 –OH in CSE 250 (RJ: 2-4pm/Thu) Midterm 4/28 –In class –Open book etc. –Practice materials on Webpage

Today’s Plan A little more practice with recursion –Base Pattern-> Base Expression –Induction Pattern-> Induction Expression Higher-Order Functions –or, why “take” and “return” functions ?

Functions are “first-class” values Arguments, return values, bindings … What are the benefits ? Creating, (Returning) Functions

Returning functions In general, these two are equivalent: let lt = fun x -> fun y -> x < y; Returned value is a function let lt x y = x < y;; Identical but easier to write! let f = fun x1 -> … -> fun xn -> e let f x1 … xn = e

Returning functions let lt x y = x < y; int ! (int ! bool) let lt = fun x -> fun y -> x < y lt 5 20;; lt 20 7;; let is5lt = lt 5;; let is10lt = lt 10;; Parameterized “tester” Create many similar testers Where is this useful ?

Remember this ? Use “tester” to partition list –Tester parameterized by “pivot” h Reuse code to sort any type of list –Use different “lt” to sort in different orders let rec sort lt l = match l with [] -> [] | (h::t) = let (l,r) = partition (lt h) t in (sort lt lt r)) ;; Tail Rec ?

Function Currying Tuple version: Curried version: let f (x1,…,xn) = e let f x1 … xn = ej T 1 * … * T n ! T T 1 ! … ! T n ! T

Could have done: But then no “testers” possible Must pick good order of arguments Function Currying let lt x y = x < y; Multiple argument functions by returning a function that takes the next argument Named after a person (Haskell Curry) let lt (x,y) = x<y;

let rec sort lt l = match l with [] -> [] | (h::t) = let (l,r) = partition (lt h) t in (sort lt lt r)) ;; Using parameterized testers partition Takes a tester (and a list) as argument Returns a pair: (list passing test, list failing test) Can be called with any tester!

Functions are “first-class” values Arguments, return values, bindings … What are the benefits ? Creating, (Returning) Functions Using, (Taking) Functions Parameterized, similar functions (e.g. Testers) Useful if parameterized functions can be passed to, hence used/called by other functions…

Why take functions as input ? let rec evens l = match l with [] -> [] | h::t -> if is_even h then h::(evens t) else evens t let rec lessers x l = match l with [] -> [] | h::t -> if h<x then h::(lessers x t) else lessers x t let rec filter f l = match l with [] -> [] | h::t -> if (f h) then h::(filter f t)else filter f t

Factoring and Reuse let rec lessers x l = match l with [] -> [] | h::t -> if h<x then h::(lessers x t) else lessers x t let rec filter f l = match l with [] -> [] | h::t -> if (f h) then h::(filter f t)else filter f t “Factor” code: Generic pattern Specific instance let lessers x l = filter (fun i -> i<x) l

Factoring and Reuse let rec filter f l = match l with [] -> [] | h::t -> if (f h) then h::(filter f t)else filter f t “Factor” code: Generic pattern Specific instance let rec evens l = match l with [] -> [] | h::t -> if is_even h then h::(evens t) else evens t let evens l = filter is_even l

Encoding Patterns as functions let rec filter f l = match l with [] -> [] | h::t -> if (f h) then h::(filter f t) else (filter f t);; filter,neg,partition: higher-order functions Take a any tester as argument! let neg f = fun x -> not (f x) let partition f l= (filter f l, filter(neg f) l))

Iteration Pattern let rec listUppercase xs = match xs with [] -> [] | h::t -> (uppercase h)::(listUppercase t) let rec listSquare xs = match xs with [] -> [] | h::t -> (h * h)::(listSquare t) let addPair (x,y) = x + y let rec listAddPair xs = match l with [] -> [] | (hx,hy)::t ->(addPair (hx,hy))::(listAddPair t)

Iteration Pattern let rec listUppercase xs = match xs with [] -> [] | h::t -> (uppercase h)::(listUppercase t) let rec map f l = match l with [] -> [] | (h::t) -> (f h)::(map f t) let rec map f l = match l with [] -> [] | (h::t) -> (f h)::(map f t) uppercase let listUpperCase l = map upperCase l let listSquare l = map (fun x -> x*x) l let listAddpair l = map (fun (x,y) -> x+y) l

Higher-order functions: map Type says it all ! Applies “f” to each element in input list Makes a list of the results (’a ! ’b) ! ’a list ! ’b list let rec map f l = match l with [] -> [] | (h::t) -> (f h)::(map f t) let rec map f l = match l with [] -> [] | (h::t) -> (f h)::(map f t)

Factoring Iteration w/ “map” “Factored” code: Reuse iteration template Avoid bugs due to repetition Fix bug in one place ! let rec map f l = match l with [] -> [] | (h::t) -> (f h)::(map f t) let rec map f l = match l with [] -> [] | (h::t) -> (f h)::(map f t)

Another pattern: Accumulation let max x y = if x > y then x else y ; let listMax l = let concat l = let rec help cur l = match l with [] -> cur | h::t -> help (max cur h) t in helper 0 l;; let rec help cur l = match l with [] -> cur | h::t -> help (cur^h) t in helper “” l;;

Whats the pattern ?

Tail Rec ?

Whats the pattern ? Let rec fold f cur l = case l of [] -> cur | h::t -> fold f (f cur h) t What is: fold f base [v1;v2;…;vn] ? f(,v3)f(…(,vn) f(base,v1) f(,v2) f(,v3) f(…(,vn) Tail Rec ?

Examples of fold let concat = let multiplier = Currying! This is a function! let listMax = Currying! This is a function! fold max 0 fold (^) “” Pick correct base case!

Examples of fold let f l = fold (::) [] l What does this do ?

Funcs taking/returning funcs Identify common computation “patterns” Filter values in a set, list, tree … Iterate a function over a set, list, tree … Accumulate some value over a collection Pull out (factor) “common” code: Computation Patterns Re-use in many different situations map fold

Another fun function: “pipe” let pipe x f = f x let (|>) x f = f x Compute the sum of squares of numbers in a list ? let sumOfSquares xs = xs |> map (fun x -> x * x) |> fold_left (+) 0 Tail Rec ?

Funcs taking/returning funcs Identify common computation “patterns” Filter values in a set, list, tree … Convert a function over a set, list, tree … Iterate a function over a set, list, tree … Accumulate some value over a collection Pull out (factor) “common” code: Computation Patterns Re-use in many different situations map fold

Functions are “first-class” values Arguments, return values, bindings … What are the benefits ? Creating, (Returning) Functions Using, (Taking) Functions Parameterized, similar functions (e.g. Testers) Iterator, Accumul, Reuse computation pattern w/o exposing local info

Functions are “first-class” values Arguments, return values, bindings … What are the benefits ? Creating, (Returning) Functions Using, (Taking) Functions Parameterized, similar functions (e.g. Testers) Iterator, Accumul, Reuse computation pattern w/o exposing local info Compose Functions: Flexible way to build Complex functions from primitives.

Higher-order funcs enable modular code Each part only needs local information Funcs taking/returning funcs Data Structure Library list Data Structure Client Uses list Provides meta-functions: map,fold,filter to traverse, accumulate over lists, trees etc. Meta-functions don’t need client info (tester ? accumulator ?) Uses meta-functions: map,fold,filter With locally-dependent funs (lt h), square etc. Without requiring Implement. details of data structure