[Pkg-haskell-commits] [SCM] haskell-testpack branch, master, updated. debian/1.0.2-1-4-gb0d6b36
John Goerzen
jgoerzen at complete.org
Fri Apr 23 14:42:28 UTC 2010
The following commit has been merged in the master branch:
commit 609709e89c8614ad19294a4beebae0353d146d93
Author: John Goerzen <jgoerzen at complete.org>
Date: Tue Oct 5 20:33:54 2004 +0100
Initial stab at split
Keywords:
(jgoerzen at complete.org--projects/missingh--head--1.0--patch-11)
diff --git a/MissingH/Listutil.hs b/MissingH/Listutil.hs
index bbeb479..03f29fc 100644
--- a/MissingH/Listutil.hs
+++ b/MissingH/Listutil.hs
@@ -22,8 +22,11 @@ Written by John Goerzen, jgoerzen\@complete.org
-}
module MissingH.Listutil(-- * Tests
- startswith, endswith
+ startswith, endswith,
+ -- * Conversions
+ split, join
) where
+import Data.List(intersperse)
{- | Returns true if the given list starts with the specified elements;
false otherwise.
@@ -51,3 +54,35 @@ Example:
-}
endswith :: Eq a => [a] -> [a] -> Bool
endswith x l = startswith (reverse x) (reverse l)
+
+{- | Given a delimiter and a list (or string), split into components.
+
+Example:
+
+> split "," "foo,bar,,baz," -> ["foo", "bar", "", "baz", ""]
+
+> split "ba" ",foo,bar,,baz," -> [",foo,","r,,","z,"]
+-}
+split :: Eq a => [a] -> [a] -> [[a]]
+split delim str =
+ let splitworker :: Eq a => [a] -> [a] -> [a] -> [[a]]
+ splitworker delim [] [] = []
+ splitworker delim [] accum = [accum]
+ splitworker delim str accum =
+ if delim == str then
+ accum : [] : []
+ else if startswith delim str then
+ accum : splitworker delim (drop (length delim) str) []
+ else splitworker delim (tail str) (accum ++ [head str])
+ in
+ splitworker delim str []
+
+{- | Given a delimiter and a list of items (or strings), join the items
+by using the delimiter.
+
+Example:
+
+> join "|" ["foo", "bar", "baz"] -> "foo|bar|baz"
+-}
+join :: a -> [a] -> a
+join delim l = foldl (++) [] (intersperse delim l)
diff --git a/MissingH/Strutil.hs b/MissingH/Strutil.hs
index 90847c6..ae8cebd 100644
--- a/MissingH/Strutil.hs
+++ b/MissingH/Strutil.hs
@@ -30,7 +30,7 @@ module MissingH.Strutil(-- * Whitespace Removal
-- * Conversions
split
) where
-import MissingH.Listutil(startswith, endswith)
+import MissingH.Listutil(startswith, endswith, split)
wschars = " \t\r\n"
@@ -65,24 +65,3 @@ lstrip s = case s of
rstrip :: String -> String
rstrip = reverse . lstrip . reverse
-{- | Given a delimiter and a list (or string), split into components.
-
-Example:
-
-> split "," "foo,bar,,baz," -> ["foo", "bar", "", "baz", ""]
-
-> split "ba" ",foo,bar,,baz," -> [",foo,","r,,","z,"]
--}
-split :: String -> String -> [String]
-split delim str =
- let splitworker :: String -> String -> String -> [String]
- splitworker delim [] [] = []
- splitworker delim [] accum = [accum]
- splitworker delim str accum =
- if delim == str then
- accum : [] : []
- else if startswith delim str then
- accum : splitworker delim (drop (length delim) str) []
- else splitworker delim (tail str) (accum ++ [head str])
- in
- splitworker delim str []
--
haskell-testpack
More information about the Pkg-haskell-commits
mailing list