On 17 May 2010 12:56, Abby Henríquez Tejera <parad...@gmail.com> wrote:
> I'm a Haskell newbie and there's a bit of Haskell code that I don't
> understand how it works. In the prelude, defining the class Show, the
> function showList is implemented twice, one for String and another one
> for other lists:
>
>    showList cs = showChar '"' . showl cs
>                 where showl ""       = showChar '"'
>                       showl ('"':cs) = showString "\\\"" . showl cs
>                       showl (c:cs)   = showLitChar c . showl cs

This is the default implementation; if an instance doesn't define
showList then this value is used.

>    showList []       = showString "[]"
>    showList (x:xs)   = showChar '[' . shows x . showl xs
>                        where showl []     = showChar ']'
>                              showl (x:xs) = showChar ',' . shows x .
>                                             showl xs

This is how the Show instance for Char defines showList (i.e. it
overrides the default one).

There would then be something like:

instance (Show a) => Show [a] where
    show = showList

So, depending on the type used, it will either use the special ".."
method (for String = [Char]) or the default one (or another special
one if another data type overrides the default implementation for
Show).

See http://book.realworldhaskell.org/read/using-typeclasses.html for
more information.

-- 
Ivan Lazar Miljenovic
ivan.miljeno...@gmail.com
IvanMiljenovic.wordpress.com
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to