[Haskell-beginners] Why ShowS?

Yitzchak Gale gale at sefer.org
Thu Aug 11 19:25:39 CEST 2011


I wrote:
>> Here is a simple example of how to use ShowS...

Christian Maeder wrote:
> I think, this examples shows, why ShowS is rarely used!

I understand your feelings. The sections of ++ do look awkward
on the surface, though in practice it's not really much of an
issue.

> It would even be longer if you used "showString" instead of
> these (ugly) sections with ++ (instead of using ++ directly).

Yes. That's why I used the sections in my examples,
instead of the Prelude function "showString".

Most other function-composition-style pretty-printing
libraries using something shorter than "showString", like
"text", to replace the ++ sections. Then it comes out the
same length. You could use something even shorter.

But saving a few keystrokes is not the point. The point is
writing the expression as a compositional pipeline, which is
conceptually nice, and more or less the same complexity in
syntax.

> I don't think, there's a performance issue, either. (++ is
> right-associative).

I think you're right. That was more of an issue for early
compilers.

> Creating a (better formatted) list of strings and then using "concat",
> "unwords", "unlines" or "intercalate ", " is quite a good choice.

Indeed, it's fine, and it's the most popular.

Once you realize the beauty and power of the compositional
combinator approach in general, though, using that approach
also for rendering becomes an attractive alternative. Especially
in situations where you might want to insert other
combinators into the pipeline.

Anyway, I think that's a more complete answer to Christopher's
original question about ShowS.

Thanks,
Yitz



More information about the Beginners mailing list