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:
d914d08
)
Add intercalate and split to Data.List
author
Josef Svenningsson
<josef.svenningsson@gmail.com>
Tue, 24 Oct 2006 17:23:57 +0000
(17:23 +0000)
committer
Josef Svenningsson
<josef.svenningsson@gmail.com>
Tue, 24 Oct 2006 17:23:57 +0000
(17:23 +0000)
Data/List.hs
patch
|
blob
|
history
diff --git
a/Data/List.hs
b/Data/List.hs
index
7c3cede
..
f9a1226
100644
(file)
--- a/
Data/List.hs
+++ b/
Data/List.hs
@@
-35,6
+35,7
@@
module Data.List
, reverse -- :: [a] -> [a]
, intersperse -- :: a -> [a] -> [a]
, reverse -- :: [a] -> [a]
, intersperse -- :: a -> [a] -> [a]
+ , intercalate -- :: [a] -> [[a]] -> [a]
, transpose -- :: [[a]] -> [[a]]
-- * Reducing lists (folds)
, transpose -- :: [[a]] -> [[a]]
-- * Reducing lists (folds)
@@
-92,6
+93,8
@@
module Data.List
, span -- :: (a -> Bool) -> [a] -> ([a], [a])
, break -- :: (a -> Bool) -> [a] -> ([a], [a])
, span -- :: (a -> Bool) -> [a] -> ([a], [a])
, break -- :: (a -> Bool) -> [a] -> ([a], [a])
+ , split -- :: Eq a => a -> [a] -> [[a]]
+
, group -- :: Eq a => [a] -> [[a]]
, inits -- :: [a] -> [[a]]
, group -- :: Eq a => [a] -> [[a]]
, inits -- :: [a] -> [[a]]
@@
-396,6
+399,12
@@
intersperse _ [] = []
intersperse _ [x] = [x]
intersperse sep (x:xs) = x : sep : intersperse sep xs
intersperse _ [x] = [x]
intersperse sep (x:xs) = x : sep : intersperse sep xs
+-- | 'intercalate' @xs xss@ is equivalent to @('concat' ('intersperse' xs xss))@.
+-- It inserts the list @xs@ in between the lists in @xss@ and concatenates the
+-- result.
+intercalate :: [a] -> [[a]] -> [a]
+intercalate xs xss = concat (intersperse xs xss)
+
-- | The 'transpose' function transposes the rows and columns of its argument.
-- For example,
--
-- | The 'transpose' function transposes the rows and columns of its argument.
-- For example,
--
@@
-668,6
+677,19
@@
unzip7 = foldr (\(a,b,c,d,e,f,g) ~(as,bs,cs,ds,es,fs,gs) ->
deleteFirstsBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
deleteFirstsBy eq = foldl (flip (deleteBy eq))
deleteFirstsBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
deleteFirstsBy eq = foldl (flip (deleteBy eq))
+-- | 'split' @x xs@ divides the list @xs@ at every occurrence of @x@ and
+-- removes those occurrences.
+--
+-- Example:
+--
+-- > split 'i' "mississippi" -> ["m","ss","ss","pp",""]
+-- > split 'i' [] -> [""]
+split :: Eq a => a -> [a] -> [[a]]
+split x xs = let (f,r) = break (==x) xs
+ in f : case r of
+ [] -> []
+ (_:ys) -> split x ys
+
-- | The 'group' function takes a list and returns a list of lists such
-- that the concatenation of the result is equal to the argument. Moreover,
-- each sublist in the result contains only equal elements. For example,
-- | The 'group' function takes a list and returns a list of lists such
-- that the concatenation of the result is equal to the argument. Moreover,
-- each sublist in the result contains only equal elements. For example,