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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.