lambda expressions, functions and binding · 2012-10-23 · informatics 1 functional programming...

90
Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella University of Edinburgh

Upload: others

Post on 31-May-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Informatics 1Functional Programming Lecture 8

Tuesday 23 October 2012

Lambda expressions, functions andbinding

Don SannellaUniversity of Edinburgh

Page 2: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Required text and reading

Haskell: The Craft of Functional Programming (Third Edition),Simon Thompson, Addison-Wesley, 2011.

Reading assignment

Monday 24 September 2012 Chapters 1–3 (pp. 1–66)

Monday 1 October 2012 Chapters 4–7 (pp. 67–176)

Monday 8 October 2012 Chapters 8–9 (pp. 177–212)

Monday 15 October 2012 Chapters 10–12 (pp. 213–286)

Monday 22 October 2012 Class test

Monday 29 October 2012 Chapters 13–14 (pp. 287–356)

Monday 5 November 2012 Chapters 15–16 (pp. 357–414)

Monday 12 November 2012 Chapters 17–21 (pp. 415–534)

Page 3: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Part I

Lambda expressions

Page 4: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

A failed attempt to simplifyf :: [Int] -> Intf xs = foldr (+) 0 (map sqr (filter pos xs))

wheresqr x = x * xpos x = x > 0

The above cannot be simplified to the following:

f :: [Int] -> Intf xs = foldr (+) 0 (map (x * x) (filter (x > 0) xs))

Page 5: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

A successful attempt to simplifyf :: [Int] -> Intf xs = foldr (+) 0 (map sqr (filter pos xs))

wheresqr x = x * xpos x = x > 0

The above can be simplified to the following:

f :: [Int] -> Intf xs = foldr (+) 0

(map (\x -> x * x)(filter (\x -> x > 0) xs))

Page 6: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Lambda calculusf :: [Int] -> Intf xs = foldr (+) 0

(map (\x -> x * x)(filter (\x -> x > 0) xs))

The character \ stands for λ, the Greek letter lambda.

Logicians write

\x -> x > 0 as λx. x > 0

\x -> x * x as λx. x× x.

Lambda calculus is due to the logician Alonzo Church (1903–1995).

Page 7: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Evaluating lambda expressions(\x -> x > 0) 3

=let x = 3 in x > 0

=3 > 0

=True

(\x -> x * x) 3=

let x = 3 in x * x=

3 * 3=

9

Page 8: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Lambda expressions and currying(\x -> \y -> x + y) 3 4

=((\x -> (\y -> x + y)) 3) 4

=(let x = 3 in \y -> x + y) 4

=(\y -> 3 + y) 4

=let y = 4 in 3 + y

=3 + 4

=7

Page 9: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Evaluating lambda expressionsThe general rule for evaluating lambda expressions is

(λx.N)M

=

(let x = M inN)

This is sometimes called the β rule (or beta rule).

Page 10: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Part II

Sections

Page 11: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Sections(> 0) is shortand for (\x -> x > 0)

(2 *) is shortand for (\x -> 2 * x)

(+ 1) is shortand for (\x -> x + 1)

(2 ˆ) is shortand for (\x -> 2 ˆ x)

(ˆ 2) is shortand for (\x -> x ˆ 2)

Page 12: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Sectionsf :: [Int] -> Intf xs = foldr (+) 0

(map (\x -> x * x)(filter (\x -> x > 0) xs))

f :: [Int] -> Intf xs = foldr (+) 0 (map (ˆ 2) (filter (> 0) xs))

Page 13: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Part III

Composition

Page 14: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Composition(.) :: (b -> c) -> (a -> b) -> (a -> c)(f . g) x = f (g x)

Page 15: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Evaluation composition(.) :: (b -> c) -> (a -> b) -> (a -> c)(f . g) x = f (g x)

sqr :: Int -> Intsqr x = x * x

pos :: Int -> Boolpos x = x > 0

(pos . sqr) 3=

pos (sqr 3)=

pos 9=

True

Page 16: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Compare and contrast

possqr :: Int -> Boolpossqr x = pos (sqr x)

possqr 3=

pos (sqr 3)=

pos 9=

True

possqr :: Int -> Boolpossqr = pos . sqr

possqr 3=

(pos . sqr) 3=

pos (sqr 3)=

pos 9=

True

Page 17: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Composition is associative(f . g) . h = f . (g . h)

((f . g) . h) x=

(f . g) (h x)=

f (g (h x))=

f ((g . h) x)=

