Reputation: 421
The code below is used to generate the intersection of two lists:
unionSet :: Eq a => [a] -> [a] -> [a]
unionSet a b = foldl (\acc x -> if elem x acc then acc else acc ++ [x]) a b
Why does the foldl
function work but when I use foldr
it generates errors?
Upvotes: 1
Views: 242
Reputation: 19221
foldr
has the type
(a -> b -> b) -> [a] -> b -> b
while foldl
has the type
(b -> a -> b) -> [a] -> b -> b
Note the order of the two parameters.
(\x acc -> ...
would fix the error.
Upvotes: 6