CSE 341 -- Programming Languages

Autumn 2002

Department of Computer Science and Engineering, University of Washington

Steve Tanimoto (instructor)

Assignment 2

Version 1.03b of October 21

Scheme Challenge 

Due date and time: Thursday, October 24, 2002 (post online before the beginning of your section).

Turn in this assignment electronically using INFACT.


 

Title: Scheme Challenge.

Objectives:

Instructions: Part 1. Do this part individually.

A.  Rephrasing.

 Write a Scheme procedure that takes a list representing an
  English sentence in the active voice and returns a sentence
  meaning the same thing, but in the passive voice.
 For example, the behavior should work as follows:

 (active-to-passive '(MIKE CARVED THE PUMPKIN))
  ->
  (the pumpkin was carved by mike)

 You may assume the following:
 1. The sentence has the form
     [SNP][PTAVV][SSNP]
 2. A simple noun phrase ([SNP]) is either someone's one-word name (e.g., MIKE)
 or a common noun
    with an article (A, AN, or THE) in front of it.

 3. A past-tense active voice verb ([PTAVV]) is one of the verbs
    STUDIED, ATE, TOOK, BOUGHT, WROTE, READ, CARVED, CELEBRATED.
 4. A simple singular noun phrase ([SSNP]) is a simple noun phrase
    that represents a single person, place, thing, or idea.
    For example A BOOK is such a phrase but THE PEOPLE is not.
 5. You may hard-code lists of words or word pairs for making
    the translations or for testing the properties of words.


  You should define a helper procedure called VERB-PAST-PARTICIPLE.
  VERB-PAST-PARTICIPLE should take a word like THREW and
  return THROWN.  Any word that it doesn't specifically know about should be
  returned unchanged.  This will work for regular verbs like
  CELEBRATED, which has its past participle identical to its
  direct past form.  Some of the irregular verbs it should know about
  are to eat (ATE -> EATEN), to take (TOOK -> TAKEN), to write
  (WROTE -> WRITTEN), and optionally, the following:
  to do (DID -> DONE), to throw (THREW -> THROWN) and to ride
  (RODE -> RIDDEN).

B. More Rephrasing

 Extend your answer to A by choosing and implementing one of the
 following options:

 i. Handle plural objects as well as singular objects for the
    second noun phrase.
    Define and make use of a procedure PLURAL?
    PLURAL? would return #f on PUMPKIN but #t on PUMPKINS, etc.
    (Again, you can hard-code a list of plural words in your procedure.)
    The sentence (MIKE CARVED THE PUMPKINS) should then translate to
    (THE PUMPKINS WERE CARVED BY MIKE).

 ii. Handle present-tense verbs as well as past-tense verbs.
    You may assume that the subject noun phrases will be singular
    as in (MIKE EATS COLESLAW); you don't have to handle
    (THE GUESTS EAT COLESLAW).  The former list should translate
    to (coleslaw is eaten by mike).

 iii. Handle sentences in which the future tense is used, as in
    (MIKE WILL EAT COLESLAW).  This should translate to
    (coleslaw will be eaten by mike).

 iv. Handle sentences in which a simple prepositional phrase
    may follow either or both of the two noun phrases.  For example,
    (THE MAN WITH A KNIFE ATE THE OYSTER WITH A PEARL).
    This should translate to
    (THE OYSTER WITH A PEARL WAS EATEN BY THE MAN WITH A KNIFE).
    You may assume that such a prepositional phrase has the form 
    [Pp][SNP]
    Where Pp is a preposition such as OF, WITH, FROM, IN, ABOUT,
    OVER, UNDER, ON, BY, or THROUGH.

 v. Recognize whether a sentence is an active-voice or passive
    voice sentence, and then translate it to the other form.
    Your top level function should then be called CHANGE-VOICE
    rather than ACTIVE-TO-PASSIVE.

C. Extra Rephrasing (optional)
 For 5 points of extra credit, implement all four options above.
 For 10 points of extra credit, implement all five options
  above in such a way that they all can work together in a
  natural way.
  In this option, you should have procedures named PAST?
  and PRESENT? that return #t if their argument is a past or
  present-tense verb, respectively.


 Test examples will be provided later.

 Log into INFACT

 Post your answer as (group-invisible) replies to the message
 from SLT entitled, "Assignment 2, Part 1." (posted on Oct 15).


Part 2. Do this part in teams (your INFACT groups).
In this part, turn in your solutions using INFACT, but only one 
solution per team is required for each problem.
One team member should reply to
the instructor's message corresponding to Assignment 2, Part 2, D,
or E.

D. Team warmup with macro programming.
    (Note modification made Oct 14)

    Write a Scheme macro INFIX that takes keywords PLUS and TIMES that
    lets the user write simple arithmetic expressions in
    a kind of parenthesized infix but that evaluate properly in Scheme.
    Examples:
    (INFIX 1 PLUS 2) -> 3
    (INFIX (INFIX 10 TIMES 1/2) PLUS (INFIX 3/4 TIMES 8)) -> 11
    You can assume that the addition and multiplication are always
    performed on two operands at a time.

E. The Scheme Challenge.

Write a collection of Scheme procedures that translate algebra
problems stated in English into collections of Scheme formulas.
(Optional:) Provide a Scheme procedure to solve the problems.

This will be a solver for word problems using algebra.
An example of a word problem, encoded as a list, is the following: 

     (if the area of a circle is pi times the square of the radius

     and the radius is 10 meters

     then what is the area of the circle ?)

In 1964, the Ph.D. thesis of Daniel Bobrow presented a program called
STUDENT which could solve problems posed in this manner.  In this
option, you will use Scheme to reimplement some of the functionality
of STUDENT.

Here is some additional information on STUDENT. As an overall strategy for implementing the student, one might break the program down into several parts: (a) translating the user's description of the problem into a set of equations -- this involves translating English descriptions of expressions into Scheme/mathematical representations which is what the sample code gives you, and (b) solving the equations for the particular variable that corresponds to the solution.
For (a), you may take advantage of the procedure (MATCH P S) that is posted here. It makes parsing of sentences expressed as lists of numbers and symbols fairly easy. See Chapter 22 of Scheming on the Web for information about how to use MATCH.
Here is a faster substitute for MATCH called GREEDY-MATCH. It's not as flexible, but can match long sentences efficiently.
The optional equation-solving part can be done by defining a procedure that tests for various conditions and makes appropriate changes to the equations -- for example, if trying to solve (= (+ X Y) 5) for X, a rule could check whether X occurs on the left hand side of the equation as part of a sum, and if so, subtracts the other part of the sum from each side, in this case giving the new equation (= X (- 5 Y)). It will be a good idea to create some helping procedures that test expressions and equations for certain properties; for example you could define a procedure HAS-ONE-UNKNOWN that would return true for (= (+ X 5) (+ (* 2 X) 10)) but false for (= Y (+ X 1)). Then an equation having one unknown could be solved and the result plugged in to help solve another equation.
Programming style: Comment your procedure definitions clearly. In all of your exercises, use a neat and consistent format for your code, indenting subexpressions appropriately and choosing symbol names that are appropriate to the function they perform in your programs.
 
Required Features to implement:
Optional Features to implement:

Some Features NOT required and NOT suggested as optional:

When you turn in your solution, include a breakdown of what parts of the solution were done by each of the team members.
Here are some style guidelines courtesy of Jed and Eric.