[Haskell-beginners] how to split a list

Francesco Ariis fa-ml at ariis.it
Mon Mar 26 18:11:44 UTC 2018


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!


More information about the Beginners mailing list