[Haskell-cafe] generalized list comprehensions
jonathanccast at fastmail.fm
Mon Nov 10 13:35:16 EST 2008
On Mon, 2008-11-10 at 18:19 +0000, Andrew Coppin wrote:
> Duncan Coutts wrote:
> > On Sun, 2008-11-09 at 19:18 +0000, Andrew Coppin wrote:
> >> 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.
> I don't actually use *lists* all that much - or at least not list
> transformations. And if I'm going to do something complicated, I'll
> usually write it as a do-expression rather than a comprehension.
> > 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.
> Erm... yeah, it's not too bad once I change all the formatting to make
> it clear what's what.
> Wouldn't it be a lot easier as a do-block though?
This was my first thought, too:
warn verbosity $
"This package indirectly depends on multiple versions of the same "
++ "package. This is highly likely to cause a compile failure.\n"
(name, uses) <- inconsistencies
(pkg, ver) <- uses
"package " ++ display pkg ++ " requires "
++ display (PackageIdentifier name ver) ++ "\n"
is equivalent; it's at least clearer in that the generators come before
the value, rather than after.
More information about the Haskell-Cafe