Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Haskell Question Given data Tree a = Leaf | Branch a (Tree a) (Tree a) deriving (Show, Eq) foldTree :: b -> (a -> b

Haskell Question

Given

data Tree a = Leaf

| Branch a (Tree a) (Tree a) deriving (Show, Eq)

foldTree :: b -> (a -> b -> b -> b) -> Tree a -> b

foldTree e _ Leaf = e

foldTree e n (Branch a n1 n2) = n a (foldTree e n n1) (foldTree e n n2)

mapTree :: (a -> b) -> Tree a -> Tree b

mapTree f = foldTree Leaf (\x t1 t2 -> Branch (f x) t1 t2)

1(a) Implement takeWhileTree, applied to a predicate p and a tree t, returns the largest prefix tree of t (possibly empty) where all elements satisfy p.

image text in transcribed

1(b) Implement zipTree f xs ys that returns the tree obtained by applying f to each pair of corresponding elements of xs and ys. If one branch is longer than the other, then the extra elements are ignored.

image text in transcribed

Thank you!

Main > tree1 = Branch 1 (Branch 2 Leaf Leaf) (Branch 3 Leaf Leaf) Main > takeWhileTree ( takeWhileTree ( tree2 = Branch 4 (Branch 2 Leaf Leaf) (Branch 3 Leaf Leaf) Main > takeWhileTree ( zipTree (+) (Branch 1 Leaf (Branch 2 Leaf Leaf)) (Branch 3 Leaf Leaf) (Branch 4 Leaf Leaf) Main > tree1 = Branch 1 (Branch 2 Leaf Leaf) (Branch 3 Leaf Leaf) Main > takeWhileTree ( takeWhileTree ( tree2 = Branch 4 (Branch 2 Leaf Leaf) (Branch 3 Leaf Leaf) Main > takeWhileTree ( zipTree (+) (Branch 1 Leaf (Branch 2 Leaf Leaf)) (Branch 3 Leaf Leaf) (Branch 4 Leaf Leaf)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Quality Audit A Management Evaluation Tool

Authors: Charles A. Mills

1st Edition

0070424284, 978-0070424289

More Books

Students also viewed these Accounting questions