6.090 IAP '05 - Homework 7
Assigned Wednesday January 19th.
Due 10am Thursday January 20th.
Submit a print-out of your .scm file. Should you
encounter difficulty printing, the file may be emailed to
"No, not the dinosaur" you explain to Ben Bitdiddle, "it allows you to
look up related words." As usual, Ben Bitdiddle is trying to
implement the wrong thing and it's your job to help him out.
A thesaurus is comprised of words and their synonyms. In order to
simplify the implementation, we're going to write an abstraction for
thesaurus entry, which contains a word and a list of its synonyms.
Complete the constructors and selectors for the abstraction:
(define (make-entry word synonyms)
(define (entry-word entry)
(define (entry-synonyms entry)
;; example usage:
(define e (make-entry "victory" (list "conquest" "triumph" "win")))
;Value: ("conquest" "triumph" "win")
A thesaurus is a list of entries. Of primary interest are two
procedures: adding an entry to the thesaurus and looking a word up in
the thesaurus. First, we'll implement adding entries:
(define empty-thesaurus nil)
(define (add-to-thesaurus entry thesaurus)
(define t (add-to-thesaurus (make-entry "ball" (list "globe" "orb" "rondure" "sphere"))
;Value: "t --> ((\"ball\" ...))"
;Value: ("ball" ("globe" "orb" "rondure" "sphere"))
Once you've gotten a working add-to-thesaurus, download and
evaluate hw7code.scm. This will give you
two thesauri: simple-thesaurus and full-thesaurus.
The simple thesaurus is for simple testing, and the full version is
for serious testing.
In order to look a word up in the thesaurus, we must search through
the entries until we find one that matches. You should use
string=? to compare strings. Remember to respect the entry
abstraction. If the word is in the thesaurus, return the matching
entry. If the word is not in the thesaurus, return false.
(define (lookup word thesaurus)
(lookup "victory" simple-thesaurus)
;Value: ("victory" ("conquest" "triumph" "win"))
(lookup "defeat" simple-thesaurus)
Supposing that you are given the procedure pick-random, which
randomly picks an element of a list:
(define (pick-random lst)
(if (null? lst)
(list-ref lst (random (length lst)))))
Write the procedure transmogrify, which takes in a word and a
thesaurus and returns one of the synonyms of the word, picked at
random. If the word is not in the thesaurus, return the orginal word.
(define (transmogrify word thesaurus)
Now we can use this tool to rewrite sentences to make them sound more
linguistically impressive. Assume a sentence is given as a list of
words. You can use string-explode to convert a string into
this form. To put the string back together, you can use
restore-string. For example:
(string-explode "Assume a sentence is given as a list of words")
;Value: ("Assume" "a" "sentence" "is" "given" "as" "a" "list" "of" "words")
(restore-string (list "this" "is" "a" "string"))
;Value: "this is a string"
Write the procedure transmogrify-sentence that takes in a
sentence as a list of words and returns a new list of words with each
of the words transmogrified.
(define (transmogrify-sentence sentence simple-thesaurus)
(restore-string (transmogrify-sentence (string-explode "I went to the store")
;Value: "I went to the outlet"
(string-explode "He hit the ball with the bat to end the game with a grand victory")
;Value: "He strike the rundure with the bludgeon to end the game with a impressive triumph"