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?
You don't need to write filterJust function. It is already in base and it is called catMaybes:
Also, you can see better way to define this function:
catMaybes :: [Maybe a] -> [a]
catMaybes ls = [x | Just x <- ls]
So all you need to do is just add import Data.Maybe (catMaybes) into your module.
Here this function is using the "MonadFail sugar" design pattern for lists. You can read more about it and other patterns in the following blog post:
/= 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)).filterJust works for all types a, even those that don't implement Eq: [Maybe a] -> [a]
Therefore filterJust can't use /=.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With