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

Matthias Fischmann fis at wiwi.hu-berlin.de
Tue Oct 17 12:36:33 EDT 2006

```On Tue, Oct 17, 2006 at 01:21:38PM -0300, V?ctor A. Rodr?guez wrote:
> From: "Víctor A. Rodríguez" <victor at bit-man.com.ar>
> 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?

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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature