[Haskell-cafe] Faster set intersections?
Doug McIlroy
doug at cs.dartmouth.edu
Mon Dec 10 03:19:45 UTC 2018
> The following expressions both cause GHCI to hang:
>
> S.intersection (S.fromList [1,2]) (S.fromList [1..])
> S.intersection (S.fromList [1..]) (S.fromList [1,2])
>
> Is there a smarter way to take the intersection of sets when at least one
> of them is small (but I don't know which one that is)?
"Small" is not enough. If all you know is that the lists
represent sets of integers, then
S.intersection (S.fromList [-1,-2]) (S.fromList [1..])
must diverge. A sufficient condition for success in such examples
is that the sets come from a well-ordered domain and are presented
in order. Then it's easy to write a merge-like algorithm.
Both sets can be infinite; every finite prefix of the intersection
will eventually appear.
(Note that the integers are not well-ordered, though the positive
integers are.)
Doug
More information about the Haskell-Cafe
mailing list