如何测试我的 haskell 函数
我刚刚开始使用 Haskell,并尝试先编写一些测试。基本上,我想定义一些函数,然后调用该函数来检查行为。
add :: Integer -> Integer -> Integer
add a b = a+b
-- Test my function
add 2 3
如果我在 Hugs98 中加载那个小脚本,则会出现以下错误:
Syntax error in declaration (unexpected `}', possibly due to bad layout)
如果我删除最后一行,加载脚本,然后在 Hugs 解释器中输入“add 2 3”,它就可以正常工作。
所以问题是:如何将函数的调用与函数定义放在同一脚本中?我只想加载脚本并能够检查它是否符合我的预期...我不想一直手动输入它们。
I just started with Haskell and tried to do write some tests first. Basically, I want to define some function and than call this function to check the behavior.
add :: Integer -> Integer -> Integer
add a b = a+b
-- Test my function
add 2 3
If I load that little script in Hugs98, I get the following error:
Syntax error in declaration (unexpected `}', possibly due to bad layout)
If I remove the last line, load the script and then type in "add 2 3" in the hugs interpreter, it works just fine.
So the question is: How can I put calls of my functions in the same script as the function definition? I just want to load the script and be able to check if it does what I expect it to...I don't want to type them in manually all the time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
其他人已经说过如何解决您眼前的问题,但为了进行测试,您应该使用 QuickCheck 或 一些其他自动化测试库。
然后在 Hugs 会话中运行
quickCheck prop_5
和quickCheck prop_leftIdentity
。 QuickCheck 的功能远不止于此,但这将帮助您入门。(这里有一个 QuickCheck 教程,但它已经过时了。有人知道涵盖 QuickCheck 2 的教程吗?)
Others have said how to solve your immediate problem, but for testing you should be using QuickCheck or some other automated testing library.
Then run
quickCheck prop_5
andquickCheck prop_leftIdentity
in your Hugs session. QuickCheck can do a lot more than this, but that will get you started.(Here's a QuickCheck tutorial but it's out of date. Anyone know of one that covers QuickCheck 2?)
最适合初学者的方法可能是 doctest 模块。
使用“cabal install doctest”下载它,然后将代码放入“Add.hs”文件中并从命令行运行“doctest Add.hs”。
您的代码应该如下所示,格式很重要:
HTH Chris
the most beginner friendly way is probably the doctest module.
Download it with "cabal install doctest", then put your code into a file "Add.hs" and run "doctest Add.hs" from the command line.
Your code should look like this, the formatting is important:
HTH Chris
进行顶级定义:
然后在 Hugs 会话中调用 test1。
Make a top level definition:
Then call test1 in your Hugs session.
简而言之,你不能。将其包装在函数中并调用它。您的文件充当有效的 Haskell 模块,并且具有“飞行”表达式并不是编写它的有效方法。
您似乎有脚本语言背景,但不要尝试将 Haskell 视为其中之一。
In short, you can't. Wrap it in a function and call it instead. Your file serves as a valid Haskell module, and having "flying" expression is not a valid way to write it.
You seem to come from a scripting language background, but don't try treating Haskell as one of them.
如果您安装了 ghc,则
runhaskell
命令将解释并运行文件中的main
函数。然后在命令行上
不确定,但 Hugs 可能具有与 runhaskell 命令类似的功能。或者,如果将文件加载到 Hugs 解释器中,则只需调用
main
即可运行它。If you have ghc installed, then the
runhaskell
command will interpret and run themain
function in your file.Then on the command line
Not sure, but hugs probably has a similar feature to the
runhaskell
command. Or, if you load the file into the hugs interpreter, you can simply run it by callingmain
.我试图做同样的事情,我只是做了一个函数来运行我的所有测试用例(使用警卫),如果它们全部通过,则返回 1,如果失败则抛出错误。
test :: Num b => ; a->b
测试 x
| sumALL [1] /= 1 = 错误“测试失败”
| sumALL [0,1,2] /= 3 = 错误“测试失败”
...
|否则 = 1
I was trying to do the same thing and I just made a function that ran through all my test cases (using guards) and returned 1 if they all passed and threw an error if any failed.
test :: Num b => a->b
test x
| sumALL [1] /= 1 = error "test failed"
| sumALL [0,1,2] /= 3 = error "test failed"
...
| otherwise = 1