<div class="im">Forgot to post the list :(<br><br>On Wed, Dec 9, 2009 at 10:40 PM, legajid <span dir="ltr"><<a href="mailto:legajid@free.fr" target="_blank">legajid@free.fr</a>></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="initial"<br>
letters=['a'..'z']<br>
<br>
myloop = do<br>
myloop2<br>
<br>
myloop2 = do<br>
putStrLn word<br>
putStrLn letters<br>
<br>
main = do<br>
putStrLn "Enter the word"<br>
word <- enter_word<br>
myloop <br>
enter_word= do<br>
return("abcdefghij")<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'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="initial"<br>letters=['a'..'z']<br>
<br>myloop = do<br>
myloop2<br><br>myloop2 = do<br></div> word <- get -- récupère l'état.<br> liftIO $ do<br> putStrLn word<br> putStrLn letters<br><br>main = runStateT mafonction word <br><br>mafonction = do<br> liftIO $ putStrLn "Enter the word"<br>
w <- enter_word<br> put w -- enregistre l'état<div class="im"><br> myloop<br><br>enter_word= do<br> return("abcdefghij")<br><br></div></div><div style="font-family: arial,helvetica,sans-serif;">
Note
how the myloop function wasn'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's why every time you use IO you have to use
liftIO to reach the underlying IO monad. So there's still some
rewriting.<br>
</div>
<br>David.</div>