Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test my haskell functions

Tags:

haskell

hugs

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.

like image 390
mort Avatar asked Oct 13 '11 08:10

mort


1 Answers

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?)

like image 50
dave4420 Avatar answered Oct 04 '22 21:10

dave4420