Last active
June 21, 2016 21:08
-
-
Save mathiasverraes/d65640372ac60c252b7aa05b2e49d9a9 to your computer and use it in GitHub Desktop.
fooling around with haskell types and money
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE ExistentialQuantification #-} | |
module Money where | |
data Money a = Currency a => Money {currency :: a, amount :: Float} | |
instance Show (Money a) where | |
show (Money c a) = show c ++ " " ++ show a | |
instance Eq (Money a) where | |
(Money cx x) == (Money cy y) = cx == cy && x == y | |
class (Eq a, Show a) => Currency a where symbol :: a | |
data EUR = EUR deriving (Show, Eq) | |
instance Currency EUR where symbol = EUR | |
data USD = USD deriving (Show, Eq) | |
instance Currency USD where symbol = USD | |
eur :: Float -> Money EUR | |
eur = Money EUR | |
usd :: Float -> Money USD | |
usd = Money USD | |
instance Currency a => Monoid (Money a) where | |
mappend = (+) | |
mempty = Money symbol 0 | |
instance Currency a => Num (Money a) where | |
(Money c x) + (Money _ y) = Money c (x + y) | |
(Money c x) * (Money _ y) = Money c (x * y) | |
abs (Money c a) = Money c (abs a) | |
signum (Money c a) = Money c (signum a) | |
fromInteger x = Money symbol (fromInteger x) | |
negate (Money c a) = Money c (negate a) | |
Author
mathiasverraes
commented
Jun 21, 2016
Most of the functions from the Num class don't make a lot of sense, but you need them to be able to do (+)
You need to explain this to me in person. I understand exactly nothing but I'm confident it's pretty clever 😉
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment