Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So:</p> <pre><code>repeat :: a -&gt; [a] randomIO :: Random a =&gt; IO a sequence :: Monad m =&gt; [m a] -&gt; m [a] </code></pre> <p>=></p> <pre><code>repeat (randomIO :: IO Float) :: [IO Float] </code></pre> <p>So when you do:</p> <pre><code>random &lt;- repeat (randomIO :: IO Float) </code></pre> <p>You're actually exploiting the list monad here, so <code>random</code> has type <code>IO Float</code>. Since you're in the list monad, your last statement needs to have type <code>[a]</code>, but it has type <code>IO ()</code> since it's a call to <code>print</code>, hence the type error.</p> <p>The whole point of sequence is to transform this <code>[IO a]</code> into an <code>IO [a]</code> that you can perform to obtain a list of random values, and hopefully print this list. Now, when you perform an IO like this, it needs to be performed all at once, unless using <code>unsafeInterleaveIO</code>, which is not recommended in this case. So it tries to get that infinite list... and hangs (it might stack overflow at some point, I'm not sure).</p> <p>To get an infinite list of random values, you don't need all this, just to obtain a random seed, and compute random values purely from the seed.</p> <p>You should be able to construct an infinite list of random values using these <a href="http://hackage.haskell.org/packages/archive/random/latest/doc/html/System-Random.html#t%3aRandom" rel="nofollow">functions</a>:</p> <pre><code>randomIO :: Random a =&gt; IO a -- to provide an IO Int mkStdGen :: Int -&gt; StdGen -- to obtain a random generator from that Int randoms :: RandomGen g =&gt; g -&gt; [a] -- to generate the infinite list </code></pre> <p>Notice that the last two functions are pure. Reading <a href="http://www.haskell.org/pipermail/haskell-cafe/2007-July/thread.html#29706" rel="nofollow">this thread</a> might give you some more ideas.</p> <hr> <p>EDIT:</p> <p>Example of how you should use <code>mkStdGen</code>:</p> <pre><code>randomList :: Random a =&gt; IO [a] randomList = do seed &lt;- randomIO let gen = mkStdGen seed return (randoms gen) </code></pre> <p>I can't test it right now but this should work. You probably want to adapt this to your use case though.</p> <p>For your other question:</p> <pre><code>map :: (a -&gt; b) -&gt; [a] -&gt; [b] print :: Show a =&gt; a -&gt; IO () </code></pre> <p>=> map print :: Show a => [a] -> [IO ()]</p> <p>This probably isn't what you want, right? If you just want to print a list, no need for <code>map</code>, <code>print</code> can handle lists.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload