RU beehive logo ITEC dept promo banner
ITEC 380
2012fall
ibarland
tlewis32

homelecturesexamshwsbreeze (snow day)

hw10
Finishing N6
static scope

Due Dec.07 (Fri) at the start of class.
Submit: a hardcopy with the first question's fill-in-the-blanks1 and the additional tests for N6 (since N4), and the additional/changed function(s) for N6 (since N4).
Since N6 is an extension N5, you don't need any code or test-cases for N5 different from N6.

We continue to build on the language implementation started in hw07. You can implement this homework in either Java or Racket. Please indicate in your submitted file, what sections of code are udpated and what is unchanged from hw09/hw09-soln. You don't need to turn in any hardcopy of unchanged-code (but submit a fully-working copy in the drop-box).

  1. (Scope, 15pts) The racket form set! changes the binding of a variable:

    (define x 5)
    (set! x (* 2 x))
    ; the variable x is now bound to 10.

     1. (define a 10)
     2. (define b 20)
     3. (define make-foo
     4.   (let {[b 30]}
     5.      (lambda ()            ; ← make-foo is bound to this function.
     6.         (let {[c 40]}
     7.            (lambda (cmd)   ; ← make-foo returns this function as its answer.
     8.              (let {[d 50]}
     9.                (cond [(symbol=? cmd 'incA) (set! a (+ a 1))]
    10.                      [(symbol=? cmd 'incB) (set! b (+ b 1))]
    11.                      [(symbol=? cmd 'incC) (set! c (+ c 1))]
    12.                      [(symbol=? cmd 'incD) (set! d (+ d 1))]
    13.                      [(symbol=? cmd 'get-all) (list a b c d)])))))))
          

    1. The scope of the a declared in line 1 is lines                  through                 .

    2. The scope of the b declared in line 2 is lines                  through                 .

    3. The scope of the b declared in line 4 is lines                  through                 .

    4. The scope of the c declared in line 6 is lines                  through                 .

    5. The scope of the d declared in line 8 is lines                  through                 .
    Suppose that make-foo is called exactly three times (but that a function returned by make-foo is not called).

    1. How many variables named a are created?                 

    2. How many variables named b are created?                 

    3. How many variables named c are created?                 

    4. How many variables named d are created?                 
    (Hint: Each of the above four answers are different.)
    (set! a 500)
    (set! b 600)
    (define counter1 (make-foo))
    (define counter2 (make-foo))
    (define counter3 (make-foo))
    
    (counter1 'get-all)  ; >>> TODO:   (list                            )
    (counter1 'incA)
    (counter1 'incB)
    (counter1 'incC)
    (counter1 'incD)
    (counter1 'get-all)  ; >>> TODO:   (list                            )
    
    (counter2 'get-all)  ; >>> TODO:   (list                            )
    

  2. (20pts) Complete N5, as per hw07—Interpreting N: project.

  3. (15pts, including 5pts for implementing recursion)
    Complete N6, as per hw07—Interpreting N: project.

1If you want to have your fill-in-the-blank questions as a comment in your code file, that's fine — just have them at the top of the first page you turn in, so I see it immediately.      

homelecturesexamshwsbreeze (snow day)


©2012, Ian Barland, Radford University
Last modified 2012.Dec.02 (Sun)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme