Lexical and Dynamic Scoping - Answers to Mini-Exercises

  1. What does this print? What would it print if Racket used dynamic scoping?
    (define y 0)
    (define (y-printer) (display y))
    
    (let ((y 10))
      (y-printer))
    
    
    Racket: 0
    Racket with dynamic scoping: 10
    
  2. What is the value of the let? What would it be if Racket used dynamic scoping?
    (define y 0)
    
    (let ((y 10)
          (f (lambda (x) (+ x y))))
      (f 1))
    
    
    Racket: 1
    Racket with dynamic scoping: 11  (since we'd find the y=10 binding instead
    of the global one)
    
  3. Consider the following Racket code.
    (define n 0)
    
    (define (get-n) n)
    
    (define (squid) (get-n))
    
    (define (octopus n) (squid))
    
    what does (squid) evaluate to? What does (octopus 8) evaluate to? What would be the result in a dynamically scoped version of Racket?
    Racket: (squid) evaluates to 0; (octopus 8) also evaluates to 0 

    Racket with dynamic scoping: (squid) evaluates to 0; (octopus 8) evaluates to 8