<br><br>
<div class="gmail_quote">2008/5/8 Madoc &lt;<a href="mailto:madocdoyu@gmail.com">madocdoyu@gmail.com</a>&gt;:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">Hello, 
<p>I am just learning Haskell. Now, I encountered something that I cannot solve by myself. Your advice will be greatly appreciated. 
<p>Given a list of numbers, I want to modify each of those numbers by adding a random offset. However, each such modified number shall stay within certain bounds, given by the integers <tt>minValue</tt> and <tt>maxValue</tt>. After that, I want to continue computation with the resulting list of type <tt>[Int]</tt>. But for demonstration, I made a program that just prints out the list: 
<blockquote><pre>import IO; import Random

minValue = 0::Int
maxValue = 1000::Int

normalize a | a &lt; minValue = minValue
            | a &gt; maxValue = maxValue
            | otherwise = a

modify a = do
  offset &lt;- randomRIO(-100::Int, 100)
  return(normalize(a + offset))

main = putStrLn $ show $ map (modify) [0, 200, 400, 600, 800, 1000]
</pre></blockquote>This program will not compile. GHC complains: 
<blockquote><pre>test.hs:14:18:
    No instance for (Show (IO Int))
      arising from a use of `show&#39; at test.hs:14:18-21
    Possible fix: add an instance declaration for (Show (IO Int))
    In the first argument of `($)&#39;, namely `show&#39;
    In the second argument of `($)&#39;, namely
        `show $ map (modify) [0, 200, 400, 600, ....]&#39;
    In the expression:
          putStrLn $ show $ map (modify) [0, 200, 400, 600, ....]
</pre></blockquote>I understand that the result of the <tt>modify</tt> function is not an <tt>Int</tt>, as I would like to have it, but instead <tt>IO Int</tt>, and that cannot be applied to <tt>show</tt>. (I also did not quite understand why I need those brackets around the <tt>return</tt> value of the <tt>modify</tt> value. It won&#39;t compile if I leave them out, but I can accept that for now.) 
<p>I also figured out how to generate a modified list of type <tt>[IO Int]</tt> and of type <tt>IO [Int]</tt>. However, I could not find out how to completely get rid of the IO monad and just get a mofied list of type <tt>[Int]</tt>, which is what I really want. 
<p>Please, do You have any advice for me? I tried for some hours, and now I am really angry at that IO monad that sticks to my pretty integers like glue! 
<p>Also, any comment on the programming style and how I could achive my goals easier would be appreciated. (I left out comments and function types for the sake of brevity.)</p></p></p></p></p></blockquote>
<div>&nbsp;</div>
<div>You should use newStdGen to produce a random generator, then randomRs to produce a list of random numbers (without using IO!). </div>
<div>&nbsp;</div>
<div>But if you really want this version with IO interspersed through the algorithm to work, then something like this should do it (uncompiled):</div>
<div>&nbsp;</div></div>
<div class="gmail_quote">main = do&nbsp;</div>
<div class="gmail_quote">&nbsp;xs&nbsp;&lt;- mapM&nbsp;&nbsp;(modify) [0, 200, 400, 600, 800, 1000]<br>&nbsp;putStrLn $ show $ xs</div>
<div class="gmail_quote">&nbsp;</div>
<div class="gmail_quote">The only way to &quot;get rid of the IO monad&quot;, is to use &quot;&lt;-&quot; to bind it to a value from within the IO monad.</div><br><br clear="all"><br>-- <br>Sebastian Sylvan<br>+44(0)7857-300802<br>
UIN: 44640862