projects
/
ghc-base.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
bc0d9c0
)
'subsequences' is now more lazy and also faster
author
Twan van Laarhoven
<twanvl@gmail.com>
Wed, 2 Jan 2008 23:16:29 +0000
(23:16 +0000)
committer
Twan van Laarhoven
<twanvl@gmail.com>
Wed, 2 Jan 2008 23:16:29 +0000
(23:16 +0000)
Data/List.hs
patch
|
blob
|
history
diff --git
a/Data/List.hs
b/Data/List.hs
index
279438f
..
4cc9fdf
100644
(file)
--- a/
Data/List.hs
+++ b/
Data/List.hs
@@
-736,10
+736,19
@@
tails xxs@(_:xs) = xxs : tails xs
-- | The 'subsequences' function returns the list of all subsequences of the argument.
--
-- | The 'subsequences' function returns the list of all subsequences of the argument.
--
--- > subsequences "abc" == ["","c","b","bc","a","ac","ab","abc"]
+-- > subsequences "abc" == ["","a","b","ab","c","ac","bc","abc"]
subsequences :: [a] -> [[a]]
subsequences :: [a] -> [[a]]
-subsequences [] = [[]]
-subsequences (x:xs) = subsequences xs ++ map (x:) (subsequences xs)
+subsequences xs = [] : nonEmptySubsequences xs
+
+-- | The 'nonEmptySubsequences' function returns the list of all subsequences of the argument,
+-- except for the empty list.
+--
+-- > nonEmptySubsequences "abc" == ["a","b","ab","c","ac","bc","abc"]
+nonEmptySubsequences :: [a] -> [[a]]
+nonEmptySubsequences [] = []
+nonEmptySubsequences (x:xs) = [x] : foldr f [] (nonEmptySubsequences xs)
+ where f ys r = ys : (x : ys) : r
+
-- | The 'permutations' function returns the list of all permutations of the argument.
--
-- | The 'permutations' function returns the list of all permutations of the argument.
--