This was a challenge from a book: filter out Right
s from a list of [Either String Integer]
.
This works just fine
lst1 :: [Either String Integer]
lst1 = [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]
lefts' :: [Either a b] -> [a]
lefts' [] = []
lefts' (x:xs) = if isLeft x
then (\(Left z) -> z) x : lefts' xs
else lefts' xs
But I wanted to write the lambda as a separate function and could not figure out how to do it. I tried:
getLeftVal :: Either a b -> a
getLeftVal (Left l) = l
But no surprise it gives the pattern matches not exhaustive warning.
I could of course do:
getLeftVal :: Either a b -> a
getLeftVal x = (\(Left z) -> z) x
But is redundant and extra code.
I tried a few other things but none worked.