从Either升级到IO

时间:2015-02-22 16:19:58

标签: haskell monads

我想从Ead转换为IO而没有来自Either的任何线索。是否有更优雅的方式来做到这一点?

我已经编写了这个函数,但我想使用库中的东西或Prelude:

liftEither :: forall t (m :: * -> *) a. Monad m => (t -> m a) -> Either String t -> m a
liftEither f (Right a)  = f a
liftEither _ (Left msg) = fail msg

1 个答案:

答案 0 :(得分:5)

使用either中的Data.Either函数:

这样的内容
liftEither :: Monad m => (t -> m a) -> Either String t -> m a
liftEither f xs = either fail f xs

甚至更简单:

liftEither :: Monad m => (t -> m a) -> Either String t -> m a
liftEither = either fail