belongs :: Eq(a) => a -> [a] -> Maybe a
belongs e (h:t) = if ( h == e) then Nothing else belongs e t
belongs e [] = Just e
nub :: Eq(a) => [a] -> [a]
nub l = nub' l []
where
nub' [] new_list = new_list
nub' (h:t) new_list = nub' t (new_list ++ [(belongs h new_list)])
Error:
Occurs check: cannot construct the infinite type: a0 = Maybe a0
Expected type: [a0]
Actual type: [Maybe a0]
In the second argument of `belongs', namely `new_list'
In the expression: (belongs h new_list)
In the second argument of `(++)', namely `[(belongs h new_list)]'
I understand error. But I don't know how to repair it. I don't understand why Maybe Eq(a)
cannot be matched with a
. So how to repair it. I would like use Maybe here if is it possible.