[Haskell-cafe] Can this be improved?

Udo Stenzel u.stenzel at web.de
Fri Dec 30 08:21:53 EST 2005


Chris Kuklewicz wrote:
> > *MyShow> main
> > Hello" ""World #"[17,18,19]"!"
> > I also think [4,5,6]" and "7" are ""cool""."
> 
> The extra double quotes being what I am trying to avoid with MyShow

This is your only special case, a single list of a sigle type that is
different from other lists?  To avoid undecidable, incoherent and other
nasty instances, just do what the Prelude does:

class MyShow t where
    swim     :: t -> String
    swimList :: [t] -> String
    swimList [] = "[]"
    swimList xs = '[' : foldr1 (\x -> (swim x ++) . (',' :)) "]" xs

instance MyShow Char where
    swim     = (:[])
    swimList = id


Untested, but you get the idea.  It's pure Haskell 98.  Heck, it's
almost Prelude.Show.  It won't scale to more type constructors easily,
but you didn't ask for that ability, either ;-)


Udo.
-- 
"As far as Clinton supposedly cheating on his wife, what do people think
he's going to do?  Be president of another country while he's president
of ours?"
	-- Tom R., age 12
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20051230/2248dac1/attachment.bin


More information about the Haskell-Cafe mailing list