首页 > 解决方案 > 递归使用 Either

问题描述

所以我在下面定义了一个这样的函数:

myFunction :: String -> Either String MyType

我正在阅读字符串直到最后。Left用于处理错误。Right假设把元组还给我。在myFunction我打电话anotherFunction并检查它是否返回LeftRight像这样:

myFunction :: String -> Either String MyType
myFunction "" = Right -- want to return MyType here
myFunction s = 
     case anotherFunction s of
         Left c -> Left c
         Right (v1, v2, t) -> -- want to call myFunction again with t

anotherFunction返回时,Right我想myFunction再次递归调用而不会丢失v1and v2。我怎样才能通过递归来实现呢?

这是 MyType 的确切定义:

data MyType = MyString String | MyInt Int | MyMap [(String, MyType)] deriving (Show, Eq)

标签: haskellrecursionpattern-matchingeither

解决方案


推荐阅读