相关疑难解决方法(0)

使用连续的“要么/可能”时减少嵌套

这可能是一个非常基本的 Haskell 问题,但让我们假设以下函数签名

-- helper functions
getWeatherInfo :: Day -> IO (Either WeatherException WeatherInfo)
craftQuery :: WeatherInfo -> Either QueryException ModelQuery
makePrediction :: ModelQuery -> IO (Either ModelException ModelResult)
Run Code Online (Sandbox Code Playgroud)

将上述所有内容链接到一个predict day函数中的天真方法可能是:

predict :: Day -> IO (Maybe Prediction)
predict day = do
    weather <- getWeatherInfo day
    pure $ case weather of
        Left ex -> do
            log "could not get weather: " <> msg ex
            Nothing
        Right wi -> do
            let query = craftQuery wi
            case query of
                Left …
Run Code Online (Sandbox Code Playgroud)

monads haskell flatten either maybe

8
推荐指数
1
解决办法
138
查看次数

我如何处理多级缩进?

我正在编写一个具有非常复杂的循环的脚本:

main = do
    inFH <- openFile "..." ReadMode
    outFH <- openFile "..." WriteMode

    forM myList $ \ item ->
        ...
        if ... 
            then ...
            else do
                ...
                case ... of
                    Nothing -> ...
                    Just x  -> do
                        ...
                            ...
Run Code Online (Sandbox Code Playgroud)

代码很快就会飞到右边,所以我想把它分成几块,使用例如where子句.问题是,许多这些...包含读/写语句到两个把手inFHoutFH,使用where的语句将呈现这两个名字断章取义.我每次使用where语句时都必须发送这两个变量.

有没有更好的方法来解决这个问题?

haskell code-organization indentation

4
推荐指数
2
解决办法
296
查看次数

如何用ExceptT代替大量IO(要么ab)

我有一个连接到数据库然后运行查询的函数。每个步骤都会产生IO (Either SomeErrorType SomeResultType).

在学习 Haskell 时,我真正喜欢使用 monad 和类似 monad 的原因之一Either是能够使用 monad 函数(例如>>=和 组合器)mapLeft来简化对预期错误状态的大量处理。

通过阅读博客文章、Control.Monad.Trans文档和其他关于 SO 的答案,我的期望是我必须以某种方式使用转换器/电梯从一个IO上下文移动到另一个Either上下文。

这个答案特别好,但我正在努力将其应用到我自己的案例中。

我的代码的一个更简单的示例:

simpleVersion :: Integer -> Config -> IO ()
simpleVersion id c = 
  connect c >>= \case 
      (Left e)     -> printErrorAndExit e
      (Right conn) -> (run . query id $ conn)
              >>= \case 
                    (Left e)  -> printErrorAndExit e
                    (Right r) -> print r
                                   >> release conn
Run Code Online (Sandbox Code Playgroud)

ExceptT …

error-handling monads haskell monad-transformers

4
推荐指数
1
解决办法
1169
查看次数