【发布时间】:2020-01-18 07:14:29
【问题描述】:
我确定我在这里遗漏了一些非常明显的东西。以下是我试图在概念层面实现的目标:
action1 :: (MonadIO m) => m [a]
action1 = pure []
action2 :: (MonadIO m) => m [a]
action2 = pure [1, 2, 3]
action3 :: (MonadIO m) => m [a]
action3 = error "should not get evaluated"
someCombinator [action1, action2, action3] == m [1, 2, 3]
这个假设的someCombinator 存在吗?我尝试过使用<|> 和msum,但无法得到我想要的。
我想,这可以概括为两种方式:
-- Will return the first monadic value that is NOT an mempty
-- (should NOT blindly execute all monadic actions)
-- This is something like the msum function
someCombinator :: (Monoid a, Monad m, Traversable t, Eq a) => t m a -> m a
-- OR
-- this is something like the <|> operator
someCombinator :: (Monad m, Alternative f) => m f a -> m f a -> m f a
【问题讨论】:
-
严格来说,
Monoid a不足以测试给定值是否为“空”。您还需要Eq a,以便与mempty进行比较。 -
@FyodorSoikin 感谢您的提示。更新了问题中的代码 sn-p。
标签: haskell monads alternative-functor