Haskell default equality

Haskell does some nice work for us .It defines default equality for us based on value constructor,a simple illustration :

data Color = Red |Green|Blue
deriving (Show)

instance Eq Color where
a == b = case (a ,b ) of
(Red,Red) -> True
(Green,Green) -> True
(Blue,Blue) -> True
_ -> False

or a cleaner approach :
data Color = Red |Green|Blue
deriving (Show)

instance Eq Color where
Red == Red = True
Blue == Blue = True
Green == Green = True
_ == _ =False
haskell default equality would result into same result if we deriving from Eq

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s