Search code examples
haskellrecursionpattern-matching

How do I read what's in a binary tree in Haskell?


I have the following type:

data Tree = Branch (Tree) (Tree) | Leaf Int deriving(Show)

now I want to create a function that gives the highest value leaf in the tree. But I am stuck, because I don't know how to get the two following trees of a given tree.

For example I have a tree a that looks like this: let a = Branch (Leaf 10) (Leaf 2)

How do I read the Leaf with value 10 in another function?


Solution

  • A typical skeleton looks like this:

    maxTree :: Tree -> Int
    maxTree (Branch x y) = {- TODO -}
    maxTree (Leaf n) = {- TODO -}