【问题标题】:Using the Maybe Monad in "reverse"在“反向”中使用 Maybe Monad
【发布时间】:2011-08-02 03:36:11
【问题描述】:

假设我有许多功能:

f :: a -> Maybe a
g :: a -> Maybe a
h :: a -> Maybe a

我想用以下方式组合它们:如果 f 返回 Nothing,则计算 g。如果 g 返回 Nothing,则计算 h。如果其中任何一个计算 Just a,则停止该链。整个作文 (h . g . f) 当然应该返回 Maybe a。

这与 Maybe monad 的典型用法相反,如果没有返回,通常你会停止计算。

像这样链接计算的 Haskell 习惯用法是什么?

【问题讨论】:

    标签: haskell monads


    【解决方案1】:

    mplus 正是您正在寻找的,它是MonadPlus 类型类的一部分。这是它的定义:

    instance MonadPlus Maybe where
       mzero = Nothing
    
       Nothing `mplus` ys  = ys
       xs      `mplus` _ys = xs
    

    在你的情况下使用它:

    combined x = (f x) `mplus` (g x) `mplus` (h x) 
    

    【讨论】:

    • Alternative(Applicative 仿函数)也可以,因为 (<|>) 与 Maybe 的 mplus 相同。
    • 或者你可以使用 Data.Generics.Aliases.orElse
    【解决方案2】:

    mplus 可能更好,但这也应该可以:

    import Data.List
    import Data.Maybe 
    import Control.Monad 
    
    join $ find isJust [f x, g y, h z]
    

    【讨论】:

      【解决方案3】:

      我猜你的意思是:

      f,g,h:: a -> Maybe b
      

      使用 MonadPlus

      f x `mplus` g x `mplus` h x
      

      您可能想要使用 StateT Monad:

      function = runReaderT $ ReaderT f `mplus` ReaderT g `mplus` ReaderT h
      

      f,g,h 是 ReaderT a Maybe b(直到 ReaderT)

      或使用 msum:

      function = runReaderT $ msum $ map ReaderT [f,g,h]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-14
        • 2015-05-21
        • 2019-04-15
        相关资源
        最近更新 更多