title: Performance of replicateM in IO monad
body: <p>I have an IO action called <code>action</code> in which I am doing a fairly heavy computation. I am using an IO monad in in order to have easy access to random numbers further down in the computation.</p>
  
  <p>I also have the below functions which replicate the action and take the mean of the result. Because the action takes a fair amount of time to complete, I was wondering what the effect on the performance is from doing the sampling in this manner. Would it be better to do the sampling later in the evaluation so that parts of the program that are the same for each sample are not repeated or does the Haskell compiler optimise this already?</p>
  
  <pre><code>samp :: (Fractional b) =&gt; Int -&gt; IO b -&gt; IO b
  samp n action = do
    samples &lt;- replicateM n action
    return $ mean samples
  
  mean :: (Fractional a) =&gt; [a] -&gt; a
  mean as =  s / (genericLength as)
    where s = sum as
  </code></pre>