dramforever

a row of my life

CPS、ANF、Monad 和 Callback hell

2017-03-28

CPS、Callback Hell

所有操作,需要你传一个叫什么 continuation 的东西来接受它的结果

main cont = getLine $ \name ->
    putStrLn ("Hello, " ++ name) $ \() ->
        getLine $ \age ->
            putStrLn ("Hello, " ++ age ++ "-year-old " ++ name) $
                cont

ANF

所有操作,需要你把它 let 到一个变量

main =
    let name = getLine
    in  let () = putStrLn ("Hello, " ++ name)
        in  let age = getLine
            in putStrLn ("Hello, " ++ age ++ "-year-old " ++ name)

Monadic bind

所有操作,需要你把它 bind 出来

main = getLine >>= $ \name ->
    putStrLn ("Hello, " ++ name) >>= $ \() ->
        getLine >>= $ \age ->
            putStrLn ("Hello, " ++ age ++ "-year-old " ++ name)

Monadic do

所有操作,需要你把它 <- 到一个变量

main = do
    name <- getLine
    () <- putStrLn ("Hello, " ++ name)
    age <- getLine
    putStrLn ("Hello, " ++ age ++ "-year-old " ++ name)

这有啥区别啊

看起来很对的几个事情