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

How can I implement Describable for Bool?

I am reading Get Programming with Haskell by Will Kurt.

It says:

To help solidify the idea, you’ll write a simple type class of your
own. Because you’re learning Haskell, a great type class to have is
Describable . Any type that’s an instance of your Describable type
class can describe itself to you in plain English. So you require only
one function, which is describe . For whatever type you have, if it’s
Describable , calling describe on an instance of the type will tell
you all about it. For example, if Bool were Describable , you’d expect
this:

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

GHCi> describe True
"A member of the Bool class, True is opposite of False"
GHCi> describe False
"A member of the Bool class, False is the opposite of True"

The code provided is:

class Describable a where
    describe :: a -> String

i think i have to use deriving (Describable) on Bool type. Then have to implement the describe function. However, I am not sure how the code will actually look like.

Please help.

>Solution :

You can only use deriving for the classes that support auto-deriving, which won’t work for this Describable class. You’ll need to create an instance:

class Describable a where
    describe :: a -> String

instance Describable Bool where
    describe True = "..."
    describe False = "..."
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