Homework 9

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

Use a separate sheet of paper for your answers! Everything should be submitted in one packet, all printed out for me to see.

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 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 after the last line in the program is executed.

2 Hello, world!

Write a C++ program that prints (exactly) the string "Hello, world!" (followed by a newline), and demonstrates assignments using as many different kinds of l-types that you can in C++.

For full credit, your assignments should be somehow meaningful or useful. So for example, the program

int a = 3;
a++ = 29;
cout << "Hello, world!" << endl;

would not receive full credit, because the assignments with a had nothing to do with the output. Something like

string s = "Hello, vorld!";
s[7] = 'w';
cout << s << endl;

would be more like it. But of course you will need to have more assignments than this to demonstrate all the different kinds of l-values!

Print out your code and turn it in, labeling all the different l-values that you use.

In addition, you must submit your code electronically. Name your file ex2.cpp and submit using the command 413sub hw 09.