[Haskell-cafe] wordsBy in the base libraries?

2007-10-22 Thread Maxime Henrion
Hello all, What do you think about having a wordsBy function in the standard libraries? It often comes in handy. wordsBy :: (a - Bool) - [a] - [[a]] wordsBy p s = case dropWhile p s of [] - [] ':rest - (s':w) : wordsBy p s'' where (w, s'') = break p rest This version

Re: [Haskell-cafe] wordsBy in the base libraries?

2007-10-22 Thread Maxime Henrion
This got a bit mangled, here's the fixed version: wordsBy :: (a - Bool) - [a] - [[a]] wordsBy p s = case dropWhile p s of [] - [] s':rest - (s':w) : wordsBy p s'' where (w, s'') = break p rest ___ Haskell-Cafe mailing list

Re: [Haskell-cafe] wordsBy in the base libraries?

2007-10-22 Thread Neil Mitchell
Hi We certainly need a function to split a list into sections. Whether it be wordsBy, or something like linesBy, or some new split variant. wordsBy :: (a - Bool) - [a] - [[a]] wordsBy p s = case dropWhile p s of [] - [] s':rest - (s':w) : wordsBy p s'' where (w, s'')

Re: [Haskell-cafe] wordsBy in the base libraries?

2007-10-22 Thread Jules Bean
Maxime Henrion wrote: Hello all, What do you think about having a wordsBy function in the standard libraries? It often comes in handy. I speculate (but don't know) that the reason we don't have one is that there are quite a few choices to make: * delimiter as function (Char -

Re: [Haskell-cafe] wordsBy in the base libraries?

2007-10-22 Thread Maxime Henrion
Jules Bean wrote: Maxime Henrion wrote: Hello all, What do you think about having a wordsBy function in the standard libraries? It often comes in handy. I speculate (but don't know) that the reason we don't have one is that there are quite a few choices to make: * delimiter

Re: [Haskell-cafe] wordsBy in the base libraries?

2007-10-22 Thread Yitzchak Gale
Maxime Henrion wrote: This version takes care of avoiding a redundant character check It still has several redundant checks: s'' is known to begin with space if it is non-empty, but that is checked again in the recursive call of wordsBy. In fact, break already checked whether s'' is empty, but

Re: [Haskell-cafe] wordsBy in the base libraries?

2007-10-22 Thread Yitzchak Gale
Neil Mitchell wrote: You are still over by one test. Try instead: wordsBy :: (a - Bool) - [a] - [[a]] wordsBy p s = case dropWhile p s of [] - [] s':rest - (s':w) : wordsBy p (drop 1 s'') where (w, s'') = break p rest This still has the redundant empty list tests, and

Re: [Haskell-cafe] wordsBy in the base libraries?

2007-10-22 Thread Neil Mitchell
Hi You are still over by one test. Try instead: wordsBy :: (a - Bool) - [a] - [[a]] wordsBy p s = case dropWhile p s of [] - [] s':rest - (s':w) : wordsBy p (drop 1 s'') where (w, s'') = break p rest This still has the redundant empty list tests, Perhaps.

Re: [Haskell-cafe] wordsBy in the base libraries?

2007-10-22 Thread Neil Mitchell
Hi If you want to guarantee that level of optimization, you probably have to inline everything by hand and not use any library functions at all. I wonder how much of that is already done by the compiler, though. A reasonable level of optimisation for this function would be that the

Re: [Haskell-cafe] wordsBy in the base libraries?

2007-10-22 Thread Yitzchak Gale
Neil Mitchell wrote: ...Also remember that p may be arbitrarily expensive, while a test for an empty list is cheap. In the particular case of words, p (i.e. isSpace) is very expensive... A reasonable level of optimisation for this function would be that the predicate is invoked at most once

Re: [Haskell-cafe] wordsBy in the base libraries?

2007-10-22 Thread Richard A. O'Keefe
On 22 Oct 2007, at 10:50 pm, Neil Mitchell wrote: wordsBy :: (a - Bool) - [a] - [[a]] wordsBy p s = case dropWhile p s of [] - [] s':rest - (s':w) : wordsBy p (drop 1 s'') where (w, s'') = break p rest For things like this, I think it may be easier to just write down a