DOC PREVIEW
UW CSE 341 - Study Notes

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSE 341 - Programming LanguagesFinal Exam - Autumn 2005Your Name:Open book and notes. No laptop computers, PDAs, or similar devices. (Calculators are OK, although you won’tneed one.) Please answer the problems on the exam paper — if you need extra space use the back of a page.110 minutes, 100 points total1. (14 points)Suppose the following ML functions and exception have been defined:exception UnequalLengthLists;fun zip2 ([], []) = []| zip2 ((x::xs), (y::ys)) = (x,y) :: zip2 (xs, ys)| zip2 _ = raise UnequalLengthLists;fun zip2curried [] [] = []| zip2curried (x::xs) (y::ys) = (x,y) :: zip2curried xs ys| zip2curried _ _ = raise UnequalLengthLists;fun average (x,y) = (x+y)/2.0;What is the value of each of the following expressions? If evaluating it raises an exception, say so. (Hint: thiscode uses the built-in function map in ML, which is curried.)(a) zip2([1.0, 2.0, 3.0], [3.0, 4.0, 5.0])(b) map average (zip2([1.0, 2.0, 3.0], [3.0, 4.0, 5.0]))(c) zip2([1.0, 2.0, 3.0], [3.0, 4.0])What is the type of each of the following expressions? Some of them may give type errors — if so, say that.(a) zip2(b) zip2curried(c) zip2 average(d) map average12. (8 points - 2 points each for parts a and b; 4 points for part c) What is the result of evaluating each of thefollowing Scheme expressions? If there is more than one expression, just give the result of evaluating the finalexpression.(a) (define a 3)(define b 4)(define c 5)(let ((b (+ 2 4))(c (+ b 10)))(+ a b c))(b) (define a 3)(define b 4)(define c 5)(let*((b (+ 2 4))(c (+ b 10)))(+ a b c))(c) (define z ’y)(define y ’x)(define x 42)(define w ’w)(listz(eval z)(eval (eval z))(eval (eval (eval z)))w(eval w)(eval (eval w))(eval (eval (eval w))))3. (6 points)(a) What is the result of evaluating the following expressions in Scheme? (Just give the value of the finalexpression.)(define y 3)(define (f x) (+ x y))(let ((x 10)(y 20))(f 100))(b) Suppose Scheme used dynamic scoping rather than lexical scoping. In that case, what would be the valueof the final expression?24. (12 points) Write a Scheme definition for my-cond, a macro for the built-in Scheme “cond”. Use define-syntax.The definition for cond is as follows:(cond <clause1> <clause2> ...)Syntax: Each <clause> should be of the form(<test> <expression1> ...)where <test> is any expression. The last <clause> may be an “else clause,” which has the form(else <expression1> <expression2> ...).Semantics: A cond expression is evaluated by evaluating the <test> expressions of successive <clause>s inorder until one of them evaluates to a true value. When a <test> evaluates to a true value, then the remaining<expression>s in its <clause> are evaluated in order, and the result of the last <expression> in the<clause> is returned as the result of the entire cond expression. If the selected <clause> contains only the<test> and no <expression>s, then the value of the <test> is returned as the result. If there are no true<clause>s, cond should return nothing. In your macro, you can get this result with the expression (void).Macros: As a refresher on macro syntax, here are a couple examples we saw in class:(define-syntax my-if ; macro name(syntax-rules (then else) ; literals it uses, if any((my-if e1 then e2 else e3) ; pattern(if e1 e2 e3)))) ; template(define-syntax my-or(syntax-rules ()((my-or) #f)((my-or e) e)((my-or e1 e2 ...)(let ((temp e1))(if temptemp(my-or e2 ...))))))35. (6 points)(a) Supose that you have a global variable k declared in Scheme:(define k #f)What is the value returned by evaluating the following Scheme expression?(+ 10(call/cc(lambda (c)(set! k c)(*5 6)))20)(b) After the above expression has been evaluated and k reassigned, what is the value returned by this expres-sion?(k 100)(c) And this one?(*3 (k 200))6. (12 points) Tacky but easy-to-grade true/false questions!(a) An advantage of static typing over dynamic typing is that it is more flexible — more programs can executecorrectly.(b) An advantage of static typing over dynamic typing is that more errors can be caught at compile time ratherthan run time.(c) An advantage of static typing over dynamic typing is that type declarations provide machine-checkabledocumentation.(d) After a continuation has been resumed in Scheme, it is used up — it can’t be resumed again.(e) In a pure functional language, any function will return the same answers if call-by-name is used instead oflazy evaluation.(f) In a pure functional language, any function will return the same answers if call-by-name is used instead ofcall-by-value.7. (6 points) Discuss two reasons inner classes are useful for defining iterators in Java.48. (8 points) Consider the following Smalltalk class definitions.Object subclass: #AnimalclassVariableNames: ’’poolDictionaries: ’’describeTranscript show: ’An animal’.self additionalDescription.additionalDescriptionTranscript show: ’ - no further information available’.__________________________________________________Animal subclass: #SeaCreatureinstanceVariableNames: ’’classVariableNames: ’’poolDictionaries: ’’additionalDescriptionTranscript show: ’ that lives in the sea’.Transcript cr.self yetMoreDescription.yetMoreDescriptionTranscript show: ’Also a source of 341 variable names’.__________________________________________________SeaCreature subclass: #OctopusinstanceVariableNames: ’’classVariableNames: ’’poolDictionaries: ’’additionalDescriptionTranscript show: ’ that has tentacles and’.super additionalDescription.__________________________________________________Octopus subclass: #GiantOctopusinstanceVariableNames: ’’classVariableNames: ’’poolDictionaries: ’’yetMoreDescriptionTranscript show: ’It can grow up to 300 pounds’.5What is printed when each of the following expressions is evaluated?• Animal new describe• SeaCreature new describe• Octopus new describe• GiantOctopus new describe69. (16 points)Consider the following Java code fragments. In each case, does the code compile correctly? If so, does itexecute without error, or is there an exception? (Hints: the List interface includes an add method. The classArrayList implements the List interface.)(a) List<String> s = new ArrayList<String>();s.add("squid");(b) ArrayList<Object> s = new ArrayList<String>();s.add("squid");(c) ArrayList<String> s = new ArrayList<Object>();s.add("squid");(d) String [] s = new String [10];s[0] = "squid";(e) Object [] s = new String[10];s[0] = "squid";(f) String


View Full Document

UW CSE 341 - Study Notes

Documents in this Course
Macros

Macros

6 pages

Macros

Macros

6 pages

Macros

Macros

3 pages

Mutation

Mutation

10 pages

Macros

Macros

17 pages

Racket

Racket

25 pages

Scheme

Scheme

9 pages

Macros

Macros

6 pages

Load more
Download Study Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?