[Haskell-cafe] generalized list comprehensions
Jonathan Cast
jonathanccast at fastmail.fm
Mon Nov 10 13:32:37 EST 2008
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
One particular special case is where there is exactly one generator,
which has three further special cases:
[ value | boolean ]
[ value | pat <- expr ]
[ value | let binds ]
These are all valid because they are special cases of the general list
comprehension syntax; the de-sugarings are all just special cases of the
general list comprehension de-sugaring rules:
[ value | ] = [ value ]
[ value | boolean, generators ]
= if boolean then [ value | generators ] else []
[ value | pat <- expr, generators ]
= expr >>= \ x -> case x of pat -> [ value | generators ]; _ -> []
[ value | let binds, generators ]
= let binds in [ value | generators ]
So the special cases simplify to
[ value | boolean ] = if boolean then [ value ] else []
[ value | pat <- expr ]
= expr >>= \ x -> case x of pat -> [ value ]; _ -> []
[ value | let binds ] = let binds in [ value ]
Why wouldn't this work?
jcc
More information about the Haskell-Cafe
mailing list