简体   繁体   English

Haskell 中的 >> 符号是什么意思

[英]What does the >> symbol mean in Haskell

I was reading the Guestbook example for Happstack and noticed the >> symbol which I didn't see before in the textbooks I studied to learn Haskell (for instance see line 23).我正在阅读 Happstack 的留言板示例,并注意到在我学习 Haskell 的教科书中没有看到的>>符号(例如参见第 23 行)。 What is it?它是什么?

I could not find it in Google because it ignores the >> totally (Bing does not but comes up with tons of non-related results).我在 Google 中找不到它,因为它完全忽略了>> (Bing 没有提供大量不相关的结果)。

In do-notation在符号中

a >> b >> c >> d

is equivalent to相当于

do a
   b
   c
   d

(and similarly a >>= (b >>= (c >>= d)) is equivalent to (类似地a >>= (b >>= (c >>= d))等价于

do r1 <- a
   r2 <- b r1
   r3 <- c r2
   d r3

Hayoo recognises this kind of operator: http://holumbus.fh-wedel.de/hayoo/hayoo.html Hayoo 认得这种运营商: http ://holumbus.fh-wedel.de/hayoo/hayoo.html

(>>) is like (>>=) , in that it sequences two actions, except that it ignores the result from the first one. (>>)类似于(>>=) ,因为它对两个动作进行排序,只是它忽略了第一个动作的结果。

At the ghci command prompt, you can type:在 ghci 命令提示符下,您可以键入:

:info >>

And get a result like:并得到如下结果:

class Monad m where
...
(>>) :: m a -> m b -> m b
...
        -- Defined in GHC.Base
infixl 1 >>

From there, you can just take a look at the source code to learn more.从那里,您可以查看源代码以了解更多信息。

And just for the sake of answering your question:只是为了回答你的问题:

k >> f = k >>= \_ -> f

From Hackage, >> is described as:从 Hackage 中, >>被描述为:

"Sequentially compose two actions, discarding any value produced by the first, like sequencing operators (such as the semicolon) in imperative languages." “顺序组合两个动作,丢弃第一个产生的任何值,如命令式语言中的排序运算符(如分号)。”

I think a good example is printing two strings sequentially using >> .我认为一个很好的例子是使用>>顺序打印两个字符串。 Open GHCI and type the following:打开 GHCI 并输入以下内容:

putStr "Hello " >> putStrLn "World"

This is equivalent to the do notation:这相当于do符号:

do putStr "Hello "
   putStrLn "World"

I'm no Haskell expert, but >> is an operator that is used for working with monads, which are an unusual feature that (among many other things) enable imperative-style programming in Haskell.我不是 Haskell 专家,但>>是一个用于处理 monad 的运算符,这是一个不寻常的功能(除其他外)在 Haskell 中启用命令式编程。 There are many tutorials available on monads;有很多关于 monad 的教程; here's one good one .这是一个很好的

Essentially, a >> b can be read like "do a then do b , and return the result of b ".本质上, a >> b可以读作“做a然后做b ,并返回b的结果”。 It's similar to the more common bind operator >>= .它类似于更常见的绑定运算符>>=

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

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