[Haskell-beginners] Why ShowS?

Christian Maeder Christian.Maeder at dfki.de
Thu Aug 11 17:24:59 CEST 2011


Am 11.08.2011 11:13, schrieb Yitzchak Gale:
> Here is a simple example of how to use ShowS:
>
> showPerson :: String ->  Int ->  Int ->  String
> showPerson name age shoeSize =
>    concat [ "Name: ", name, ", Age: ", show age,
>    ", Shoe size: ", show shoeSize]
> ...
> putStrLn $ showPerson name age shoe
>
> becomes, in the ShowS style:
>
> showsPerson :: String ->  Int ->  Int ->  ShowS
> showsPerson name age shoeSize =
>    ("Name: " ++) . (name ++) . (", Age: " ++) . shows age .
>    (", Shoe size: " ++) . shows shoeSize
> ...
> putStrLn $ showsPerson name age shoe ""

I think, this examples shows, why ShowS is rarely used!

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

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

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

Cheers Christian

>
> Regards,
> Yitz
>



More information about the Beginners mailing list