# If-then-else

### From HaskellWiki

## Contents |

## 1 Replace syntactic sugar by a function

For processing conditions, theif' :: Bool -> a -> a -> a if' True x _ = x if' False _ y = y

Unfortunately there is no such function in the Prelude.

## 2 Advocacy

### 2.1 Advantages

The advantages of the function- types: classification, documentation
- higher order functions: combinators

For example, the Hoogle query

[Bool] -> [a] -> [a] -> [a]

may return

zipWith3 if'

### 2.2 Use cases

Each of the following functions could be defined in terms ofThat function is harder to explain in English, than by its implementation. :-)

zipIf :: [Bool] -> [a] -> [a] -> [a] zipIf = zipWith3 if'

infixr 1 ? (?) :: Bool -> a -> a -> a (?) = if'

From a list of expressions choose the one, whose condition is true. The first parameter is the default value. It is returned if no condition applies.

select :: a -> [(Bool, a)] -> a select = foldr (uncurry if')

See Case.

Easy lifting into monads (MonadReader in this case)

ifF :: (a -> Bool) -> (a -> b) -> (a -> b) -> (a -> b) ifF = liftM3 if'

### 2.3 Why add this function to Prelude?

Actually people could define### 2.4 If-Then-Else vs. guards

Some people (any exact statistics?) prefer guards toThis practice has its own drawbacks, see Syntactic sugar/Cons and Things to avoid.

### 2.5 Is If-Then-Else so important?

Counting- frequently guards are used instead of if-then-else
- there is no standard function, and this let people stick to work-arounds.

### 2.6 What is so bad about the if-then-else sugar?

Since syntactic sugar introduces its own syntactic rules, it is hard to predict how it interferes with other syntactic constructs. This special syntax for instance led to conflicts with do notation. A syntactic extension to solve this problem is proposed for Haskell'. It is not known what conflicts this extension might cause in future.

### 2.7 Why breaking lots of old and unmaintained code?

Haskell withoutIf we arrive at Haskell two some day, (http://haskell.org/hawiki/HaskellTwo (Web Archive)) it will certainly be incompatible to former Haskell versions. This does not mean, that old code must be thrown away. There should be one tool, that converts Haskell 98 and Haskell' to Haskell-2.

Having**one**tool for this purpose is better than blowing

**all**language tools with legacy code. Syntactic replacements like

### 2.8 Summary

- Light proposal, compatible with Haskell 98: Add to the Prelude, maybe with a different name.if'
- Full proposal, incompatible with Haskell 98 and Haskell': Additionally remove syntaxif-then-else

### 2.9 See also

## 3 Objections

Haskell is not intended to be a minimalistic language, but to be one that is easy to read.If properly indented, like

if a then b else c

or

if a then b else c