cse 130 : spring 2011 programming languages ranjit jhala uc san diego lecture 6: higher-order...
DESCRIPTION
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-pasteTRANSCRIPT
![Page 1: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/1.jpg)
CSE 130 : Spring 2011
Programming Languages
Ranjit JhalaUC San Diego
Lecture 6: Higher-Order Functions
![Page 2: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/2.jpg)
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 ?
![Page 3: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/3.jpg)
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
![Page 4: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/4.jpg)
Example : Factorial
let rec fac n = if n=0 then 1 else n * fac (n-1);;
Base ExpressionInductive
Expression
Induction Condition
![Page 5: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/5.jpg)
Example : Clone
let rec clone x n = if n=0 then [] else x::(clone x (n-1));;
Base Expression
Inductive Expression
Induction Condition
![Page 6: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/6.jpg)
Example : interval
let rec interval i j = if i > j then [] else i::(interval (i+1) j);
Base Expression
Inductive Expression
Induction Condition
![Page 7: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/7.jpg)
Example : List Append
let rec append l1 l2 = match l1 with [] -> l2 | h::t -> h::(append t l2));;
Roll our own @
Base Expression
Inductive Expression
Base “pattern”
Ind. “pattern”
![Page 8: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/8.jpg)
Example : List Maximum
let max x y = if x > y then x else ylet 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
![Page 9: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/9.jpg)
“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)
![Page 10: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/10.jpg)
“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)
![Page 11: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/11.jpg)
“last thing” function does is a recursive call
Tail Recursive Factorial
let rec fac n =
![Page 12: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/12.jpg)
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
![Page 13: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/13.jpg)
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 ?
![Page 14: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/14.jpg)
Functions are “first-class” values• Arguments, return values, bindings …• What are the benefits ?
Creating,(Returning)Functions
![Page 15: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/15.jpg)
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
![Page 16: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/16.jpg)
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 ?
![Page 17: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/17.jpg)
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 l)@(h::(sort lt r));;
Tail Rec ?
![Page 18: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/18.jpg)
Function Currying
Tuple version:
Curried version:
let f (x1,…,xn) = e
let f x1 … xn = ej
T1 * … * Tn ! T
T1!… !Tn !T
![Page 19: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/19.jpg)
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;
![Page 20: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/20.jpg)
let rec sort lt l = match l with [] -> [] | (h::t) = let (l,r) = partition (lt h) t in
(sort lt l)@(h::(sort 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!
![Page 21: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/21.jpg)
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 canbe passed to, hence used/called by other functions…
![Page 22: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/22.jpg)
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
![Page 23: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/23.jpg)
Factoring and Reuselet 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
![Page 24: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/24.jpg)
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
![Page 25: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/25.jpg)
Encoding Patterns as functionslet 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))
![Page 26: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/26.jpg)
Iteration Patternlet 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)
![Page 27: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/27.jpg)
Iteration Patternlet 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)
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
![Page 28: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/28.jpg)
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)
![Page 29: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/29.jpg)
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)
![Page 30: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/30.jpg)
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;;
![Page 31: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/31.jpg)
Whats the pattern ?
![Page 32: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/32.jpg)
Whats the pattern ?Tail Rec ?
![Page 33: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/33.jpg)
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 ?
![Page 34: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/34.jpg)
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!
![Page 35: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/35.jpg)
Examples of fold
let f l = fold (::) [] l What does this do ?
![Page 36: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/36.jpg)
Funcs taking/returning funcsIdentify 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
![Page 37: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/37.jpg)
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 ?
![Page 38: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/38.jpg)
Funcs taking/returning funcsIdentify 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 collectionPull out (factor) “common” code:• Computation Patterns• Re-use in many different situations
map
fold
![Page 39: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/39.jpg)
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
![Page 40: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/40.jpg)
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.
![Page 41: CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions](https://reader035.vdocuments.site/reader035/viewer/2022070605/5a4d1ad87f8b9ab059973e9f/html5/thumbnails/41.jpg)
Higher-order funcs enable modular code• Each part only needs local information
Funcs taking/returning funcs
Data StructureLibrarylist
Data Structure
ClientUses list
Provides meta-functions: map,fold,filterto traverse, accumulate overlists, 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