[英]difference between (>>=) and (>=>)
我需要对(>> =)和(> =>)做一些澄清。
*Main Control.Monad> :type (>>=)
(>>=) :: Monad m => m a -> (a -> m b) -> m b
*Main Control.Monad> :type (>=>)
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
我知道bind运算符(>> =),但我没有得到(> =>)有用的上下文。 请用简单的玩具例子说明。
编辑:根据@Thomas评论进行更正
(>=>)
函数有点像(.)
,但不是使用a -> b
,而是使用a -> mb
。
-- Ask the user a question, get an answer.
promptUser :: String -> IO String
promptUser s = putStrLn s >> getLine
-- note: readFile :: String -> IO String
-- Ask the user which file to read, return the file contents.
readPromptedFile :: String -> IO String
readPromptedFile = promptUser >=> readFile
-- Ask the user which file to read,
-- then print the contents to standard output
main = readPromptedFile "Read which file?" >>= putStr
这有点做作,但它说明了(>=>)
。 像(.)
,你不需要它,但它通常用于以无点样式编写程序。
请注意, (.)
具有与(>=>)
相反的参数顺序,但也有(<=<)
,它是flip (>=>)
。
readPromptedFile = readFile <=< promptUser
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.