public
Created

Example of overloading if-then-else in Haskell

  • Download Gist
RebindableIf.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
{-# 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"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.