Question: USING HASKELL Part 2. (45 points) Data types, type classes, proof by induction [Read Chapters 8 and 16 Use the following data type for Problems

USING HASKELL

USING HASKELL Part 2. (45 points) Data types, type classes, proof by

Part 2. (45 points) Data types, type classes, proof by induction [Read Chapters 8 and 16 Use the following data type for Problems 5 and 6 data Tree2 a b-Leaf a | Branch b (Tree2 a b) (Tree2 a b) Problem 5. (10 points) Make Tree2 an instance of Show. Do not use deriving; define the instance yourself. Make the output look somewhat nice (e.g., indent nested branches) Problem 6. (10 points) Implement the two functions that traverse the tree in the given order collecting the values from the tree nodes into a list preorder :: (a -> c) > (b -> c) ->Tree2 a b > [c] inorder:: (a -> c) -> (b- c) -> Tree2 a b -> [c] Notice that the data type Tree2 can store different types of values in the leaves than on the branching nodes. Thus, each of these functions takes two functions as arguments: The first function maps the values stored in the leaves to some common type c, and the second functioin maps the values stored in the branching nodes to type c, thus, resulting in a list of type [c] Problem 7. (25 points) Make sure you read Chapter 16 before attempting this problem.] Chapter 16. Exercise 6, page 247. This problem has two parts. Given the following data type data Tree = Leaf Int | Node Tree Tree 1. (5+5 10) Given a tree, function leaves counts the number of leaves in the tree, and function nodes the number of internal nodes in the tree. Define leaves and nodes. The function types are as follows. leavesTree ->Int nodes:: Tree >Int 2. (Base case 5 points + inductive case 10 points) Prove the following property by induction on trees. leaves t = nodes t + 1 Part 2. (45 points) Data types, type classes, proof by induction [Read Chapters 8 and 16 Use the following data type for Problems 5 and 6 data Tree2 a b-Leaf a | Branch b (Tree2 a b) (Tree2 a b) Problem 5. (10 points) Make Tree2 an instance of Show. Do not use deriving; define the instance yourself. Make the output look somewhat nice (e.g., indent nested branches) Problem 6. (10 points) Implement the two functions that traverse the tree in the given order collecting the values from the tree nodes into a list preorder :: (a -> c) > (b -> c) ->Tree2 a b > [c] inorder:: (a -> c) -> (b- c) -> Tree2 a b -> [c] Notice that the data type Tree2 can store different types of values in the leaves than on the branching nodes. Thus, each of these functions takes two functions as arguments: The first function maps the values stored in the leaves to some common type c, and the second functioin maps the values stored in the branching nodes to type c, thus, resulting in a list of type [c] Problem 7. (25 points) Make sure you read Chapter 16 before attempting this problem.] Chapter 16. Exercise 6, page 247. This problem has two parts. Given the following data type data Tree = Leaf Int | Node Tree Tree 1. (5+5 10) Given a tree, function leaves counts the number of leaves in the tree, and function nodes the number of internal nodes in the tree. Define leaves and nodes. The function types are as follows. leavesTree ->Int nodes:: Tree >Int 2. (Base case 5 points + inductive case 10 points) Prove the following property by induction on trees. leaves t = nodes t + 1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!