Add 'subsequences' and 'permutations' to Data.List (ticket #1990)

David Benbennick dbenbenn at gmail.com
Tue Dec 18 14:18:56 EST 2007


Support, though I'd tweak the code in your patch a little:

subsequences            :: [a] -> [[a]]
subsequences []         =  [[]]
subsequences (x:xs)     =  let s = subsequences xs in s ++ map (x:) s

permutations            :: [a] -> [[a]]
permutations []         =  [[]]
permutations (x:xs)     =  concatMap interleave $ permutations xs
  where interleave []     =  [[x]]
        interleave (y:ys) =  (x:y:ys) : map (y:) (interleave ys)


In subsequences, make sure we don't calculate "subsequences xs" twice.
 In permutations, use : instead of ++ in one place, use concatMap
instead of list comprehension, and take out unnecessary parameter to
interleave.


More information about the Libraries mailing list