[TYS] Summarize a list of numbers
Author 
Message 
wtd

Posted: Sun Nov 22, 2009 1:56 am Post subject: [TYS] Summarize a list of numbers 


So, let's say we have a list of numbers like so:
code:  [1, 3, 4, 5, 7, 8, 10, 11, 13, 15] 
We want to summarize this by getting a list of start and end points.
code:  [(1, 1), (3, 5), (7, 8), (10, 11), (13, 13), (15, 15)] 
Implement this behavior recursively using a left fold. You may either implement your own left fold, or use one from a programming language's standard library. For those implementing in a language which does not support tailcall elimination, don't worry about stack overflows. However, the code should be written such that tailcall elimination would be at least possible. You may also assume the list is sorted in ascending order. 





Sponsor Sponsor



Prabhakar Ragde

Posted: Sun Nov 22, 2009 3:44 pm Post subject: Re: [TYS] Summarize a list of numbers 


It's more natural to use a right fold, and to produce the flat list, which you can easily bundle up into pairs afterwards.
If you use a right fold, in a lazy language you can produce the list lazily, and it will work on infinite lists. PR 





wtd

Posted: Sun Nov 22, 2009 4:05 pm Post subject: RE:[TYS] Summarize a list of numbers 


I think you're right. That'll teach me to post when I'm tired. 






