[Haskell-cafe] Best way to build strings?

Albert Lai trebla at vex.net
Sat Jul 23 13:59:52 EDT 2005


Andy Gimblett <A.M.Gimblett at swansea.ac.uk> writes:

>     show (External p q) = "(" ++ show p ++ " [] " ++ show q ++ ")"
> 
> but to me the extensive use of ++ is not particularly readable.

[...]

>         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.

I wish to toss out a new thought.  To that end let me blow up the example
to underline a scalability issue:

A. q ++ " " ++ a ++ " " ++ z ++ " [" ++ m ++ " -> " ++ k ++ " |" ++ p ++ "| "
   ++ g ++ " -> " ++ c ++ "] " ++ h ++ " " ++ b ++ " " ++ f ++ " " ++ i
B. printf "%s %s %s [%s -> %s |%s| %s -> %s] %s %s %s %s" q a z m k p g c h
   b f i

B looks clearer because without parsing you can see that the output will
contain a |blah| between two blah->blah's inside square brackets, etc.

A looks clearer because without counting you can see that p is the
thing that will go into |blah|, the first blah->blah will be m->k,
etc.

The best of both worlds may be something like the notation in the HOL
theorem prover:

``^q ^a ^z [^m -> ^k |^p| ^g -> ^c] ^h ^b ^f ^i``

Do you agree that this is much better?

Could someone implement something like this in GHC please? :)


More information about the Haskell-Cafe mailing list