[Haskell-beginners] Typeclass problems

Patrick LeBoutillier patrick.leboutillier at gmail.com
Thu Feb 4 17:14:33 EST 2010


Hi all,

I've written a small type class that is meant to behave a bit like Show:

class Show a => Echo a where
  echo :: a -> String
  echoList :: [a] -> String
  echoListSep :: a -> String

  echo = show
  echoListSep _ = " "

  echoList [] = ""
  echoList [x] = echo x
  echoList (x:xs) = echo x ++ echoListSep x ++ echoList xs


instance Echo Char where
  echo c = [c]
  echoListSep _ = ""

instance Echo a => Echo [a] where
  echo = echoList

instance Echo Int where
instance Echo Integer where
instance Echo Double where
instance Echo Float where


gen = map (const 1)

f xs = map echo $ gen xs


However the code doesn't compile on the last line.
But it does compile if I replace 'echo' with 'show'.
What's missing to make my typeclass behave like Show and accept input
of any type (besides all the missing instances...)?
I looked at the code for Show but I didn't see anything that looked "magical"...


Thanks,

Patrick


-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada


More information about the Beginners mailing list