将Haskell GHCi输出重定向到文本文件 [英] Redirecting Haskell GHCi output to text file

查看:106
本文介绍了将Haskell GHCi输出重定向到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

将Haskell GHCi命令结果输出到txt文件

我是Haskell的新手,我正在尝试将测试用例的输出结果重定向到文本文件。现在,它的设置方式是 AddAllTestCases.hs 包含我需要运行的所有测试用例,以测试我创建的功能。我通过加载 AddAllTestCases.hs 在GHCi上运行测试用例,然后只需键入 main 并按Enter。这会导致测试用例输出结果完美地打印在GHCi内部。

I am new to Haskell and I am trying to redirect test cases output results to a text file. The way it is set up now, is a AddAllTestCases.hs contains all the test cases I need to run in order to test a function I created. I run the test cases on GHCi by loading AddAllTestCases.hs and then simply typing main and hitting enter. That causes test case output results to print inside the GHCi perfectly.

由于存在数百个测试用例,因此需要将输出结果重定向到文本文件。

Because there hundreds of test cases, I need to redirect output results to text file.

尝试#1:

writeFile "myoutput.txt" $ show $ main

我收到以下错误:

没有(Show(IO()))的实例因使用表演而产生

尝试# 2在CMD中(尝试创建可执行文件,然后将可执行结果输出到文本文件中):

Attempt #2 in CMD (trying to create an executable, then outputting executable results to text file):

ghc --make AddAllTests.hs -o testResults .exe

这给了我以下错误:

警告:使用-o重定向了输出,但由于没有Min模块,因此不会生成任何输出

这很奇怪,因为当我正在使用GHCi(尝试#1),然后键入 main ,它可以完美地执行所有操作,我认为这意味着存在一个主模块?

This is weird because when I am using GHCi (attempt #1) and I type in main it executes everything perfectly, which I would assume, implies that there is a main module?

非常感谢将测试用例结果重定向到文本文件的任何帮助。

I greatly appreciate any help with redirecting test case results to a text file.

非常感谢!

推荐答案

您需要一个 Main 模块(和一个 main 操作)来生成可执行文件。您可以将模块重命名为 Main ,也可以在命令行上将模块指定为 Main

You need a Main module (and a main action) to produce an executable. You can rename your module to Main, or you can specify the module to be considered Main on the command line,

ghc --make -main-is AddAllTests AddAllTests.hs -o testResults.exe

生成没有名为 Main 的模块的可执行文件。

to produce an executable without a module named Main.

没有编译的方法将是

ghc AddAllTests.hs -e "main" > testResults.txt

另一种方法是拥有一个只列出所有测试用例的文件,

Another method would be to have a file in which you just list all test cases,

3 + 2 :: Rational
reverse "foobar"
:q

并运行 ghci 并重定向输入和输出

and run ghci with redirected in- and output

ghci < testCases > testResults.txt

这篇关于将Haskell GHCi输出重定向到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