Prelude function suggestions
msjogren at gmail.com
Thu Jul 29 06:40:08 EDT 2004
On 29 Jul 2004 12:32:19 +0200, Peter Simons <simons at cryp.to> wrote:
> Simon Marlow writes:
> > split :: (a -> Bool) -> [a] -> [[a]]
> > tokens :: (a -> Bool) -> [a] -> [[a]]
> Unfortunately, neither function would help me solve my
> \r\n-line-ending case. I need separators that are longer
> than one "character". I also think the idea of being able to
> use the full "current prefix" for the decision is important.
> I can't use these function, because they would, for example,
> erroneously split the string "abc\ndef".
> So far, I like the breakWhere function Ketil proposed best.
I often use an even more generic function:
splitter :: ([a] -> (b,[a])) -> [a] -> [b]
splitter _  = 
splitter f xs = b : splitter f rest
where (b,rest) = f xs
The name might not be ideal, but the general mechanics of it is pretty nice.
More information about the Libraries