Reputation: 5063
belongs :: Eq a => a -> [a] -> [a]
belongs e (h:t) = if ( h == e) then [] else belongs e t
belongs e [] = [e]
nub :: Eq a => [a] -> [a]
nub l = nub' l []
where
nub' [] new_list = new_list
nub' (x:xs) new_list = (nub' xs ((\ e new_list ->
case (belongs e new_list) of
[] -> new_list
(h:t) -> (h:new_list))(x new_list)))
Couldn't match expected type
[a]
with actual type[a0] -> [a0]
In the return type of a call ofnub'
Probable cause:nub'
is applied to too few arguments In the expression:nub' l []
In an equation fornub
:nub l = nub' l [] where nub' [] new_list = new_list nub' (x : xs) new_list = (nub' xs ((\ e new_list -> case (belongs e new_list) of { [] -> ... (h : t) -> ... }) (x new_list)))
Couldn't match expected type
[a0] -> [a0]
with actual type[a1]
In the second argument ofnub'
, namely[]
In the expression:nub' l []
In an equation fornub
:nub l = nub' l [] where nub' [] new_list = new_list nub' (x : xs) new_list = (nub' xs ((\ e new_list -> case (belongs e new_list) of { [] -> ... (h : t) -> ... }) (x new_list)))
Why it doesn't work? I cannot understand. Please give me a hand. The errors are understandable but I don't know their causes.
Upvotes: 0
Views: 76
Reputation: 5477
Are you trying to force a function call with the lambda and the extra parenthesis ((\e new_list -> ...)(x new_list))
? If so, that doesn't work for one, and even if that syntax would work then your parenthesis would be mismatched.
Forego the 'attempted function call' and the lambda and just use x
and new_list
directly in nub'
, or if you really want to rename use a let e = x in
to bind e
. That should fix the type errors.
If you really want the lambda, then you should know that function application is done using spaces and not parenthesis, so like (\x -> x + 1) 3
.
Upvotes: 3