CS 30 Homework 9

Due by the beginning of class Tuesday, November 12
  1. Read the article A Conversation with Einstein's Brain by Douglas R. Hofstadter, which was handed out in class.

  2. Read the article The Appeal of Parallel Distributed Processing by McClelland, Rumelhart, and Hinton, which was handed out in class. This article introduces neural network or connectionist models of cognition. Our goal for the next two weeks will be to implement a couple of neural network models of memory and learning based on some of the ideas discussed in this paper.

  3. To do this, we will employ the technique of object-oriented programming. The oop-intro.scm code discussed in class outlines the basic approach we will use. Study this code until you understand how it works (in particular, version 5 of make-object).

  4. Assuming that we always store numbers in the objects created by make-object, add a new method called get-sum that returns the sum of the two numbers stored in an object, without changing their values. You do not need to introduce any new internal variables for this. Your new version of make-object should behave exactly as shown below:
    (define obj (make-object 7 8))
    ((obj 'get-sum)) => 15
    ((obj 'get-sum)) => 15
    
  5. Add a method called get-both that returns both of the object values in a list. For example:
    (define obj (make-object 7 8))
    ((obj 'get-both)) => (7 8)
    
  6. Add a method called scale that takes a number n as input and changes the object's values by multiplying each one by n. The scale method itself should return the symbol ok. Your objects should behave exactly as shown below:
    (define obj (make-object 7 8))
    ((obj 'get-x)) => 7
    ((obj 'get-y)) => 8
    ((obj 'scale) 10) => ok
    ((obj 'get-x)) => 70
    ((obj 'get-y)) => 80
    ((obj 'scale) 2) => ok
    ((obj 'get-x)) => 140
    ((obj 'get-y)) => 160
    
  7. Add a method called equal? that takes another object as input and returns true if both of the other object's values are equal to the first object's values, or false otherwise. For example:
    (define obj1 (make-object 7 8))
    (define obj2 (make-object 7 2))
    (define obj3 (make-object 7 8))
    ((obj1 'equal?) obj2) => #f
    ((obj1 'equal?) obj3) => #t
    
    Hint: you will need to use the methods get-x and get-y (or else get-both) in order to extract the values from the other object for comparison.

EXTRA CREDIT

  1. Add a method called swap that exchanges the order of the values stored in an object. Your objects should behave exactly as shown below:
    (define obj (make-object 7 8))
    ((obj 'get-x)) => 7
    ((obj 'get-y)) => 8
    ((obj 'swap)) => ok
    ((obj 'get-x)) => 8
    ((obj 'get-y)) => 7
    
    Hint: in order to perform the swap, you will need to create a temporary variable using let.

Turning in your homework

Put a file with your definition of make-object into a folder called Your name HW 9, and drop this folder into the CS 30 drop box. You do not need to turn in a hardcopy.