
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

On 17 May 2010 12:56, Abby Henríquez Tejera
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.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

On 17 mayo, 04:00, Ivan Miljenovic
On 17 May 2010 12:56, Abby Henríquez Tejera
wrote: [...] 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).
Seehttp://book.realworldhaskell.org/read/using-typeclasses.htmlfor more information.
I had already read the RWH chapter, but still didn't understand it, but now (preparing to answer you why I didn't «see» it), it suddenly all came clear. Oh, I hate this moments, now I feel stupid :). Thank you very much. Salud, Abby

Abby Henríquez Tejera
I had already read the RWH chapter, but still didn't understand it, but now (preparing to answer you why I didn't «see» it), it suddenly all came clear. Oh, I hate this moments, now I feel stupid :).
Heh, everyone has those moments, so don't worry about it ;-) -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

On May 17, 2010, at 09:42 , Abby Henríquez Tejera wrote:
I had already read the RWH chapter, but still didn't understand it, but now (preparing to answer you why I didn't «see» it), it suddenly all came clear. Oh, I hate this moments, now I feel stupid :).
You're going to have a lot of those if you stick around. :) -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

"Brandon S. Allbery KF8NH"
On May 17, 2010, at 09:42 , Abby Henríquez Tejera wrote:
I had already read the RWH chapter, but still didn't understand it, but now (preparing to answer you why I didn't «see» it), it suddenly all came clear. Oh, I hate this moments, now I feel stupid :).
You're going to have a lot of those if you stick around. :)
... and possibly come to cherish them (as the veil of confusion opens before you to a beautiful vista of elegance)! -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

On 17 mayo, 04:00, Ivan Miljenovic
On 17 May 2010 12:56, Abby Henríquez Tejera
wrote: [...] 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).
Seehttp://book.realworldhaskell.org/read/using-typeclasses.htmlfor more information.
I had already read the RWH chapter, but still didn't understand it, but now (preparing to answer you why I didn't «see» it), it suddenly all came clear. Oh, I hate this moments, now I feel stupid :). Thank you very much. Salud, Abby

Your question is actually deeper than some of the people answering you
seem to realize.
How does ghci decide what to do when you say
show []
?
The expression [] has type [a], which means it could be a list of any
type 'a', including Char.
Normally, when Haskell can't determine the type in this kind of
context it will complain.
You can try compiling the program
main = putStrLn (show [])
and you'll see the error message.
But in ghci there is a special defaulting rule that will use the type
() ambiguous types.
So ghci will use the 'Show ()' instance, which uses the default
implementation for showList.
-- Lennart
On Mon, May 17, 2010 at 4:56 AM, Abby Henríquez Tejera
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 _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (5)
-
Abby Henríquez Tejera
-
Brandon S. Allbery KF8NH
-
Ivan Lazar Miljenovic
-
Ivan Miljenovic
-
Lennart Augustsson