Skip to content

Instantly share code, notes, and snippets.

@ahammar
Created May 11, 2012 04:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahammar/2657492 to your computer and use it in GitHub Desktop.
Save ahammar/2657492 to your computer and use it in GitHub Desktop.
Example of overloading if-then-else in Haskell
{-# LANGUAGE RebindableSyntax #-}
import Prelude
data Height = Tall | Short
class IfThenElse b where
ifThenElse :: b -> a -> a -> a
instance IfThenElse Bool where
ifThenElse True t _ = t
ifThenElse False _ f = f
instance IfThenElse Height where
ifThenElse Tall t _ = t
ifThenElse Short _ f = f
main = do
putStrLn $ if Tall then "A" else "B"
putStrLn $ if False then "C" else "D"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment