[Haskell-cafe] list comprehension with multiplegenerator|targets

Donn Cave donn at avvanta.com
Mon Nov 10 04:24:40 UTC 2014


quoth Raphaël_Mongeau <raphaelsimeon at gmail.com>
 
> This :
> pv a = [t | Just t <- pvc a]
> is strange, can we really do pattern matching inside list comprehension?

Sure, but from a list - I'm sorry, I meant "map pvc a", not "pvc a".

pv a = [t | Just t <- map pvc a]
    where
        pvc A = Just 'A'
        pvc B = Just 'B'
        pvc _ = Nothing

You know, the pattern match in the list comprehension is just what
I wanted it for in the first place - remember ['A' | A <- s] ?
That's OK, it just isn't useful because I can do this for only
one target.

> ... and I think is more clear with a lambdaCase.

lambdaCase is a great thing, but given that it's hardly any different

> pv l = catMaybes $ flip map l $ \case
>     A -> Just 'A'
>     B -> Just 'B'
>     _ -> Nothing

from

pv l = catMaybes $ map pvc l
    where
        pvc A = Just 'A'
        pvc B = Just 'B'
        pvc _ = Nothing

... in this case I don't think we're desperate enough to use a
nonstandard extension.

	Donn


More information about the Haskell-Cafe mailing list