(f . (g . h)) x

Page 18: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Thinking functionallyf :: [Int] -> Intf xs = foldr (+) 0 (map (ˆ 2) (filter (> 0) xs))

f :: [Int] -> Intf = foldr (+) 0 . map (ˆ 2) . filter (> 0)

Page 19: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Applying the functionf :: [Int] -> Intf = foldr (+) 0 . map (ˆ 2) . filter (> 0)

f [1, -2, 3]=

(foldr (+) 0 . map (ˆ 2) . filter (> 0)) [1, -2, 3]=

foldr (+) 0 (map (ˆ 2) (filter (> 0) [1, -2, 3]))=

foldr (+) 0 (map (ˆ 2) [1, 3])=

foldr (+) 0 [1, 9]=

10

Page 20: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Part IV

Variables and binding

Page 21: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Variablesx = 2y = x+1z = x+y*y

*Main> z11

Page 22: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Variables—bindingx = 2y = x+1z = x+y*y

*Main> z11

Binding occurrenceBound occurrenceScope of binding

Page 23: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Variables—bindingx = 2y = x+1z = x+y*y

*Main> z11

Binding occurrenceBound occurrenceScope of binding

Page 24: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Variables—bindingx = 2y = x+1z = x+y*y

*Main> z11

Binding occurrenceBound occurrenceScope of binding

Page 25: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Variables—renamingxavier = 2yolanda = xavier+1zeuss = xavier+yolanda*yolanda

*Main> zeuss11

Page 26: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Part V

Functions and binding

Page 27: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions—bindingf x = g x (x+1)g x y = x+y*y

*Main> f 211

Page 28: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions—bindingf x = g x (x+1)g x y = x+y*y

*Main> f 211

Binding occurrenceBound occurrenceScope of binding

Page 29: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions—bindingf x = g x (x+1)g x y = x+y*y

*Main> f 211

Binding occurrenceBound occurrenceScope of binding

There are two unrelated uses of x!

Page 30: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions—bindingf x = g x (x+1)g x y = x+y*y

*Main> f 211

Binding occurrenceBound occurrenceScope of binding

Page 31: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions—bindingf x = g x (x+1)g x y = x+y*y

*Main> f 211

Binding occurrenceBound occurrenceScope of binding

Page 32: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions—bindingf x = g x (x+1)g x y = x+y*y

*Main> f 211

Binding occurrenceBound occurrenceScope of binding

Page 33: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions—formal and actual parametersf x = g x (x+1)g x y = x+y*y

*Main> f 211

Formal parameterActual parameter

Page 34: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions—formal and actual parametersf x = g x (x+1)g x y = x+y*y

*Main> f 211

Formal parameterActual parameter

Page 35: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions—formal and actual parametersf x = g x (x+1)g x y = x+y*y

*Main> f 211

Formal parameterActual parameter

Page 36: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions—renamingfred xavier = george xavier (xavier+1)george xerox yolanda = xerox+yolanda*yolanda

*Main> fred 211

Different uses of x renamed to xavier and xerox.

Page 37: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Part VI

Variables in a where clause and binding

Page 38: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Variables in a where clausef x = z

wherey = x+1z = x+y*y

*Main> f 211

Page 39: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Variables in a where clause—bindingf x = z

wherey = x+1z = x+y*y

*Main> f 211

Binding occurrenceBound occurrenceScope of binding

Page 40: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Variables in a where clause—bindingf x = z

wherey = x+1z = x+y*y

*Main> f 211

Binding occurrenceBound occurrenceScope of binding

Page 41: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Variables in a where clause—bindingf x = z

wherey = x+1z = x+y*y

*Main> f 211

Binding occurrenceBound occurrenceScope of binding

Page 42: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Variables in a where clause—bindingf x = z

wherey = x+1z = x+y*y

*Main> f 211

Binding occurrenceBound occurrenceScope of binding

Page 43: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Variables in a where clause—hole in scopef x = z

wherey = x+1z = x+y*y

y = 5

*Main> y5

Binding occurrenceBound occurrenceScope of binding

Page 44: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Part VII

Functions in a where clause and binding

Page 45: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions in a where clausef x = g (x+1)

whereg y = x+y*y

*Main> f 211

Page 46: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions in a where clause—bindingf x = g (x+1)

whereg y = x+y*y

*Main> f 211

Binding occurrenceBound occurrenceScope of binding

Variable x is still in scope within g!

Page 47: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions in a where clause—bindingf x = g (x+1)

