scope for variables

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.

On Wed, 2009-12-09 at 22:40 +0100, legajid wrote:
Hello, i wrote the following (very simplified) program :
word :: [Char] word="initial" letters=['a'..'z']
myloop = do myloop2
word from topmost declaration (="initial") if it was used
myloop2 = do putStrLn word
word from topmost declaration (="initial")
putStrLn letters
main = do putStrLn "Enter the word" word <- enter_word word="abcdefghij" and is local variable/constant. 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 ?
It is practically 'non-sense' and would make a program less modular. If function depends on some value make it an argument. Generally you should avoid IO as much as possible.
Thanks, Didier.
Regards PS. Actually it is possible to do it with global variables but: 1. It uses an extentions 2. You should know why in most cases it is not what you want 3. It requires some knoledge about compilation process as 'officially' (w/out extention) it is nondeterministic.

Forgot to post the list :(
On Wed, Dec 9, 2009 at 10:40 PM, legajid
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")
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. 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. 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 ) module Main where import Control.Monad.State word :: [Char] word="initial" letters=['a'..'z'] myloop = do myloop2 myloop2 = do word <- get -- récupère l'état. liftIO $ do putStrLn word putStrLn letters main = runStateT mafonction word mafonction = do liftIO $ putStrLn "Enter the word" w <- enter_word put w -- enregistre l'état myloop enter_word= do return("abcdefghij") 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. David.

Hi, Can anyone introduce me how to use regex replace or give me web page links about this topic? thank you in advance! Regards _________________________________________________________________ Windows Live: Friends get your Flickr, Yelp, and Digg updates when they e-mail you. http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/soc...

Hi,
Bryan O'Sullivan has an excellent blog post about regex [1]. You could also
check Real World Haskell [2].
[1]-
http://www.serpentine.com/blog/2007/02/27/a-haskell-regular-expression-tutor...
[2]-
http://book.realworldhaskell.org/read/efficient-file-processing-regular-expr...
2009/12/10 Kui Ma
Hi,
Can anyone introduce me how to use regex replace or give me web page links about this topic? thank you in advance!
Regards
------------------------------ Windows Live: Friends get your Flickr, Yelp, and Digg updates when they e-mail you.http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/soc...
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Thu, Dec 10, 2009 at 11:20 AM, Adolfo Builes
Hi, Bryan O'Sullivan has an excellent blog post about regex [1]. You could also check Real World Haskell [2].
[1]- http://www.serpentine.com/blog/2007/02/27/a-haskell-regular-expression-tutor... [2]- http://book.realworldhaskell.org/read/efficient-file-processing-regular-expr...
I'd actually recommend the pcre-light package: http://hackage.haskell.org/package/pcre-light Not only does it give you Perl-compatible regexes, but it also *doesn't* force you through the IO monad. It's good stuff.

Hi, Would Text.Regex.subRegex do? It is in package regex-compat. Usage is easy, see [1]. Sincerely, jan. [1]: http://hackage.haskell.org/packages/archive/regex-compat/0.92/doc/html/Text-... On Thu, Dec 10, 2009 at 06:50:11PM +0800, Kui Ma wrote:
Hi,
Can anyone introduce me how to use regex replace or give me web page links about this topic? thank you in advance!
Regards
-- Heriot-Watt University is a Scottish charity registered under charity number SC000278.

yes it's very simple but does satisfy my need. Thank you very much! actually I had spent time researching the regex packages on Hackage, it seems they doesn't provide such APIs for replace, but from my point of view the use of *replace* is as imporant as *match* in regular expression. Or I didn't find the right place? Thanks again!
Date: Thu, 10 Dec 2009 17:57:59 +0000 From: jakubuv@gmail.com To: mklklk@hotmail.com CC: beginners@haskell.org Subject: Re: [Haskell-beginners] how to use regex replace
Hi,
Would Text.Regex.subRegex do? It is in package regex-compat. Usage is easy, see [1].
Sincerely, jan.
[1]: http://hackage.haskell.org/packages/archive/regex-compat/0.92/doc/html/Text-...
On Thu, Dec 10, 2009 at 06:50:11PM +0800, Kui Ma wrote:
Hi,
Can anyone introduce me how to use regex replace or give me web page links about this topic? thank you in advance!
Regards
-- Heriot-Watt University is a Scottish charity registered under charity number SC000278.
_________________________________________________________________ Keep your friends updated―even when you’re not signed in. http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/soc...
participants (7)
-
Adolfo Builes
-
David Virebayre
-
Jan Jakubuv
-
Kui Ma
-
legajid
-
Maciej Piechotka
-
Tom Tobin