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.

Slides:



Advertisements
Similar presentations
Introduction A function is called higher-order if it takes a function as an argument or returns a function as a result. twice :: (a  a)  a  a twice.
Advertisements

ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Chapter 8 A Module of Regions. The Region Data Type  A region represents an area on the two-dimensional Cartesian plane.  It is represented by a tree-like.
Chapter 6 Shapes III: Perimeters of Shapes. The Perimeter of a Shape  To compute the perimeter we need a function with four equations (1 for each Shape.
Chapter 11 Proof by Induction. Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes.
Defining new types of data. Defining New Datatypes Ability to add new datatypes in a programming language is important. Kinds of datatypes – enumerated.
Cse536 Functional Programming 1 6/10/2015 Lecture #6, Oct 13, 2004 Reading Assignments –Read chapter 5 of the Text Polymorphic and Higher-Order Functions.
Cse536 Functional Programming 1 6/10/2015 Lecture 10, Oct. 29, 2004 Special Rescheduled Friday Lecture Today’s Topics – Pictures – Low level graphics bit-maps:
Cse536 Functional Programming 1 6/11/2015 Lecture #5, Oct 11, 2004 Reading Assignments –Finish Chapter 3, Simple Graphics –Read Chapter 4, Shapes II, Drawing.
Cse536 Functional Programming 1 6/12/2015 Lecture #7, Oct. 18, 2004 Today’s Topics – Trees – Kinds of trees - branching factor –functions over trees –patterns.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Chapter 9 More About Higher-Order Functions. Currying Recall the function: simple n a b = n * (a+b) Note that: simple n a b is really (((simple n) a)
Advanced Programming Handout 8 Drawing Regions (SOE Chapter 10)
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Chapter 2 A Module of Shapes: Part I. Defining New Datatypes  The ability to define new data types in a programming language is important.  Kinds of.
Chapter 10 Drawing Regions. Pictures  Drawing Pictures Pictures are composed of Regions o Regions are composed of shapes Pictures add Color data Picture.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
Advanced Programming Handout 4. Introductions  Me: Benjamin C. Pierce (known as Benjamin, or, if you prefer, Dr. Pierce, but not Ben or Professor) 
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Advanced Programming Handout 7 More About Higher-Order Functions (SOE Chapter 9)
Cse536 Functional Programming 1 6/28/2015 Lecture #3, Oct 4, 2004 Reading Assignments –Finish chapter 2 and begin Reading chapter 3 of the Text Today’s.
Chapter 5 Polymorphic and Higher-Order Functions.
Advanced Programming Handout 5 Recursive Data Types (SOE Chapter 7)
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
Course Outline Book: Discrete Mathematics by K. P. Bogart Topics:
Preparing for FRP Shapes, Regions, and Drawing. Shape types from the Text data Shape = Rectangle Float Float | Ellipse Float Float | RtTriangle Float.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Unit 10 Transformations. Lesson 10.1 Dilations Lesson 10.1 Objectives Define transformation (G3.1.1) Differentiate between types of transformations (G3.1.2)
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Conditional Expressions
Recursion.
Theory of Computation Lecture 4: Programs and Computable Functions II
Functional Programming Lecture 12 - more higher order functions
Functions and patterns
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Higher Order Functions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Types and Classes in Haskell
CSCE 314: Programming Languages Dr. Dylan Shell
Pattern Matching Pattern matching allows us to do things like this:
Higher Order Functions
PROGRAMMING IN HASKELL
CSE-321 Programming Languages Introduction to Functional Programming
HIGHER ORDER FUNCTIONS
Sun Mon Tue Wed Thu Fri Sat
CSE 3302 Programming Languages
Functions and patterns
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
foldr and foldl applied to addition (using infix notation)
Sun Mon Tue Wed Thu Fri Sat
Functions and patterns
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Sun Mon Tue Wed Thu Fri Sat
Presentation transcript:

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

Cse536 Functional Programming 2 6/10/2015 Recall Abnormal Schedule Next 2 Weeks Sun Mon Tue Wed Thu Fri Sat Oct 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

Cse536 Functional Programming 3 6/10/2015 Assignment See the web page for more detailed descriptions This is due Friday, Oct 29, 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

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

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 =

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)

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

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?

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

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 }

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

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

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

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

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)

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

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

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

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

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

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

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

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

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 (:)) []

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 () ?