[Haskell-cafe] An alternative break

Joachim Breitner mail at joachim-breitner.de
Wed May 28 18:29:41 EDT 2008


Hi,

Am Mittwoch, den 28.05.2008, 23:53 +0200 schrieb Pieter Laeremans:
> Hello,
> 
> I need a break function that splits the list one element further than
> the ordinary break.
> This is the simplest solution I could imagine:
> 
> breakI :: (a -> Bool) -> [a] -> ([a], [a])
> breakI p s = case break p s of
>                ([], []) -> ([], [])
>                (x, []) -> (x, [])
>                (x,  l)  ->  (x ++ [head l], tail l )
> 
> Is there a better way to write this ?

appending an element to a list is expensive, so if this is a problem you
can try this:

breakI _ []             =  ([], [])
breakI p (x:xs')
           | p x        =  ([x],xs')
           | otherwise  =  let (ys,zs) = breakI p xs' in (x:ys,zs)

It is basically the Prelude.break from
http://haskell.org/ghc/docs/latest/html/libraries/base/src/GHC-List.html#break
with the forth line (with p x) changed.

Greetings,
Joachim

-- 
Joachim "nomeata" Breitner
  mail: mail at joachim-breitner.de | ICQ# 74513189 | GPG-Key: 4743206C
  JID: nomeata at joachim-breitner.de | http://www.joachim-breitner.de/
  Debian Developer: nomeata at debian.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20080528/b6c8dd23/attachment.bin


More information about the Haskell-Cafe mailing list