Filtering Nothing and unpack Just

前端 未结 2 911
长发绾君心
长发绾君心 2021-01-17 18:56

I\'m having trouble with this program.

filterJust :: [Maybe a] -> [a]

filterJust [] = []
filterJust x = map fromJust (filter (isJust) x)
<
2条回答
  •  野的像风
    2021-01-17 19:29

    • /= can only be used on values of a type that implements Eq ((/=) :: (Eq a) -> a -> a -> Bool).
    • Maybe a supports Eq only if a does (there's an instance (Eq a) => Eq (Maybe a)).
    • Your type signature says that filterJust works for all types a, even those that don't implement Eq: [Maybe a] -> [a]

    Therefore filterJust can't use /=.

提交回复
热议问题