I am confused on the implementation of (\\)

Submitted by metaperl on Wed, 10/05/2005 - 5:05pm.

From the Haskell Online Report we have:

(\\) :: Eq a => [a] -> [a] -> [a]
(\\) = foldl (flip delete)

The (flip delete) changes the type signature to the following:


*Main> :t (flip delete)
forall a. (Eq a) => [a] -> a -> [a]

and foldl usually is obvious: it folds the list from the left, or the beginning. Meaning, it takes the seed element and the first element of the list and produces a result. This result is the new seed to be used with the next element of the list and so on until a result is produced.

But what is the flipped version of foldl doing in this case? It receives an entire list in the position it normally just receives an element.

Submitted by lightstep on Fri, 10/07/2005 - 6:37am.

foldl is the fundamental list iteration combinator.

"foldl f seed list" iterates over the list, applying f to the elements of it, and to the seed. The type of the seed can be very complex, it may even be a list.

In the case of (\\), "l1 \\ l2" reduces to "foldl (flip delete) l1 l2", iterating over the elements of l2, deleting each of them from the intermediate result. We get l1 with each element of l2 removed from it.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.