






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The answer key for the final exam of the Programming Languages course (CSE 341) taken in Autumn 2012. The exam consists of six questions covering topics such as Haskell, Racket, Prolog, and MUPL. The questions require students to write functions, demonstrate macro hygiene, and identify errors in code. detailed answers and explanations for each question.
Typology: Exams
1 / 10
This page cannot be seen from the preview
Don't miss anything!
Total points possible: 134
mapfns [(+1), (*2), sqrt] [2.0, 3.5, 4.0] => [3.0, 7.0, 2.0] mapfns [(+1), (+1)] [1..] => [2, 3]
mapfns :: [t -> a] -> [t] -> [a]
mapfns (f:fs) (x:xs) = f x : mapfns fs xs mapfns _ _ = []
(twice '()) => #f (twice '(a b b c d e)) => #t (twice '(a b c a b)) => #f
(define (twice s) (cond ((null? s) #f) ((null? (cdr s)) #f) ((equal? (car s) (cadr s)) #t) (else (twice (cdr s)))))
(define-syntax my-or (syntax-rules () ((my-or) #f) ((my-or e) e) ((my-or e1 e2 ...) (let ((temp e1)) (if temp temp (my-or e2 ...))))))
It relies on macro hygiene. Demonstrate this by writing Racket code that would result in a wrong answer from my-or if Racket didn’t have macro hygiene. Also say both what the correct answer is, and what the answer would be if Racket didn’t have macro hygiene.
(let ((temp 3)) (or #f (= temp 3)))
Without macro hygiene, when the macro is expanded it uses a variable temp in the if expression. This will have the value #f, which shadows the binding of temp to 3. The expression should have the value #t,but without macro hygiene it would evaluate to #f.
twice([X,X|Xs],X). twice([X|Xs],Y) :- twice(Xs,Y).
What are all the answers returned for the following goals? If there are an infinite number, say that, and include at least the first 3 answers.
(a) twice([1,2,2],B). Answer: B=2. (b) twice([1,2,2,1],B). Answer: B=2. (c) twice([1,2,3,2,1],B). Answer: false. (d) twice([1,2,2,2,3,1,1],B). Answers: B=2; B=2; B= (e) twice(As,1). Answers: As = [1, 1|X1]; As = [X1, 1, 1|X2] ; As = [X1, X2, 1, 1|X3]; .... (infinite number of answers – here X1 etc are going to be variable names that Prolog generates)
twice([X,X|Xs],X) :- !. twice([X|Xs],Y) :- twice(Xs,Y).
What are all the answers returned for the following goals? (These are the same as for Question 5.) If there are an infinite number, say that, and include at least the first 3 answers.
(a) twice([1,2,2],B). Answer: B=2. (b) twice([1,2,2,1],B). Answer: B=2. (c) twice([1,2,3,2,1],B). Answer: false.
What are all the answers are returned for the goal ancestor(dolphin,X)? For full credit you need to write down the answers in exactly the order that your program will return them. The answer X=dolphin should be produced first, but after that you can return the answers in any order; duplicates are OK.
?- ancestor(dolphin,X). X = dolphin ; X = mammal ; X = animal ; X = object ; X = seacreature ; X = animal ; X = object ; false.
reverse(Xs,Rs) :- reverse_dl(Xs,Rs[]).
reverse_dl([],T\T). reverse_dl([X|Xs],Rs\T) :- reverse_dl(Xs,Rs[X|T]).
Draw the derivation tree for the following goal:
?- reverse([a,b,c],R).
Please see the scan of the hand-drawn tree at the end of this pdf.
class C def print_me "C1 print_me" end def test 1 end end
module M def print_me "M1 print_me" end def test 10+super end end
module M def print_me "M2 print_me" end def test
end end
class C2 < C include M end
class C3 < C include M1, M end
Now define variables c1, c2, and c3 as follows:
c1 = C1.new c2 = C2.new c3 = C3.new
What is printed as a result of evaluating the following expressions? (Remember that the value returned by a method is the value of the last expression, if there isn’t an explicit return.)
c1.print_me => "C1 print_me"
c2.print_me => "M1 print_me"
c3.print_me => "M1 print_me"
c1.test => 1
c2.test => 11
c3.test => 110
initialize Initialize this to be an empty set. add(element) Add an element to this set, if it’s not already a member. (This changes the set.) union(other) Return a new set that is the union of the receiver and other. This doesn’t change the set (no side effect).
a = new String[5]; b = a; a[0] = "oyster"; test(a,1); test(b,2); test( (Object[]) a, 3); test( (String[]) b, 4); for(int i = 0; i<a.length; i++) { System.out.print(a[i]); System.out.print(" "); } System.out.print("\n"); } public static void test(Object[] c, int i) { c[i] = "clam"; } public static void test(String[] c, int i) { c[i] = "squid"; } }
Executes without error - output is: oyster squid clam clam squid
/***************** Test2 *****************/ import java.util.LinkedList; class Test2 { public static void main(String[] args) { LinkedList
Doesn't compile - there are type errors:
Test2.java:21: test(java.util.LinkedList<java.lang.Object>,int) is already defined in Test public static void test(LinkedList
These errors are all because LinkedList
class Test3 { public static void main(String[] args) { String[] a; Object[] b; a = new String[2]; b = a; a[0] = "oyster"; System.out.println("added an oyster"); b[1] = new Integer(5); System.out.println("added an integer"); for(int i = 0; i<a.length; i++) { System.out.print(a[i]); System.out.print(" "); } System.out.print("\n"); } }
Compiles, but gets a runtime exception:
added an oyster Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer at Test3.main(Test3.java:9)
(a) A Haskell expression of type IO t can never occur inside another expression of type (Num t) => [t]