# how to split a list

8 messages
Open this post in threaded view
|

## how to split a list

 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
Open this post in threaded view
|

## Re: how to split a list

 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
Open this post in threaded view
|

## Re: how to split a list

 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
Open this post in threaded view
|

## Re: how to split a list

 > 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
Open this post in threaded view
|

## Re: how to split a list

 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
Open this post in threaded view
|

## Re: how to split a list

 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