Search code examples
haskellhaskell-platform

Haskell powerset sublists with fixed length


It's well known that the powerset of a list: {1,2,3,4} is {{},{1},{2},{1,2},{3},{1,3},{2,3},{1,2,3},{4},{1,4},{2,4},{1,2,4},{3,4},{1,3,4},{2,3,4},{1,2,3,4}}

the haskell code I got for that problem is:

potencia [] = [[]]

potencia (a:bs) = potencia bs ++ map (a:) (potencia bs)

Now, how would I get a list of sublists of the same length?, for example, the list above would generate the next list of sublists of length 3 = {{1,2,3},{1,2,4},{1,3,4}}

I'm a student sorry for my english, thanks in advance... XD


Solution

  • How about

    sublists  _     0 = [[]]
    sublists  []    _ = []
    sublists (x:xs) n = sublists xs n ++ map (x:) (sublists xs $ n - 1)
    

    Which is very similar to the code you had but just has two decreasing parameters, the length and the list.

    Also, for more advanced Haskellers

    powerset = flip runCont id . foldM step [[]]
      where step xs x = cont $ \c -> c xs ++ c (map (x:) xs)
    

    is a powerset implementation without recursion using continuations. Doing the same with the sublists function is an interesting challenge.