9.4 Ambiguity of unparenthesized LISP expressions
+ 3 * 4 5 6 is an ambiguous unparenthesized prefix expression in LISP because it can have several different interpretations, depending on where the parentheses are inserted.
- (+ 3 (* 4 5) 6)
- (+ 3 (* 4 5 6))
Those are two of the options that can be derived from the original unparenthesized expression. The first possibility would result in 29 but the second would equal 123. This is a pretty big discrepancy and makes the original expression very ambiguous.
9.10 Short-circuit And in C
Why doesn't this work as a short-circuit version of and (in C)?
int and { return a ? b : 0 }a) Why doesn't this work?
Since short-circuit evaluation works from left-to-right, and stops evaluating once the value of the expression is known. In this case, a is the expression, so once the value of a is known, the value of b is not attained and we have garbage if a is true.
b) Would it work if normal order of evaluation were used? Why?
This would work with normal order of evaluation, because evaluation does not stop once the logical value of the expression is known, so b is returned properly.
9.20 Imitating a while with a do-while
int x = 1; while(x < 5) { printf("x: %d in a while\n", x); x++; } x = 1; do { printf("x: %d in a do-while\n", x); x++; } while(x < 5);
There is no way to imitate a do-while with a while loop, but you can imitate a while with a do-while to a certain degree. For example, the code above would print the same amount of items,
with the same values for x, but if you changed the value of x to 5, only the do-while would
print. In a way this imitates the while, but only to a small extent. There might be another way, of which I am unaware at the moment.
9.21 Imitating a for with a do-while
int x = 1; do { printf("x: %d in a do-while\n", x); x++; } while(x < 5);The former code is equivalent to the following for-loop:
int y; for(y = 1; y < 5; y++){ printf("y: %d in a for\n", y); }9.22 Imitating a while with a for
The following is an imitation of a while loop because both would go on forever.
int x = 1; while(x) { printf("x: %d in a while\n", x); x++; } int y; for(y = 1;; y++){ printf("y: %d in a for\n", y); }9.31
int n; cin >> n; int q = (n + 3) / 4; switch(n % 4) { case 0: do { n++; case 3: n++; case 2: n++; case 1: n++; } while(--q > 0); } cout << "output: " << n << endl;a) n=0 --> 4
b) n=1 --> 2c) n=5 --> 10d) n=8 --> 16The pattern is 2n, for n > 0, otherwise if n=0 then it's a 4 andif it's negative we get the same number back.9.34
int a[] = {1,2,3,4,5,0}; int i = 0; while(a[i++] != 0); cout << i << endl; for(i = 0; a[i] != 0;i++); cout << i << endl;
These are not equivalent because the comparison for 0 is incremented before compared,so the output of i varies for the first it's 6 and 5 for the second.9.35 Behavior of two snippets of C++ codea)int i, n = 3; for(i = 1; i <= n; i++){ printf("i = %d\n", i); n = 2; }
b) int x; for(x = 1; x <= 3; x++){ printf("x = %d\n", x); x = 3; }
Both of these are relatively simple, a prints
i = 1
i = 2
b prints
i = 1
The reason for the first output, of a, is due to the
<= since n is assigned 2, but i is still equal to 2, so it will print once
more before the for loop terminates.
For b) x = 3 after the first run, and then it's incremented by x++ so x is now
4 which is greater than 3 so the loop terminates.
No comments:
Post a Comment