HaskellWiki

Haskell | Wiki community | Recent changes
Random page | Special pages

 

Not logged in
Log in | Help

Ternary operator

Categories: Code | Idioms

With a bit of work, we can define a ternary conditional operator in Haskell. Courtesy of Andrew Baumann. This appears only to be valid in Hugs?

import qualified Prelude
 
data Cond a = a : a
 
infixl 0 ?
infixl 1 :
 
(?) :: Prelude.Bool -> Cond a -> a
Prelude.True  ? (x : _) = x
Prelude.False ? (_ : y) = y
 
test = 1 Prelude.< 2 ? "yeah" : "no!"

Further reading

Retrieved from "http://www.haskell.org/haskellwiki/Ternary_operator"

This page has been accessed 810 times. This page was last modified 03:36, 6 July 2007. Recent content is available under a simple permissive license.