Skip to content

Instantly share code, notes, and snippets.

@horus
Created September 25, 2012 06:58
Show Gist options
  • Save horus/3780368 to your computer and use it in GitHub Desktop.
Save horus/3780368 to your computer and use it in GitHub Desktop.
replaceMin problem & value recursion
{-# LANGUAGE DoRec #-}
data Tree a = L a | B (Tree a) (Tree a)
deriving (Show)
rpMinWithIO :: (Ord a, Show a) => (Tree a, a) -> IO (Tree a, a)
rpMinWithIO (L a, m) = print a >> return (L m, a)
rpMinWithIO (B l r, m) =
rpMinWithIO (l, m) >>=
\(l', lmin) ->
rpMinWithIO (r, m) >>=
\(r', rmin) ->
return (B l' r', min lmin rmin)
replaceMinWithIO :: (Ord a, Show a) => Tree a -> IO (Tree a)
replaceMinWithIO tree = do { rec (tree', m) <- rpMinWithIO (tree, m)
; return tree'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment