lambda expressions, functions and binding · 2012-10-23 · informatics 1 functional programming...
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/16.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/21.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/25.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/26.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/27.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/28.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/29.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/30.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/31.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/32.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/33.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/34.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/35.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/36.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/37.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/38.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/39.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/40.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/41.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/42.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/43.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/44.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/45.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/46.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/47.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/48.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/49.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/50.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/51.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/52.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/53.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/54.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/55.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/56.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/57.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/58.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/59.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/60.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/61.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/62.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/63.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/64.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/65.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/66.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/67.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/68.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/69.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/70.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/71.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/72.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/73.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/74.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/75.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/76.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/77.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/78.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/79.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/80.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/81.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/82.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/83.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/84.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/85.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/86.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/87.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/88.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/89.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042312/5eda9a06b814eb156475a5a9/html5/thumbnails/90.jpg)
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