[Haskell-cafe] Implicit concatenation in list comprehensions

Thomas Schilling nominolo at googlemail.com
Sun Jul 19 18:07:18 EDT 2009


2009/7/19 Max Bolingbroke <batterseapower at hotmail.com>
>
> 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", " "]

Given that this can easily be simulated via:

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

I believe that the added syntax (which every complete tool operating
on Haskell code would have to support) is not worth its price.

>
> 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,
> Max
>
> P.S. I also implemented tuple sections
> (http://hackage.haskell.org/trac/ghc/ticket/3377#comment:3) which are
> a lot more useful:

They are indeed, and here I would be inclined to consider the added
cost worth it.


--
Push the envelope.  Watch it bend.


More information about the Haskell-Cafe mailing list