module Example9 where data Tree = Leaf Int | Tree :^: Tree deriving Show -- fmr: "find min and replace" fmr :: Int -> Tree -> (Tree, Int) fmr m (Leaf i) = (Leaf m, i) fmr m (l :^: r) = (l' :^: r', min ml mr) where (l', ml) = fmr m l (r', mr) = fmr m r replaceByMin :: Tree -> Tree replaceByMin t = t' where -- Tying the recursive knot. (t', m) = fmr m t aSmallTree = Leaf 3 :^: Leaf 1 aLargerTree = (Leaf 7 :^: Leaf 3) :^: (Leaf 1 :^: (Leaf 2 :^: Leaf 9))