String
's print
, show
, and
read
Prelude> print "hello" "hello" Prelude> show "hello" "\"hello\"" Prelude> read "hello" :: String "*** Exception: Prelude.read: no parse Prelude> read "\"hello\"" :: String "hello"
Why do print
and show
for String
add
double quotes? And why does read
have the corresponding
expectation?
In other words, why don't they just stick to the verbatim string content?
Supppose they did. Then consider what would happen to (String,
String)
values, for example:
("" , ",,")
("," , ",")
(",," , "")
They would all be printed as
(,,,)
Now if you used read
to parse it back, which answer should you
get? Or if you saw that in a debugging log message, what should it mean?
Similarly consider what would happen to user-defined types like this:
data D = Dof String String deriving (Read, Show)
Then what would happen to D
values such as:
Dof "" "abc def"
Dof "abc" "def"
Dof "abc def" ""
They would all be printed as
Dof abc def
Now if you used read
to parse it back, which answer should you
get? Or if you saw that in a debugging log message, what should it mean?
Conclusion: The verbatim way is anti-compositional, aka “not a team player”. We need a disambiguating, delimiting notation.
I have more Haskell Notes and Examples