0

Can anyone help me to generate all the subsets of a given set?

Example:If I have [2,3,4] and if I want K=2, that means I need pairs of two => [[2,3], [3,2], [2,4], [4,2], [3,4], [4,3]]

I wrote this code, but it generates only the number of subsets:

arrange::Int->Int->Int
arrange n 1=n
arrange n r=n*arrange (n-1) (r-1)

Another version, but this doesn't generate all solutions of the subsets:

  arrange 0 _ =[[]]
  arrange _ []=[]
  arrange n (x:xs)=(map(x:)) (arrange (n-1) xs)++
                   (arrange n xs)
Andi
  • 19
  • 1
  • 3
  • No. But if you have any idea for my example, please help me with an implementation. – Andi May 17 '15 at 07:36
  • the appropriate name is subset and not "Arrangements of a list". please update your question. I've added an answer for what you are looking for.. – DeJaVo May 17 '15 at 07:39
  • 2
    @DeJaVo, no, that is not the appropriate name either. "Subsets" suggests that order does not matter. According to Wikipedia, these are called "partial permutations", or when specific sizes are known, "k-permutations of n" (e.g., 2-permutations of 3). – dfeuer May 17 '15 at 14:29

1 Answers1

3

Well based on your example this is a possible solution:

import Data.List (permutations)

pick :: Int -> [a] -> [[a]]
pick 0 _ = [[]]
pick _ [] = []
pick n (x:xs) = map (x:) (pick (n-1) xs) ++ pick n xs

arrange :: Int -> [a] -> [[a]]
arrange n = concatMap permutations . pick n

example

λ> arrange 2 [2,3,4]
[[2,3],[3,2],[2,4],[4,2],[3,4],[4,3]]

as you can see the trick is just picking a number of elements and then getting all permutations of the results (using concatMap to concat them together)

of course this might be homework so you might want to implement permutations by yourself ;)

Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • I wrote the code for permutations, but it still doesn't work. 'import Data.List (delete)' pick :: Int -> [a] -> [[a]] pick 0 _ = [[]] pick _ [] = [] pick n (x:xs) = map (x:) (pick (n-1) xs) ++ pick n xs permutations :: [a] -> [[a]] permutations [] = [[]] permutations xs = [ x:ys | x <- xs, ys <- permutations (delete x xs)] arrange :: Int -> [a] -> [[a]] arrange n = concatMap permutations . pick n – Andi May 17 '15 at 10:57
  • I get this error: ERROR file:{Hugs}\An III\Haskell\aranj2.hs:10 - Cannot justify constraints in explicitly typed binding *** Expression : permutations *** Type : [a] -> [[a]] *** Given context : () *** Constraints : Eq a – Andi May 17 '15 at 11:01
  • And if I modify the declaration of permutations with this one permutations :: Eq a => [a] -> [[a]], I get this error: Cannot justify constraints in explicitly typed binding *** Expression : arrange *** Type : Int -> [a] -> [[a]] *** Given context : () *** Constraints : Eq a – Andi May 17 '15 at 11:03
  • well using `delete` you have to include the `Eq a` everywhere you are using it (including my `arrange`) but there is a way (obvious) of writing `permutations` without delete ;) - but yours is fine - just add the constraints – Random Dev May 17 '15 at 11:20
  • Thank you for your indications. It helps me a lot. – Andi May 17 '15 at 11:41
  • Glad I could help - if this answers your question you might want to accept it - if not I will happily try to help you further – Random Dev May 17 '15 at 12:25
  • Could you explain what the pick function actually does? – JavascriptLoser Aug 27 '17 at 07:53
  • @JavascriptLoser it generates a list of ways to pick n elements from the input list (without reordering) - for example `pick 2 [1,2,3] = [[1,2],[1,3],[2,3]]` – Random Dev Aug 27 '17 at 11:00