Monads and custom traversal functions in Haskell

Posted by Bill on Stack Overflow See other posts from Stack Overflow or by Bill
Published on 2010-04-01T01:22:30Z Indexed on 2010/04/01 1:43 UTC
Read the original article Hit count: 349

Filed under:

Given the following simple BST definition:

data Tree x = Empty | Leaf x | Node x (Tree x) (Tree x)
          deriving (Show, Eq)

inOrder :: Tree x -> [x]
inOrder Empty                  = []
inOrder (Leaf x)               = [x]
inOrder (Node root left right) = inOrder left ++ [root] ++ inOrder right

I'd like to write an in-order function that can have side effects. I achieved that with:

inOrderM :: (Show x, Monad m) => (x -> m a) -> Tree x -> m ()
inOrderM f (Empty) = return ()
inOrderM f (Leaf y) = f y >> return ()
inOrderM f (Node root left right) = inOrderM f left >> f root >> inOrderM f right

-- print tree in order to stdout
inOrderM print tree

This works fine, but it seems repetitive - the same logic is already present in inOrder and my experience with Haskell leads me to believe that I'm probably doing something wrong if I'm writing a similar thing twice.

Is there any way that I can write a single function inOrder that can take either pure or monadic functions?

© Stack Overflow or respective owner

Related posts about haskell