[Haskell-cafe] generalized list comprehensions

Jonathan Cast jonathanccast at fastmail.fm
Mon Nov 10 13:54:01 EST 2008


On Mon, 2008-11-10 at 18:48 +0000, Andrew Coppin wrote:
> Jonathan Cast wrote:
> > On Mon, 2008-11-10 at 18:20 +0000, Andrew Coppin wrote:
> >   
> >> Mitchell, Neil wrote:
> >>     
> >>> In general:
> >>>
> >>> if boolean then [value] else []
> >>>
> >>> Can be written as:
> >>>
> >>> [value | boolean]
> >>>   
> >>>       
> >> Is there any specific reason why this is valid?
> >>     
> >
> > Is there any specific reason to dis-allow it?  The grammar here looks
> > something like (NB: I didn't double-check the report):
> >
> >     list_compr ::= [ value | generator* ]
> >     generator ::= boolean | pat <- list | let binds
> >   
> 
> Hmm, that's interesting. I didn't know that a Boolean was a valid generator.
> 
> (Presumably this has the effect of filtering?)
> 
> The only time I use list comprehensions is when I quickly want a 
> Cartesian product. I wasn't really aware it could filter as well.

Funny.  About the only time I use list comprehensions is when I want a
generalized filter.

Serious computations get the do-notation, instead.  (And sometimes I
*have* to use do-notation for filtering, because I need an error monad
(usually Maybe) for other reasons).

jcc




More information about the Haskell-Cafe mailing list