[Haskell-cafe] Best way to build strings?

Tomasz Zielonka tomasz.zielonka at gmail.com
Wed Jul 20 13:35:20 EDT 2005

On Wed, Jul 20, 2005 at 07:00:22PM +0200, Lemmih wrote:
> On 7/20/05, Andy Gimblett <A.M.Gimblett at swansea.ac.uk> wrote:
> > 
> > Is there a facility like this in Haskell?  Or something else I should
> > be using, other than lots of ++ ?
> There's Text.Printf:
> Prelude Text.Printf> printf "(%s [] %s)" "hello" "world" :: String
> "(hello [] world)"

If you only use GHC, you can also implement (or borrow) a type-safe
printf using Template Haskell. I think there's some implementation
made by Ian Lynagh.

Recently I needed to build strings containing shell commands and I
also didn't like the ++ approach. I didn't like the printf approach,
but rather wanted something more like shell's or Perl's string
interpolation, so I could write something like "($p [] $q)" and have
$p and $q expanded to values of p and q.

I created a small TH library for this (attached). It uses such syntax:
$(interp "(%{p} [] %{q})"). I used % because I knew I would often have
to use literal $'s.

Unfortunately it has some problems. First, TH sometimes doesn't like
when I use a global variable in %{ }. I had to work around it by
defining additional local helper variables.

Second, it would be nice to be able to put arbitrary Haskell expressions
inside %{ } - but I couldn't find a Haskell syntax parser producing TH
ASTs. There must be some - I guess Template Haskell uses one internally.

Best regards
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Interpolate.hs
Type: text/x-haskell
Size: 719 bytes
Desc: not available
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20050720/abc80f13/Interpolate.bin

More information about the Haskell-Cafe mailing list