cse536 functional programming 1 6/10/2015 lecture #8, oct. 20, 2004 todays topics –sets and...

25
Cse536 Functional Programming 1 03/25/22 Lecture #8, Oct. 20, 2004 Todays Topics Sets and characteristic functions Regions Is a point in a Region Currying Sections Anonymous functions function composition Reading Assignment Finish » Chapter 8 - A Module of Regions » Chapter 9 - More about Higher Order Functions Begin » Chapter 10 – Drawing Regions

Post on 19-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

104/18/23

Lecture #8, Oct. 20, 2004•Todays Topics–Sets and characteristic functions

–Regions

–Is a point in a Region

–Currying

–Sections

–Anonymous functions

–function composition

•Reading Assignment–Finish

» Chapter 8 - A Module of Regions

» Chapter 9 - More about Higher Order Functions

–Begin

» Chapter 10 – Drawing Regions

Page 2: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

204/18/23

Recall Abnormal Schedule Next 2 WeeksSun Mon Tue Wed Thu Fri Sat

Oct 24 26 27 28 29

MakeupClass

Tim SheardRegions

30

31 Nov 1 2 3Midtermexam

4 5 6

Guest Lect.

Tom Harke

The Haskell Class System

25

Guest Lect.

Tom Harke

Proofs about Haskell programs

No Class

Page 3: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

304/18/23

Assignment• See the web page for more detailed

descriptions• This is due Friday, Oct 29, 2004. Hand in at

special Friday Lecture.

• 1) Exercise 5.9 page 73 of the text• 2) See the Web page• 3) Exercise 7.4 page 86 of the text• 4) Exercise 7.5 page 86 of the text

Page 4: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

404/18/23

Sets and Characteristic functions• How can we represent an infinite set in a

useful way in Haskell? E.g.– the set of all even numbers– the set of all prime numbers

• We could use an infinite list, but then searching it might take a long time!

• A characteristic function for a Set z (a set of objects with type z) is a functions from z -> Booltype Set a = a -> Bool

even :: Int -> Booleven x = (x `mod` 2) == 0

Page 5: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

504/18/23

Room for thought

• If sets are represented by characteristic functions, then how do you represent the:– Union of two sets?

– The Intersections of two sets

– The complement of a set.

• In Class exercise: write Haskell functions for these

x `union` y =

x `intersect` y =

complement x =

Page 6: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

604/18/23

The Region datatype• A region represents an area on the two

dimensional plane

-- A Region is either:

data Region =

Shape Shape -- primitive shape

| Translate Vector Region -- translated region

| Scale Vector Region -- scaled region

| Complement Region -- inverse of region

| Region `Union` Region -- union of regions | Region `Intersect` Region -- intersection of regions

| Empty

deriving Show

type Vector = (Float, Float)

Page 7: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

704/18/23

Why Regions?

•Regions are interesting because

– They allow us to build complicated “shapes” from simple ones

– They illustrate the use of tree-like data structures

» What makes regions tree-like?

– They “solve” the problem of only having rectangles and ellipses centered about the origin.

– They make a beautiful analogy with mathematical sets

Page 8: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

804/18/23

What is a region?•A Region is all those points that lie

within some area in the 2 dimensional plane.

• This often (almost always?) an infinite set.

•An efficient representation is as a characteristic function.

•What do they look like? What do they represent?

Page 9: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

904/18/23

Translate (u,v) r

r

(u,v)

Page 10: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

1004/18/23

(1,-1)

(3,-3)

(3,-1)

(1,-3)

(2,1)

(1,2)

(-1,-1)

Scale (x,y) R ={ (a*x,b*y) | (a,b) R }

Page 11: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

1104/18/23

r Complement r

Page 12: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

1204/18/23

Region Characteristic functions•We define the meaning of a region by

its characteristic function.• containsR :: Region -> Vector -> Bool•How would you write this function?

– Recursion, using pattern matching over the structure of a Region

– What are the base cases of the recursion?

• Start with a characteristic function for a primitive Shape

Page 13: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

1304/18/23

Rectangle(Rectangle s1 s2) `containsS` (x,y)

= let t1 = s1/2

t2 = s2/2

in -t1<=x && x<=t1 && -t2<=y && y<=t2

s1

s2t1 t2

Page 14: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

1404/18/23

Ellipse(Ellipse r1 r2) `containsS` (x,y)

= (x/r1)^2 + (y/r2)^2 <= 1

r1

r2

Page 15: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

1504/18/23

Left of a line that bisects the plane

A = (ax,ay)

