data Tree a = Nil | Node a (Tree a) (Tree a) deriving (Show) foldTree :: r -> (a -> r -> r -> r) -> Tree a -> r foldTree f _ Nil = f foldTree f g (Node x t1 t2) = g x (foldTree f g t1) (foldTree f g t2) tree1 :: Tree String tree1 = Node "a" (Node "b" (Node "c" Nil Nil) Nil) (Node "b" (Node "d" Nil Nil) Nil) finds :: Eq a => a -> Tree a -> [Tree a] finds needle = snd . foldTree (Nil, []) (\x (sub1, m1) (sub2, m2) -> let tree = Node x sub1 sub2 in if x == needle then (tree, tree : m1 ++ m2) else (tree, m1 ++ m2)) main :: IO () main = print $ finds "b" tree1