问题描述
我正在使用 Aeson
和 Network.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 ()
是一个从 s
类型值到 State s
“actions”,一个“action”构造函数的函数。
putStrLn "Hi"
是一个 IO
操作。 putStrLn :: String -> IO ()
是一个从 String
到 IO
动作的函数,一个“动作”构造函数。
IO
是一个 Monad。 State s
是一个 Monad。
“动作”是 M a
类型的任何值,其中 M
是 Monad。