Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [TYS] Summarize a list of numbers
Index -> Programming, General Programming -> Functional Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: 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 tail-call elimination, don't worry about stack overflows. However, the code should be written such that tail-call elimination would be at least possible. You may also assume the list is sorted in ascending order.
Sponsor
Sponsor
Sponsor
sponsor
Prabhakar Ragde




PostPosted: 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




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, General Programming -> Functional Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: