[Haskell-cafe] Convert IO Int to Int
jerzy.karczmarczuk at info.unicaen.fr
jerzy.karczmarczuk at info.unicaen.fr
Tue Jun 9 12:23:04 EDT 2009
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
More information about the Haskell-Cafe
mailing list