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

Robert Dockins robdockins at fastmail.fm
Tue Oct 17 12:34:12 EDT 2006


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





More information about the Haskell-Cafe mailing list