Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Cse536 Functional Programming Lecture #7, Oct. 18, 2004 •Today’s Topics – Trees – Kinds of trees - branching factor –functions over trees –patterns of recursion - the fold for trees –Arithmetic expressions –Infinite trees •Reading Assignment – Finish: Chapter 6 - Shapes III; Perimeters of Shapes –Finish: Chapter 7 - Trees - Start (for next time) Chapter 8 - A Module of Regions Chapter 9 - More About Higher Order Functions 5/24/2017 1 Cse536 Functional Programming Trees • Trees are important data structures in computer science • Trees have interesting properties – – – – – They usually are finite, but unbounded in size Sometimes contain other types inside Sometimes the things contained are polymorphic differing “branching factors” different kinds of leaf and branching nodes • Lots of interesting things can be modeled by trees – lists (linear branching) – arithmetic expressions – parse trees (for languages) • In a lazy language it is possible to have infinite trees 5/24/2017 2 Cse536 Functional Programming Examples data List a = Nil | MkList a (List a) data Tree a = Leaf a | Branch (Tree a) (Tree a) data IntegerTree = IntLeaf Integer | IntBranch IntegerTree IntegerTree data SimpleTree = SLeaf | SBranch SimpleTree SimpleTree data InternalTree a = ILeaf | IBranch a (InternalTree a) (InternalTree a) data FancyTree a b = FLeaf a | FBranch b (FancyTree a b) (FancyTree a b) 5/24/2017 3 Cse536 Functional Programming Match up the trees • IntegerTree A A B • Tree B i • SimpleTree j A k B C • List • InternalTree A 2 • FancyTree 5/24/2017 B 6 9 4 Cse536 Functional Programming Functions on Trees • Transforming one kind of tree into another mapTree :: (a->b) -> Tree a -> Tree b mapTree f (Leaf x) = Leaf (f x) mapTree f (Branch t1 t2) = Branch (mapTree f t1) (mapTree f t2) • Collecting the items in a tree fringe :: Tree a -> [a] fringe (Leaf x) = [x] fringe (Branch t1 t2) = fringe t1 ++ fringe t2 • what kind of information is lost using fringe? 5/24/2017 5 Cse536 Functional Programming More functions treeSize :: Tree a -> Integer treeSize (Leaf x) = 1 treeSize (Branch t1 t2) = treeSize t1 + treeSize t2 treeHeight :: Tree a -> Integer treeHeight (Leaf x) = 0 treeHeight (Branch t1 t2) = 1 + max (treeHeight t1) (treeHeight t2) 5/24/2017 6 Cse536 Functional Programming Capture the pattern of recursion foldTree :: (a -> a -> a) -> (b -> a) -> Tree b -> a foldTree b l (Leaf x) = l x foldTree b l (Branch t1 t2) = b (foldTree b l t1) (foldTree b l t2) mapTree2 f = foldTree Branch (Leaf . f) fringe2 = foldTree (++) (\ x -> [x]) treeSize2 = foldTree (+) (const 1) treeHeight2 = foldTree (\ x y -> 1 + max x y) (const 0) 5/24/2017 7 Cse536 Functional Programming Flattening Trees data Tree a = Leaf a | Branch (Tree a) (Tree a) flatten :: Tree a -> [a] flatten (Leaf x) = [x] flatten (Branch x y) – flatten x ++ flatten y What is the complexity of flattening a deep fully filled out tree? 5/24/2017 8 Cse536 Functional Programming Flattening with accumulating parameter data Tree a = Leaf a | Branch (Tree a) (Tree a) flatten :: Tree a -> [a] Flatten t = flat t [] flat (Leaf x) xs = x:xs Flat (Branch a b) – flat a (flat b xs) 5/24/2017 9 Cse536 Functional Programming Arithmetic Expressons data Expr2 = | | | | C2 Float Add2 Expr2 Sub2 Expr2 Mul2 Expr2 Div2 Expr2 Expr2 Expr2 Expr2 Expr2 • using infix constructor functions data Expr = | | | | 5/24/2017 C Float Expr :+ Expr :Expr :* Expr :/ Expr Expr Expr Expr Infix constructor operators start with a colon (:) , just like constructor functions start with an upper case letter 10 Cse536 Functional Programming Example uses e1 = (C 10 :+ (C 8 :/ C 2)) :* (C 7 :- C 4) evaluate evaluate evaluate evaluate evaluate evaluate :: Expr -> (C x) = x (e1 :+ e2) (e1 :- e2) (e1 :* e2) (e1 :/ e2) Float = = = = evaluate evaluate evaluate evaluate e1 e1 e1 e1 + * / evaluate evaluate evaluate evaluate e2 e2 e2 e2 Main> evaluate e1 42.0 5/24/2017 11 Cse536 Functional Programming Infinite Trees • Can we make an Expr tree that represents the infinite expression: 1 + 2 + 3 + 4 …. sumFromN n = C n :+ (sumFromN (n+1)) sumAll = sumFromN 1 add1 add1 add1 add1 add1 (C (x (x (x (x n) :+ ::* :/ = C (n+1) y) = add1 y) = add1 y) = add1 y) = add1 x x x x :+ ::* :/ add1 add1 add1 add1 y y y y sumAll2 = C 1 :+ (add1 sumAll2) 5/24/2017 12 Cse536 Functional Programming Observing Infinite Trees • We can observe an infinite tree by printing a finite prefix of it. We need a take-like function for trees. showE 0 _ = "..." showE n (C m) = show m showE n (x :+ y) = "(" ++ (showE (n-1) x) ++ "+" ++ (showE (n-1) y) ++ ")" Main> showE 5 sumAll2 "(1.0+(2.0+(3.0+(4.0+(...+...)))))" Main> showE 5 sumAll "(1.0+(2.0+(3.0+(4.0+(...+...)))))" 5/24/2017 13 Cse536 Functional Programming The Region datatype • A region represents an area on the two dimensional plane • Its represented by a tree-like data-structure -- A Region is either: data Region = Shape Shape | Translate Vector Region | Scale Vector Region | Complement Region | Region `Union` Region | Region `Intersect` Region | Empty deriving Show 5/24/2017 ------- primitive shape translated region scaled region inverse of region union of regions intersection of regions 14 Cse536 Functional Programming Regions and Trees • Why is Region tree-like? • What’s the strategy for writing functions over Regions? • Is there a fold-function for Regions? – How many parameters does it have? – What is its type? • Can one make infinite regions? • What does a region mean? 5/24/2017 15