PLQ #7Done on:   Tuesday, November 10th

Question 1 @ 2020-11-10 18:33

set! is a special form that will modify the value of a variable. What is the advantage of set! returning a bogus value like (BogusV)?

  1. It allows you to chain set! assignments.

  2. It allows use of the old value that is overwritten.

  3. It forces you to be explicit about the values you want to return, resulting in more robust code.

  4. It allows side effects within an expression.

  5. There is no advantage to set! returning a bogus value, in fact, it’s explicitly less useful since it removes a potentially useful value.

  6. It’s easier to use in purely functional languages with no side-effects.

Question 2 @ 2020-11-10 18:40

What would the following evaluate to if we changed set! in our TOY language to return the new value that was assigned instead of a bogus value?

{with {x 4}
  {with {y {set! x 3}}
    {+ {set! x {+ x y}}
      x}}}

Question 3 @ 2020-11-10 18:47

Say we have a basic Point class-like thing defined in Racket.

(define (make-point x y)
  (let ([xb (box x)]
        [yb (box y)])
    (lambda (msg)
      (match msg
        ['getx (unbox xb)]
        ['gety (unbox yb)]))))

What should the following produce?

(define p (make-point 0 1))
(set-box! xb 1)
(p 'getx)
  1. 0
  2. 1
  3. Error: xb is undefined.
  4. Error: p is undefined.
  5. Error: p is is not a callable function.

Question 4 @ 2020-11-10 18:50

Consider the following code:

(define (make-foo x)
  (let ([x (box x)])  ;*
    (lambda (msg)      ;*
      (match msg
        ['getx            (unbox x)]
        [(list 'setx newx) (set-box! x newx)]))))

(define (make-bar x)
  (lambda (msg)        ;*
    (let ([x (box x)]) ;*
      (match msg
        ['getx            (unbox x)]
        [(list 'setx newx) (set-box! x newx)]))))

(define a-foo (make-foo 1))
(define a-bar (make-bar 2))
(a-foo (list 'setx a-bar))
(a-bar (list 'setx a-foo))

What does the following line evaluate to now?

(list (a-bar 'getx) (a-foo 'getx))
  1. Trick question, this implementation is incorrect and won’t compile.
  2. Trick question, it will get stuck in an infinite loop.
  3. (1 1)
  4. (1 2)
  5. (1 #)
  6. (2 #)
  7. (# 1)
  8. (# 2)
  9. (# #)
  10. All of the above answers are wrong…