Haskell opposite of intersect (List)
Asked Answered
H

1

7

I'm trying to get the "opposite" of intersect of two list: like:

let all  = [1..5]
let mask = [2,3]
let res  = ???
-- let res = all `intersect` mask <-- reverse/opposite ?
-- I want to get [1,4,5] ?
Hopehopeful answered 20/12, 2012 at 7:31 Comment(1)
I think that's called a relative complement.Bewhiskered
T
19

You're looking for set difference, which is the \\ operator from Data.List:

Prelude> import Data.List ((\\))
Prelude Data.List> let all  = [1..5]
Prelude Data.List> let mask = [2,3]
Prelude Data.List> all \\ mask
[1,4,5]
Toritorie answered 20/12, 2012 at 7:36 Comment(1)
@Hopehopeful But be aware that (\\) removes only one element from the first list per element of the second, e.g. [1,2,3,2] \\ [2] = [1,3,2]. If that's not good for your use case, filter (`notElem` second) first is an alternative.Pearman

© 2022 - 2024 — McMap. All rights reserved.