Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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 + 1Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started