做haskell s'做'符号总是涉及单子?
我一直认为Haskell的do
- 音调只是单元组成的糖(>> =
)。 然后我遇到了它的实例,不确定这里使用了什么单调,还是有一个:
rollDieThreeTimes :: (Die, Die, Die)
rollDieThreeTimes = do
let s = mkStdGen 0
(d1, s1) = randomR (1, 6) s
(d2, s2) = randomR (1, 6) s1
(d3, _) = randomR (1, 6) s2
(intToDie d1, intToDie d2, intToDie d3)
intToDie :: Int -> Die
...
我的问题:
do do
- do - 调用这个示例?- 如果没有
的格式让
- 没有单一的陈述? - 如果有一个单子,那是什么?而且,一个人将如何将此
转换为
- notation to> =
表单?
I had always figured that Haskell's do
-notation was just sugar for monad composition (>>=
).
Then I came across this instance of it, and am not sure what monad is in use here, or whether there even is one:
rollDieThreeTimes :: (Die, Die, Die)
rollDieThreeTimes = do
let s = mkStdGen 0
(d1, s1) = randomR (1, 6) s
(d2, s2) = randomR (1, 6) s1
(d3, _) = randomR (1, 6) s2
(intToDie d1, intToDie d2, intToDie d3)
intToDie :: Int -> Die
...
My questions:
- Is there even a monad underlying the
do
-notation of this example? - If not, is there some special form of
do
-notation that permits this
format oflet
-statements with no monad? - If there is a monad, which is it? And, how would one convert this
do
-notation into>>=
form?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于DO通知,要了解的一件重要的事情是,在类型检查之前,它将应用其规则。这些规则是(大致):
做{x; y}
==>x>> do {y}
(随着规则递归应用)do {x< - a; b}
==>a>> = \ x->做{b}
do {{pat}< - a; b}
==>做{让{dect}; b}
==>让{b}
do {a}
==&gt in do {b}中的{dect}a
最后两个规则是这里的重要规则 - 任何具有(隐式)半龙的任何表达式都会成为自身。因此,只要您不使用前两个规则,就可以在代码中具有任何类型的表达式,而无需任何单调。遵循这些规则,您的代码从
且
IS 然后是类型。
One important thing to know about do-notation is that its rules are applied before type checking. Those rules are (roughly):
do { x; y }
==>x >> do { y }
(with rules applied recursively)do {x <- a; b }
==>a >>= \x -> do { b }
do {{pat} <- a; b }
==>do { let {decls}; b }
==>let {decls} in do { b }
do { a }
==>a
those last two rules are the important ones here - any expression on its own which has no (implicit) semicolons will just become itself. So as long as you don't make use of the first two rules, you can have expressions of any type in your code, without the need for any monad. Following those rules, your code goes from
to
and is then type checked.
do
应始终在单片上下文中使用。但是,如果您在do
block中只有一个表达式,那么它只有该表达式,因此没有引用&gt;&gt; =
,也没有monad发生约束。因此,您可以编写
等同于,
但实际上,您应该写后者。
我还没有读过HPFFP,Google告诉我这可能是您收到此示例的书。它来自有关状态的一章,似乎作者将其作为一个简单的国家示例引入,然后添加状态。也许他们出于教学原因选择了这种不寻常的结构,例如,使用Monadic Binds代替Let Bindings,以与以后的版本保持相似。
do
should always be used in a monadic context. But if you only have one expression in thedo
-block, then it desugars to just that expression, so there are no references to>>=
, and no Monad constraint arises.So you can write
which is equivalent to
but really, you should just write the latter.
I haven't read HPFFP, which Google tells me is probably the book you got this example from. It comes from the chapter on State, and it seems like the author(s) introduce this as a simple state-less example, then later add state to it. Perhaps they chose this unusual construction for pedagogical reasons, for example to keep it similar to a later version using monadic binds in place of the let bindings.