Skip to content

Instantly share code, notes, and snippets.

@laserpants
Last active October 19, 2017 06:46
Show Gist options
  • Save laserpants/478a2b2b81f9fb20d723cc4707147538 to your computer and use it in GitHub Desktop.
Save laserpants/478a2b2b81f9fb20d723cc4707147538 to your computer and use it in GitHub Desktop.
Haskell ternary operator.
module Main where
(.?.) :: Bool -> (a, a) -> a
(.?.) True = fst
(.?.) False = snd
infixl 3 .?.
(>:<) :: a -> b -> (a, b)
(>:<) = (,)
main :: IO ()
main = do
print $ 3 == 5 .?. "yes" >:< "no" -- "no"
print $ 5 > 3 .?. "yes" >:< "no" -- "yes"
print $ 3 - 3 == 0 .?. "yes" >:< "no" -- "yes"
print $ null [] .?. "yes" >:< "no" -- "yes"
print $ null [1] .?. "yes" >:< "no" -- "no"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment