Homework 10

This is the archived website of SI 413 from the Fall 2013 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

1 Recycling Boxes

Consider the following SPL code:

new f := lambda a {
  new g := lambda b { ret := b + b/2; };
  new h := lambda c {
    new x := a*c;
    ret := lambda d { ret := g@d < x; };
  ret := h;
new foo := f@3@4;
write foo@8;
foo := 20;

Here are the frames and closures that exist just before the last line is executed. (Note: it would be good practice to see if you could recreate this diagram yourself!)

Frames and closures for ex 1 

  1. Using the labels of each frame above, indicate what the reference count for each frame is at this point in the program.

  2. Repeat (a), showing what happens to the reference counts after the last line in the program is executed.

  3. Using the labels of each frame above, indicate which frames would be garbage collected at this point using the mark and sweep method.

  4. Repeat (c), showing what happens in mark and sweep after the last line in the program is executed.

2 Cooking Spaghetti

In class we talked about "spaghetti code", which is code that uses GOTO statements way too liberally and becomes almost impossible to follow. I want you to write your own example of spaghetti code, in the C++ language.

Submit your code electronically as 413 hw 10 in a file called ex2.cpp. Your program should compile and run without any arguments or special flags. You also need to submit a description of what your code does, written out and turned in with the other exercises for this assignment.

Grading this problem will consist of me first looking at your code, briefly, and trying to figure out what it does. I will then look at your description of what it actually does and then run it. If your code actually does something, and it's what you describe, but I couldn't discern that by looking at it, then you'll do well on this problem.

It may help to go back and read Dijkstra's original article on GOTOs.