提示 runStmt 重定向时无输出

问题描述 投票:0回答:1

跟随this链接并尝试

stack run
此模块:

module Main where

import Language.Haskell.Interpreter

main :: IO ()
main = do
  _ <- runInterpreter
     $ setImports ["Prelude"]
    >> runStmt "x <- pure 42"
    >> runStmt "print x"
  return ()

从命令行重定向时没有输出:

user@localhost ~/hs-project $ stack run > 42.txt

我只能猜测 - 与上述帖子的答案不同 - hint 的输出通过管道,因为“

print
”在解释器内部运行,不是来自模块。

haskell io-redirection haskell-stack hint
1个回答
0
投票

尝试让解释器在退出之前刷新其输出:

_ <- runInterpreter
   $ setImports ["Prelude", "System.IO"]
   >> runStmt "x <- pure 42"
   >> runStmt "print x"
   >> runStmt "hFlush stdout"

或在生成输出之前关闭缓冲:

_ <- runInterpreter
   $ setImports ["Prelude", "System.IO"]
   >> runStmt "hSetBuffering stdout NoBuffering"
   >> runStmt "x <- pure 42"
   >> runStmt "print x"

这两种方法似乎都适合我。

© www.soinside.com 2019 - 2024. All rights reserved.