Re: Why is there no splitBy in the list module?

Simon Marlow schrieb:
Donald Bruce Stewart wrote:
Hacking up your own custom split (or a tokens/splitOnGlue) must be one of the most common questions from beginners on the irc channel.
Anyone rememeber what the result of the "let's get split into the base library" movement's work was?
ISTR there wasn't a concensus, so nothing happened. Which is silly, really - I agree we should definitely have a Data.List.split.
Maybe someone can extract a result from the old discussion about "Prelude function suggestions" http://thread.gmane.org/gmane.comp.lang.haskell.libraries/1684/focus=1684 Cheers Christian Our current (special) version is: {- | A function inspired by the perl function split. A list is splitted on a seperator element in smaller non-empty lists. The seperator element is dropped from the resulting list. -} splitOn :: Eq a => a -- ^ seperator -> [a] -- ^ list to split -> [[a]] splitOn x xs = let (l, r) = break (==x) xs in (if null l then [] else [l]) ++ (if null r then [] else splitOn x $ tail r)

ma, 2006-07-10 kello 14:26 +0200, Christian Maeder kirjoitti: ...
Maybe someone can extract a result from the old discussion about "Prelude function suggestions"
http://thread.gmane.org/gmane.comp.lang.haskell.libraries/1684/focus=1684 splitOn :: Eq a => a -- ^ seperator -> [a] -- ^ list to split -> [[a]]
Was the consensus that several functions should be added to the predule, wasn't it? splitOn :: Eq a => a -> [a] -> [[a]] breakWhere :: ([a] -> Bool) -> [a] -> [[a]] splitter :: ([a] -> (b,[a])) -> [a] -> [b] etc How about the following? 1) On split splitRE :: [a] -> [a] -> [[a]] where the first list is a regular exression like in vim-editor. E.g. splitRE "(<br>){2}" myHTMLString Ok, the type has to be String and there seems to be the regexlib, giving splitRE = splitRegex . mkRegex splitList :: Eq a => [a] -> [a] -> [[a]] where the first list is a sublist used to split the second list. Somehow, I more often need this one than the breakWhere -version (but still finding it helpfull if breakWhere is put somewhere). See also the suggestion 4. 2) Other string/list functions taking string/lists as argument and not just a char/item. E.g. intersperse [a] -> [a] -> [a] map ([a] -> [b]) -> [a] -> [b] replicate :: Int -> [a] -> [b] etc I'll guess the "list versions" of takeWhile, dropWhile and the other in this section (sublist, extracting) are almost the same as splitRE above, except that splitRE is only for strings. "List versions" of elem and notElem are subList and notSubList. Are these somewhere (couldn't spot them in 2 seconds and don't rembr)? 3) Reshaping of a list Couldn't spot this either in another 2 seconds... Sorry if this is somewhere. reshape :: Int -> Int -> [a] -> [[a]] reshape r c a = [map (a!!) [c*i..c*i+(c-1)] | i<-[0..(r-1)]] Prelude Text.Regex> reshape 2 4 [1..8] [[1,2,3,4],[5,6,7,8]] Prelude Text.Regex> reshape 4 2 [1..8] [[1,2],[3,4],[5,6],[7,8]] Ok, it seems that sometimes there is a need for a version where multidimensional matrices are reshaped freely to any dimension as long as there are the correct number of items... (but haven't thought how to implement this one). 4) splitRE again splitRE :: [a] -> String -> [a] -> [[a]] First one here is the regex pattern basic elements of type a. Second one is the regex string having place holders for the basic elements. Third one is the list over which the split is done. E.g. splitRE [1, 15, 3] "\(@1@2\){2}@3" myNumberList would make splits when it faces sublist [1,15,1,15,3]. Here, @ would be used as a metacharacter to place the list items in to the regular expression of type a. Other text.regex-functions for type a? br, Isto

On Mon, Jul 10, 2006 at 02:26:23PM +0200, Christian Maeder wrote:
Our current (special) version is:
{- | A function inspired by the perl function split. A list is splitted on a seperator element in smaller non-empty lists. The seperator element is dropped from the resulting list. -} splitOn :: Eq a => a -- ^ seperator -> [a] -- ^ list to split -> [[a]] splitOn x xs = let (l, r) = break (==x) xs in (if null l then [] else [l]) ++ (if null r then [] else splitOn x $ tail r)
just a note: I find splitBy a much nicer routine to provide.
splitBy :: (a -> Bool) -- ^ whether char is a seperator -> [a] -- ^ list to split -> [[a]]
John -- John Meacham - ⑆repetae.net⑆john⑈

John Meacham schrieb:
just a note: I find splitBy a much nicer routine to provide.
I would support this, if it helped to find a consensus. It's more difficult to decide if empty lists as elements of the result list (at the beginning, at the end or in the middle) should be returned. I would say yes, because removing is quite easy afterwards. The problem typically occurs with a final newline, that may produce an empty last line (that again may be closed by a further final newline by mistake) Christian
splitBy :: (a -> Bool) -- ^ whether element is a seperator -> [a] -- ^ list to split -> [[a]]
P.S. inspecting more than one element looks like an over-generalization to me and should be left to parsers or regexp libs.
participants (3)
-
Christian Maeder
-
isto
-
John Meacham