<div dir="ltr"><div><div><div><div><div><div><div>Hi,<br><br></div>The problem that I wish to solve is to divide a (sored) list of integers into sublists such that each sublist contains numbers in consecutive sequence.<br><br></div><div>For example,<br></div><div><b>Input:</b> [1,2,3,7,8,10,11,12]<br></div><b>Output:</b> [[1,2,3],[7,8],[10,11,12]]<br><br></div>I have written the following code and it does the trick.<br><br><span style="font-family:monospace,monospace">-- Take a list and divide it at first point of non-consecutive number encounter<br>divide :: [Int] -> [Int] -> ([Int], [Int])<br>divide first [] = (first, [])<br>divide first second = if (last first) /= firstSecond - 1 then (first, second)<br>                      else divide (first ++ [firstSecond]) (tail second)<br>                      where firstSecond = head second<br><br><br>-- Helper for breaking a list of numbers into consecutive sublists<br>breakIntoConsecsHelper :: [Int] -> [[Int]] -> [[Int]]<br>breakIntoConsecsHelper [] [[]] = [[]]<br>breakIntoConsecsHelper lst ans = if two == [] then ans ++ [one]<br>                                 else ans ++ [one] ++ breakIntoConsecsHelper two []<br>                                 where<br>                                      firstElem = head lst<br>                                      remaining = tail lst<br>                                      (one, two) = divide [firstElem] remaining<br><br><br>-- Break the list into sublists of consective numbers<br></span></div><div><span style="font-family:monospace,monospace">breakIntoConsecs :: [Int] -> [[Int]]<br>breakIntoConsecs lst = breakIntoConsecsHelper lst [[]]<br><br></span></div><div><span style="font-family:monospace,monospace">-- Take the tail of the result given by the function above to get the required list of lists.</span><br></div><div><br></div>However, I was wondering if there was a better way of doing this. Any help would be highly appreciated.<br><br></div>Thank you.<br></div>Best Regards,<br></div>Saqib Shamsi<br></div>