The most efficient way to create a Data.Set of all pairs of elements in Set?

For an arbitrary set containing an arbitrary number of elements of an arbitrary type, for example

mySet1 = Set.fromList [1,2,3,4]

or

mySet2 = Set.fromList ["a","b","c","d"]

or

mySet3 = Set.fromList [A, B, C, D]

for some data constructors A, B, C, D, ...

What is the most computationally efficient way to generate a set of all disordered pairs of elements - is it a given set? I.e.

setPairs mySet1 == Set.fromList [(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)]

or

setPairs mySet2 == fromList [ ("a","b")
                            , ("a","c")
                            , ("a","d")
                            , ("b","c")
                            , ("b","d")
                            , ("c","d") ]

or

setPairs mySet2 == fromList [ (A,B)
                            , (A,C)
                            , (A,D)
                            , (B,C)
                            , (B,D)
                            , (C,D) ]

My initial naive guess:

setPairs s = fst $ Set.fold
    (\e (pairAcc, elementsLeft) ->
        ( Set.fold
              (\e2 pairAcc2 ->
                  Set.insert (e2, e) pairAcc2
              ) pairAcc $ Set.delete e elementsLeft
        , Set.delete e elementsLeft )
    ) (Set.empty, s) s

but of course this may not be the best solution?

+5
source share
3 answers

, , . O (n ^ 2), , . , S.fromDistinctAscList, O (n) .

, , , , , .

pairs s = S.fromDistinctAscList . concat $ zipWith zip (map (cycle . take 1) ts) (drop 1 ts)
   where ts = tails $ S.toList s

Edit

/ ( , , , /):

pairs s = S.fromDistinctAscList [(x,y) | (x:xt) <- tails (S.toList s), y <- xt]
+6

. replicateM Control.Monad .

λ> replicateM 2 [1..4]
[[1,1],[1,2],[1,3],[1,4],[2,1],[2,2],[2,3],[2,4],[3,1],[3,2],[3,3],[3,4],[4,1],[4,2],[4,3],[4,4]]

,

λ> filter (\[x,y] -> x < y)  $ replicateM 2 [1 .. 4]
[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]

,

λ> map (\[x,y] -> (x,y)) $ filter (\[x,y] -> x < y)  $ replicateM 2 [1 .. 4]
[(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)]

pairs:

import Data.Set
import Control.Monad
import Data.List

mySet = Data.Set.fromList [1,2,3,4]

--setOfPairs = Data.Set.fromList [(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)]
setOfPairs = Data.Set.fromList $ pairs mySet

pairs :: Ord a => Set a -> [(a,a)]
pairs x = Data.List.map (\[x,y] -> (x,y)) $ Data.List.filter (\[x,y] -> x < y) $ replicateM 2 $ toList x

, , pairs mySet, mySet.

, ?

UPD

" " , pairs:

pairs :: Ord a => Set a -> [(a,a)]
pairs set = [(x,y) | let list = toList set, x <- list, y <- list, x < y]
+1

, . , , , , .

import Data.List
import qualified Data.Set as S

pairs :: S.Set String -> S.Set (String,String)
pairs s = S.fromList $ foldl' (\st e -> (zip l e) ++ st) [] ls
          where (l:ls) = tails $ S.toList s

zip , . , , filterM foldM , . .

[EDIT]

, [ - poweret] , toList.

import Data.List
import qualified Data.Set as S
import qualified Data.Foldable as F

pairs :: (Ord a) => S.Set a -> S.Set (a,a)
pairs s = S.fromList $ foldl two [] $ F.foldlM (\st e -> [[e]++st,st]) [] s
          where two st (x:xa:[]) = (x,xa) : st
                two st _ = st

power-set , . .

+1

All Articles