Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with Maybe type in Haskell list comprehensions

I have a list comprehension in Haskell that specifies a predicate on a Maybe type:

[x | x <- listOfMaybes, isJust(f y), x == fromJust(f y)]

is there a way to simplify this expression?

like image 531
Rich Ashworth Avatar asked Nov 27 '22 07:11

Rich Ashworth


2 Answers

If you simply want to filter out all the Nothings, shang's answer gives the most concise solution. However, it can be done easily in a list comprehension:

[x | Just x <- f y]
like image 61
Stephan Avatar answered Dec 05 '22 03:12

Stephan


You can simply use catMaybes (from Data.Maybe) to filter out all Nothing values.

like image 45
shang Avatar answered Dec 05 '22 03:12

shang