[Haskell-cafe] Why does Haskell have the if-then-else syntax?

Donn Cave donn at drizzle.com
Wed Jul 26 23:46:53 EDT 2006

Quoth "Sebastian Sylvan" <sylvan at student.chalmers.se>:
| On 7/27/06, mvanier <mvanier at cs.caltech.edu> wrote:
| > As opposed to what?
| For example case-of, guards (in combination with let or where), or
| just a function:
| if :: Bool -> a -> a -> a
| if True t _ = t
| if False _ e = e
| -- example usage
| myAbs x = if (x < 0) (negate x) x

That looks to me like a different way to spell if then else, but maybe
that's the answer to the question - conceptually, for every "then" there
really is an "else", however you spell it, and only in a procedural language
does it make any sense to leave it implicit.  The exception that proves the
rule is "else return ()" -, e.g.,

if_ :: Bool -> IO () -> IO ()
if_ True f = f
if_ False _ = return ()

main = do
	args <- getArgs
	if_ (length args > 0)
		(print args)

Strictly speaking that generalizes to any functional context where a generic
value can be assigned to the else clause, but there don't tend to be that
many other such contexts.  Does that answer the question?

	Donn Cave, donn at drizzle.com

More information about the Haskell-Cafe mailing list