Thanks. `mplus` is the glue I was looking for. The original algorithm has a bug. Following is what I have for now:
searchTree pred rootLoc | pred (getLabel rootLoc) = Just rootLoc | otherwise = (right rootLoc >>= searchTree pred) `mplus` (firstChild rootLoc >>= searchTree pred) Jian MightyByte <mightyb...@gmail.com> wrote: > I haven't tested it, but I think you're looking for something like this: > > searchTree2 :: (a -> Bool) -> TreeLoc a -> Maybe (TreeLoc a) > searchTree2 pred rootLoc = > if pred (getLabel rootLoc) > then Just rootLoc > else firstChild rootLoc >>= siblings > where siblings loc = searchTree2 pred loc `mplus` > (searchTree2 pred =<< right loc) > > > On Mon, Mar 8, 2010 at 1:11 PM, Jian Fan <abe...@gmail.com> wrote: >> Hi, >> >> There doesn't seem to be a function to search the tree so >> I come up with following function: >> >> searchTree :: (a -> Bool) -> TreeLoc a -> Maybe (TreeLoc a) >> searchTree pred rootLoc = >> if pred (getLabel rootLoc) then >> Just rootLoc >> else case firstChild rootLoc of >> Just loc -> case searchTree pred loc of >> Just loc -> Just loc >> Nothing -> case right loc of >> Just rLoc -> searchTree pred rLoc >> Nothing -> Nothing >> Nothing -> Nothing >> >> Which feels quite ugly. Any suggestions? Thanks. >> >> Jian >> >> _______________________________________________ >> Haskell-Cafe mailing list >> Haskell-Cafe@haskell.org >> http://www.haskell.org/mailman/listinfo/haskell-cafe >> _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe