Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show on string not id function?

Tags:

haskell

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.

like image 305
whd Avatar asked Dec 09 '22 00:12

whd


1 Answers

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 showing, 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 shown 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.

like image 175
dflemstr Avatar answered Dec 22 '22 21:12

dflemstr