获取Either函数的Left元素

时间:2016-06-14 01:29:55

标签: haskell functional-programming

我在Haskell中有一个函数 getCorError :: a -> b -> Either c [Error], 是Error = Expected c
在另一个返回-> [Error]的函数上,如果[Expected (getCorError a b)]返回左边,我想返回getCorError,否则返回[错误]。 在执行[Expected (getCorError a b)]时出现类型错误。我尝试过很多东西,比如在胃肠道检查之前和之后写下左手,以及其他许多东西,我还没能做到这一点。 感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

如果我的问题正确,那么您要找的是either函数。

您应该能够像这样使用它:

either (return . Expected) id $ getCorError a b

当然,在定义ab的情况下 - 不可能给你更多的提示,因为你的问题在这里确实缺乏细节。

这种方式是这样的:

如果getCorError a b

  • 返回Left c,然后使用return (Expected c),此处的return只是\a -> [a](它位于列表monad中)
  • 返回Right errs,然后使用id errs