如何从Either获得价值?

时间:2017-10-26 01:15:43

标签: haskell

我在ghci中有这个代码,尝试解码base64代码:

let a=pack "MTExMTEx"
let b=decode a
:t b
b :: Either String ByteString

那么如何从任何一个获取解码字节串?是否有一些像Maybe的那样的函数来自于Just?我找不到它,谢谢。

3 个答案:

答案 0 :(得分:6)

使用case

case decode a of
  Left err -> {- what to do if decode gave an error -}
  Right msg -> {- what to do if decode succeeded -}

Alexandre建议的either函数与此基本相同,两个分支仅作为函数;即它相当于写:

either 
  (\err -> {- what to do if decode gave an error -}) 
  (\msg -> {- what to do if decode succeeded -}) 
  (decode a)

答案 1 :(得分:5)

您可以使用while (answer == "yes") { ... } if (customers >= 5) cout<<"\nWell that was a good day! I had " <<customers <<" customer<s> today. Tomorrow is another day ..." << endl; else cout<<"\nWell that was a poor day! I had " <<customers <<" customer<s> today. Tomorrow is another day ..." << endl; 中的either功能。

它的签名是:

Data.Either

这意味着它需要两个函数作为输入:第一个应用,如果它是either :: (a -> c) -> (b -> c) -> Either a b -> c ,第二个应用,如果它是Left。第三个参数是您的Right数据类型。请注意,两个函数的返回值的类型必须相同。

答案 2 :(得分:2)

您正在寻找具有类型签名的ids= []; selChk(val) { this.ids.push(val); console.log(this.ids); } fromRight

Data.Either

第一个值是默认值(如果您有fromRight :: b -> Either a b -> b 而不是Left,则会获得什么。

Right