Newbie and working with IO Int and Int

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). How can I deal with this problem ?? Thanks in advance. -- Víctor A. Rodríguez (http://www.bit-man.com.ar) El bit Fantasma (Bit-Man) Perl Mongers Capital Federal (http://cafe.pm.org/) GNU/Linux User Group - FCEyN - UBA (http://glugcen.dc.uba.ar/)

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? -- ** UNTESTED CODE ** 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)

On Oct 17, 2006, at 12:21 PM, 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).
Probably the best way to deal with this is to use the QuickCheck library. http://www.cs.chalmers.se/~rjmh/QuickCheck/ It makes this sort of thing fairly painless, because you don't have to muck about with generating random data manually.
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).
How can I deal with this problem ??
See: http://www.haskell.org/ghc/dist/current/docs/libraries/base/ System-Random.html If you use 'getStdGen' or 'newStdGen' (which are in the IO monad), then you can later use the pure functions 'random', 'randomR' and friends. Alternately, you can manually seed the PRNG with 'mkStdGen' and avoid the IO monad altogether.
Thanks in advance. -- Víctor A. Rodríguez (http://www.bit-man.com.ar) El bit Fantasma (Bit-Man) Perl Mongers Capital Federal (http://cafe.pm.org/) GNU/Linux User Group - FCEyN - UBA (http://glugcen.dc.uba.ar/)
Rob Dockins Speak softly and drive a Sherman tank. Laugh hard; it's a long way to the bank. -- TMBG

On Tue, Oct 17, 2006 at 01:21:38PM -0300, V?ctor A. Rodr?guez wrote:
To: haskell-cafe@haskell.org From: "Víctor A. Rodríguez"
Date: Tue, 17 Oct 2006 13:21:38 -0300 Subject: [Haskell-cafe] Newbie and working with IO Int and Int 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).
How can I deal with this problem ??
you should probably keep reading on. (-: have you seen the stuff on the new wiki? http://www.haskell.org/haskellwiki/Books_and_tutorials#Using_monads short answer: the type IO Int describes computations that yield integers, and in order to get to the Int you need to run the computation. if you have a pure function f and want to feed it with random values, you need to do something like: randomIO >>= \ x -> randomIO >>= \ y -> return (f x y) the complete expression has type IO Int again. don't try to strip off that IO; there are ways, but they don't teach you how to do it right. hth, m.
participants (4)
-
Matthias Fischmann
-
Robert Dockins
-
Seth Gordon
-
Víctor A. Rodríguez