无法理解Monad>>的结果应用 [英] Can't understand result of Monad >> application
问题描述
操作>>描述如下:
依次组成两个动作,舍弃由首先,像命令运算符(例如分号)一样语言.
Sequentially compose two actions, discarding any value produced by the first, like sequencing operators (such as the semicolon) in imperative languages.
以下是使我感到困惑的示例:
Here is the example which confuses me:
> ([1] ++ [2]) >> ([2] ++ [3])
[2,3,2,3]
我期望列表[2,3]是表达式右边部分的结果.如何解释[2,3,2,3]的结果?
I'm expecting the list [2,3] which would be result of right part of expression. How can result of [2,3,2,3] be explained?
推荐答案
(>>)
默认情况下定义为
a >> b = a >>= (\_ -> b)
因此被忽略的值是给定单价值 ma
中的 a
.专门列出的>> =
的类型为:
so the value being ignored is an a
in a given monadic value m a
. The type of >>=
specialised to list is:
(>>=) :: [a] -> (a -> [b]) -> [b]
l>> = f
为列表 l
的每个元素调用 f
以产生一个列表列表,然后将其连接起来
l >>= f
invokes f
for each element of the list l
to product a list of lists which is then concatenated.
例如
[1,2] >>= (\i -> [i, -i])
> [1,-1,2,-2]
忽略每个输入元素并返回值 [2,3]
将得到n个副本列表 [2,3]
,其长度为<代码> n
Ignoring each input element and returning the value [2,3]
will result in n copies of the list [2,3]
for an input list of length n
例如
[1] >>= (\_ -> [2,3])
> [2,3]
[1,2] >>= (\_ -> [2,3])
> [2,3,2,3]
第二个示例等效于([1] ++ [2])>>([2] ++ [3])
.
这篇关于无法理解Monad>>的结果应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!