User-defined control structures
Haskell has no shorthand ternary operator. The built-in if
-then
-else
is always ternary, and is an expression (imperative languages tend to have ?:
=expression, if
=statement). If you want, though,
True ? x = const x
False ? _ = id
will define (?)
to be the ternary operator:
(a ? b $ c) == (if a then b else c)
You'd have to resort to macros in most other languages to define your own short-circuiting logical operators, but Haskell is a fully lazy language, so it just works.
-- prints "I'm alive! :)"
main = True ? putStrLn "I'm alive! :)" $ error "I'm dead :("