lambdas in Python and Java Fill in the lamba in a program **************************************************************** 6.4 Translate the following expression into postfix and prefix notation: [-b+sqrt(b*b-4*a*c)]/(2*a) need to check the book on that... Do you need a special symbol for unary negation? Answer: Let ~ represent unary negation (yes, it's needed). [Well... 0-b is also b] Postfix: b ~ b b * 4 a * c * - sqrt + 2 a * /. Prefix: / + ~ b sqrt - * b b * * 4 a c * 2 a. **************************************************************** 7.2 In the following code, which of the variables will a compiler consider to have compatible types under structural equivalence? Under strict name equivalence? Under loose name equivalence? type T = array [1..10] of integer S=T A:T B:T C:S D : array [1..10] of integer Answer: All four arrays are structurally equivalent. Under name equivalence, array D is incompatible with the others. Under strict name equivalence A and B are also incompatible with C; under loose name equivalence A, B, and C are all mutually compatible. **************************************************************** 8.1 Suppose we are compiling for a machine with 1-byte characters, 2-byte shorts, 4- byte integers, and 8-byte reals, and with alignment rules that require the address of every primitive data element to be an even multiple of the element's size. Suppose further that the compiler is not permitted to reorder fields. How much space will be consumed by the following array? Explain. A : array [0..9] of record s : short c : char t : short d : char r : real i : integer 240 bytes. Within each element of the array, s has offset 0, c has offset 2, t has offset 4 (forced up 1 by alignment), d has offset 6, r has offset 8 (forced up 1 by alignment), and i has offset 16. Adding the length of i, that brings us to 20 bytes, but 20 is not an even multiple of 8. We therefore pad each element out to 24 bytes so that field r will be aligned in each array element. A has 10 elements, so the total length is 240 bytes. What if short and int was the same size (2 bytes) What about a VAX: 32 bit word https://livingcomputers.org/Discover/Online-Systems/User-Documentation/OpenVMS-7-3/5_VAX_Macro_Assembler_Reference.aspx Consider the following C declaration, compiled on a 64-bit x86machine: struct { int n; char c; } A[10][10]; If the address of A[0][0] is 1000 (decimal), what is the address of A[3][7]? Answer: 1000+(3*10*8)+(7*8) = 1296 What if address of struct is 2000? What if a FORTRAN-style array? integer, dimension (0:10, 0:10) :: A integer, dimension (1:9, 1:9) :: A What is the virtual base? Off the wall questions... Which is faster way to sum an array: sum = 0 ; for (int i=0; i<10000; ++) { for (int j=0; j<10000; ++) { sum = sum + V[i][j] ; or sum = sum + V[j][i] } } 8.16 Explain the meaning of the following C declarations: double *a[n]; double (*b)[n]; double (*c[n])(); double (*d())[n]; Answer: double *a[n]; // array of n pointers to doubles double (*b)[n]; // pointer to array of n doubles double (*c[n])(); // array of n pointers to functions returning doubles double (*d())[n]; // function returning pointer to array of n doubles What was that rule??? I'll have to put it in the exam! **************** Not for exam... **************************************************************** Problem 9.4 (p 463) Problem 9.10 (p. 464) Problem 9.15 (p. 465) 9.4 Consider the following (erroneous) program in C: void foo() { int i; printf("%d ", i++); } int main() { int j; for (j = 1; j <= 10; j++) foo(); } Local variable i in subroutine foo is never initialized. On many systems, however, the program will display repeatable behavior, printing 0 1 2 3 4 5 6 7 8 9. Suggest an explanation. Also explain why the behavior on other systems might be different, or nondeterministic. Stack is not zero-filled 9.10 Suppose you are writing a program in which all parameters must be passed by name. Can you write a subroutine that will swap the values of its actual parameters? Explain. (Hint: Consider mutually dependent parameters like i and A[i].) Answer: No, it does not appear to be possible to write a fully general swap routine with call-by-name parameters. For a discussion of the problem, see the article by A. C. Fleck [Fle76]. Problem 9.15 (p. 465) Consider the following declaration in C: double(*foo(double (*)(double, double[]), double)) (double, ...); Describe in English the type of foo. Recall the rule for C: start at the identifier; work as far right as possible, without escaping parentheses, then work as far left as possible without escaping parentheses, then move out one level and repeat. Moving right from foo we find an argument list, so foo is a function, specifically a function of two parameters, neither of which is named. The first parameter to foo is a pointer to a function. For the sake of clarity, call that function F; if it had had a name the name would have appeared between the * and the following right parenthesis. F’s first argument is a double; its second argument is an array of doubles, of unspecified length. The second parameter to foo is also a double. Moving left from foo we find another *, so foo returns a pointer. To figure out the type of the thing to which it points, we move out a level of parentheses. Moving right from there we find another argument list, so foo returns a pointer to a function. That function, call it G, takes variable-length argument lists: the first parameter is a double; the others (zero or more of them) can be of any type. Finally, moving left, G returns a double. Because no function body is provided, this is a declaration of foo, but not a definition. The body must be provided elsewhere.