[Haskell-cafe] Newbie and working with IO Int and Int

Seth Gordon sethg at ropine.com
Tue Oct 17 12:30:32 EDT 2006

Víctor A. Rodríguez wrote:
> Hi all,
> I'm really newbie to Haskell, and working on a program I'm trying to make
> some testing.
> I make some test on certain know values ( e.g. adding 10 to 15 must return
> 25) and some test on random values (eg. adding rnd1 to rnd2 must return
> rnd1+rnd2).
> The problem that makes me mad is the random number generation. I can obtain
> random numbers through module Random but all of them return IO Int values
> (all I need are Ints) instead of Int.
> I know that I can adjust my own functions to use IO Int instead of Int but
> the call to certain functions must contain Int parameters, because these
> ones can't be changed to accept IO Int (I read
> http://haskell.org/hawiki/ThatAnnoyingIoType and know that can convert from
> IO Int to Int :-P).

What's wrong with doing it this way?


verifyAdd :: Int -> Int -> Int -> Bool
verifyAdd a b sum | a + b == sum = True
                  otherwise    = False

testAddMundane :: Int -> Int -> Bool
testAddMundane a b = verifyAdd a b (a + b)

-- all the IO-dependent stuff is below this line --

testAddRandom :: IO Bool
testAddRandom = do a <- randomIO
                   b <- randomIO
                   return verifyAdd a b (a + b)

More information about the Haskell-Cafe mailing list