Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Haskell-cafe] Convert IO Int to Int

1,737 views
Skip to first unread message

ptrash

unread,
Jun 9, 2009, 6:29:19 AM6/9/09
to haskel...@haskell.org

Hi,

I am using the System.Random method randomRIO. How can I convert its output
to an Int?

Thanks...
--
View this message in context: http://www.nabble.com/Convert-IO-Int-to-Int-tp23940249p23940249.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Tobias Olausson

unread,
Jun 9, 2009, 6:33:29 AM6/9/09
to ptrash, haskel...@haskell.org
You can not convert an IO Int to Int, or at least, you shouldn't.
However, you can do as follows:

test :: IO ()
test = do
int <- randomRIO -- or whatever it is called
print $ useInt int

useInt :: Int -> Int
useInt x = x+10

//Tobias

2009/6/9 ptrash <ptr...@web.de>:

--
Tobias Olausson
tob...@gmail.com

Jochem Berndsen

unread,
Jun 9, 2009, 6:34:07 AM6/9/09
to ptrash, haskel...@haskell.org
ptrash wrote:
> Hi,
>
> I am using the System.Random method randomRIO. How can I convert its output
> to an Int?
>
> Thanks...

You cannot [1], you should read up on monads and I/O in Haskell, for example
http://haskell.org/haskellwiki/IO_inside

[1] Yes, you can, but no, you don't want to.

Regards,

--
Jochem Berndsen | joc...@functor.nl
GPG: 0xE6FABFAB

Yusaku Hashimoto

unread,
Jun 9, 2009, 7:21:17 AM6/9/09
to ptrash, haskel...@haskell.org

On 2009/06/09, at 19:33, Tobias Olausson wrote:

> You can not convert an IO Int to Int, or at least, you shouldn't.
> However, you can do as follows:
>
> test :: IO ()
> test = do
> int <- randomRIO -- or whatever it is called
> print $ useInt int
>
> useInt :: Int -> Int
> useInt x = x+10

Or, you can lift pure function into IO. the below test' function
almost same as above test function. (But I used randomIO instead of
randomRIO because it seemed to be a typo :-)

test' = print =<< fmap useInt randomIO

I think it is more handy than using do notation, when you want to do
something simple with monads. And converting IO Int to IO anything is
much easier and safer than converting IO Int to Int.

ghci> :m +System.Random Data.Char
ghci> :t fmap (+1) randomIO
fmap (+1) randomIO :: (Num a, Random a) => IO a
ghci> :t fmap show randomIO
fmap show randomIO :: IO String
ghci> :t fmap chr randomIO
fmap Data.Char.chr randomIO :: IO Char
ghci> :t fmap (+) randomIO
fmap (+) randomIO :: (Num a, Random a) => IO (a -> a)

Thanks,
Hashimoto

ptrash

unread,
Jun 9, 2009, 9:00:26 AM6/9/09
to haskel...@haskell.org

Ok, thanks for the information.
--
View this message in context: http://www.nabble.com/Convert-IO-Int-to-Int-tp23940249p23942344.html

ptrash

unread,
Jun 9, 2009, 9:52:52 AM6/9/09
to haskel...@haskell.org

Hmm...it am not getting through it. I just want to generate a random number
and then compare it with other numbers. Something like

r = randomRIO (1, 10)
if (r > 5) then... else ...
--
View this message in context: http://www.nabble.com/Convert-IO-Int-to-Int-tp23940249p23943301.html

Magnus Therning

unread,
Jun 9, 2009, 9:57:43 AM6/9/09
to ptrash, haskel...@haskell.org
On Tue, Jun 9, 2009 at 2:52 PM, ptrash<ptr...@web.de> wrote:
>
> Hmm...it am not getting through it. I just want to generate a random number
> and then compare it with other numbers. Something like
>
> r = randomRIO (1, 10)
> if (r > 5) then... else ...

