Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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

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

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

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

4 Cse536 Functional Programming 4 6/12/2015 Match up the trees IntegerTree Tree SimpleTree List InternalTree FancyTree AB A BC 2 69 A B A B i jk

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

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

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

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

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

10 Cse536 Functional Programming 10 6/12/2015 Arithmetic Expressons data Expr2 = C2 Float | Add2 Expr2 Expr2 | Sub2 Expr2 Expr2 | Mul2 Expr2 Expr2 | Div2 Expr2 Expr2 using infix constructor functions data Expr = 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

11 Cse536 Functional Programming 11 6/12/2015 Example uses e1 = (C 10 :+ (C 8 :/ C 2)) :* (C 7 :- C 4) evaluate :: Expr -> Float evaluate (C x) = x evaluate (e1 :+ e2) = evaluate e1 + evaluate e2 evaluate (e1 :- e2) = evaluate e1 - evaluate e2 evaluate (e1 :* e2) = evaluate e1 * evaluate e2 evaluate (e1 :/ e2) = evaluate e1 / evaluate e2 Main> evaluate e1 42.0

12 Cse536 Functional Programming 12 6/12/2015 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 (C n) = C (n+1) add1 (x :+ y) = add1 x :+ add1 y add1 (x :- y) = add1 x :- add1 y add1 (x :* y) = add1 x :* add1 y add1 (x :/ y) = add1 x :/ add1 y sumAll2 = C 1 :+ (add1 sumAll2)

13 Cse536 Functional Programming 13 6/12/2015 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+(...+...)))))"

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

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


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

Similar presentations


Ads by Google