CS 2010 Homework 10   - Due October 28

Setup

hw10-teachpack.ss (and clear any other teachpack)
Download and select this teachpack for the assignment. To select the teachpack, first use the Clear Teachpacks menu item to clear old teachpacks. Then use the Add Teachpack... menu item in the Language menu. (Do not use DrScheme's Open menu item for hw10-teachpack.ss.)

The teachpack adds the following primitive operations:

create-window : list-of-list-of-control -> window
Creates a window given a list of rows, where each row is a list of message, button, and/or text controls. Each control can be added to a window only once.
make-text : string -> control
Creates a text input control given a label string for the control
make-button : string (evt -> true) -> control
Creates a button given a label for the button and a callback function. The function is called when the button is clicked, and it must return true. (In this teachpack, there are no useful operations on the event that is supposed to the callback function, so your function will always ignore it.)
make-message : string -> control
Creates a message control to display a string, given an initial string to display.
text-contents : control[text] -> string
Returns a string for the text that is currently entered in the text control.
draw-message : control[message] string -> true
Changes the string that is displayed by the message control.
read-from-string : string -> snl-or-false
Converts a string to a Scheme value obtained by putting a quote mark in front of the string and treating it as a Scheme expression. If the string qith a quote is not a valid expression, or if adding a quote does not produce a snlvalue (as defined below), the result is false.

The read-from-string function uses the following data definitions:

 ;;  An snl is either
 ;;   - sym
 ;;   - num
 ;;   - list-of-snl
 ;;
 ;;  A list-of-snl is either
 ;;   - empty
 ;;   - (cons snl list-of-snl)

See also the slides for October 22 for background on this assignment.

Assignment

Exercise 10.1, Definitions

Implement add-definition, which takes a symbol and a number and records that the given symbol is "defined" to be the given number, but only if it has not been defined before. If the symbol has been defined before, the result is "already defined", and the definition is not changed. Otherwise, the definition is remembered and the result is "defined".

Implement get-value, which takes a symbol and returns either a number or false. The result is a number when the given symbol is defined, and the result number is the defined value of the symbol. If the symbol has not been defined, the result is false.

Implement clear-definitions, which takes no arguments and returns (void), but also forgets all previous definitions.

In all cases above, "defined" means "defined in your model of DrScheme". Evaluating (add-definition 'x 10) should not cause x in DrScheme's interactions window to produce 10, but it should cause (get-value 'x) to produce 10. Clearly, the functions add-definition, get-value, and clear-definitions share state.

Example:

   (clear-definitions) "should be" (void)
   (add-definition 'x 10) "should be" "defined"
   (add-definition 'y 7) "should be" "defined"
   (get-value 'x) "should be" 10
   (get-value 'y) "should be" 7
   (get-value 'z) "should be" false
   (add-definition 'x 11) "should be" "already defined"
   (get-value 'x) "should be" 10
   (clear-definitions) "should be" (void)
   (get-value 'x) "should be" false
   (get-value 'y) "should be" false
   (get-value 'z) "should be" false

Exercise 10.2, Parsing

More data definitions:

 ;;  A defn-snl is
 ;;   (list 'define sym num)
 ;; 
 ;;  An expr-snl is
 ;;    sym

Implement the following functions:

  ; is-defn? : snl -> bool
  ;  Returns true if s is a defn-snl
  (define (is-defn? s) ...)

  is-expr? : snl -> bool
  ;  Returns true if s is a expr-snl
  (define (is-expr? s) ...)

Exercise 10.3, Executing Commands

Implement the function execute which takes a snl and produces a string:

Example:

   (clear-definitions) "should be" (void)
   (execute 'x) "should be" "no definition"
   (execute '(define x 12)) "should be" "defined"
   (execute 'x) "should be" "12"
   (execute '(x x x)) "should be" "bad input"
   (execute '(define x 10)) "should be" "already defined"

Exercise 10.4, The GUI

Create a GUI with the following controls:

Connect your GUI to your execute and clear-definitions functions. Use text-contents and read-from-string to convert the text in the input field into a snl.

At this point, you should have a roughly functioning DrScheme, as illustrated inthe following sequence:
hit Reset

Exercise 10.5, Assignment

Implement set-definition, which takes a symbol and a number and changes the defined value of the given symbol to the given number, but only if the symbol was previously defined. The result should be "assigned" if a definition is changed, or "not defined" if the given symbol was not previously defined.

Example:

   (clear-definitions) "should be" (void)
   (set-definition 'x 5)  "should be" "not defined"
   (add-definition 'x 10) "should be" "defined"
   (get-value 'x) "should be" 10
   (set-definition 'x 5)  "should be" "assigned"
   (get-value 'x) "should be" 5

Exercise 10.6, Parsing Assignment

A new data definition:

 ;;  An assign-snl is
 ;;   (list 'set! sym num)

Implement the following function:

  is-assign? : snl -> bool
  ;  Returns true if s is an assign-snl
  (define (is-assign? s) ...)

Exercise 10.7, Executing Assignment

Change execute by adding a new case:

At this point, your GUI will support assignment.


Last update: Monday, October 27th, 2003
mflatt@cs.utah.edu