You have to do it inside the IO monad, something like

myFunc = do
r <- randomRIO (1, 10
if r > 5
then ...
else ...

/M

--
Magnus Therning (OpenPGP: 0xAB4DFBA4)
magnus@therning.org Jabber: magnus@therning.org
http://therning.org/magnus identi.ca|twitter: magthe

Henning Thielemann

unread,
Jun 9, 2009, 10:06:19 AM6/9/09
to ptrash, haskel...@haskell.org

On Tue, 9 Jun 2009, ptrash wrote:

> I am using the System.Random method randomRIO. How can I convert its output
> to an Int?

in general:
http://haskell.org/haskellwiki/How_to_get_rid_of_IO

about randomIO:
http://haskell.org/haskellwiki/Avoiding_IO#State_monad

Daniel Fischer

unread,
Jun 9, 2009, 10:15:49 AM6/9/09
to haskel...@haskell.org
Am Dienstag 09 Juni 2009 15:57:24 schrieb Magnus Therning:
> On Tue, Jun 9, 2009 at 2:52 PM, ptrash<ptr...@web.de> wrote:
> > Hmm...it am not getting through it. I just want to generate a random
> > number and then compare it with other numbers. Something like
> >
> > r = randomRIO (1, 10)
> > if (r > 5) then... else ...
>
> You have to do it inside the IO monad, something like
>
> myFunc = do
> r <- randomRIO (1, 10
> if r > 5
> then ...
> else ...
>
> /M

Or make the source of the pseudo-random numbers explicit:

import System.Random

function :: (RandomGen g, Random a) => g -> other args -> result
function gen whatever
| r > 5 = blah newgen something
| r < 3 = blub newgen somethingElse
| otherwise = bling
where
(r,newgen) = randomR (lo,hi) gen

and finally, when the programme is run:

main = do
args <- getArgs
sg <- getStdGen
foo <- thisNThat
print $ function sg foo

If you're doing much with random generators, wrap it in a State monad.

Krzysztof Skrzętnicki

unread,
Jun 9, 2009, 12:05:12 PM6/9/09
to Daniel Fischer, haskel...@haskell.org

To avoid reinventing the wheel one can use excellent package available
on Hackage:
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/MonadRandom

> The die function simulates the roll of a die, picking a number between 1 and 6, inclusive, and returning it in the Rand monad.
> Notice that this code will work with any source of random numbers g.
>
> die :: (RandomGen g) => Rand g Int
> die = getRandomR (1,6)
>
> The dice function uses replicate and sequence to simulate the roll of n dice.
>
> dice :: (RandomGen g) => Int -> Rand g [Int]
> dice n = sequence (replicate n die)
>
> To extract a value from the Rand monad, we can can use evalRandIO.
>
> main = do
> values <- evalRandIO (dice 2)
> putStrLn (show values)

Best regards

Krzysztof Skrzętnicki

jerzy.kar...@info.unicaen.fr

unread,
Jun 9, 2009, 12:23:24 PM6/9/09
to haskel...@haskell.org
Magnus Therning writes:

> ptrash wrote:

>> ...am not getting through it. I just want to generate a random number


>> and then compare it with other numbers. Something like
>>
>> r = randomRIO (1, 10)
>> if (r > 5) then... else ...
>
> You have to do it inside the IO monad, something like
>
> myFunc = do
> r <- randomRIO (1, 10

This may continue forever...
With nice references to monads, to Unsafe@#*!, etc. ...

We may say, as many tutorials do : "this is not what you want!" (which I
hate ; you are not my conscience, whoever you are...), or just give some
code, not always readable...

Perhaps I belong to a minority here, but I strongly believe that at
THIS level, the first thing to do - unless I am dead wrong - is to explain
to our friend ptrash (who could find a less gothic pseudo) that in a pure
functional programming, the construction
r = whatEver(par1,par2)
being a function call, cannot give "just a random number", something which
is not (intuitively) determined, and changes with every call, despite the
constancy of the arguments.

For most of us, acquainted with the stuff, it becomes trivial, but if
somebody doesn't know that a classical pseudo-random generator modifies a
"seed", and in such a way involves a "side effect", then sending him to the
monadic heaven is dangerous.

Please, tell him first about random streams, which he can handle without
IO. Or, about ergodic functions (hashing contraptions which transform ANY
parameter into something unrecognizable). When he says : "I know all that",
THEN hurt him badly with monads.

Jerzy Karczmarczuk

Bulat Ziganshin

unread,
Jun 9, 2009, 1:37:38 PM6/9/09
to jerzy.kar...@info.unicaen.fr, haskel...@haskell.org
Hello jerzy,

Tuesday, June 9, 2009, 8:23:04 PM, you wrote:

> Please, tell him first about random streams, which he can handle without
> IO. Or, about ergodic functions (hashing contraptions which transform ANY
> parameter into something unrecognizable). When he says : "I know all that",
> THEN hurt him badly with monads.

i think that for someone coming from imperative programming teeling
about IO monad is the easiest way. and then he will learn how to do it
FP way


--
Best regards,
Bulat mailto:Bulat.Z...@gmail.com

Jorge Branco Branco Aires

unread,
Jun 9, 2009, 3:15:52 PM6/9/09
to Bulat Ziganshin, jerzy.kar...@info.unicaen.fr, haskel...@haskell.org
Bulat Ziganshin wrote:
> Hello jerzy,
>
> Tuesday, June 9, 2009, 8:23:04 PM, you wrote:
>
>
>> Please, tell him first about random streams, which he can handle without
>> IO. Or, about ergodic functions (hashing contraptions which transform ANY
>> parameter into something unrecognizable). When he says : "I know all that",
>> THEN hurt him badly with monads.
>>
>
> i think that for someone coming from imperative programming teeling
> about IO monad is the easiest way. and then he will learn how to do it
> FP way
>
I came from a imperative programming background. I didn't feel like this
help me at all back then. At least in the beginning you want to detach
yourself from an imperative style, not try to simulate it with some
weird structure that you don't really understand.

More generally I really wish IO hadn't been the first Monad I played
with. It's so close to a Functor, yet in my mind Functors were simple,
just structures that could be mapped, and Monads were these mysterious
things that allowed you to get away with side effects and that once you
were inside you could never get out.

Jorge

Luke Palmer

unread,
Jun 9, 2009, 3:46:53 PM6/9/09
to Krzysztof Skrzętnicki, haskel...@haskell.org
2009/6/9 Krzysztof Skrz�tnicki <gte...@gmail.com>

> On Tue, Jun 9, 2009 at 16:14, Daniel Fischer<daniel.i...@web.de>
> wrote:
> > If you're doing much with random generators, wrap it in a State monad.
>
> To avoid reinventing the wheel one can use excellent package available
> on Hackage:
> http://hackage.haskell.org/cgi-bin/hackage-scripts/package/MonadRandom


Please do! Prefer MonadRandom to explicit generator passing:
http://lukepalmer.wordpress.com/2009/01/17/use-monadrandom/. Keep
computations in MonadRandom, and pull them out with evalRandomIO at the last
second.

Luke


> <http://hackage.haskell.org/cgi-bin/hackage-scripts/package/MonadRandom>
>
> > The die function simulates the roll of a die, picking a number between 1
> and 6, inclusive, and returning it in the Rand monad.
> > Notice that this code will work with any source of random numbers g.
> >
> > die :: (RandomGen g) => Rand g Int
> > die = getRandomR (1,6)
> >
> > The dice function uses replicate and sequence to simulate the roll of n
> dice.
> >
> > dice :: (RandomGen g) => Int -> Rand g [Int]
> > dice n = sequence (replicate n die)
> >
> > To extract a value from the Rand monad, we can can use evalRandIO.
> >
> > main = do
> > values <- evalRandIO (dice 2)
> > putStrLn (show values)
>
> Best regards
>

> Krzysztof Skrz�tnicki

Henning Thielemann

unread,
Jun 10, 2009, 5:12:06 AM6/10/09
to Bulat Ziganshin, jerzy.kar...@info.unicaen.fr, haskel...@haskell.org

On Tue, 9 Jun 2009, Bulat Ziganshin wrote:

> Hello jerzy,
>
> Tuesday, June 9, 2009, 8:23:04 PM, you wrote:
>
>> Please, tell him first about random streams, which he can handle without
>> IO. Or, about ergodic functions (hashing contraptions which transform ANY
>> parameter into something unrecognizable). When he says : "I know all that",
>> THEN hurt him badly with monads.
>
> i think that for someone coming from imperative programming teeling
> about IO monad is the easiest way. and then he will learn how to do it
> FP way

I came from imperative programming and never wanted to use randomIO,
because it forces you to IO and randomsIO is not lazy.

ptrash

unread,
Jun 10, 2009, 7:56:26 AM6/10/09
to haskel...@haskell.org

Hi,

I have tried on the console to write

x <- randomRIO(1,10)
:t x

Everythings fine and the type of x is
x :: Integer

Now I have tried to write a Method which gives me a Number of random numbers
the same way but it doesn't work.

randomList :: Int -> [Integer]
randomList 0 = []
randomList n = do
r <- randomRIO (1, 10)
r:randomList(n-1)

It says Couldn't match expected type `IO t' against inferred type `[t]'
r <- randomRIO (1,10) causes an error. But why does it work on the console?
Is there a way to solve it another way?
--
View this message in context: http://www.nabble.com/Convert-IO-Int-to-Int-tp23940249p23960652.html

Miguel Mitrofanov

unread,
Jun 10, 2009, 7:59:36 AM6/10/09
to ptrash, haskel...@haskell.org
"r <- randomRIO (1,10)" is NOT the source of error. Why do you think it is?

ptrash wrote on 10.06.2009 15:55:
> Hi,
>
> I have tried on the console to write
>
> x <- randomRIO(1,10)
> :t x
>
> Everythings fine and the type of x is
> x :: Integer
>
> Now I have tried to write a Method which gives me a Number of random numbers
> the same way but it doesn't work.
>
> randomList :: Int -> [Integer]
> randomList 0 = []
> randomList n = do
> r <- randomRIO (1, 10)
> r:randomList(n-1)
>
> It says Couldn't match expected type `IO t' against inferred type `[t]'
> r <- randomRIO (1,10) causes an error. But why does it work on the console?
> Is there a way to solve it another way?

ptrash

unread,
Jun 10, 2009, 8:05:26 AM6/10/09
to haskel...@haskell.org

Hmm...I use the Eclipse Plugin. And this row is marked as error. Then where
is the error?
--
View this message in context: http://www.nabble.com/Convert-IO-Int-to-Int-tp23940249p23960827.html

Iain Barnett

unread,
Jun 10, 2009, 8:08:52 AM6/10/09
to haskel...@haskell.org
On 10 Jun 2009, at 12:55 pm, ptrash wrote:

>
>
> Now I have tried to write a Method which gives me a Number of
> random numbers
> the same way but it doesn't work.
>
> randomList :: Int -> [Integer]
> randomList 0 = []
> randomList n = do
> r <- randomRIO (1, 10)
> r:randomList(n-1)
>
> It says Couldn't match expected type `IO t' against inferred type
> `[t]'
> r <- randomRIO (1,10) causes an error. But why does it work on the
> console?
> Is there a way to solve it another way?
>

I had the same problem a while back, the thread is here
http://www.mail-archive.com/haskel...@haskell.org/msg46194.html

the console uses IO already, so it's not a problem there. I ended up
learning about the >>= operator, and that helped a lot. Anyway, lots
of helpful links in that mail thread.

Iain

Sebastian Sylvan

unread,
Jun 10, 2009, 9:08:48 AM6/10/09
to ptrash, haskel...@haskell.org
On Wed, Jun 10, 2009 at 12:55 PM, ptrash <ptr...@web.de> wrote:

>
> Hi,
>
> I have tried on the console to write
>
> x <- randomRIO(1,10)
> :t x
>
> Everythings fine and the type of x is
> x :: Integer
>

The type of x *in the context of an IO computation* is Integer. GHCi is
basically an IO computation.
Another example:

foo :: Integer -> Integer
foo x = x+1

main :: IO ()
main = do
x <- randomRIO (1,10)
print (foo x)

This is fine. In the context of the IO computation "main", x is bound to the
result of "randomRIO (1,10)", and you can pass it to functions expecting
Integer values (not IO Integer!). So in this way, and this way only, you can
access the Integer returned by an IO action. You can *not* access the
Integer returned by an IO action from within a normal function, *only* by by
binding it to a variable (with "<-") inside *another IO action*.

I'm not sure what text you're using to learn Haskell, but a very basic and
fundamental property of Haskell (and indeed 99% of why it's cool, IMO) is
that code which does side effects (like reading from a global random number
seed), and code which does not do side effects (i.e. functions which always
return the same result given the same input) are kept extremely separate.
This appears to be the source of your confusion. It's simply not possible to
do side effect inside a normal function, just like it's not possible to cast
an arbitrary integer to a pointer in Java - the language is designed to not
require it, and the benefits of being able to trust that your program obeys
certain properties are worth it.

>randomList :: Int -> [Integer]
>randomList 0 = []
>randomList n = do
> r <- randomRIO (1, 10)
> r:randomList(n-1)

In this code you're trying to do side effects from within a pure function.
This is *not* allowed. You must either make randomList an IO action (i.e
returning IO [Integer]), or remove any impurities from its
implementation. For example you can make randomList take a randon number
generator and use the random number generator to produce random values:

randomList :: (RandomGen g) -> Int -> g -> [Integer]
randomList 0 _ = []
randomList n generator = r : randomList (n-1) newGenerator
where (r, newGenerator) = randomR (1,10) generator

This is totally pure, since if you pass in the same random number generator
multiple times, you'll get the exact same
result. Note that randomR returns a random values and a new random number
generator (you wouldn't want to pass along the same one in the recursive
call to randomList as that would give you an identical random number each
time you use it!).

So where do you get the random number generator from? Well one way is to
make your own using "mkStdGen", which produces one from a seed (it will give
you an identical one given an identical seed). Another way is to use
"newStdGen" to generate one from within an IO action:

main :: IO ()
main = do
generator <- newStdGen
print ( randomList 10 generator )


The point, though, is that things having side effects (such as newStdGen)
can only be used in the context of something else having side effects. So
the "IO" type is "contagious", as soon as you use it in a function, then
that function must also return IO, and so on for anything using *that*
function and son.


--
Sebastian Sylvan
+44(0)7857-300802
UIN: 44640862

Sebastian Sylvan

unread,
Jun 10, 2009, 9:10:49 AM6/10/09
to ptrash, haskel...@haskell.org
On Wed, Jun 10, 2009 at 2:08 PM, Sebastian Sylvan <
sebastia...@gmail.com> wrote:

>
>
> randomList :: (RandomGen g) -> Int -> g -> [Integer]
>

Just spotted this typo, it should be:

randomList :: (RandomGen g) = Int -> g -> [Integer]

There may be other minor typos as I don't have a compiler handy.

Sebastian Sylvan

unread,
Jun 10, 2009, 9:11:33 AM6/10/09
to ptrash, haskel...@haskell.org
On Wed, Jun 10, 2009 at 2:10 PM, Sebastian Sylvan <
sebastia...@gmail.com> wrote:

>
>
> On Wed, Jun 10, 2009 at 2:08 PM, Sebastian Sylvan <
> sebastia...@gmail.com> wrote:
>
>>
>>
>> randomList :: (RandomGen g) -> Int -> g -> [Integer]
>>
>
> Just spotted this typo, it should be:
>
> randomList :: (RandomGen g) = Int -> g -> [Integer]
>
> There may be other minor typos as I don't have a compiler handy.
>

Oh come on!

randomList :: (RandomGen g) => Int -> g -> [Integer]

David Leimbach

unread,
Jun 10, 2009, 10:57:53 AM6/10/09
to ptrash, haskel...@haskell.org
This stuff is tricky for most newcomers I suspect (it was for me)

x <- randomRIO(1,10)
is "temporarilly" pulling the Integer you've named "x" out of the IO Integer
it came from.

You can think of the console as being an input/output stream inside the IO
monad, which is why it is allowed there.

The fact is these are equivalent

do
x <- randomRIO(1,10)
x : <expression>

and

randomRIO(1,10) >>= (\x -> x:<expression)


They are the same meaning, and both are illegal for the same reason.

(>>=) is the "bind" operator for Monads and it allows you to do things like
sequence an IO operation or action, then examine the contents returned by
that IO action, possibly performing some transformation on it with another
function.

x:<expression>

is the function that takes a value "x" and applies the cons function (:) to
build a list with <expression>.

The problem is that the type system of Haskell will not allow this because
the bind function (which is used by the "do" syntax behind the scenes) is of
type:

(>>=) :: (Monad m) => m a -> (a -> m b) -> m b

Which states that the first argument to (>>=) is a Monad a, or in your case
"randomRIO(1,10)" which is of type "IO Integer"
IO being the "m" part of "m a" and "Integer" being the "a" part of "m a".

What comes next is your consing function of (x : <expression>). This is of
type [Integer].

So you failed to give it a function of type (a -> m b).

(a -> m b) says that the input type to that function must be the same as the
"contained" value of the first argument to (>>=) (because they're both of
type 'a'). The "m b" part says that you must use the *same Monad* you used
in the first parameter to (>>=) which is IO, not []. However the 'b' part
says you can convert things of one type 'a' to things of another type 'b'.

This is legal:

randomRIO(1,10) >>= return (x : <expression>)

However what you've got now is not a List of Integers ([Integer]) but an IO
[Integer].

Because of the type of (>>=), you are not going to ever permanently escape
that IO "wrapper" around your values you're interested in.

The "return" is a function of the class Monad, that takes a value, and
"wraps it up" in a monad. Based on the (>>=) function's type, the system
can infer that you meant the IO monad here.

You can even test this at the console:

Prelude System.Random> randomRIO(1,10) >>= (\x -> return (x:[99]))
[7,99]
Prelude System.Random> randomRIO(1,10) >>= (\x -> return (x:[99]))
[6,99]
Prelude System.Random> randomRIO(1,10) >>= (\x -> return (x:[99]))
[2,99]

Prelude System.Random> :t randomRIO(1,10) >>= (\x -> return (x:[99]))

randomRIO(1,10) >>= (\x -> return (x:[99]))
:: (Num t, Random t) => IO [t]

vs

"randomRIO(1,10) >>= (\x -> x:[99])"
Which doesn't pass the type checking of the system because [] is not the
same monad as IO.

Perhaps you'd do well for yourself by reading "Learn You A Haskell"? I've
found it to be pretty darned good at explaining lots of haskell to
newcomers.

Dave

ptrash

unread,
Jun 10, 2009, 11:09:04 AM6/10/09
to haskel...@haskell.org

Thanks a lot.

I have put now everything into the main method and it works.
--
View this message in context: http://www.nabble.com/Convert-IO-Int-to-Int-tp23940249p23964365.html

0 new messages