Question 1 @ 2025-02-11 18:40
Which of the following advantages of dynamic scope is correct?
-
Dynamic scope facilitates easier compilation due to its reliance on the lexical structure of programs.
-
Dynamic scope allows for easy configuration of variables at runtime, making it a perfect fit for Racket and other Lisps.
-
Dynamic scope allows for easy configuration of variables at runtime, making it possible to reuse code in unexpected ways.
-
Dynamic scope ensures that all functions are mutable, leading to a more flexible codebase.
-
Dynamic scope makes recursion easy, but not as much as lexical scope.
Question 2 @ 2025-02-11 18:43
Which of the following features can be available in a dynamically-scoped language?
- Efficient compilation
- Static type checking
- Using a theorem prover for correctness proofs
- Using lambda functions as objects
- Lazy evaluation
Question 3 @ 2025-02-11 18:45
Now that surely you know how lexical scope works, you know that
(let ([y x] [x y])
(list x y)))
evaluates to '(2 1)
.
What would we get if we try that in a dynamically scoped language?
-
'(1 1)
-
'(1 2)
-
'(2 1)
-
'(2 2)
-
Error:
unbound x
-
Error:
unbound y
- Impossible to tell in a dynamically-scoped language
Question 4 @ 2025-02-11 18:47
What would the following return when executed in our dynamically-scoped language?
(lambda (x) (lambda (y) (f x y))))
(let ([x 3] [y 4])
(((currify +) 1) 2))
3, 5, 7, Unbound-x, Unbound-y, Unbound-f, Infinite-loop
Question 5 @ 2025-02-11 18:50
What would the following return when executed in our dynamically-scoped language?
(define (f x) (+ x 1))
(let ([f (lambda (x) (f x))])
(f 5))
6, 101, Unbound-x, Unbound-f, Infinite-loop
Question 6 @ 2025-02-11 18:51
We’ve seen one way to implement a lookup function in a substitution cache:
(let ([cell (assq name sc)])
...))
Say that we change that by using
on the second line. Assuming this strategy (actually similar, since it’d need to handle multiple bindings on each level), what would we get for
(let ([y x] [x y])
(list x y)))
?
-
'(1 1)
-
'(1 2)
-
'(2 1)
-
'(2 2)
-
Error:
unbound x
-
Error:
unbound y
- Impossible to tell, since the environment is mutated