Could someone explain to me why the show function on string is not id function? For example
show (show 42)
will return "\"42\""
what is weird and for me not intitutive.
show
isn't very useful if it just converts something into an arbitrary string (like toString
in Java). It is much more useful if the result is both easy to read and machine-readable, so a common use of show
is to produce a serialized representation of the value that you are show
ing, so that you can read it in again using read
, and also, for most implementations of show
, so that you can type the string that is show
n at a Haskell REPL like ghci
and get the deserialized value back.
So, if you have a string like "42"
, and show
it, you want to get the string "\"42\""
, because when you type 42
in ghci
(and equivalently using the read
function), you get a number, while when you type "42"
in ghci
, you get the string that you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With