Fold in a binary tree in haskell

0
Hello, I'm trying to define the foldTree function that takes a function and a binary polymorphic tree as parameters and returns the resultant value of accumulating the application of that function by all nodes of the tree. Then add all the nodes of the tree using this foldTree function and using recursion only

I'm defining my tree as:

data Tree a = Nulo 
            | No a (Tree a) (Tree a)
            deriving (Eq, Ord, Read, Show)

Filling in the tree

tree1 :: Tree Int
tree1 = No 1 Nulo (No 2 Nulo(No 3 Nulo Nulo))

FoldTree function

foldTree :: (b -> b -> b) -> Tree a -> b
foldTree f Nulo = 0
foldTree f (No a e d) = f (foldTree f e)(foldTree f d) 

And finally the functions somaTree and sumTree with folding

somaTree :: Tree a -> Int
somaTree Nulo = 0
somaTree (No a e d) = 

somaTreeFold :: Tree a -> Int
    
asked by anonymous 14.05.2018 / 19:07

0 answers