[Haskell-cafe] List of numbers to list of ranges

Ross Paterson ross at soi.city.ac.uk
Thu Dec 23 21:04:01 CET 2010


On Thu, Dec 23, 2010 at 10:57:43PM +0530, C K Kashyap wrote:
> Here's my attempt to convert a list of integers to a list of range tuples -
> 
> Given [1,2,3,6,8,9,10], I need [(1,3),(6,6),8,10)]

import Data.Function
import Data.List

ranges ns =
        [(head gp, last gp) |
                gp <- map (map fst) $ groupBy ((==) `on` snd) $
                        zip ns (zipWith (-) ns [1..])]



More information about the Haskell-Cafe mailing list