B = (bx,by)For a Ray specified by two points(A,B), and facing in the directionfrom A to B, a Vertex (px,py) is tothe left of the line when:

isLeftOf :: Vertex -> Ray -> Bool(px,py) `isLeftOf` ((ax,ay),(bx,by)) = let (s,t) = (px-ax, py-ay) (u,v) = (px-bx, py-by) in s*v >= t*u

(px,py)

Page 16: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

1604/18/23

Inside a (Convex) Polygon

A Vertex, P, is inside a (convex) polygon if it is to the left of every side, when they are followed in (counter-clockwise) order

P

Page 17: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

1704/18/23

Polygon(Polygon pts) `containsS` p

= let shiftpts = tail pts ++ [head pts]

leftOfList =

map isLeftOfp(zip pts shiftpts)

isLeftOfp p' = isLeftOf p p'

in foldr (&&) True leftOfList

Page 18: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

1804/18/23

RtTriangle(RtTriangle s1 s2) `containsS` p

= (Polygon [(0,0),(s1,0),(0,s2)])

`containsS` p

s1

(0,0)

(0,s2)

(s1,0)

s2

Page 19: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

1904/18/23

Putting it all togethercontainsS :: Shape -> Vertex -> Bool

(Rectangle s1 s2) `containsS` (x,y)

= let t1 = s1/2

t2 = s2/2

in -t1<=x && x<=t1 && -t2<=y && y<=t2

(Ellipse r1 r2) `containsS` (x,y)

= (x/r1)^2 + (y/r2)^2 <= 1

(Polygon pts) `containsS` p

= let shiftpts = tail pts ++ [head pts]

leftOfList =

map isLeftOfp(zip pts shiftpts)

isLeftOfp p' = isLeftOf p p'

in foldr (&&) True leftOfList

(RtTriangle s1 s2) `containsS` p

= (Polygon [(0,0),(s1,0),(0,s2)]) `containsS` p

Page 20: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

2004/18/23

containsR using patternscontainsR :: Region -> Vertex -> Bool

(Shape s) `containsR` p =

s `containsS` p

(Translate (u,v) r) `containsR` (x,y) =

r `containsR` (x-u,y-v)

(Scale (u,v) r) `containsR` (x,y) =

r `containsR` (x/u,y/v)

(Complement r) `containsR` p =

not (r `containsR` p)

(r1 `Union` r2) `containsR` p =

r1 `containsR` p || r2 `containsR` p

(r1 `Intersect` r2) `containsR` p =

r1 `containsR` p && r2 `containsR` p

Empty `containsR` p = False

Page 21: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

2104/18/23

More on Higher order functions• simple n a b = n * (a+b)

Note that:

simple n a b is really

(((simple n) a) b) in fully parenthesized notation

simple :: Float -> Float -> Float -> Float

simple n :: Float -> Float -> Float

(simple n) a :: Float -> Float

((simple n) a) b :: Float

multSumByFive a b = simple 5 a b is the same as

multSumByFive = simple 5

Page 22: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

2204/18/23

The Eta ruleThe ability to get rid of identical rightmost arguments is a

consequence of currying, and is called the eta-rule.

listSum, listProd :: [Integer] -> IntegerlistSum xs = foldr (+) 0 xslistProd xs = foldr (*) 1 xs

listSum, listProd :: [Integer] -> IntegerlistSum = foldr (+) 0listProd = foldr (*) 1

and, or :: [Bool] -> Booland xs = foldr (&&) True xsor xs = foldr (||) False xs

and, or :: [Bool] -> Booland = foldr (&&) Trueor = foldr (||) False

Page 23: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

2304/18/23

Be careful though ...

• f x = g (x+2) y x

is not equal to• f = g (x+2) y

• f x = e x

is equal to• f = e

only if x does not appear free in e

Page 24: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

2404/18/23

Simplify definitionsreverse xs = foldl revOp [] xs

where revOp acc x = x : acc

• In the prelude we have: flip f x y = f y x• so

revOp acc x = flip (:) acc x

revOp = flip (:)

• thus

reverse xs = foldl (flip (:)) [] xs

reverse = foldl (flip (:)) []

Page 25: Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying

Cse536 Functional Programming

2504/18/23

Sections and lambda• Sections are like eta for infix operators

(+ 5) = \ x -> x + 5

(4 -) = \ y -> 4 - y

• Function Compositionf . g = \ x -> f (g x)

totalSquareArea sides

= sumList (map squareArea sides)

= (sumList . (map squareArea)) sides

totalSquareArea = sumList . map squareArea why no () ?