Exercise

Download the data file

  hamlet.txt

Study it to ascertain its structure. (Does it remind you of a gradebook?)

Exercise 7.6.   Develop a data definition for representing a dialog with lists and

Add the teachpack
io.ss
structures. Here are some guidelines:

For instance,

((HAMLET 
   "My excellent good friends! How dost thou,"
   "Guildenstern? Ah, Rosencrantz! 
     Good lads, how do ye both?")
 (ROSENCRANTZ 
   "As the indifferent children of the earth.")
 (GUILDENSTERN 
   "Happy, in that we are not over-happy;"
   "On fortune's cap we are not the very button.")
 (HAMLET
   "Nor the soles of her shoe?"))

is a dialog with four parts. HAMLET speaks two parts, while his comrades speak one each. The first and third part each have two lines, while the second and fourth have one line each. In all, HAMLET speaks three lines spread over two parts in this dialog.

Note: in keeping with tradition, the names of the characters are in UPPERCASE. Thus, you must refer to the Prince of Denmark as 'HAMLET, not as 'Hamlet or 'hamlet.

Use read and open-input-file to read the file from disk. Also traverse the resulting S-expression to create instances of Dialog so that the remaining functions can process the dialog easily.

Hint: See how we read the gradebook and converted into a list of structures. 

Exercise 7.7.   Use your data definitions from exercise 7.6 to design

;; speakers : Dialog  →  (Listof Symbol)

which computes the names of the speakers who contribute to a dialog. Each speaker’s name must appear exactly once (though the order does not matter).

Hint: Design the helper function

;; eliminate-duplicates : (Listof Symbol)  →  (Listof Symbol)

which consumes a list of symbols and produces one without duplicates. More precisely, a symbol should only be on the result list if it is not in the rest of the list. 

Exercise 7.8.   Use your data definitions from exercise 7.6 to design

;; lines-of : Symbol Dialog  →  (Listof String)

which generates a single list of all the lines spoken by the character named by the first argument. 

Exercise 7.9.   Use your data definitions from exercise 7.6 to design

;; most-parts : Dialog  →  Symbol

which computes which speaker has the most parts in the dialog. 

Exercise 7.10.   Use your data definitions from exercise 7.6 to design

;;  most-lines : Dialog  →  Symbol

which computes which speaker has the most lines in the dialog. 

Exercise 7.11.   Use your data definitions from exercise 7.6 to design

;; abbreviate-for : Symbol Dialog  →  Dialog

which extracts a given actor’s part from a dialog. 

Rationale: When rehearsing for a play, it’s useful to have a list of just your parts, with only an indication of when someone else is going to speak, not what they say. For the actor playing HAMLET, for instance, the function would yield

((HAMLET 
  "My excellent good friends! How dost thou,"
  "Guildenstern? Ah, Rosencrantz! 
     Good lads, how do ye both?")
 (ROSENCRANTZ)
 (GUILDENSTERN)
 (HAMLET
  "Nor the soles of her shoe?"))

for the example in exercise 7.6