Does haskell have a conditional statement such as "x == y? A: b" in C ++ or ifelse (x == y, a, b) in R?

Does haskell have a conditional statement that executes as

x == y ? a : b 

in c ++ or

 ifelse(x==y, a, b) 

in R?

+5
source share
3 answers

Haskell if does exactly what you want.

 if x == y then a else b 

As Lee mentioned, there is a bool function in Data.Bool that does the same. In addition, due to the gentleness of Haskell, bool someLongComputation something True does not run long calculations.

+16
source

Apart from if or Data.Bool.bool , which really are exactly what you want, you can also define such an operator yourself in Haskell!

 infixr 1 ? (?) :: Bool -> a -> a -> a (True ? a) _ = a (False ? _) b = b 

GHCi> 3 == 2? equal $ nonvalue
"nonequal"
GHCi> 3 == 3? "equal" $ "nonequal"
"equal"

+15
source

There is a bool function in Data.Bool :

 import Data.Bool bool ba (x == y) 
+10
source

All Articles