

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
The questions and instructions for a programming exam focused on ocaml language, including topics such as garbage collection, fold function, type inference, currying, tuples, context-free grammar, and regular expressions. Students are required to write answers in the provided book and are allowed to consult one handwritten sheet.
Typology: Exams
1 / 2
This page cannot be seen from the preview
Don't miss anything!


INSTRUCTOR: GUILHERME FONSECA
(1) (15 points) Give short anwers: (a) Garbage collection makes dangling pointers impossible. Give one short fragment of C language code which tries to access the memory pointed by a dangling pointer. (b) There are two basic techniques that can be used to simulate multiple arguments in OCaml: tuples and Currying. Give one advantage of Currying compared to using tuples to simulate multiple arguments. (c) Describe a language that is context-free, but not regular.
(2) (10 points) The fold function can be written in OCaml as:
let rec fold f a l = match l with [] -> a | (h::t) -> fold f (f a h) t Write a fold function with a similar behavior in Ruby using codeblocks. The following Ruby code should work with your fold function and return the sum of the elements in an Array: fold([5, 5, 3, 1],0) {|x,y| x+y} # Returns 14
(3) (15 points) What is the type of f in each of the OCaml lines below. (a) let f = (2*3, 2.0 *. 3.0) (b) let f x = 1 (c) let f x y = x +. y (d) let f (x,y) = x::y (e) let f x y z = (x y)::z
(4) (10 points) Write a merge function in OCaml that receives two sorted lists and returns the sorted list containing the elements that are in either list, as the example below. You can assume that no element will appear multiple times in the input lists. Do not use any library function. All listed are sorted in increasing order, accoding to OCaml comparison operators (<, >, <=...). merge [1; 2; 7; 9] [3; 5; 8; 12] (* Returns [1; 2; 3; 5; 7; 8; 9; 12] *) The type of your function should be: ’a list -> ’a list -> ’a list 1
(5) (10 points) Suppose some programmer wrote two OCaml functions f and g, and f is supposed to be the inverse of g, that is, f (g(x)) = x for all x. The problem is that you are not sure if the implementations are correct. First, you confirmed that the types of f and g are correct. To make sure the functions are the inverse of each other, you would like to give each element x from a list of test inputs f (g(x)), and answer whether f (g(x)) = x for all values x in the list. The function testInv that you need to write takes 3 arguments using currying. The arguments are, in order: the first function f , the second function g, and the list of test inputs. You need to write a function testInv with the following behavior: let f1 x = 2+x;; let f2 x = x-2;; let f3 x = x*x;;
testInv f1 f2 [0;3;4;7];; (* returns true ) testInv f2 f3 [0;3;4;7];; ( returns false ) testInv f2 f3 [-1;2];; ( returns true *) The functions f and g take a single argument and return a value that can be compared with OCaml’s = operator. Besides that, the functions f and g have no side effect. Feel free to write other helper functions, but you must write the OCaml code for all functions that you use.
(6) (8 points) Write a regular grammar for the following language in the alphabet Σ = {a, b}: {w | w has at least one a, at least one b, and all the a’s occur before all the b’s }
(7) (10 points) Write a context-free grammar for the following language in the alphabet Σ = {a, b, c}: {aibj^ ck^ | i, j, k ≥ 0 and i ≤ k}
(8) (12 points) Consider the following BNF grammar. Quotes are not part of the string and are used to indicate the terminal symbols that are not alphanumeric. 〈expr1〉 ::= 〈expr1〉 “∪” 〈expr2〉 | 〈expr2〉 〈expr2〉 ::= 〈set〉 “∩” 〈expr2〉 | 〈set〉 〈set〉 ::= “{}” | “{” 〈element〉 〈elements〉 “}” 〈elements〉 ::= “,” 〈element〉 〈elements〉 | ε 〈element〉 ::= a | b | c | 〈set〉 (a) Which operator has higher precedence: ∪ or ∩? (b) What is the associativity of the ∪ operator? (c) What is the associativity of the ∩ operator?
(9) (10 points) One or more questions of the midterm 1 and/or pseudo-midterm 1 will be here without any changes (except for the number of points).
2