跟随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
”在解释器内部运行,不是来自模块。
尝试让解释器在退出之前刷新其输出:
_ <- 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"
这两种方法似乎都适合我。