



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Problem 1. [from Scott] Write purely functional Scheme function that returns a list of all permutations of the elements of a given list. For example, given (a b c) it should return ((a b c) (a c b) (b a c) (b c a) (c a b) (c b a)).
Problem 2. Consider the following Scheme expression: (let ((x 10)) (let ((fun (lambda (y) (+ y x)))) (let ((x 5)) (* x (fun 3) ) ) ) a) What is the result of the evaluation of this expression under dynamic scoping? b) What is the result under static scoping?
Problem 3.[From Sethi] Write Scheme functions that compare adjacent list elements and respond with a list, as described in each case. The sample responses are all with the input list
(a b a a a c c) (a) Remove the second and succeeding adjacent duplicates, yielding (a b a c) (b) Leave only the elements that are not repeated, yielding
(a b) (c) Leave only one copy of the repeated elements, yielding
(a c) (d) Count the number of repeated occurrences yielding
((1 a) (1 b) (3 a) (2 c))
Problem 4.[From Sethi] We get a flattened form of a list if we ignore all but the initial opening and final closing parenthesis in the written representation of a list. The flattened form of (a) ((b b)) (((c c c)))) is (a b b c c c). Implement a function flatten to construct flatten forms.
Problem 1. Identify the free variables in each of the following λ-expressions: (a) (λx.(λy.(λz. ((x z) (y z))))) (b) λy.λz. x z (y z) (c) λx. ((λx.x) y) 1
(d) (λx.λy. x y) (y w) (e) (λxyz. x z (y z)) (λx. x) (λx. x)
Problem 2.[From Sethi] Draw a diagram showing all possible reductions from twice(twice) f x to the normal form f (f (f (f x))). Recall that twice is λf x.f (f x).
Problem 3.[From Sethi] Show that the term ZZ where Z is λz.λx. x(z z x) satisfies the requirement of fixed-point combinators: that is, ZZM ⇒β M (ZZM ).
Problem 1. [From Scott] In the following code, which of the variables will a compiler consider to have compatible types under structural equivalence? Under name equivalence?
type T = array [1..10] of integer S = T
A : T B : T C : S D : array[1..10] of integer
Problem 2. [From Prof. B.G. Ryder] Suppose that in a programming language, arrays have their bounds as part of their type; that is, [1:10] int is not the same type as [0:9] int. Consider the following type definitions and variable declarations:
/* type definitions */ type vector [1:10] int; type matrix [1:10] vector; type mm [1:10,1:10] int; type zz [1:10] vector; type yy [0:9] vector; type ss 1..10; type tt 5.. type integ int type s struct {int a1; ss t}; type z struct {ss t1, ss t2}; type w struct {int v, int u} type q struct {integ a2; ss a3};
/* variable declarations */ mm c,d; zz a,b; yy e,f vector v1,v2; matrix m1, m2; ss t1,t2;
(a) What is returned by the call compute(1, 10, 1, J, A[J])? (b) What is returned by the call compute(1,5,1, J, 1/A[J])? (c) Using your knowledge of how compute() works, can you write a procedure that calcu- lates the maximum value from a set of values obtained by evaluating some expression containing a variable where the variable ranges from a given lower bound to an upper bound with a given step size? You can assume the values are integers. Clarification: (c) asks to write a procedure, say max(lower,upper,size,...,expr), where the actual argument for expr could be an expression containing a variable. The procedure should compute the maximum of the values taken by this expression as the variable ranges from lower to upper with step size size. As an example, the call max(0,9,3,...,A[J]) should compute the max value in the following subset of array A: A[0], A[3], A[6], A[9].
Problem 2. [From Sethi] Give a single program fragment that produces different results under each of the following parameter-passing mechanisms:
(a) call by value (b) call by reference (c) call by value-result (d) call by name
Problem 3. [From Sethi] Suppose procedure swap2 is declared as follows:
procedure swap2(x, y: integer); procedure f(): integer; var z: integer; begin (* f ) z := x; x := y; return z; end f ; begin ( swap2 *) y := f() end swap2;
Describe the effect of the procedure call swap2(i, A[i]) under each of the following parameter- passing mechanisms:
(a) call by value (b) call by reference (c) call by value-result
Problem 4. [From Sethi, continues Problem 3]. Suppose that the call by name mechanism is used to pass parameters to the procedure swap2 in Problem 3. Suppose also that an assignment E 1 := E 2 is implemented as follows: compute the l-value of E 1 ; compute the r-value of E 2 ; place the r-value of E 2 into the l-value of E 1
Explain why swap2(i, A[i]) exchanges the values of i and A[i].
Problem 1. Consider the following implementation of method append in class StringBuffer. What could go wrong when the current StringBuffer and the appended StringBuffer sb are accessed by multiple threads?
public final class StringBuffer { private int count; private char[ ] value;
.. public synchronized StringBuffer append(StringBuffer sb) { if (sb == null) sb = NULL; int len = sb.length(); int newcount = count + len; if (newcount > value.length) expandCapacity(newcount); sb.getChars(0, len, value, count); count = newcount; return this; }
public synchronized int length( ) { return count; }
public synchronized void getChars(.. .) {... } }
Problem 2. Consider the following implementation of a servlet that attempts to count requests. What could go wrong with this implementation?
public class CountingFactorizer implements Servlet { private long count = 0;
public long getCount() { return count; }
public void service(ServletRequest req, ServletResponse resp) { BigInteger i = extractFromRequest(req); BigInteger[] factors = factor(i); ++count; encodeIntoResponse(resp, factors);