whereg y = x+y*y

*Main> f 211

Binding occurrenceBound occurrenceScope of binding

Page 48: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions in a where clause—bindingf x = g (x+1)

whereg y = x+y*y

*Main> f 211

Binding occurrenceBound occurrenceScope of binding

Page 49: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions in a where clause—bindingf x = g (x+1)

whereg y = x+y*y

*Main> f 211

Binding occurrenceBound occurrenceScope of binding

Page 50: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions in a where clause—hole in scopef x = g (x+1)

whereg y = x+y*y

g z = z*z*z

*Main> g 28

Binding occurrenceBound occurrenceScope of binding

Page 51: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions in a where clause—pathological casef x = f (x+1)

wheref y = x+y*y

*Main> f 211

Binding occurrenceBound occurrenceScope of binding

Page 52: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions in a where clause—pathological casef x = f (x+1)

wheref y = x+y*y

*Main> f 211

Binding occurrenceBound occurrenceScope of binding

Page 53: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions in a where clause—formals and actualsf x = g (x+1)

whereg y = x+y*y

*Main> f 211

Formal parameterActual parameter

Page 54: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Functions in a where clause—formals and actualsf x = g (x+1)

whereg y = x+y*y

*Main> f 211

Formal parameterActual parameter

Page 55: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Part VIII

Lambda expressions and binding

Page 56: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

A wrong attempt to simplifyf :: [Int] -> [Int]f xs = map (x * x) (filter (x > 0) xs)

This makes no sense—no binding occurrence of variable!

Page 57: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Lambda expressionsf :: [Int] -> [Int]f xs =

map (\x -> x * x) (filter (\x -> x > 0) xs)

The character \ stands for λ, the Greek letter lambda.

Logicians write

(\x -> x * x) as (λx. x× x)(\x -> x > 0) as (λx. x > 0)

Page 58: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Lambda expressions—bindingf :: [Int] -> [Int]f xs = map (\x -> x*x) (filter (\x -> x > 0) xs)

Binding occurrenceBound occurrenceScope of binding

Page 59: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Lambda expressions—bindingf :: [Int] -> [Int]f xs = map (\x -> x*x) (filter (\x -> x > 0) xs)

Binding occurrenceBound occurrenceScope of binding

Page 60: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Part IX

Lambda expressions explain binding

Page 61: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Lambda expressions explain bindingA variable binding can be rewritten using a lambda expression and an application:

(N where x = M)

=

(λx.N)M

=

(let x = M inN)

A function binding can be written using an application on the left or a lambdaexpression on the right:

(M where f x = N)

=

(M where f = λx.N)

Page 62: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Lambda expressions and binding constructsf 2wheref x = x+y*y

wherey = x+1

=f 2wheref = \x -> (x+y*y where y = x+1)

=f 2wheref = \x -> ((\y -> x+y*y) (x+1))

=(\f -> f 2) (\x -> ((\y -> x+y*y) (x+1)))

Page 63: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Evaluating lambda expressions(\f -> f 2) (\x -> ((\y -> x+y*y) (x+1)))

=(\x -> ((\y -> x+y*y) (x+1))) 2

=(\y -> 2+y*y) (2+1)

=(\y -> 2+y*y) 3

=2+3*3

=11

Page 64: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Part X

Additional material:Lambda expressions and binding, revisited

Page 65: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Lambda expressions—binding(\f -> f 2) (\x -> ((\y -> x+y*y) (x+1)))

Binding occurrenceBound occurrenceScope of binding

Page 66: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Lambda expressions—binding(\f -> f 2) (\x -> ((\y -> x+y*y) (x+1)))

Binding occurrenceBound occurrenceScope of binding

Page 67: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Lambda expressions—binding(\f -> f 2) (\x -> ((\y -> x+y*y) (x+1)))

Binding occurrenceBound occurrenceScope of binding

Page 68: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Lambda expressions—formals and actuals(\f -> f 2) (\x -> ((\y -> x+y*y) (x+1)))

Formal parameterActual parameter

Page 69: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Lambda expressions—formals and actuals(\x -> ((\y -> x+y*y) (x+1))) 2

Formal parameterActual parameter

Page 70: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Lambda expressions—formals and actuals(\y -> 2+y*y) (2+1)

Formal parameterActual parameter

Page 71: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Part XI

Additional material:Comprehensions and binding

Page 72: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Comprehensionsf :: [Int] -> [Int]f xs = [ x*x | x <- xs, x > 0 ]

