Comp 210 Lab 12: Types of Equality

For structures and arrays, when changing them inside a function, those changes get reflected back at whoever called the function! We'll show an example using Scheme, but Scheme and C both behave similarly.

Rules for Structures

When creating a structure,
(define-structure vehicle (idNum numWheels price))
(define bike (make-vehicle 23 2 19.96))
What Dr Scheme really does internally, when it sees the make-vehicle, is that it creates a hat variable with a unique name.
(define-structure vehicle (idNum numWheels price))
(define g024^ (make-vehicle 23 2 19.96))
(define bike g024^)
The interesting thing, is that the hat variables count as values in themselves: So (define bike g024^) is considered completely evaluated--the hat placeholder isn't further replaced with the structure (!). Why does this matter? Suppose we now say that bike gets a flat, and that its value changes accordingly:
(set-vehicle-numWheels! bike 1)
(set-vehicle-price! bike (* 0.5 (vehicle-numWheels bike)))
What does Dr Scheme do? Well, bike is found to be a placeholder for the hat-variable g024^, and the set-vehicle-numWheels! command goes and changes what was written for g024^'s definition. So Dr Scheme pretends the the following is what had been typed:
(define-structure vehicle (idNum numWheels price))
(define g024^ (make-vehicle 23 1 9.98))
(define bike g024^)
So, you ask, why have we bothered making special hat variables out of structures? It seems to have gained us nothing except making things more complicated.

Types of Equality

The answer is that we can actually have two placeholders refer to exactly the same object.
(define-structure vehicle (idNum numWheels price))
(define bike (make-vehicle 23 2 19.96))
(define motorcycle bike)
This becomes
(define-structure vehicle (idNum numWheels price))
(define g024^ (make-vehicle 23 2 19.96))
(define bike g024^)
(define motorcycle g024^)
Notice that both bike and motorcycle refer to g024^, and if the value of g024^ changes, then both of bike and motorcycle will see a difference!

Compare this with non-structures, which don't have hat variables:

(define x 3)
(define y x)
(set! x 99)
Has the value of y changed? No. But if we'd defined x to be a structure and modified it with set-structure-field! instead of of set!, then there would be a difference.

Consider the following explanation: in the example of x and y, they each had value 3, but these were "different copies" of 3. However, bike and motorcycle are two names for the identically same object. Some equalities are more equal than other equalities...

An example: if you punch the son of the Queen of England, then the Prince Charles's nose will bleed. Thus ``Prince Charles'' and ``son of the Queen of England'' are two different names for the same object. (Similarly, "the Morning Star" and "the Evening Star" are both names for the planet Venus).

Now, go back and apply these same rules to the C example of trike and rig, to explain what happened.

Exercise Write a function addSpare which takes in a vehicle and increases by one the number of wheels it has. The function should return nothing (return type void). Now, in main, create a vehicle, and print out the number of wheels before and after passing it to addSpare.


Back to Lab 12
Back to Comp 210 Home