CPS 343/543 Lecture notes: Recursion

Coverage: [EOPL2] §3.6 (pp. 92-98)

Adding recursion to our language and interpreter

  • new production rule and constructor on p. 93
  • sample programs on p. 93
  • we will evaluate the body of a recursive procedure in a recursively-extended environment (see Fig. 3.8 on p. 94)
  • behavior of recursively-extended environment on p. 93
  • we use Scheme's letrec itself to recursively extend a procedurally-represented environment
    • involves packaging up an instance of the closure data type
    • see Fig. 3.9 on p. 95
  • we can represent a recursively-extended environment using abstract syntax as well
    • involves adding a new variant to the environment data type,
    • adding a extend-env-recursively procedure, and
    • adding a new case to apply-env which packages up an instance of the closure data type
    • see Fig. 3.10 on p. 96
  • notice that in each of the above recursively-extended environment representations, `we build a new closure each time a procedure is retrieved from the environment' [EOPL2] p. 95
  • `this is unnecessary since the environment for the closure is always the same' [EOPL2] p. 95
  • `if we use a ribcage representation of the environment, we can build the closures only once, by building an environment with a circular structure' [EOPL2] p. 95
    • return to the original environment representation (that without the recursively-extended-env-record variant)

    • (regenerated from [EOPL2] Fig. 3.11 on p. 97)

    • see code for extend-env-recursively in Fig. 3.12 on p. 97
      • use of vector-set! (function built into Scheme)
      • use of for-each (function built into Scheme)
      • use of iota (function not built into Scheme)


    [EOPL2] D.P. Friedman, M. Wand, and C.T. Haynes. Essentials of Programming Languages. MIT Press, Cambridge, MA, Second edition, 2001.

Return Home