[Haskell-beginners] Pattern matching on binary trees

Rohit Garg rpg.314 at gmail.com
Sun Sep 5 04:48:50 EDT 2010


I am working on exercises in RWH, chapter 3 and I have a a question
about the pattern matching in the depth-of-tree question.

The tree datatype is defined as

data Tree a = Node a (Tree a) (Tree a)
            | Empty
              deriving (Show)

And to pattern match on it, I need to write

depthTree (Node x u v) = 1 + max (depthTree u) (depthTree v)

My question is why do I need to prefix x with Node? Why can't it
pattern match on depthTree (x u v)? More generally, where do I need to
put data constructors in pattern matches?

Rohit Garg


More information about the Beginners mailing list