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.
|
. You have some other things to deal fix after that, including applying foldTree with the correct number of arguments within the guards. – mnoronhaTrue
orFalse
) and the corresponding value assignments. If you want to pattern match different cases (leaf vs. node), you can do so with a case statement or by manually matching several cases with function declarations. I recommend you read this: learnyouahaskell.com/syntax-in-functions – mnoronha