Lecture 2026-02-03

(( fun

Recall:

(define (make-box v)
   (local [(define val v)]
     (lambda (msg)
      (cond
        [(equal? msg 'get) val]
        [(equal? msg 'set)
          (lambda (new_value)
          (set! val new_value)
          )] 
(define (get b ) (b 'get))
(define (set b v) (b'set) v )
triangle-exclamation
(define b1 (make-box 7)
(set b1 4 )
=> (define val_1 7)
...
((b1 'set) 4)
=> (define val1 7)
...
((lambda ( new v ) (set! val1 newv)) 4 )
circle-info

Questions of the day:

  1. How is this related to our original address book problem.

  2. Why does it fix it?

How are we going to achieve it?

Stepping

How does this fix the problem?

before:

now:

circle-info

abook is not a list, it's a box, which is a function that can access the data for me, thus, we need to use get.

Boxes are built into racket

circle-info

Syntax: Expr = ....

| (box expr)

| (unbox epr)

| (set-box! expr expr ) ;; note the first expression doesn't have to be an id !

e.g.

Semantics:

circle-info

Convention: When we write an underscore before a variable name, it means the var's value is not looke dup during expression evaluation, unless (unbox __ ) is called on it.

e.g. Stepping row

circle-info

In essence, a little bit messy, but substitution model had to be adapted.

The same problem in C

  • Suppose we want to write a function:

circle-info

Notice how this is the same problem in Racket.

  • Racket solution: put the variables in a box.

  • What is a C equivalent?

    • One-field structure

  • Structure in C:

    • struct Posn { int x; int y;};

i.e.

triangle-exclamation

e.g.

Alternate e.g.

  • Can these structs replace and emulate the functionality of boxes?

The problem:

  • C ( and also racket ) passes arguments via a mechanism called "pass-by-value".

    • The function operates on a COPY of the value, not the value itself.

Note that the Racket substitution model naturally implements pass-by-value

Consider

circle-info

x here really gets mutated, but its a copy of x, not the original from the called, original remains the same.

Similarily:

So there is something special about boxes.

  • not equal to the value they hold

  • but they know how to find the value

    • unbox = find the value.

Finding the value - where is it located?

  • In memory ( RAM)

  • every value in memory has an address

  • given the address, can "find" the value.

So addresses could function as boxes

  • instead of passing a value to a function, pass an address:

circle-info

& is called the address operator.

  • Passes x's address, not it's value..

Thus recall:

Thus, assuming we pass in the address, the above is still wrong, we didn't get an int; we got an address !

  • Maybe: void inc(address x) {...} (wrong)

    • Need more info - what type of data is stored at that address?

thus we write:

circle-info

* is called the deference operator = fetch the value stored at that address.

circle-exclamation

e.g.

Alternatives:

OR

OR?

*x++ - which happens first

  • the * or the ++ ?

    • The answer is the ++

Last updated