Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to define listTree so that it returns a list containing all the elements in order?

        listTree :: Tree a -> [a]
        listTree = foldTree f z
            where
            f x y z  = x + y + z
            z = []

This is what I have so far, but my f is wrong because the expected type is Tree a -> [a] but the actual is Tree [a] -> [a]

        data Tree a
           = Tip
           | Bin (Tree a) a (Tree a)
           deriving (Show, Eq)


        foldTree :: (b -> a -> b -> b) -> b -> Tree a -> b
        foldTree f z Tip         = z
        foldTree f z (Bin l x r) = f (foldTree f z l) x (foldTree f z r)

This is fold and data type for tree

Need help with defining f.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

First of all, combining lists uses ++ in haskell. + is only used for adding numbers. Second, the variable y is of type a, not [a], so you cannot use ++ on it directly.

listTree :: Tree a -> [a]
listTree = foldTree f z
    where
    f x y z = x ++ [y] ++ z
    z = []
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading