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

C K Kashyap ckkashyap at gmail.com
Thu Dec 23 18:27:43 CET 2010


Hi all,

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)]

My attempt using foldl yields me the output in reverse. I can ofcourse
reverse the result, but what would be a better way?

f xs = foldl ff [] xs
	where
	  	[]  `ff` i = [(i,i)]
		((s,e):ns) `ff` i = if i == e+1 then
					(s,i):ns
					else
					(i,i):(s,e):ns

Regards,
Kashyap



More information about the Haskell-Cafe mailing list