Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5. (8 points) In this question, we are going to write a Scheme program that calc

ID: 3862961 • Letter: 5

Question

5. (8 points) 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) (tine 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 uord-count pair, for example, (short 1) and (is 2) are word-count pairs. We call a list of word- count pairs a word-count list. (a) (2 points) 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,

Explanation / Answer

SCHEME CODE:

a) (define (initialWCList x) (map (lambda(y) (list y 1)) x))

b) (define wordInList #f);;set wordInList to false initially
(define (mergeWC x y) (if (null? y)

(if (equal? wordInList #t) '() (list x));; if word in word-count pair is in y return empty list
;;else add the word-count pair to list and return

(if (equal? (car x) (car (car y)));;if word in x matches any of word in y set wordInList to true and increment word count
;;and add word-count pair to list

(begin (set! wordInList #t) (cons (list (car x) (+ (car (cdr (car y))) 1)) (mergeWC x (cdr y))))

(cons (car y) (mergeWC x (cdr y)))) ));;else add the word-count pair to list without incrementing

;;.........using function initialWCList and mergeWC

(display (initialWCList '(time is long but life is short) ))
(newline)
(display (mergeWC '(is 1) '((time 1) (is 1))))

OUTPUT:

((time 1) (is 1) (long 1) (but 1) (life 1) (is 1) (short 1))

((time 1) (is 2))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote