haskell - 递归使用 Either
问题描述
所以我在下面定义了一个这样的函数:
myFunction :: String -> Either String MyType
我正在阅读字符串直到最后。Left
用于处理错误。Right
假设把元组还给我。在myFunction
我打电话anotherFunction
并检查它是否返回Left
或Right
像这样:
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
再次递归调用而不会丢失v1
and v2
。我怎样才能通过递归来实现呢?
这是 MyType 的确切定义:
data MyType = MyString String | MyInt Int | MyMap [(String, MyType)] deriving (Show, Eq)
解决方案
推荐阅读
- php - Nginx 代理 PHP-FPM + Lighttpd 405 不允许 symfony
- asp.net - 如何在页面运行 ASP.NET 时存储值?
- haskell - 以 Nat 形式检索有界枚举的大小
- ruby-on-rails - 在 Rails 中定义枚举值时出错
- angular6 - 安装 Angular 的 CDK 后在编译过程中出错
- segment - 为什么 Druid 段在数据摄取后变得不可用
- websocket - 如何为 Hanami 测试 WebSockets?
- symfony - Symfony:从未调用过自定义验证器
- r - 通过逐元素向量比较找到最小差异和对应值的最佳方法是什么?
- java - 日志写入方法中的 Sonarlint 投诉