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

Write a recursive Scheme procedure that takes complete quotation as symbols in a

ID: 3624106 • Letter: W

Question

Write a recursive Scheme procedure that takes complete quotation as symbols in a list and returns the largest word in the text. The program can be written in several stages. You can write your own functions and call the function in another function. The following Scheme functions may help you in writing the program.

(symbol->string ‘APPPLES) ? “APPLES”
(string-length “APPLES”) ? 6
(define y '(Computer Science is a science of abstraction creating the right model for a problem and devising the appropriate mechanizable techniques to solve it))

note The largest the word in this text is “mechanizable”

Explanation / Answer

So, the algorithm I came up with is a simple greedy algorithm.

If we have a set of n strings (string-1, string-2, ... string-n) the longest string will be the
longest(string-1, longest(string-2, string-3, ... string-n)). Knowing this there is really only one comparison you need to make in the algorithm.

; Finds the longest word in a given list of symbols.
(define longest-word
(lambda (li) ; li should be a list of symbols, but I do not check.
    (if (null? li)
        "" ; If li is empty, return an empty string (size 0).
        (let ((recursive (longest-word (cdr li))) ; Otherwise we define two bound variables to avoid
                                                  ; making unnesserary calls. The first variable is
                                                  ; the longest word from the recursive call.
              (cur (symbol->string (car li))))    ; The second is the current word.
          (if (> (string-length cur) (string-length recursive)) ; If the current string is longer...
              cur ; return the current string
              recursive))))) ; otherwise return the other string.

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