Homework #15


This problem requires Lecture 13.

Mutable Data - (30 minutes)
Below is a definition of a function called next-symbol. When invoked, it generates the next symbol in the sequence g1, g2, etc.

(define next-symbol
  (let ((c 0))
    (lambda ()
      (set! c (+ c 1))
      (string->symbol (string-append "g" (number->string c)))
      )))

No, that's not a typo: the lambda expression is inside the let. Think about the order in which define, let, and lambda do things when evaluated.

What is displayed in the following transcript, and why?

(eq? (next-symbol) 'g1)

(eq? (next-symbol) 'g1)

(eq? (next-symbol) (next-symbol))

NOTE: when explaining why the expressions evaluate the way they do, do not just claim that the symbols are different, explain why they are different. Specifically, you should explain why the counter c does not get set to zero at each call to next-symbol and the role of the 'let c' statement and first class functions (i.e., what is next-symbol really bound to?).


You must log in first before submitting homework assignments.

Instructions on submitting homework is here


Last updated at 12:06 pm on Friday, January 03, 2003.