Why is there no splitBy in the list module? + other suggestions

isto isto.aho at dnainternet.net
Mon Jul 10 12:34:36 EDT 2006


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 at 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




More information about the Libraries mailing list