I did some research in my Haskell literature (other than in my current Bible) and found an example that effectively solves my problem.
Basically, in this workaround, you set Char as an instance of the class (in the example book it is called Visible ), and then you can set [chars] aka String to be an instance of the class ONLY with the condition that the variable of type chars is an instance of `Visible '. This is easier to understand if you look at the code below:
module Practice where class Visible a where toString :: a -> String size :: a -> Int instance Visible Char where toString ch = [ch] size _ = 1 instance Visible a => Visible [a] where toString = concat . map toString size = foldr (+) 1 . map size
My download call and GHCi function:
*Practice> :l Practice [1 of 1] Compiling Practice ( Practice.hs, interpreted ) Ok, modules loaded: Practice. *Practice> size "I love Stack!" 14 *Practice>
Eureka!
Codybugstein
source share