
Hi "do", what's its role? I know a few uses for it but can't quite understand the semantics - e.g. do putStrLn "bla bla" So, what does do, do? Thanks, Paul

On Sat, 13 Oct 2007, PR Stanley wrote:
Hi "do", what's its role? I know a few uses for it but can't quite understand the semantics - e.g. do putStrLn "bla bla" So, what does do, do?
It's syntactic sugar. http://www.haskell.org/onlinereport/exps.html#sect3.14

On Sat, 13 Oct 2007, Henning Thielemann wrote:
On Sat, 13 Oct 2007, PR Stanley wrote:
Hi "do", what's its role? I know a few uses for it but can't quite understand the semantics - e.g. do putStrLn "bla bla" So, what does do, do?
It's syntactic sugar.
http://syntaxfree.wordpress.com/2006/12/12/do-notation-considered-harmful/

PR Stanley wrote:
Hi "do", what's its role? I know a few uses for it but can't quite understand the semantics - e.g. do putStrLn "bla bla" So, what does do, do?
On Sat, 13 Oct 2007, Henning Thielemann wrote:
It's syntactic sugar.
etc. Actually, there is a SURGEON GENERAL'S WARNING: the "do" construct is a syntactic Monosodium Glutamate (MSG), known sometimes as Syntactic Ajinomoto. Whether it is responsable for the Syntactic Chinese Restaurant Syndrom or not, is under investigation. Whether it increases really the flavour of the Monosod... argh... Monadic meals, it depends on your metabolism, and of your preferred table tools. People enjoying the consumption of long, long spaghetti use rarely chopstics, and prefer efficient forks like >>=, ==><<=<<, etc. Jerzy Karczmarczuk

On 10/13/07, PR Stanley
Hi "do", what's its role? I know a few uses for it but can't quite understand the semantics - e.g. do putStrLn "bla bla" So, what does do, do?
In this example, do doesn't do anything. do doesn't do anything to a single expression (well, I think it enforces that its return value is a monad...). It's only when you give it multiple expressions that it "rewrites" them into more formal notation. For example: do putStrLn "bla" putStrLn "blah" Will be rewritten into: putStrLn "bla" >> putStrLn "blah" It introduces a block of "sequential actions" (in a monad), to do each action one after another. Both of these (since they're equivalent) mean print "bla" *and then* print "blah". do also allows a more imperative-feeling variable binding: do line <- getLine putStr "You said: " putStrLn line Will be rewritten into: getLine >>= (\line -> putStr "You said: " >> putStrLn line) Looking at the do notation again: execute getLine and bind the return value to the (newly introduced) variable 'line', then print "You said: ", then print the value in the variable line. You can think of the last line in the block as the return value of the block. So you can do something like: do line <- do putStr "Say something: " getLine putStr "You said: " putStrLn line In this example it's kind of silly, but there are cases where this is useful. Luke
participants (4)
-
Henning Thielemann
-
jerzy.karczmarczuk@info.unicaen.fr
-
Luke Palmer
-
PR Stanley