ContinuationMonadとMonadTransformer
Haskell Wikiとモナドのすべてを見ながらContinuationモナドとMonadTransformerをお勉強(学習と言うべきなんだろう、きっと)。分かったような、分かんないような。今日はひとまず撤収。
Continuationモナドは何が「うれしい」のかな?一発でexitできるところ?Continuation Passing Styleがエレガントに書ける?そーなの?
(4/24 追記)
「継続」がちゃんと分かってないんだな、きっと。しっかり勉強してからもう一度挑戦。
import Control.Monad.Cont execute :: Cont (IO ()) String -> IO () execute c = putStrLn "<<execute>> " >> runCont c putStrLn executeT :: ContT () IO String -> IO () executeT c = putStrLn "<<executeT>> " >> runContT c putStrLn myCont = callCC cont where cont :: (String -> Cont (IO ()) String) -> Cont (IO ()) String cont exit = do return $ "cont: " ++ (show 10) myContT = callCC contT where contT :: (String -> ContT () IO String) -> ContT () IO String contT exit = do liftIO (readLn :: IO Int) >>= liftIO . return . (++) "contT: " . show -- contT exit = liftIO $ do -- readLn :: IO Int >>= return . (++) "contT: " . show myExitT = callCC exitT where exitT :: (String -> ContT () IO String) -> ContT () IO String exitT exit = do n <- liftIO (readLn :: IO Int) if n == 0 then exit "exited!!!" else return $ (++) "exitT: " $ show n main = execute myCont >> executeT myContT >> executeT myExitT