首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(>>=)和(>=>)的区别

(>>=)和(>=>)的区别
EN

Stack Overflow用户
提问于 2016-08-22 13:56:18
回答 1查看 134关注 0票数 8

我需要一些关于(>>=)和(>=>)的澄清。

代码语言:javascript
复制
*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注释的更正

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-22 14:12:00

(>=>)函数有点像(.),但是它不使用a -> b,而是使用a -> m b

代码语言:javascript
复制
-- 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 (>=>)

代码语言:javascript
复制
readPromptedFile = readFile <=< promptUser
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39081456

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档