Haskell:(也许)monad构建 [英] Haskell : (maybe) monad construction
问题描述
(>> =):: Maybe a - > (a - >可能b) - >也许b
Nothing>> = _ = Nothing
(Just x)>> = f = fx
然而,我不太清楚这是如何使用它的:
<$ c $ (V n)= e n(n)= e n(n)= e n(n) >
safediv nm))
在我看来,运算符只需要一个Maybe值和一个返回一个值的函数,但是在这个例子中,使用代码看起来好像它需要2次可能值和一次函数。然而,我被告知它评估x,将结果放入n,然后评估y,将结果放入y中,然后对两者执行safediv函数。虽然我没有看到(>> =)运算符在这里扮演什么角色,这个怎么用?
如果有人能帮我解决这个问题,我会非常感激。
非常感谢! / p>
最好的问候,
Skyfe。
如下所示:
eval(Div xy)= eval x>> =(\ n - >
eval y>> =(\ m - >
safediv nm))
当你想做 eval(Div xy)
然后
- 先
eval x
: -
- if
Just n
(使用第一个>> = )
-
- 然后取
n
并查看eval y
(使用第一个>> = <如果最后一页是/ b> / b> / b>
-
- >
- 然后取
-
-
- 然后取
m
和(第二>> = )
- 然后取
-
- if
- <$>
-
savediv nm
返回结果 - 您仍然有
$ $ b
$ b
返回 Nothing
所以这里(>> =)
只是帮助您解构。 可能在<$ c中更容易阅读和理解
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ (Div xy)= do
n < - eval x
m < - eval y
safediv nm
这只是(>> =)
让我们追逐这些案例:
1.
eval x = Nothing
和 eval y = Nothing
: eval x>> =(...)= Nothing>> =(...)= Nothing
这是一样的:
2. eval x =否和
eval y =只要n
:
eval x>> =(...)= Nothing>> =(...)= Nothing
3. eval x =只是n
和 eval y =无
:
eval x>> =(\ n - > eval y> =(...))
= Just n>> =(\\\
-> eval y>> =(...))
=只要n>> =(\ n - >无)
=没有
4。 eval x =只要n
和 eval y =只需m
:
<$ p $ (\ n - >>>>> =(...))
= Just n>> =(\ n(···············
=(first>> = for for Just)= just m>> =(\ n - > safediv nm)
=(second>> = for Just)= safediv nm
I'm currently struggling with a new element of Haskell: Monads. Therefore I was introduced to this by an example of creating a >>= operator that executes a function on a maybe type (taking its actual integer value as argument to it) only if it's not equal to Nothing, and otherwise return Nothing:
(>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b
Nothing >>= _ = Nothing
(Just x) >>= f = f x
However, I'm not quite sure how this works with the following usage of it:
eval (Val n) = Just n
eval (Div x y) = eval x >>= (\n ->
eval y >>= (\m ->
safediv n m))
It seems to me that the (>>=) operator simply takes one Maybe value and a function that returns one, however in this example usage code it seems like it's taking 2 times a maybe value and once a function. I was told however that it evaluates x, puts the result in n, then evaluates y, puts the result in y, and then executes the safediv function on both. Although I don't see how the (>>=) operator plays its role here; How does this work?
If anyone could help me out on this it'd be much appreciated.
Thanks in advance!
Best regards, Skyfe.
You can read it like this:
eval (Div x y) = eval x >>= (\n ->
eval y >>= (\m ->
safediv n m))
when you want do eval (Div x y)
then
- first
eval x
: - if was
Just n
(using the first >>=)
- if was
- then take the
n
and have a look ateval y
(using the first >>=)
- then take the
- if the last is
Just m
(second >>=)
- if the last is
- then take the
m
and do a (second >>=)
- then take the
savediv n m
to return it's result - you still have then
from your closure!.
in ever other caes return Nothing
So here the (>>=)
just helps you to deconstruct.
Maybe it's easier to read and understand in the do
form:
eval (Val n) = Just n
eval (Div x y) = do
n <- eval x
m <- eval y
safediv n m
which is just syntactic sugar around (>>=)
let's chase the cases:
1.eval x = Nothing
and eval y = Nothing
:
eval x >>= (...) = Nothing >>= (...) = Nothing
2. eval x = Nothing
and eval y = Just n
:
which is just the same:
eval x >>= (...) = Nothing >>= (...) = Nothing
3. eval x = Just n
and eval y = Nothing
:
eval x >>= (\n -> eval y >>= (...))
= Just n >>= (\n -> eval y >>= (...))
= Just n >>= (\n -> Nothing)
= Nothing
4. eval x = Just n
and eval y = Just m
:
eval x >>= (\n -> Just m >>= (...))
= Just n >>= (\n -> Just m >>= (...))
= Just n >>= (\n -> Just m >>= (\m -> safediv n m))
= (first >>= for Just) = Just m >>= (\n -> safediv n m)
= (second >>= for Just) = safediv n m
这篇关于Haskell:(也许)monad构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!