如何测试我的 haskell 函数

发布于 2024-12-10 05:23:38 字数 441 浏览 0 评论 0原文

我刚刚开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

苦笑流年记忆 2024-12-17 05:23:38

其他人已经说过如何解决您眼前的问题,但为了进行测试,您应该使用 QuickCheck一些其他自动化测试库

import Test.QuickCheck
prop_5 = add 2 3 == 5
prop_leftIdentity n = add 0 n == n

然后在 Hugs 会话中运行 quickCheck prop_5quickCheck 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.

import Test.QuickCheck
prop_5 = add 2 3 == 5
prop_leftIdentity n = add 0 n == n

Then run quickCheck prop_5 and quickCheck 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?)

因为看清所以看轻 2024-12-17 05:23:38

最适合初学者的方法可能是 doctest 模块。
使用“cabal install doctest”下载它,然后将代码放入“Add.hs”文件中并从命令行运行“doctest Add.hs”。

您的代码应该如下所示,格式很重要:

module Add where

-- | add adds two numbers
--
-- >>> add 2 3
-- 5
-- >>> add 5 0
-- 5
-- >>> add 0 0
-- 0
add :: Integer -> Integer -> Integer
add a b = a+b

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:

module Add where

-- | add adds two numbers
--
-- >>> add 2 3
-- 5
-- >>> add 5 0
-- 5
-- >>> add 0 0
-- 0
add :: Integer -> Integer -> Integer
add a b = a+b

HTH Chris

温柔嚣张 2024-12-17 05:23:38

进行顶级定义:

add :: Integer -> Integer -> Integer
add a b = a + b

test1 = add 2 3

然后在 Hugs 会话中调用 test1。

Make a top level definition:

add :: Integer -> Integer -> Integer
add a b = a + b

test1 = add 2 3

Then call test1 in your Hugs session.

并安 2024-12-17 05:23:38

如何将函数调用与函数定义放在同一脚本中?我只想加载脚本并能够检查它是否符合我的预期...我不想一直手动输入它们。

简而言之,你不能。将其包装在函数中并调用它。您的文件充当有效的 Haskell 模块,并且具有“飞行”表达式并不是编写它的有效方法。

您似乎有脚本语言背景,但不要尝试将 Haskell 视为其中之一。

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.

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.

顾忌 2024-12-17 05:23:38

如果您安装了 ghc,则 runhaskell 命令将解释并运行文件中的 main 函数。

add x y = x + y
main = print $ add 2 3

然后在命令行上

> runhaskell Add.hs
5

不确定,但 Hugs 可能具有与 runhaskell 命令类似的功能。或者,如果将文件加载到 Hugs 解释器中,则只需调用 main 即可运行它。

If you have ghc installed, then the runhaskell command will interpret and run the main function in your file.

add x y = x + y
main = print $ add 2 3

Then on the command line

> runhaskell Add.hs
5

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 calling main.

黯然 2024-12-17 05:23:38

我试图做同样的事情,我只是做了一个函数来运行我的所有测试用例(使用警卫),如果它们全部通过,则返回 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文