Download Higher Order Functions in CS421 Course - Prof. Elsa Gunter and more Study notes Computer Science in PDF only on Docsity!
First Class Values Higher Order Functions Miscellaneous HOFs Sample Problems
CS421 Topic 4: Higher Order Functions
Sameer Sundresh
University of Illinois at Urbana-Champaign
May 31, 2006
Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul
Agha, Elsa Gunter, and Mark Hills
First Class Values Higher Order Functions Miscellaneous HOFs Sample Problems
First Class Values
Higher Order Functions
Miscellaneous HOFs
Sample Problems
First Class Values Higher Order Functions Miscellaneous HOFs Sample Problems
First Class Types
A type is first class if its values can be
I Passed as an argument
I Assigned as a value
I Returned as a result
Examples:
I C: scalars, pointers, structures
I C++: same as C, plus classes
I Scheme, LISP: scalars, lists (s-expressions), functions
I ML: same as Scheme, plus user defined data types
First Class Values Higher Order Functions Miscellaneous HOFs Sample Problems
First Class Types – Key Point
I The kind of data that can be manipulated well in a language
largely determines for which applications the language is well
suited
I The ability to treat functions as data is one of the strengths
of applicative programming languages
First Class Values Higher Order Functions Miscellaneous HOFs Sample Problems
Introduction to HOFs Currying Partial Application and Lambda Lifting Currying Again
Higher Order Functions
What are the types of the following functions?
1 # plus_two;;
2 - : int -> int =
First Class Values Higher Order Functions Miscellaneous HOFs Sample Problems
Introduction to HOFs Currying Partial Application and Lambda Lifting Currying Again
Higher Order Functions
What are the types of the following functions?
1 # plus_two;;
2 - : int -> int =
1 # compose plus_two plus_two;;
First Class Values Higher Order Functions Miscellaneous HOFs Sample Problems
Introduction to HOFs Currying Partial Application and Lambda Lifting Currying Again
Higher Order Functions
What are the types of the following functions?
1 # plus_two;;
2 - : int -> int =
1 # compose plus_two plus_two;;
1 - : int -> int =
1 # compose plus_two;;
First Class Values Higher Order Functions Miscellaneous HOFs Sample Problems
Introduction to HOFs Currying Partial Application and Lambda Lifting Currying Again
Higher Order Functions
What are the types of the following functions?
1 # plus_two;;
2 - : int -> int =
1 # compose plus_two plus_two;;
1 - : int -> int =
1 # compose plus_two;;
1 - : (’_a -> int) -> ’_a -> int =
First Class Values Higher Order Functions Miscellaneous HOFs Sample Problems
Introduction to HOFs Currying Partial Application and Lambda Lifting Currying Again
Higher Order Functions
What do the following functions do?
1 # plus_two;;
2 - : int -> int =
1 # compose plus_two plus_two;;
2 - : int -> int =
First Class Values Higher Order Functions Miscellaneous HOFs Sample Problems
Introduction to HOFs Currying Partial Application and Lambda Lifting Currying Again
Higher Order Functions
What do the following functions do?
1 # plus_two;;
2 - : int -> int =
1 # compose plus_two plus_two;;
2 - : int -> int =
1 # compose plus_two;;
2 - : (’_a -> int) -> ’_a -> int =
First Class Values Higher Order Functions Miscellaneous HOFs Sample Problems
Introduction to HOFs Currying Partial Application and Lambda Lifting Currying Again
Thrice
Recall our definition:
1 # let thrice f x = f (f (f x));;
2 val thrice : (’a -> ’a) -> ’a -> ’a =
How would you write thrice with compose?
First Class Values Higher Order Functions Miscellaneous HOFs Sample Problems
Introduction to HOFs Currying Partial Application and Lambda Lifting Currying Again
Thrice
Recall our definition:
1 # let thrice f x = f (f (f x));;
2 val thrice : (’a -> ’a) -> ’a -> ’a =
How would you write thrice with compose?
1 # let thrice f = compose f (compose f f);;
2 val thrice : (’a -> ’a) -> ’a -> ’a =
Is this the only way?
First Class Values Higher Order Functions Miscellaneous HOFs Sample Problems
Introduction to HOFs Currying Partial Application and Lambda Lifting Currying Again
Curried vs Uncurried
1 # let add_three x y z = x + y + z;;
2 val add_three : int -> int -> int -> int =
How does this differ from:
1 # let add_triple (u,v,w) = u + v + w;;
2 val add_triple : int * int * int -> int =
add three is curried, while add triple is uncurried
First Class Values Higher Order Functions Miscellaneous HOFs Sample Problems
Introduction to HOFs Currying Partial Application and Lambda Lifting Currying Again
Partial Application
2 - : int -> int -> int =
4 - : int = 5
5 # let plus_two = (+) 2;;
6 val plus_two : int -> int =
7 # plus_two 7;;
8 - : int = 9
Partial application with operators is also called sectioning.