Skip to content

Instantly share code, notes, and snippets.

@jwodder
Created May 11, 2014 00:39
Show Gist options
  • Save jwodder/d9ee213119fff3f46aa2 to your computer and use it in GitHub Desktop.
Save jwodder/d9ee213119fff3f46aa2 to your computer and use it in GitHub Desktop.
Ternary/conditional operator in Haskell
module Ternary where
data TernaryBranch a = a :? a deriving (Eq, Ord, Read, Show, Bounded)
instance Functor TernaryBranch where fmap f (x :? y) = f x :? f y
infixr 0 ?:, :?, ?~
(?:) :: Bool -> TernaryBranch a -> a
True ?: (y :? _) = y
False ?: (_ :? z) = z
(?~) :: Bool -> (a, a) -> a
True ?~ (y, _) = y
False ?~ (_, z) = z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment