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
Note that occurs checks elements in pre-order; so it can be factored into preorder and elem, where elem is defined in the Prelude: preorder (Leaf n) = [n] preorder (Node l n r) = n : preorder l ++ preorder r occurs x = elem x . preorder --- Also, Tree can be made to work on any type of value as a type constructor: data Tree k = Leaf k | Node (Tree k) k (Tree k) -- definitions of preorder and occurs are the same as before Stefan
signature.asc
Description: Digital signature
_______________________________________________ Haskell-Cafe mailing list [email protected] http://www.haskell.org/mailman/listinfo/haskell-cafe
