Readings: [Scott], Chapters 3 and 6. Consider the "Check Your Understanding" questions as part of your reading assignment (that is, solve as many as you can but do not turn in any solutions).
var x = 100;
function set_x(n) {x = n}
function print_x {write_integer(x);}
function first {set_x(1); print_x();}
function second {var x; set_x(2); print_x();}
set_x(0);
first();
print_x();
second();
print_x();
What does this program print if the language uses static
scoping? What does it print with dynamic scoping? Why?
var x = 100;
function set_x(n) {x = n}
function print_x {write_integer(x);}
function foo(S, P, n) {
var x;
if (n == 1 or n == 3) {set_x(n);} else {S(n);}
if (n == 1 or n == 2) {print_x();} else {P();}
}
set_x(0); foo(set_x, print_x, 1); print_x();
set_x(0); foo(set_x, print_x, 2); print_x();
set_x(0); foo(set_x, print_x, 3); print_x();
set_x(0); foo(set_x, print_x, 4); print_x();
Assume that the language uses dynamic scoping. What
does the program print if the language uses shallow
binding? What does it print with deep binding? Why?
(-b + sqrt(4 × a × c)) / (2 × a)
Do you need a special symbol for unary negation?
LOOP
line := ReadLine;
IF AllBlanks(line) THEN EXIT END;
ConsumeLine(line)
END;
Show how you might accomplish the same task using a while or
repeat loop, if midtest loops were not available. (Hint: one
alternative duplicates part of the code;
another introduces a Boolean flag variable.) How do these
alternatives compare to the midtest version?
int first_zero_row = -1; /* none */
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (A[i][j]) goto next;
}
first_zero_row = i;
break;
next: ;
}
The intent of the code is to find the first all-zero row,
if any, of an n × n matrix. Do you find the example
convincing? Is there a good structured alternative in C?
In any language?
Give answers in the form of a three page
paper. Include a brief abstract, a good introductory section,
a background section describing views on the goto statement
throughout history, a beefy section analyzing alternatives
to Rubin's problem, and a good concluding section.Organize your work in your CVS repository. The following is the suggested structure:
homework
cmsi386
src
main
docs
hw4.tex
<diagram source files>, if any
<images>, if any
ml
factorial.sml (Problem 9)
js
factorial.js (Problem 9)
ruby
factorial.rb (Problem 9)
test
ml
factorialtest.sml (Problem 9)
js
factorialtest.js (Problem 9)
ruby
factorialtest.rb (Problem 9)
Your LaTeX file will contain solutions to each problem, numbered, and in order. You may embed source code in the document, or give the answer as "See <filename>" and submit the sources for these answers at the end of the document. Generate a pdf from the LaTeX file and hand in a printed copy.
Notes