繁体   English   中英

这个简单的 do notation desugar 是为了什么?

[英]In what does this simple do notation desugar to?

我正在使用AesonNetwork.HTTP 我能够编码 json 并将其打印在屏幕上,执行以下操作:

getCode :: String -> IO ResponseCode
getCode url = simpleHTTP req >>= getResponseCode
    where req = getRequest url

main :: IO ()
main = do 
    x <- get "http://jsonplaceholder.typicode.com/todos/1"
    let y = encode x
    B.putStrLn y

但是我do明白这个表达式脱糖是为了什么。 像这样的东西:

get "http://jsonplaceholder.typicode.com/todos/1" >>= (?)

应该是什么? ?

我只知道如何脱糖:

do { x1 <- action1
   ; x2 <- action2
   ; mk_action3 x1 x2 }

对此

action1 >>= (\ x1 -> action2 >>= (\ x2 -> mk_action3 x1 x2 ))

顺便说一句,什么是action https://en.m.wikibooks.org/wiki/Haskell/do_notation没有很准确地解释。

get "http://jsonplaceholder.typicode.com/todos/1" >>= (\x -> let y = encode x in B.putStrLn y)

put (x:: s)State s “动作”。

put:: s -> State s ()是一个 function 从s类型值到State s “动作”构造函数。

putStrLn "Hi"是一个IO动作。 putStrLn:: String -> IO ()是从String s 到IO动作的 function 动作,一个“动作”构造函数。

IO是一个单子。 State s是一个单子。

“动作”是M a类型的任何值,其中M是 Monad。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM