[Haskell-cafe] ShowList magic

Abby Henríquez Tejera paradoja at gmail.com
Sun May 16 22:56:03 EDT 2010


Hi.

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

and



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

The thing is... how does Haskell «know» which to execute? It works
even for the blank string:
Prelude> show ""
"\"\""
Prelude> show []
"[]"

Salud,
Abby


More information about the Haskell-Cafe mailing list