Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't seem to get my head around the 'list difference' (\\) operator

I have heard the term 'list difference' (\\) operator in Haskell but still don't quite know how to get my head around it. Any examples or ideas?

like image 499
maclunian Avatar asked May 29 '11 13:05

maclunian


2 Answers

The (\\) operator (and the difference function) implements set difference, so, if you have two lists, a and b, it returns only those elements of a that are not in b, as illustrated:

enter image description here

like image 127
Don Stewart Avatar answered Oct 04 '22 21:10

Don Stewart


Simply put, it takes two lists, goes through the second and for each item, removes the first instance of the same item from the first list.

> [1..10] \\ [2, 3, 5, 8]
[1,4,6,7,9,10]
> [1, 2, 1, 2, 1, 2] \\ [2]
[1,1,2,1,2]
> [1, 2, 1, 2, 1, 2] \\ [2, 2]
[1,1,1,2]
> [1, 2, 1, 2, 1, 2] \\ [2, 2, 1]
[1,1,2]
like image 35
Samir Talwar Avatar answered Oct 04 '22 21:10

Samir Talwar