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

2010-12-23 Thread C K Kashyap
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

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

2010-12-23 Thread Daniel Fischer
On Thursday 23 December 2010 18:27:43, C K Kashyap wrote: 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

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

2010-12-23 Thread Henning Thielemann
On Thu, 23 Dec 2010, 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)] That's an interesting problem! My first attempt: List.unfoldr (\xs - case xs of [] - Nothing; y:ys - case span (uncurry

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

2010-12-23 Thread Henning Thielemann
On Thu, 23 Dec 2010, Daniel Fischer wrote: On Thursday 23 December 2010 18:27:43, C K Kashyap wrote: 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

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

2010-12-23 Thread Daniel Fischer
On Thursday 23 December 2010 18:27:43, C K Kashyap wrote: 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

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

2010-12-23 Thread Ross Paterson
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

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

2010-12-23 Thread Stephen Tetley
I'd go with direct recursion for this one - the pattern of consumption and production that generates the answer doesn't seem to neatly match any of the standard recursion combinators (map, unfold, fold, mapAccum, ...) nor exotic ones (skipping streams c.f. the Stream fusion paper, apomorphisms,

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

2010-12-23 Thread Stephen Tetley
On 23 December 2010 21:12, Stephen Tetley stephen.tet...@gmail.com wrote: I'd go with direct recursion for this one - the pattern of consumption and production that generates the answer doesn't seem to neatly match any of the standard recursion combinators (map, unfold, fold, mapAccum, ...)

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

2010-12-23 Thread Henning Thielemann
On Thu, 23 Dec 2010, Stephen Tetley wrote: On 23 December 2010 21:12, Stephen Tetley stephen.tet...@gmail.com wrote: I'd go with direct recursion for this one - the pattern of consumption and production that generates the answer doesn't seem to neatly match any of the standard recursion

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

2010-12-23 Thread Stephen Tetley
On 23 December 2010 22:01, Henning Thielemann lemm...@henning-thielemann.de wrote: This could be seen as type Step st a = (Maybe a, st). I have thought about mapping from [Int] to [Maybe (Int, Int)] by mapAccumL, then compressing the result with catMaybes. However we need to append a final

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

2010-12-23 Thread Johannes Waldmann
This one looks somewhat symmetrical: f xs = let xys = filter ( \ (x,y) - y - x 1 ) $ zip xs ( tail xs ) in zip ( [ head xs ] ++ map snd xys ) ( map fst xys ++ [ last xs ] ) ___ Haskell-Cafe mailing list

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

2010-12-23 Thread Martin Erwig
All the previous solutions seem to assume that the list of numbers is already sorted. In cases where this assumption cannot be made, an alternative solution is to simply insert the numbers into a diet. eecs.oregonstate.edu/~erwig/papers/abstracts.html#JFP98