Docsity

Lecture 4: Higher Order Functions in CS421 - Thrice, Currying, and Folds - Prof. Elsa Gunt, Study notes of Computer Science

Partial preview of the text

Download Lecture 4: Higher Order Functions in CS421 - Thrice, Currying, and Folds - Prof. Elsa Gunt and more Study notes Computer Science in PDF only on Docsity!

First Class Values Higher Order Functions Reverse, Folds, and Maps Miscellaneous HOFs Sample Problems

CS421 Lecture 4: Higher Order Functions

Mark Hills

[email protected]

University of Illinois at Urbana-Champaign

June 5, 2008

Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul

Agha, and Elsa Gunter

First Class Values Higher Order Functions Reverse, Folds, and Maps Miscellaneous HOFs Sample Problems

(^1) First Class Values

2 Higher Order Functions

(^3) Reverse, Folds, and Maps

(^4) Miscellaneous HOFs

(^5) Sample Problems

First Class Values Higher Order Functions Reverse, Folds, and Maps Miscellaneous HOFs Sample Problems

First Class Types

A type is first class if it can be

Passed as an argument

Assigned as a value

Returned as a result

Examples:

C: scalars, pointers, structures

C++: same as C, plus objects

Scheme, LISP: scalars, lists (s-expressions), functions

ML: same as Scheme, plus user defined data types

First Class Values Higher Order Functions Reverse, Folds, and Maps Miscellaneous HOFs Sample Problems

First Class Types – Key Point

The kind of data that can be manipulated well in a language

largely determines for which applications the language is well

suited

The ability to treat functions as data is one of the strengths

of applicative programming languages

First Class Values Higher Order Functions Reverse, Folds, and Maps 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 Reverse, Folds, and Maps 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 Reverse, Folds, and Maps 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 Reverse, Folds, and Maps 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 Reverse, Folds, and Maps 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 Reverse, Folds, and Maps 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 Reverse, Folds, and Maps 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 Reverse, Folds, and Maps 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 =

First Class Values Higher Order Functions Reverse, Folds, and Maps 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 Reverse, Folds, and Maps 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.