Concise if-then-else notation in do-blocks in Haskell

gatoatigrado picture gatoatigrado · May 25, 2011 · Viewed 9.2k times · Source

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"

Answer

Zach picture Zach · May 25, 2011

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"