loops - Haskell 的 EitherT 发生了什么?

标签 loops haskell monad-transformers either

当阅读一篇名为 Breaking from a Loop 的旧博客文章(2012 年)时由 Gabriel Gonzalez 撰写,很明显,备受关注的 EitherT 已经以某种方式离开了生态系统。 EitherT package声明它已被弃用,取而代之的是 either - 实际上它根本不关心 monad 转换器。

我注意到有人试图清理大约 10 年前就已经存在的错误处理困惑情况。我的猜测是不再需要EitherT

我认为博客文章中提出的循环中断方法非常简洁。所以我想知道:今天 EitherT 的替代品是什么。我认为,出于这个目的,对 ErrorT 的批评仍然存在,并且 ContT 对于这个小问题来说确实是相当沉重的机器。

以下是博客文章中讨论的惯用语,供引用:

import Control.Monad.Transfomers.EitherT

exit = left 

main = runEitherT $ forever $ do
    str <- lift getLine
    when (str == "exit") $ exit ()

(顺便说一下,同一作者的 Control.Break 规避了整个 EitherTErrorTContT除了T一团糟。)

最佳答案

今天 EitherT 的替代品是 ExceptT:

import Control.Monad.Except

-- use `throwError` in place of `left`
exit = throwError

-- use `runExceptT` in place of `runEitherT`
main = runExceptT $ forever $ do
    str <- lift getLine
    when (str == "exit") $ exit ()

关于loops - Haskell 的 EitherT 发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63654252/

相关文章:

haskell - 简化 Haskell 类型签名

haskell - 参数化 Maybe monad

haskell - 如何修复 MonadReader 和 MonadIO 上约束的函数缺少的 IO 实例?

python - 循环控制,哪个更高效

c - fgetc 没有停止我的循环

javascript - 二维数组求和

haskell - StateT s (ExceptT e m) 和 exceptT e (StateT s m) 有什么区别?

mysql - 循环处理查询中的每一行

haskell - 在 Data.Array.Unboxed 中使用 newtype 和 ghc 7.10

haskell - 摇一摇: how do I depend on dynamically generated source files?