
Dear Cafe - Sometimes, a comment can change the meaning of a program. No? Well, but "commenting out" a line of code certainly does. So, can you guess what this program will output? main = do if True then do putStrLn "A" else do -- putStrLn "B" putStrLn "done" I nominate this for inclusion in a hypothetical Haskell equivalent of http://www.javapuzzlers.com/ - J.

I'm not quite sure, but I'd guess either one of
1.) It does not compile at all because of suspicious indentation
2.) It will just print "A" but not "done" because that is considered beeing
in the else-branch.
Johannes Waldmann
Dear Cafe -
Sometimes, a comment can change the meaning of a program. No? Well, but "commenting out" a line of code certainly does. So, can you guess what this program will output?
main = do if True then do putStrLn "A" else do -- putStrLn "B" putStrLn "done"
I nominate this for inclusion in a hypothetical Haskell equivalent of http://www.javapuzzlers.com/
- J. _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

The answer is very simple ;-} The report says that after a do the first symbol to encounter determines the indentation level; so putStrLn “done” is part of the else branch. Obvious, isn’t it. Se also https://wiki.haskell.org/Common_Misunderstandings Doaitse
Op 15 nov. 2016, om 13:31 heeft Norbert Melzer
het volgende geschreven: I'm not quite sure, but I'd guess either one of
1.) It does not compile at all because of suspicious indentation 2.) It will just print "A" but not "done" because that is considered beeing in the else-branch.
Johannes Waldmann
mailto:johannes.waldmann@htwk-leipzig.de> schrieb am Di., 15. Nov. 2016 um 13:11 Uhr: Dear Cafe - Sometimes, a comment can change the meaning of a program. No? Well, but "commenting out" a line of code certainly does. So, can you guess what this program will output?
main = do if True then do putStrLn "A" else do -- putStrLn "B" putStrLn "done"
I nominate this for inclusion in a hypothetical Haskell equivalent of http://www.javapuzzlers.com/ http://www.javapuzzlers.com/
- J. _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post. _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

On 15 November 2016 at 12:53, Doaitse Swierstra
The answer is very simple ;-} The report says that after a do the first symbol to encounter determines the indentation level; so putStrLn “done” is part of the else branch. Obvious, isn’t it.
The outer do block should be desugared first, and the `putStrLn "done"` has
the same indent as the `if` so it gets a semicolon, in that block, meaning
it looks like this:
main = do
{ if True
then do
putStrLn "A"
else do
-- putStrLn "B"
; putStrLn "done"
}
On 15 November 2016 at 12:31, Norbert Melzer
I'm not quite sure, but I'd guess either one of
1.) It does not compile at all because of suspicious indentation 2.) It will just print "A" but not "done" because that is considered beeing in the else-branch.
I get *both* of these behaviours! When building, it complains about an empty 'do' block. When running through `stack ghci` it doesn't seem to mind. [linuxadmin@trac-leeds-09-vm01 13:07:57 ~/indent-puzzle ] $ stack build indent-puzzle-0.1.0.0: build Preprocessing executable 'indent-puzzle-exe' for indent-puzzle-0.1.0.0... [1 of 1] Compiling Main ( app/Main.hs, .stack-work/dist/x86_64-linux/Cabal-1.22.5.0/build/indent- puzzle-exe/indent-puzzle-exe-tmp/Main.o ) /home/linuxadmin/indent-puzzle/app/Main.hs:5:10: Empty 'do' block -- While building package indent-puzzle-0.1.0.0 using: /home/linuxadmin/.stack/setup-exe-cache/x86_64-linux/setup- Simple-Cabal-1.22.5.0-ghc-7.10.3 --builddir=.stack-work/dist/x86_64-linux/Cabal-1.22.5.0 build exe:indent-puzzle-exe --ghc-options " -ddump-hi -ddump-to-file" Process exited with code: ExitFailure 1 [linuxadmin@trac-leeds-09-vm01 13:08:00 ~/indent-puzzle ] $ stack ghci indent-puzzle-0.1.0.0: build Preprocessing executable 'indent-puzzle-exe' for indent-puzzle-0.1.0.0... [1 of 1] Compiling Main ( app/Main.hs, .stack-work/dist/x86_64-linux/Cabal-1.22.5.0/build/indent- puzzle-exe/indent-puzzle-exe-tmp/Main.o ) /home/linuxadmin/indent-puzzle/app/Main.hs:5:10: Empty 'do' block -- While building package indent-puzzle-0.1.0.0 using: /home/linuxadmin/.stack/setup-exe-cache/x86_64-linux/setup- Simple-Cabal-1.22.5.0-ghc-7.10.3 --builddir=.stack-work/dist/x86_64-linux/Cabal-1.22.5.0 build exe:indent-puzzle-exe --ghc-options " -ddump-hi -ddump-to-file" Process exited with code: ExitFailure 1 Warning: build failed, but optimistically launching GHCi anyway The following GHC options are incompatible with GHCi and have not been passed to it: -threaded Using main module: 1. Package `indent-puzzle' component exe:indent-puzzle-exe with main-is file: /home/linuxadmin/indent- puzzle/app/Main.hs Configuring GHCi with the following packages: indent-puzzle GHCi, version 7.10.3: http://www.haskell.org/ghc/ :? for help [1 of 1] Compiling Main ( /home/linuxadmin/indent-puzzle/app/Main.hs, interpreted ) Ok, modules loaded: Main. *Main> main A *Main>

