我需要一些关于(>>=)和(>=>)的澄清。
*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 我知道绑定操作符(>>=),但是我没有得到(>=>)有用的上下文。请用简单的玩具例子来解释。
编辑:基于@Thomas注释的更正
发布于 2016-08-22 14:12:00
(>=>)函数有点像(.),但是它不使用a -> b,而是使用a -> m b。
-- 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 <=< promptUserhttps://stackoverflow.com/questions/39081456
复制相似问题