这个问题在这里已经有了答案:
9年前关闭。
Possible Duplicate:
Outputting Haskell GHCi command results to a txt file
我是 Haskell 的新手,我正在尝试将测试用例输出结果重定向到文本文件。现在设置的方式是
AddAllTestCases.hs
包含我需要运行的所有测试用例,以便测试我创建的函数。我通过加载 AddAllTestCases.hs
在 GHCi 上运行测试用例然后只需输入 main
并点击进入。这会导致测试用例输出结果完美地打印在 GHCi 中。因为有数百个测试用例,我需要将输出结果重定向到文本文件。
尝试#1:
writeFile "myoutput.txt" $ show $ main
我收到以下错误:
No instance for (Show(IO())) arising from a use of show
在 CMD 中尝试 #2(尝试创建可执行文件,然后将可执行结果输出到文本文件):
ghc --make AddAllTests.hs -o testResults.exe
这给了我以下错误:
Warning: output was redirected with -o, but no output will be generated because there is no Min module
这很奇怪,因为当我使用 GHCi(尝试 #1)并输入
main
时它完美地执行了一切,我认为这意味着有一个主模块?我非常感谢将测试用例结果重定向到文本文件的任何帮助。
提前谢谢了!
最佳答案
您需要一个 Main
模块(和 main
操作)来生成可执行文件。您可以将您的模块重命名为 Main
,或者您可以指定要考虑的模块Main
在命令行上,
ghc --make -main-is AddAllTests AddAllTests.hs -o testResults.exe
生成没有名为
Main
的模块的可执行文件.没有编译的方法是
ghc AddAllTests.hs -e "main" > testResults.txt
另一种方法是创建一个文件,在其中列出所有测试用例,
3 + 2 :: Rational
reverse "foobar"
:q
并运行
ghci
带有重定向的输入和输出ghci < testCases > testResults.txt
关于haskell - 将 Haskell GHCi 输出重定向到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14688119/