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
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
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'')
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 -
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
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
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
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.
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
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
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
11 matches
Mail list logo