I'm having trouble with this program.
filterJust :: [Maybe a] -> [a]
filterJust [] = []
filterJust x = map fromJust (filter (isJust) x)
but ghci keeps reporting this
EDIT:
I don't want to use any additional module so i made this:
filterJust :: [Maybe a] -> [a]
filterJust x = map unpack (filter (Nothing /=) x)
unpack (Just a) = a
and i get this message
and i dont understand why. I should be able to use Eq functions without importing anthing right?
/=
can only be used on values of a type that implementsEq
((/=) :: (Eq a) -> a -> a -> Bool
).Maybe a
supportsEq
only ifa
does (there's aninstance (Eq a) => Eq (Maybe a)
).filterJust
works for all typesa
, even those that don't implementEq
:[Maybe a] -> [a]
Therefore
filterJust
can't use/=
.You don't need to write
filterJust
function. It is already inbase
and it is calledcatMaybes
: http://hackage.haskell.org/package/base-4.9.0.0/docs/Data-Maybe.html#v:catMaybesAlso, you can see better way to define this function:
So all you need to do is just add
import Data.Maybe (catMaybes)
into your module.