I. Lisp/Scheme Expression Evaluation (20 Points 3/5/3/3/3/3)

Total Page:16

File Type:pdf, Size:1020Kb

I. Lisp/Scheme Expression Evaluation (20 Points 3/5/3/3/3/3)

Name______Computer Science 260 Fall 2005 Test 3 Closed book. Closed notes.

I. Lisp/scheme expression evaluation (20 points 3/5/3/3/3/3)

Lisp uses a prefix notation for expression evaluation. Evaluate these expressions: Be careful to show a list where a list is appropriate versus an atom. Also assume the following statement has previously been executed:

(define (ssq x y) ( + ( * x x ) (* y y ) ) )

(/ 72 8 3 2)

______

(ssq 5 6)

______

(car '(+ 6 7) )

______

(+ (* 4 5) (/ 6 2))

______

(cons 'a '(1 2 3))

______

(car (cdr (cdr '(+ (* 4 5) (/ 6 2)) ) ) )

______II. Lisp/scheme functions/recursion (10 points + 10 BONUS points) Given the following Scheme function:

(define (su S1 S2 ) ( cond ( (null s1) s2 ) ( (MEMBER (car S1) S2) (su (cdr S1) S2) ) ( else (cons (car S1) (su (cdr S1) S2) ) ) ) )

where S1 and S2 are simple lists (no sublists) and assume MEMBER is a function that takes an element and checks to see if it is in a list

Briefly describe what the function su does.

BONUS: Give an example call to the function that shows how to use the function (assuming no use of sublists) and show the output produced by your example.

CALL->

OUTPUT-> III. Concurrency ( 29 points 2/2/2/2/3/3/6/3/3/3 each ) Assume that a variable T begins with the value 3. Two parallel activities want to modify T. One wants to execute T = T * 2 and the other wants to execute T = T + 1;

List four possible outcomes (resulting values of T) which are possible if one does NOT use any synchronizing mechanisms.

------

------

------

------

What is a critical section?

Given the name of one primitive used to handle critical sections.

Give a specific application scenario where the use of these primitives to manage critical sections would be important and define the problem that could result from a lack of use of the primitive. Given the following thread class, show three possible outputs that could result from it’s execution. public class SimpleThread extends Thread { public SimpleThread(String str) { super(str); } public void run() { System.out.println("A" + getName()); System.out.println("B" + getName()); } } public class TwoThreadsDemo { public static void main (String[] args) { new SimpleThread("X").start(); new SimpleThread("Y").start(); } } OUTPUT 1 OUTPUT 2 OUTPUT 3

III. Scope Rules (16 points 6/10) Given the following program structure if a c/c++ program with three files: #include …

void main () {

}

#include …

void one () {

}

#include …

void two () {

}

Show how to properly declare two integer variables (i and k) in the main routine file and then how to make declarations for one to be able to access i and for two to be able to access k. Given the following program: program main; var x,y: integer; procedure sub1; begin {sub1} writeln('At begin of sub 1 x=',x, 'y=', y); x:=4; y:= 23 end; {sub1} procedure sub2; var x,y: integer; begin {sub2} x:=10; y:= 6; sub1; writeln('At end sub 2 x=',x, 'y=', y) end; {sub2} begin {main} x:=12; y:=14; sub2; writeln('At end of main x=',x, 'y=', y) end. {main}

Assuming the program was compiled and run with DYNAMIC scope rules, show the output printed. Don’t get caught up on syntax, just address the obvious.

Recommended publications