<div class="im">Forgot to post the list :(<br><br>On Wed, Dec 9, 2009 at 10:40 PM, legajid <span dir="ltr">&lt;<a href="mailto:legajid@free.fr" target="_blank">legajid@free.fr</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

Hello,<br>
i wrote the following (very simplified) program :<br>
<br>
word :: [Char]<br>
word=&quot;initial&quot;<br>
letters=[&#39;a&#39;..&#39;z&#39;]<br>
<br>
myloop = do<br>
 myloop2<br>
<br>
myloop2 = do<br>
 putStrLn word<br>
 putStrLn letters<br>
<br>
main = do<br>
      putStrLn &quot;Enter the word&quot;<br>
      word &lt;- enter_word<br>
      myloop <br>
enter_word= do<br>
   return(&quot;abcdefghij&quot;)<br></blockquote></div><div><br>Remind
that a haskell function depends only on its parameters. Unless you use
an unsafe trick, often frowned upon, mutable global variables don&#39;t
exist.<br><br>
So normally you have to modify your functions to pass the string as a
parameter. While it may seem tedious at first, perhaps you will realise
that having the type signature mention that you need a string is a
precious information. Also, start by modifying only myloop2, ghc will
complain about every function that use the new myloop2 the wrong way,
so refactoring is not that hard.<br>
<br>In the case you have a more complicated program where state is an
important part, you can use the State monad, or more specifically in
your example a State transformer that uses IO as the underlying monad.
Your program would look like : ( you can copy and paste this in a
source file, then play with it in ghci )<br>
<br><div style="margin-left: 40px; font-family: courier new,monospace;">module Main where<br><br>import Control.Monad.State<div class="im"><br><br>word :: [Char]<br>word=&quot;initial&quot;<br>letters=[&#39;a&#39;..&#39;z&#39;]<br>
<br>myloop = do<br>
 myloop2<br><br>myloop2 =  do<br></div>  word &lt;- get  -- récupère l&#39;état.<br>  liftIO $ do<br>    putStrLn word<br>    putStrLn letters<br><br>main = runStateT mafonction word <br><br>mafonction = do<br>     liftIO $ putStrLn &quot;Enter the word&quot;<br>

     w &lt;- enter_word<br>     put w           -- enregistre l&#39;état<div class="im"><br>     myloop<br><br>enter_word= do<br>  return(&quot;abcdefghij&quot;)<br><br></div></div><div style="font-family: arial,helvetica,sans-serif;">
Note
how the myloop function wasn&#39;t modified. On the other hand, have a look
at the type signatures using ghci. Your function now is in the StateT
String IO monad. So that&#39;s why every time you use IO you have to use
liftIO to reach the underlying IO monad.  So there&#39;s still some
rewriting.<br>
</div>



<br>David.</div>