[Haskell-cafe] Implicit concatenation in list comprehensions

Max Bolingbroke batterseapower at hotmail.com
Sun Jul 19 10:58:08 EDT 2009

Dear Cafe,

For fun, I spent a few hours yesterday implement support for this
syntax in GHC, originally propsed by Koen Claessen:

>>> [k, "=", v, " " | (k, v) <- [("foo", "1"), ("bar", "2")]
["foo", "=", "1", " ", "bar", "=", "2", " "]

This is a generalisation of list comprehensions that allows several
items to be concatenated onto the result list at once, by having
several comma-separated items before the pipe. One situation where I
have found this to be useful is when you are writing Haskell programs
that call lots of external programs and you need to set the flags
based on some tests, like so:

rawSystem "myProgram" $
  ["foo" | fooing_enabled] ++
  ["bar1", "bar2" | baring_enabled]

I have submitted a ticket to GHC HQ with the patch
(http://hackage.haskell.org/trac/ghc/ticket/3380#comment:5), but as it
is just a small convenience it most likely won't make it in unless
there is more demand for the feature. So, now is the time to speak up
in favour of (or indeed, against) the added syntax!

All the best,

P.S. I also implemented tuple sections
(http://hackage.haskell.org/trac/ghc/ticket/3377#comment:3) which are
a lot more useful:

>>> (, True, "Hello", ) 1 "World"
(1, True, "Hello", "World")

More information about the Haskell-Cafe mailing list