I have seen references to the showS trick to build strings (e.g., in this discussion), but I have never seen a good description of it.
What is the showS trick?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In the standard library,
ShowSis defined as:This is a difference list.
The trick is that a string
xsis represented as aShowSby the function that prepends it to any other list:(xs ++). This allows efficient concatenation, avoiding the problems of nested left-associative concatenation (i.e.((as ++ bs) ++ cs) ++ ds). For example:It’s called
ShowSbecause it’s used in the implementation of the standardShowtypeclass to allow efficientshowing of large, deeply-nested structures; as well asshow, you can implementshowsPrec, which has the type:This allows handling of operator precedence, and returns a
ShowSvalue. The standard instances implement this instead ofshowfor efficiency;show ais then defined in terms of it, asshowsPrec 0 a "". (This default definition is in theShowtypeclass itself, so you can just implementshowsPrecfor a complete instance.)