How can I write a really good mode?

How to write a Scheme function to find mode of a list of numbers?

  • Write the function mode that takes in a list of integers and returns the most frequently occuring number. For instance,(mode '(1 2 2 3 3 3 4)) returns 3, as it occurs in that list more than any other number. you can assume that the input list will have only one mode; that is, you won't see an input such as '(224 4). You can also assume that the input list is SORTED in ascending order. if the input list is null, simply return '() I'm not sure how to even get started on this if you could give me any direction I would really appreciate it.

  • Answer:

    A straighfoward way of doing it http://pastebin.com/Y5TmNcvD uncreative, but it works. Based on the idea that the longest run of equal numbers in a sequential list is the mode. A bit more clever goes like so http://pastebin.com/92dXNXPh based on the idea if you remove the last number in a run of equal number in a sequential list, the mode will be the last number left before the list is empty, if there is only one candidate for the mode. You could even catch cases on multiple modes if instead of "(null? (cdr L))" as the test in the second cond clause, you wrote a "no-duplicates?" predicate function.

sunkist sweetheart at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

A straighfoward way of doing it http://pastebin.com/Y5TmNcvD uncreative, but it works. Based on the idea that the longest run of equal numbers in a sequential list is the mode. A bit more clever goes like so http://pastebin.com/92dXNXPh based on the idea if you remove the last number in a run of equal number in a sequential list, the mode will be the last number left before the list is empty, if there is only one candidate for the mode. You could even catch cases on multiple modes if instead of "(null? (cdr L))" as the test in the second cond clause, you wrote a "no-duplicates?" predicate function.

ratter_o...

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.