Converter in haskell

{-# LANGUAGE MultiParamTypeClasses #-}
data Km =Km Double
deriving (Show,Eq)
data Mile = Mile Double
deriving (Show,Eq)
data MyMile = MyMile Double
deriving (Show,Eq)

class Converter a b where
convert:: a->b

instance Converter (Km ) (Mile ) where
convert (Km a) = Mile (a/3)
instance Converter (Km ) (MyMile ) where
convert (Km a) = MyMile (a/2)

–sample runs
convert (Km 5) :: MyMile
MyMile 2.5

convert (Km 3.2) :: Mile
Mile 1.0666666666666667

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