list - Is it worth converting to Sets? -


i'm dealing lists in program, , want able check whether 2 lists intersect or not. attempt @ implementation

commonele :: (eq a) => [a] -> [a] -> bool commonele _ [] = false commonele [] _ = false commonele (x:xs) ys     |x `elem` ys = true     |otherwise = commonele xs ys   

this works, i'm trying careful efficiency things don't blow up. this question explains in 1 of answer more efficient use sets check intersections. list automatically have distinct elements, using sets might idea, natural way build data in list comprehension, i'd have use fromlist function turn set. how tell implementation more efficient?

for record i'm going have check lots of small lists (~10^5 of size <100).

you mention in comments sets pairs of coordinates (int,int) each coordinate in range [1..10].

in case should use "bit set" can use processor's bitwise , and or operations set intersection , union.

the module data.bitset.dynamic can used purpose:

import qualified data.bitset.dynamic b  type bset = b.bitset b.fasterinteger  -- convert list of points bit set toset :: [(int,int)] -> bset toset points = b.fromlist [ fromintegral (r*10+c) | (r,c) <- points ]  -- 2 bit sets have common elements? commonelements :: bset -> bset -> bool commonelements b = not $ b.null $ b.intersection b  -- add point bit set addpoint :: bset -> (int,int) -> bset addpoint (r,c) = b.insert (fromintegral $ r*10+c)  -- convert bit set list of points topoints :: bset -> [(int,int)] topoints = [ (q,r) | x <- b.tolist a, let (q,r) = divmod (fromintegral x) 10 ]  -- set have point? haspoint :: bset -> (int,int) -> bool haspoint (r,c) = b.member (fromintegral $ r*10+c) 

detecting if 2 sets have common elements or if point in set fast.


Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -