[Haskell-cafe] Separate a string into a list of strings

Ketil Malde ketil+haskell at ii.uib.no
Tue Jun 13 02:51:12 EDT 2006

"Jared Updike" <jupdike at gmail.com> writes:

> On 6/12/06, Neil Mitchell <ndmitchell at gmail.com> wrote:

>> I tend to use the module TextUtil (or Util.Text) from Yhc for these
>> kind of string manipulations:

> Funny. I have a module called Useful.hs with some of these same sorts
> of functions. (coming from Python where I used .split(',') and
> .replace('\r', '') and such a lot):

Clifford Beshers writes:

> Here is a solution using the Posix regex module.

In addition, there are similar things in John Goerzen's MissingH, and
in FPS.  It'd be nice if the Data.List interface included these.
Seems there is a two-d matrix, one is the split criterion (matching
element, number of elements, boolean function on elements), the other
is the return type (split off one (-> ([a],[a])) or split up the whole
string (-> [[a]])).  Arbitrarily¹ naming the former 'split' and
the latter 'break', you could have something like:

split :: a -> [a] -> ([a],[a])
splitAt :: Int -> [a] -> ([a],[a])
splitWhen :: (a -> Bool) -> [a] -> ([a],[a])

break :: a -> [a] -> [[a]]
breakAt :: Int -> [a] -> [[a]]
breakWhen :: (a -> Bool) -> [a] -> [[a]]


¹ Well, perhaps not quite, it seems more natural to me to 'split in
two' and 'break into pieces'.
If I haven't seen further, it is by standing in the footprints of giants

More information about the Haskell-Cafe mailing list