[Haskell-cafe] list comprehension with multiple generator|targets

Mateusz Kowalczyk fuuzetsu at fuuzetsu.co.uk
Mon Nov 10 03:18:10 UTC 2014


On 11/10/2014 03:10 AM, Raphaël Mongeau wrote:
> I don't think list comprehension is the solution. What you want is a map.
> 
> Would this work?
> 
> 
> data V = A | B | C
> 
> f :: [V] -> String
> f l = flip map l $ \x -> case x of
>     A -> 'A'
>     B -> 'B'
>     C -> 'C'

Looks like the job for LambdaCase, ‘flip map l $ \case …’

Also it doesn't do what the OPs function does because it doesn't skip C.

> main = print $ f [A,B,C,C,A]
> 
> 2014-11-09 21:58 GMT-05:00 Donn Cave <donn at avvanta.com>:
> 
>> I'm guessing this isn't supported, but might be worth asking -
>> can I extend a list comprehension like  ['A' | A <- s] to multiple values?
>> Like,
>>
>> data V = A | B | C
>>
>> pv :: [V] -> [Char]
>> pv [] = []
>> pv (A:x) = 'A':(pv x)
>> pv (B:x) = 'B':(pv x)
>> pv (_:x) = pv x
>>
>> -- can that be a list comprehension, like
>>
>> pv s = [
>>           'A' | A <- s
>>           -- ??
>>           ]
>>
>> thanks,
>>         Donn


-- 
Mateusz K.


More information about the Haskell-Cafe mailing list