Home > Mobile >  Show Class Haskell
Show Class Haskell

Time:11-21

I'm having trouble with a task where I need to derive the Currency data type from the class, specifying dollars in the form 18.04$ and yen in the form 500¥.

Now I'm not allowed to use deriving (Show), which confuses me a bit, could someone explain me how to proceed?


data Currency = Dollar Dollar | Yen Yen
data Dollar = Dollar Integer Integer
data Yen = Yen Integer

class (Show a) => Currency a where 
...

CodePudding user response:

There is probably a "clash" between the data constructors. You can omit the Yen and Dollar data types, and work with:

data Currency = Dollar Integer Int | Yen Integer

The Currency type constructor has no type parameter, hence Currency a makes no sense. What you probably want to do is write:

instance Show Currency where
    show (Yen y) = show y    "¥"
    show (Dollar d c)
        | c < 10 = '$' : show d    '.' : 0 : show c
        | otherwise = '$' : show d    '.' : show c
  • Related