Question
Write a Scheme function int-to-words that takes a nonnegative integer between 0 and 99, inclusive, as a parameter and returns a list of words corresponding
Write a Scheme function int-to-words that takes a nonnegative integer between 0 and 99, inclusive, as a parameter and returns a list of words corresponding to the integers reading in English. Make your function as short as possible; i.e., dont write a COND statement with 100 tests.
Example calls to your function are given below:
(int-to-words 13) ; should return (thirteen) (int-to-words 42) ; should return (forty two) Note, in order to simplify things, we are not expecting dashes in the output (i.e., 42 is output as forty two not forty-two).
Hint: You may want to use the built-in integer division functions quotient and remainder.
In this question, we are going to write a Scheme program that calculates word counts. The input is a list of words and the output should be counts for words that appear in the input list. For example, if the input is (time is long but life is short) Then one output can be ((short 1) (is 2) (life 1) (but 1) (long 1) (time 1)) Note that you are not asked to sort the words in the output. Therefore, the output is correct as long as the counts are correct. Do the following steps to implement the word-count program. In our discussion, we call a word with a count a word-count pair, for example, (short 1) and (is 2) are word-count pairs. We call a list of wordcount pairs a word-count list.
Write a function initialWCList that takes a list of words and creates a word-count list. The resulting word-count list should have the word count 1 for every word. Use the map function we discussed in class to implement initialWCList. For instance, 2 (initialWCList (time is long but life is short)) should generate ((time 1) (is 1) (long 1) (but 1) (life 1) (is 1) (short 1))
Write a function mergeWC. It takes two inputs. The first is a word-count pair and the second is a word-count list. This function generates a new word-count list. If the input wordcount pair has a corresponding pair in the word-count list with the same word, then the counts should be merged; otherwise, the output word-count list should have the input word-count list with the word-count pair at the end of the list. For instance, (mergeWC (is 1) ((time 1) (is 1))) should generate ((time 1) (is 2)) As another example (mergeWC (life 1) ((time 1) (is 2))) should generate ((time 1) (is 2) (life 1))
Write a function mergeByWord, which takes a wordcount list and produces a new word-count list; the output wordcount list should have one word-count pair for each word that appears in the input list and the count should be the sum of all counts for that word in the input list. For instance, if the input list is ((time 1) (is 1) (long 1) (but 1) (life 1) (is 1) (short 1)) then the output should be ((short 1) (is 2) (life 1) (but 1) (long 1) (time 1)) 3 Write mergeByWord based on the reduce function we discussed in class and mergeWC. The reduce function is not built-in in Scheme; you can type it in yourself: (define (reduce f l v) (if (null? l) v (f (car l) (reduce f (cdr l) v))))
Finally, write a wordcount function that takes in a list of words and outputs the right word-count list. Write this function based on initialWCList and mergeByWord.
PLEASE PLEASE write these functions in Scheme so that I can use DrRacket to complile them, thank you!!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started