Haskell:(也许)monad构建 [英] Haskell : (maybe) monad construction

查看:131
本文介绍了Haskell:(也许)monad构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在努力使用Haskell的新元素:Monads。因此,我通过创建一个>> =运算符的例子来介绍这一点,该运算符在可能类型上执行一个函数(只将它的实际整数值作为它的参数),只要它不等于Nothing,否则返回Nothing: p>

 (>> =):: 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 和(第二>> =



        • <$>
        • 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 >>=)
      • then take the n and have a look at eval y (using the first >>=)
        • if the last is Just m (second >>=)
        • then take the m and do a (second >>=)
        • savediv n m to return it's result - you still have the n 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