[Haskell-cafe] generalized list comprehensions

Andrew Coppin andrewcoppin at btinternet.com
Mon Nov 10 13:48:44 EST 2008


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.



More information about the Haskell-Cafe mailing list