Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 –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

2 Cse536 Functional Programming 2 6/10/2015 Recall Abnormal Schedule Next 2 Weeks Sun Mon Tue Wed Thu Fri Sat Oct 2426272829 Makeup Class Tim Sheard Regions 30 31Nov 123 Midterm exam 456 Guest Lect. Tom Harke The Haskell Class System 25 Guest Lect. Tom Harke Proofs about Haskell programs No Class

3 Cse536 Functional Programming 3 6/10/2015 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

4 Cse536 Functional Programming 4 6/10/2015 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 -> Bool type Set a = a -> Bool even :: Int -> Bool even x = (x `mod` 2) == 0

5 Cse536 Functional Programming 5 6/10/2015 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 =

6 Cse536 Functional Programming 6 6/10/2015 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)

7 Cse536 Functional Programming 7 6/10/2015 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

8 Cse536 Functional Programming 8 6/10/2015 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?

9 Cse536 Functional Programming 9 6/10/2015 Translate (u,v) r r (u,v)

10 Cse536 Functional Programming 10 6/10/2015 (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 }

11 Cse536 Functional Programming 11 6/10/2015 r Complement r

12 Cse536 Functional Programming 12 6/10/2015 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

13 Cse536 Functional Programming 13 6/10/2015 Rectangle (Rectangle s1 s2) `containsS` (x,y) = let t1 = s1/2 t2 = s2/2 in -t1<=x && x<=t1 && -t2<=y && y<=t2 s1 s2 t1 t2

14 Cse536 Functional Programming 14 6/10/2015 Ellipse (Ellipse r1 r2) `containsS` (x,y) = (x/r1)^2 + (y/r2)^2 <= 1 r1 r2

15 Cse536 Functional Programming 15 6/10/2015 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 direction from A to B, a Vertex (px,py) is to the 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)

16 Cse536 Functional Programming 16 6/10/2015 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

17 Cse536 Functional Programming 17 6/10/2015 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

18 Cse536 Functional Programming 18 6/10/2015 RtTriangle (RtTriangle s1 s2) `containsS` p = (Polygon [(0,0),(s1,0),(0,s2)]) `containsS` p s1 (0,0) (0,s2) (s1,0) s2

19 Cse536 Functional Programming 19 6/10/2015 Putting it all together containsS :: 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

20 Cse536 Functional Programming 20 6/10/2015 containsR using patterns containsR :: 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

21 Cse536 Functional Programming 21 6/10/2015 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

22 Cse536 Functional Programming 22 6/10/2015 The Eta rule The ability to get rid of identical rightmost arguments is a consequence of currying, and is called the eta-rule. listSum, listProd :: [Integer] -> Integer listSum xs = foldr (+) 0 xs listProd xs = foldr (*) 1 xs listSum, listProd :: [Integer] -> Integer listSum = foldr (+) 0 listProd = foldr (*) 1 and, or :: [Bool] -> Bool and xs = foldr (&&) True xs or xs = foldr (||) False xs and, or :: [Bool] -> Bool and = foldr (&&) True or = foldr (||) False

23 Cse536 Functional Programming 23 6/10/2015 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

24 Cse536 Functional Programming 24 6/10/2015 Simplify definitions reverse 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 (:)) []

25 Cse536 Functional Programming 25 6/10/2015 Sections and lambda Sections are like eta for infix operators (+ 5) = \ x -> x + 5 (4 -) = \ y -> 4 - y Function Composition f. g = \ x -> f (g x) totalSquareArea sides = sumList (map squareArea sides) = (sumList. (map squareArea)) sides totalSquareArea = sumList. map squareArea why no () ?


Download ppt "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."

Similar presentations


Ads by Google