[Haskell-cafe] Possible Improvements

Tomasz Zielonka tomasz.zielonka at gmail.com
Mon Dec 3 00:28:31 EST 2007

On Mon, Dec 03, 2007 at 05:20:35AM +0000, PR Stanley wrote:
> Hi
> data Tree = Leaf Int | Node Tree Int Tree
> occurs :: Int -> Tree -> Bool
> occurs m (Leaf n) = m == n
> occurs m (Node l n r) = m == n || occurs m l || occurs m r
> It works but I'd like to know if it can be improved in any way.
> Thanks, Paul

I'm not sure this will count as improvement, but you can build the same
kinds of trees with a simpler datatype:

data Tree = Empty | Node Tree Int Tree

This way there is only one constructor with the Int value, which can
simplify other code a bit.

Best regards

More information about the Haskell-Cafe mailing list