[Haskell-cafe] Best way to build strings?

Bryn Keller xoltar at xoltar.org
Wed Jul 20 12:28:35 EDT 2005


How about this?

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

Hope that helps,

Bryn


Andy Gimblett wrote:

>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 ++ ?
>
>Thanks,
>
>-Andy
>
>  
>



More information about the Haskell-Cafe mailing list