[Haskell-cafe] generalized list comprehensions

Mitchell, Neil neil.mitchell.2 at credit-suisse.com
Mon Nov 10 11:14:35 EST 2008


> > Generalised? Heck, I don't use list comprehension at all! :-P
> 
> Perhaps you should! :-)

You definitely should! Take a look at the Uniplate paper for some
wonderful concise uses of list comprehensions for abstract syntax tree
traversals. If you use a language like F# they become even more common -
due to a clunkier syntax for lambdas, less library functions and no
operator sections. In my F# I rarely use a map at all.

But my faviourite list comprehension trick was shown to me by Colin
Runciman:

prettyPrint b (lhs :+: rhs) = ['('|b] ++ f lhs ++ " + " ++ f rhs ++
[')'|b]

Imagine b represents whether something should be bracketed or not. In
general:

if boolean then [value] else []

Can be written as:

[value | boolean]

Thanks

Neil

==============================================================================
Please access the attached hyperlink for an important electronic communications disclaimer: 

http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
==============================================================================



More information about the Haskell-Cafe mailing list