GHCi is very lax with do blocks; but as far as I know GHC should refuse to
compile that snippet and give a diagnosis about an empty do.
- nitrix
On Nov 15, 2016 10:12 AM, "Lana Black"
I havve just tested this with ghc8, and surprisingly when B is commented out the last putStrLn is treated as a part of if-else. So, with B commented out the only thing printed out is A and not 'done'.
*From: *David Turner *Sent: *Tuesday, November 15, 2016 1:14 PM *To: *Haskell Cafe *Subject: *Re: [Haskell-cafe] commenting out a line of code
On 15 November 2016 at 12:53, Doaitse Swierstra
wrote: The answer is very simple ;-} The report says that after a do the first symbol to encounter determines the indentation level; so putStrLn “done” is part of the else branch. Obvious, isn’t it.
The outer do block should be desugared first, and the `putStrLn "done"` has the same indent as the `if` so it gets a semicolon, in that block, meaning it looks like this:
main = do { if True then do putStrLn "A" else do -- putStrLn "B" ; putStrLn "done" }
On 15 November 2016 at 12:31, Norbert Melzer
wrote: I'm not quite sure, but I'd guess either one of
1.) It does not compile at all because of suspicious indentation 2.) It will just print "A" but not "done" because that is considered beeing in the else-branch.
I get *both* of these behaviours! When building, it complains about an empty 'do' block. When running through `stack ghci` it doesn't seem to mind.
[linuxadmin@trac-leeds-09-vm01 13:07:57 ~/indent-puzzle ] $ stack build indent-puzzle-0.1.0.0: build Preprocessing executable 'indent-puzzle-exe' for indent-puzzle-0.1.0.0... [1 of 1] Compiling Main ( app/Main.hs, .stack-work/dist/x86_64-linux/Cabal-1.22.5.0/build/indent-pu zzle-exe/indent-puzzle-exe-tmp/Main.o )
/home/linuxadmin/indent-puzzle/app/Main.hs:5:10: Empty 'do' block
-- While building package indent-puzzle-0.1.0.0 using: /home/linuxadmin/.stack/setup-exe-cache/x86_64-linux/setup-S imple-Cabal-1.22.5.0-ghc-7.10.3 --builddir=.stack-work/dist/x86_64-linux/Cabal-1.22.5.0 build exe:indent-puzzle-exe --ghc-options " -ddump-hi -ddump-to-file" Process exited with code: ExitFailure 1 [linuxadmin@trac-leeds-09-vm01 13:08:00 ~/indent-puzzle ] $ stack ghci indent-puzzle-0.1.0.0: build Preprocessing executable 'indent-puzzle-exe' for indent-puzzle-0.1.0.0... [1 of 1] Compiling Main ( app/Main.hs, .stack-work/dist/x86_64-linux/Cabal-1.22.5.0/build/indent-pu zzle-exe/indent-puzzle-exe-tmp/Main.o )
/home/linuxadmin/indent-puzzle/app/Main.hs:5:10: Empty 'do' block
-- While building package indent-puzzle-0.1.0.0 using: /home/linuxadmin/.stack/setup-exe-cache/x86_64-linux/setup-S imple-Cabal-1.22.5.0-ghc-7.10.3 --builddir=.stack-work/dist/x86_64-linux/Cabal-1.22.5.0 build exe:indent-puzzle-exe --ghc-options " -ddump-hi -ddump-to-file" Process exited with code: ExitFailure 1 Warning: build failed, but optimistically launching GHCi anyway The following GHC options are incompatible with GHCi and have not been passed to it: -threaded Using main module: 1. Package `indent-puzzle' component exe:indent-puzzle-exe with main-is file: /home/linuxadmin/indent-puzzle /app/Main.hs Configuring GHCi with the following packages: indent-puzzle GHCi, version 7.10.3: http://www.haskell.org/ghc/ :? for help [1 of 1] Compiling Main ( /home/linuxadmin/indent-puzzle/app/Main.hs, interpreted ) Ok, modules loaded: Main. *Main> main A *Main>
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
participants (6)
-
Alex Belanger
-
David Turner
-
Doaitse Swierstra
-
Johannes Waldmann
-
Lana Black
-
Norbert Melzer