First page Back Continue Last page Summary Graphic
Solutions for Exercise 1 (2)
module Star {
Star[][] trek;
int[][] enterprise() {
while (false);
{ return newarray int[]("trek"); }
}
int voyager(int x, y) {
if x > y
int z = 2;
else
return enterprise()[-!y];
return !-5;
}
}
module LinearAlgebra {
int scalarProduct(int[] xs, int[] ys, int n) {
if (n == 0)
return 0;
else
return xs[n-1] * ys[n-1] + scalarProduct(xs,ys,n-1);
}
int scalarProduct2(int[] xs, int[] ys, int n) {
int result;
result = 0;
while (n > 0) {
n = n - 1;
result = result + xs[n] * ys[n];
}
return result;
}
void nextToken() {
while (ch <= ' ')
nextCh();
switch (ch) {
case EOF_CH:
token = EOF; break;
case '-': case '+': case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7': case '8': case '9':
buf.setLength(0); buf.append(ch);
if ((ch == '-') || (ch == '+')) {
nextCh(); if ((ch < '0') || (ch > '9')) error("missing number");
} else
nextCh();
while ((ch >= '0') && (ch <= '9')) {
buf.append(ch); nextCh();
}
token = NUM; chars = buf.toString(); break;
case '<':
nextCh();
if (ch == '=') {token = LTEQ; nextCh();} else token = LT;
break;
...
default:
error("illegal character: " + ch);
}
}
What about comments?