Quoth John Meacham <[EMAIL PROTECTED]>:
...
| just a note: I find splitBy a much nicer routine to provide.
|
|> splitBy :: (a -> Bool) -- ^ whether char is a separator
|>         -> [a] -- ^ list to split
|>         -> [[a]]

Starting from the assumption that in most cases the list to be split
will be [Char], so we can reasonably hold this up against string functions
in other languages -- I think split is commonly

  split :: String -> String -> [String]

i.e., the separator is [a], not a.  Of course next they'll want regular
expressions, but I use string separators of length > 1 a lot and rarely
bother with regexps.

But string splitBy might be an interesting alternative to regexp splits.
Something like

  splitBy :: ([a] -> Maybe Int) -> [a] -> [[a]]

  breakBy :: ([a] -> Maybe Int) -> [a] -> Maybe ([a], [a])
  breakBy f s = by [] f s
        where
                by _ _ [] = Nothing
                by c f s@(a:ax) = case f s of
                        Just n -> Just ((reverse c), (drop n s))
                        Nothing -> by (a:c) f ax

  splitBy f s = case breakBy f s of
        Just (l, r) -> l:(splitBy f r)
        Nothing -> [s]

Where in the simplest case the matching function would simply compare with
a separator string, but could easily do more - eat up trailing blanks,
for example.

Note use of Maybe, basically to disambiguate end of data with and without
match.

        Donn Cave, [EMAIL PROTECTED]
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to