PLQ #4Done on:   Tuesday, October 18th

Question 1 @ 2022-10-18 18:19

Pros and cons of lexical and dynamic scope

  1. Dynamic scope is has proven itself as the better approach due to its easy implementation.

True False

Question 2 @ 2022-10-18 18:21

Pros and cons of lexical and dynamic scope

  1. Lexical scope gives us the advantage of higher-order functions.

True False

Question 3 @ 2022-10-18 18:22

Pros and cons of lexical and dynamic scope

  1. Dynamic scope is a necessary evil for the required customization level of modern apps.

True False

Question 4 @ 2022-10-18 18:24

Pros and cons of lexical and dynamic scope

  1. Lexical scope gives us the ability to reason about code.

True False

Question 5 @ 2022-10-18 18:25

Pros and cons of lexical and dynamic scope

  1. Dynamic scope makes compilation possible.

True False

Question 6 @ 2022-10-18 18:27

Pros and cons of lexical and dynamic scope

  1. Lexical scope makes functions-as-objects possible.

True False

Question 7 @ 2022-10-18 18:30

Pros and cons of lexical and dynamic scope

  1. Lexical scope was traditionally tolerated since it makes recursion available immediately.

True False

Question 8 @ 2022-10-18 18:32

What would the following return when executed in our dynamic language?

#lang pl dynamic

(define (currify f)
  (lambda (x) (lambda (y) (f x y))))

(define (add x y) (+ x y))

(let ([x 3] [y 4])
  (((currify add) 1) 2))

2 3 7 10 Unbound-x Unbound-y Unbound-f Unbound-add Infinite-loop

Question 9 @ 2022-10-18 18:37

With the OLD (and broken) substitution caching evaluator, what is the result of executing the following, and why is it so?

(run "{with {x 1}
        {fun {y}
          {+ x y}}}")

  1. Unbound x

    By the time we call the function, x has no mapping in the cache.

  2. {fun {y} {+ x y}}

    Empty cache => nothing to look-up and substitute in the body.

  3. {fun {y} {+ 1 y}}

    x is bound => it’s in the cache => it gets substituted in the body.

  4. {fun {y} {+ x y}}

    The cache has an x binding, but a fun evaluates to its syntax.

  5. {fun {y} {+ 1 y}}

    x is bound => its value immediately substitutes all x occurrences in the body without affecting the cache..

Question 10 @ 2022-10-18 18:41

In the Flang interpreter using environments (the good one):

(define-type ENV
  [EmptyEnv]
  [Extend Symbol VAL ENV])

what will the environment look like when evaluating the final addition expression?

{with {x 1}
  {with {y 1}
    {with {x 2}
      {+ x y}}}}

  1. (Extend ’x (Num 1) (Extend ’y (Num 1) (Extend ’x (Num 2) (EmptyEnv))))
  2. (Extend ’x (Num 2) (Extend ’y (Num 1) (Extend ’x (Num 1) (EmptyEnv))))
  3. (Extend ’x (Num 1) (Extend ’y (Num 1) (EmptyEnv)))
  4. (Extend ’y (Num 1) (Extend ’x (Num 1) (EmptyEnv)))
  5. (Extend ’x (Num 2) (Extend ’y (Num 1) (EmptyEnv)))

Question 11 @ 2022-10-18 18:47

In the environment-based Flang, what is the cost of an identifier lookup?


  1. Linear to the size of the expression tree.

  2. Exponential to the runtime.

  3. Linear to the binding depth.

  4. Linear to the number of unique identifiers.

  5. O(n²) for an expression that is n-nodes wide.