An alternative break

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

An alternative break

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 ?

thanks in advance,

Pieter

--
Pieter Laeremans <[hidden email]>
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: An alternative break

pweaver
On Wed, May 28, 2008 at 2:53 PM, Pieter Laeremans <[hidden email]> wrote:

> 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 ?
>
Your first two cases are redundant; you can eliminate the first one.
Other than that, it looks fine.

> thanks in advance,
>
> Pieter
>
> --
> Pieter Laeremans <[hidden email]>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: An alternative break

Joachim Breitner-2
In reply to this post by Pieter Laeremans
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: [hidden email] | ICQ# 74513189 | GPG-Key: 4743206C
  JID: [hidden email] | http://www.joachim-breitner.de/
  Debian Developer: [hidden email]

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

signature.asc (196 bytes) Download Attachment