*Main> f [1,-2,3][1,9]

Page 73: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Comprehensions—bindingf :: [Int] -> [Int]f xs = [ x*x | x <- xs, x > 0 ]

*Main> f [1,-2,3][1,9]

Binding occurrenceBound occurrenceScope of binding

Page 74: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Comprehensions—bindingf :: [Int] -> [Int]f xs = [ x*x | x <- xs, x > 0 ]

*Main> f [1,-2,3][1,9]

Binding occurrenceBound occurrenceScope of binding

Page 75: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Comprehensions—pathological casef :: [Int] -> [Int]f x = [ x*x | x <- x, x > 0 ]

*Main> f [1,-2,3][1,9]

Binding occurrenceBound occurrenceScope of binding – Note hole in scope!

Page 76: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Squares of Positives—pathological casef :: [Int] -> [Int]f x = [ x*x | x <- x, x > 0 ]

*Main> f [1,-2,3][1,9]

Binding occurrenceBound occurrenceScope of binding

Page 77: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

List comprehension with two qualifiersf n = [ (i,j) | i <- [1..n], j <- [i..n] ]

*Main> f 3[(1,1),(1,2),(1,3),(2,2),(2,3),(3,3)]

Page 78: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

List comprehension with two qualifiers—bindingf n = [ (i,j) | i <- [1..n], j <- [i..n] ]

*Main> f 3[(1,1),(1,2),(1,3),(2,2),(2,3),(3,3)]

Binding occurrenceBound occurrenceScope of binding

Page 79: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

List comprehension with two qualifiers—bindingf n = [ (i,j) | i <- [1..n], j <- [i..n] ]

*Main> f 3[(1,1),(1,2),(1,3),(2,2),(2,3),(3,3)]

Binding occurrenceBound occurrenceScope of binding

Page 80: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

List comprehension with two qualifiers—bindingf n = [ (i,j) | i <- [1..n], j <- [i..n] ]

*Main> f 3[(1,1),(1,2),(1,3),(2,2),(2,3),(3,3)]

Binding occurrenceBound occurrenceScope of binding

Page 81: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Part XII

Additional material:Higher-order functions and binding

Page 82: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Higher-order functionsf :: [Int] -> [Int]f xs = map sqr (filter pos xs)

wheresqr x = x*xpos x = x > 0

*Main> f [1,-2,3][1,9]

Page 83: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Higher order functions—bindingf xs = map sqr (filter pos xs)

wheresqr x = x*xpos x = x > 0

*Main> f [1,-2,3][1,9]

Binding occurrenceBound occurrenceScope of binding

Page 84: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Higher-order functions—bindingf xs = map sqr (filter pos xs)

wheresqr x = x*xpos x = x > 0

*Main> f [1,-2,3][1,9]

Binding occurrenceBound occurrenceScope of binding

Page 85: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Higher-order functions—bindingf xs = map sqr (filter pos xs)

wheresqr x = x*xpos x = x > 0

*Main> f [1,-2,3][1,9]

Binding occurrenceBound occurrenceScope of binding

Page 86: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Higher-order functions—bindingf xs = map sqr (filter pos xs)

wheresqr x = x*xpos x = x > 0

*Main> f [1,-2,3][1,9]

Binding occurrenceBound occurrenceScope of binding

Page 87: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Higher-order functions—bindingf xs = map sqr (filter pos xs)

wheresqr x = x*xpos x = x > 0

*Main> f [1,-2,3][1,9]

Binding occurrenceBound occurrenceScope of binding

Page 88: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Higher-order functions—bindingf xs = map sqr (filter pos xs)

wheresqr x = x*xpos x = x > 0

*Main> f [1,-2,3][1,9]

Binding occurrenceBound occurrenceScope of binding

Page 89: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Higher-order functions—bindingf xs = map sqr (filter pos xs)

wheresqr x = x*xpos x = x > 0

*Main> f [1,-2,3][1,9]

Binding occurrence—not shown (in standard prelude)Bound occurrenceScope of binding

Page 90: Lambda expressions, functions and binding · 2012-10-23 · Informatics 1 Functional Programming Lecture 8 Tuesday 23 October 2012 Lambda expressions, functions and binding Don Sannella

Higher-order functions—bindingf xs = map sqr (filter pos xs)

wheresqr x = x*xpos x = x > 0

*Main> f [1,-2,3][1,9]

Binding occurrence—not shown (in standard prelude)Bound occurrenceScope of binding