Batuhan Tasdoven
Batuhan Tasdoven

Reputation: 798

Convert type a to char

I have

data Dictionary a = Empty
                  | Branch (a, Bool, Dictionary a) (Dictionary a)
                  deriving (Ord, Eq)

instance Show (Dictionary a) where show = showDict

showDict (Branch (val, e, dict) dict2) = "My dict is : " ++ val 

I know it is definitely wrong but I could not find how to write this one. In showDict function type of val is a but expected type is [Char].

Thanks in advance.

Upvotes: 1

Views: 938

Answers (2)

Satvik
Satvik

Reputation: 11208

instance (Show a) => Show (Dictionary a) where show = showDict

You have to tell that a belongs to showable type class otherwise you can not use show on val .

Upvotes: 1

Fred Foo
Fred Foo

Reputation: 363607

To turn val into a string, show it:

showDict (Branch (val, e, dict) dict2) = "My dict is : " ++ show val

And don't forget the type constraints on showDict:

instance Show a => Show (Dictionary a) where show = showDict

Upvotes: 5

Related Questions