PLQ #6Done on:   Tuesday, November 3rd

Question 1 @ 2020-11-03 18:07

What is the result of the following?

(define a (box 'b))
(define b (box 'b))
(list (eq? a b) (equal? a b))

(Reminder: eq? is pointer equality, and equal? is a recursive structural equality.)

  1. (list #t #t)
  2. (list #t #f)
  3. (list #f #t)
  4. (list #f #f)
  5. An error, equal? of mutable values is nonsensical
  6. Infinite loop, due to the cycle of pointers
  7. An error, due to the cycle of pointers

Question 2 @ 2020-11-03 18:10

Why do we use a box when implementing a cyclic data structure?

  1. A box’s mutability allows us to continuously extend our data structure such that it behaves cyclically without creating a cycle.

  2. A box’s I/O side effect (at least the “O” part) allows us to easily identify when we encounter a cycle.

  3. A box’s mutability allows it to ultimately refer to itself via a cycle of pointers.

  4. A box is particularly compatible with the “recursive function constructor” (the Y Combinator) which allows us to generate a cyclic data structure with ease.

  5. We don’t use a box because it does not allow us to create a cycle of pointers.

  6. We can’t use a box because of its lack of subtyping.

Question 3 @ 2020-11-03 18:14

We have seen in the past Racket’s let local binder, and an example that shows how it respects lexical scope:

(let ([x 1] [y 2])
  (let ([x y] [y x])
    (list 'x= x 'y= y)))
=>
'(x= 2 y= 1)

There is also let* which is shorthand for nesting each binding at a time as if it’s in its own let:

(let ([x 1] [y 2])
  (let* ([x y] [y x])
    (list 'x= x 'y= y)))
=>
'(x= 2 y= 2)

And last week we also added letrec as a new kind of binder, one that creates a recursive environment. Ignoring types, what would we get from:

(let ([x 1] [y 2])
  (letrec ([x y] [y x])
    (list 'x= x 'y= y)))

?

  1. In this case, it behaves similarly to a let, so: ’(x= 2 y= 1)
  2. In this case, it behaves similarly to a let*, so: ’(x= 2 y= 2)
  3. ’(x= 1 y= 1)
  4. ’(x= nil y= nil)
  5. ’(x= ’y y= ’x)
  6. A syntax error since letrec should always be used with function expressions (lambdas)
  7. An evaluation error, since x and y are not initialized before use