13

I cannot figure out how to make the concise if-then-else notation work, mentioned at [ http://hackage.haskell.org/trac/haskell-prime/wiki/DoAndIfThenElse ]. This works,

import System.Environment main = do args <- getArgs if (args !! 0) == "hello" then print "hello" else print "goodbye" 

but this does not, and inserting said semicolons (see link) just result in parse errors for me.

import System.Environment main = do args <- getArgs if (args !! 0) == "hello" then print "hello" else print "goodbye" 
6
  • 3
    What version of GHC are you using? - or, are you using GHC? :) Commented May 24, 2011 at 22:40
  • 6.12.3 (unfortunately, the OpenSuSE binaries are rather out of date) Commented May 24, 2011 at 22:59
  • Why are you separating your then and else from their lines? Commented May 24, 2011 at 23:07
  • 2
    Unfortunately it was fixed in 6.13. Commented May 24, 2011 at 23:16
  • I know you probably used it as a toy example, but nitpicking reveals you could have used: print $ if head args == "hello" then "hello" else "goodbye" Commented May 24, 2011 at 23:33

4 Answers 4

12

The link you provided describes a proposal, which sounds like it is not part of the Haskell standard (although the link mentions that it's implemented in jhc, GHC and Hugs). It's possible that the version of the Haskell compiler you're using, or the set of flags you're using, does not allow for the optional-semicolon behavior described in the link.

Try this:

import System.Environment main = do args <- getArgs if (args !! 0) == "hello" then print "hello" else print "goodbye" 
Sign up to request clarification or add additional context in comments.

2 Comments

The proposal was accepted into Haskell 2010. It works without changes on GHC 7.0.2.
Oh! Good to know! It's possible that gatoatigrado was using an older version of GHC without support for that.
5

In Haskell 98 “if … then … else …” is a single expression. If it’s split to multiple lines, the ones following the first one must be indented further.

Just like the following is wrong…

do 1 + 2 

…and the following works…

do 1 + 2 

…the following is also wrong…

do if True then 1 else 2 

…and the following works.

do if True then 1 else 2 

As the other comments already mention, Haskell 2010 allows the “then” and “else” parts on the same level of indentation as the “if” part.

Comments

3

Haskell syntax and language are extended though {-# LANGUAGE ... #-} pragmas at the start of the source files. The DoAndIfThenElse extension is recognized since it is one of those listed in the Cabal documentation. Current GHC enables this by default.

Comments

2

I usually indent the else one space more than the if. Unless then whole if fits nicely on a single line.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.