Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Show Class Currency Haskell

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 
...

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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:

class Show Currency where
    show (Yen y) = show y ++ "¥"
    show (Dollar d c)
        | c < 10 = '$' : show d ++ '.' : 0 : show c
        | otherwise = '$' : show d ++ '.' : show c
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading