bill williams
bill williams

Reputation: 53

Generates parse input error in haskell

The below code sequence is generating a parse error on input |. If the input was a leaf node then call leafFunc with value If the input was a tree node then call TreeFunc with left Subtree,value,right Subtree

data Tree t = Leaf t
        | Node (Tree t) t (Tree t)

foldTree :: (t1 -> t -> t1 -> t1) -> (t -> t1) -> Tree t -> t1
foldTree treeFn leafFn tree= | foldTree (Leaf v) = leafFn(v)
                             | foldTree (Node left v right) = treeFn(left v right)


Input : foldTree (\t1 t t2->t1 + 5*t + t2) (\x->x+9) (Leaf 5)
Expected Output : 14

Input : foldTree (\t1 t t2->t1 + 3*t + t2) (\x->x+5) (Tree (Leaf 3) 2 (Leaf 4))
Expected Output : 23

I am a newbie in haskell.

Upvotes: 0

Views: 110

Answers (2)

Bargros
Bargros

Reputation: 531

I'm guessing this is what you want

data Tree t = Leaf t | Node (Tree t) t (Tree t) deriving Show

foldTree :: (t1 -> t -> t1 -> t1) -> (t -> t1) -> Tree t -> t1
foldTree treeFn leafFn tree 
                | foldTree (Leaf v) = leafFn v
                | foldTree (Node left v right) = treeFn left v right

Upvotes: 1

jim
jim

Reputation: 11

data Tree t = Leaf t
              | Node (Tree t) t (Tree t)

foldTree :: (Tree t -> t -> Tree t -> t1) -> (t -> t1) -> Tree t -> t1
foldTree treeFn leafFn (Leaf v) = leafFn v
foldTree treeFn leafFn (Node left v right) = treeFn left v right

Upvotes: 1

Related Questions