[Haskell-cafe] generalized list comprehensions

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Mon Nov 10 11:04:42 EST 2008


On Sun, 2008-11-09 at 19:18 +0000, Andrew Coppin wrote:
> Derek Elkins wrote:
> > As far as I can tell, no one actually uses parallel list comprehensions.
> > With any luck, the same will be true for generalized list
> > comprehensions.
> >   
> 
> Generalised? Heck, I don't use list comprehension at all! :-P

Perhaps you should! :-)

When I first started with Haskell I kind of had the idea that list
comprehensions were just for beginners and that 'real' hackers used just
concatMaps and filters.

A couple years later I 'rediscovered' list comprehensions and I now use
them frequently. There are many cases in real programs where simple and
not-so-simple list comprehensions are the clearest way of expressing the
solution. In particular the easy support for refutable pattern matching
in the generators allows some succinct and clear code.

Just a random example out of Cabal:

warn verbosity $
     "This package indirectly depends on multiple versions of the same "
  ++ "package. This is highly likely to cause a compile failure.\n"
  ++ unlines [ "package " ++ display pkg ++ " requires "
            ++ display (PackageIdentifier name ver)
             | (name, uses) <- inconsistencies
             , (pkg, ver) <- uses ]

Pretty concise and clear I think.

Duncan



More information about the Haskell-Cafe mailing list