how to split a list

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

how to split a list

PICCA Frederic-Emmanuel
Hello, I try to achieve this but I can not find a convenient (elegant solution)

let l =[1,2, 3, 4, 6, 7, 9, 10]

I want this

[[1, 2, 3, 4][6, 7],[9, 10]]

In fact I want to split a list with all consecutive series.

I imagine that it existe a one-liner for this but I did not find it :))

Cheers and thanks for your help

Frederic
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: how to split a list

Francesco Ariis
On Mon, Mar 26, 2018 at 05:52:58PM +0000, PICCA Frederic-Emmanuel wrote:
> Hello, I try to achieve this but I can not find a convenient (elegant solution)
>
> let l =[1,2, 3, 4, 6, 7, 9, 10]
>
> I want this
>
> [[1, 2, 3, 4][6, 7],[9, 10]]

There's the usual zip trick

    λ> l = [1,2, 3, 4, 6, 7, 9, 10]
    λ> zipWith (-) l [1..]
    [0,0,0,0,1,1,2,2]
    λ> zip l it
    [(1,0),(2,0),(3,0),(4,0),(6,1),(7,1),(9,2),(10,2)]

Now you can groupBy on the second element. I am not sure there is a shorter
method!
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: how to split a list

Francesco Ariis
On Mon, Mar 26, 2018 at 08:11:44PM +0200, Francesco Ariis wrote:
> There's the usual zip trick
>
>     λ> l = [1,2, 3, 4, 6, 7, 9, 10]
>     λ> zipWith (-) l [1..]
>     [0,0,0,0,1,1,2,2]
>     λ> zip l it
>     [(1,0),(2,0),(3,0),(4,0),(6,1),(7,1),(9,2),(10,2)]

To be more explicit:

    λ> :m +Data.List (groupBy)
    λ> :m +Data.Function (on)
    λ> l = [1,2, 3, 4, 6, 7, 9, 10]
    λ> map (map snd) $ groupBy ((==) `on` snd) $ zip l (zipWith (-) l [1..])
    [[0,0,0,0],[1,1],[2,2]]

Maybe with some lens or with a foldr you can end up with something shorter
than this (69 characters).
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: how to split a list

PICCA Frederic-Emmanuel
> To be more explicit:

>    λ> :m +Data.List (groupBy)
>    λ> :m +Data.Function (on)
>    λ> l = [1,2, 3, 4, 6, 7, 9, 10]
>    λ> map (map snd) $ groupBy ((==) `on` snd) $ zip l (zipWith (-) l [1..])
>    [[0,0,0,0],[1,1],[2,2]]

I imagine that you mean fst instead of snd in order to produce the right output :)

How can I check if this code produce list copies and is efficient with big lists ?


thanks


Frederic
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: how to split a list

Francesco Ariis
On Mon, Mar 26, 2018 at 06:26:19PM +0000, PICCA Frederic-Emmanuel wrote:
> I imagine that you mean fst instead of snd in order to produce the right output :)

Indeed :P

> How can I check if this code produce list copies and is efficient with big lists ?

`time` and profiling tools provided by GHC [1]

If you feel lost, shout and I'll set up and example

[1] http://book.realworldhaskell.org/read/profiling-and-optimization.html
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: how to split a list

PICCA Frederic-Emmanuel
Just one question about this.

I do not want to put a print in order to generate the newList object.


let l = []
let newList = superChnker l
print newList


How Can I tell to haskell, produce the result even if I do not use it.
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: how to split a list

Theodore Lief Gannon
Wow... after attempting a simpler solution, I now understand a discussion from a little while back on one of the other lists about how terribly unexpected the default groupBy behavior is.

On Mon, Mar 26, 2018 at 12:21 PM, PICCA Frederic-Emmanuel <[hidden email]> wrote:
Just one question about this.

I do not want to put a print in order to generate the newList object.


let l = []
let newList = superChnker l
print newList


How Can I tell to haskell, produce the result even if I do not use it.
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: how to split a list

Francesco Ariis
In reply to this post by PICCA Frederic-Emmanuel
On Mon, Mar 26, 2018 at 07:21:22PM +0000, PICCA Frederic-Emmanuel wrote:
> How Can I tell to haskell, produce the result even if I do not use it.

deepseq [1]!

[1] https://hackage.haskell.org/package/deepseq-1.4.3.0/docs/Control-DeepSeq.html#v:deepseq
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners