Search code examples
listhaskellelementset-difference

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?


Solution

  • 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]