[Haskell-cafe] Re: Possible Improvements
apfelmus at quantentunnel.de
Mon Dec 3 03:46:26 EST 2007
PR Stanley wrote:
> 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.
That's entirely fine.
The logical or || doesn't evaluate it's second argument occurs m r if
the first argument occurs m l turns out to be already True. In other
words, thanks to lazy evaluation, the search stops if m has been found
in the left subtree, it won't search the right subtree anymore.
More information about the Haskell-Cafe