[Haskell-beginners] scope for variables

legajid legajid at free.fr
Wed Dec 9 16:40:51 EST 2009


Hello,
i wrote the following (very simplified) program :

word :: [Char]
word="initial"
letters=['a'..'z']

myloop = do
  myloop2
 
myloop2 = do
  putStrLn word
  putStrLn letters

main = do
       putStrLn "Enter the word"
       word <- enter_word
       myloop 

enter_word= do
    return("abcdefghij")

"word" is initially binded to the value "initial"
myloop, calling myloop2, displays this value
"Improving" my program, i add "main" that enters a new value for "word" 
(here a fix one, ideally a getLine)
To display this new value, i must call myloop with this parameter, which 
is then passed to myloop2.
Is there a way to make a "global binding ?", thus changing the value of 
"word", so that i don't have to modify the code for myloop and myloop2 
(now being passed a parameter), that worked fine. This would make the 
program modular and easy to modify, without modifying all existing 
functions. Or is it non-sense in Haskell ?

Thanks,
Didier.






More information about the Beginners mailing list