[Haskell-cafe] Best way to build strings?

Andy Gimblett A.M.Gimblett at swansea.ac.uk
Wed Jul 20 12:06:23 EDT 2005

A small stylistic question: what's the "best" way to build strings
containing other values?  For example, I have:

data Process = Stop |
               Prefix String Process |
               External Process Process

instance Show Process where
    show Stop = "Stop"
    show (Prefix l p) = "(" ++ l ++ "->" ++ show p ++ ")"
    show (External p q) = "(" ++ show p ++ " [] " ++ show q ++ ")"

but to me the extensive use of ++ is not particularly readable.

I'm very fond of Python's interpolation approach, where we'd have
something like the following for the External case:

    def __str__(self):
        return "(%s [] %s)" % (self.p, self.q)

which to me seems clearer, or at least easier to work out roughly what
the string's going to look like.  (The %s does an implicit "convert to
string", btw).

Is there a facility like this in Haskell?  Or something else I should
be using, other than lots of ++ ?



Andy Gimblett
Computer Science Department
University of Wales Swansea

More information about the Haskell-Cafe mailing list