







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
Intro to Programming Languages- Additional exercises solutions to help prepare for Finals.
Typology: Exercises
1 / 13
This page cannot be seen from the preview
Don't miss anything!
Useful definitions and code snippets: let rec foldl(f:'a'b->'b)(acc: 'b)(l:'a list):'b = match l with | [] - > acc | x::xs - > foldl f (f(x,acc)) xs let rec foldr (f:'a'b->'b)(acc: 'b)(l:'a list):'b = match l with | [] - > acc | x::xs - > f(x, (foldr f acc xs)) Streams : type 'a str = Cons of 'a * ('a stream) and 'a stream = unit - > 'a str let head (s :'a stream) : 'a = match s() with Cons (hd,tl) - > hd let tail (s :'a stream) : 'a stream = match s() with Cons (hd,tl) - > tl let rec nats (i: int) : int stream = fun () - > Cons (i, nats (i+1)) let rec map (f: 'a - > 'b) (s:'a stream) : 'b stream = fun () - > Cons (f (head s), map f (tail s)) let rec ones : int stream = fun () - > Cons (1, ones) let rec zip f s1 s2 () = Cons (f (head s1) (head s2), zip f (tail s1) (tail s2)) Language L1 :
Part 3 Short Answer [80 Marks, each worth 10] P3- 1 Consider the following two OCaml functions: let mystery1 d = foldr (fun (a,c) - > match a with [] - > [] | x::xs - > (x,x)::c) [] d let rec mystery2 d = match d with | x::xs - > (match x with y::ys - > (y,y)::(mystery2 xs) | _ - > []) | _ - > [] [3 Marks] What is the type of mystery1? mystery1 : 'a list list - > ('a * 'a) list [3 Marks] What is the type of mystery2? mystery2 : 'a list list - > ('a * 'a) list [4 Marks] Are mystery1 and mystery2 equivalent where by equivalent we mean if provided with the same input they produce the same output? Please, explain. [Hint: think about what the OCaml interpreter would do when you give to mystery1 and mystery2 the same input] Yes, they are equivalent. They have the same type, so they accept the same objects. When provided with a list of list, they both produce a list of pairs with each pair being composed by the first element of each internal list, until when an empty list is found.
P3- 2 Consider the following two code snippets from OCaml programs. Please describe what the type of f1, f2, a1 and a2 must be if the type of b is int list and the type of u is int. Note, you may need to use type variables (i.e. ‘a ‘b ‘c etc. where precise type constraints are not possible to determine). Program 1 Program 2 f1 a 1 b u ((f 2 b)(a 2 u)) [5 Marks] What is the type of f and a in Program 1? f1: 'a - > int list - > int - > 'b a 1 : ‘a [5 Marks] What is the type of f and a in Program 2? f2: int list - > 'a - > 'b a 2 : int - > 'a
P3-4 Consider the following pseudo code: fun f0(){ var x=0; fun f1(fun g){ var x= g() } fun f2(){ return x+3; } fun f3(){ var x=2; f1(f2) } f3(); } var z=f0(); What is the value of z after the call to f0 terminates when deep binding is used? z= What if shallow binding was used? z= What if ad-hoc binding is used? z= 5
P3- 5 Consider the following grammar:
; a a ; / \ /
a a a a [4 Marks] Rewrite the grammar to give precedence to + over ; maintaining it ambiguous (you may need to introduce new non terminals):
< x add 2, m> => < 6 add 2, m> -----------------------------------------------------------EVAL < (x add 2) add 5 , m> => < ( 6 add 2) add 5 , m> ------------------------------------------------------------------------------EVAL < ((x add 2) add 5) add 2, m> => < ((6 add 2) add 5) add 2, m> COMPOSE 6+2= ---------------------------------------ADD < 6 add 2, m> => < 8 , m> -----------------------------------------------------------EVAL < (6 add 2) add 5 , m> => < 8 add 5 , m> ------------------------------------------------------------------------------EVAL < ((6 add 2) add 5) add 2, m> => < (8 add 5) add 2, m> COMPOSE 8+5= ---------------------------------------ADD < 8 add 5 , m> => < 13 , m> ------------------------------------------------------------------------------EVAL < (8 add 5) add 2, m> => < 13 add 2, m> COMPOSE 13+2= ---------------------------------------ADD < 13 add 2 , m> => < 15 , m>
P3-7 [10 Marks] Activation Records Consider the following program void fun1(float r) { int s; fun3(s,r); } void fun2(int x, float s) { int y; return q+1; // here } void fun3(int q, float r) { fun2(y,y); } void main() { float p; fun1(p); } Draw all the activation record instances active when the instruction at the line with the comment ‘ //here ‘ is being executed. (The information in the activation record consist in: return address, dynamic link, parameters and local variables).
P3- 8 [10 Marks] Infinite Lists We will define a new infinite sequence of numbers that we will call seq. The first three numbers in seq are 1, 2, 3. The nth element in seq is computed by adding the n-3 number, the n- 2 number, and the n-1 number. For example, the 4 th number in seq would be 3+2+1, or 6. The 5 th would be 6+3+2, or 11. Given the helper functions in page 2, create an infinite list that corresponds to seq. seq = [1; 2; 3; 6; 11; 20; 37; 68; 125; 230 … let rec seq () = Cons (1,fun () - > Cons(2, fun() - > Cons(3,zip (+) seq (zip (+) (tail seq) (tail (tail seq))))))