Mock Exam Endterm
Mock Exam Endterm
On demand
Name:
Matriculation Number:
Seat:
Viel Erfolg!
1 2 3 4 5 6 7 Sum
0 0 0 0 0 0 0 0
You are allowed to use the following functions and constructor types unless stated otherwise:
Functions:
• fst: 𝛼 * 𝛽 → 𝛼
• snd: 𝛼 * 𝛽 → 𝛽
• foldl: ( 𝛼 → 𝛽 → 𝛽 ) → 𝛼 list → 𝛽 → 𝛽
• fold: ( 𝛼 → 𝛽 → 𝛽 ) → 𝛼 list → 𝛽 → 𝛽
• iter: ( 𝛼 → 𝛼 ) → int → 𝛼 → 𝛼
Constructor Types:
• type comparison = LE | EQ | GR
2 / 11
Exercise 1: Syntax and Semantics (0 Points)
Exercise 1.1 (Derivation trees)
Derive the following expression according to the dynamic semantics in the given environment 𝐸:
Expression: g 6
Environment: 𝐸 := [𝑔 ⊲ (𝑥, if f x then x /2 else (𝑥 + 1)/2, [ 𝑓 ⊲ (𝑦, 𝑦 mod 2 = 0, [])])]
(a) Give the type derivation rule for the two case Match statement
𝐸 ⊢ match x with | v1 → e1 | v2 → e2 : t2
(b) Extend the function parse_simple_expr and create the function parse_match , which will handle the
case of the match statement. Use the function expect_value to ensure the match only uses values in the
case distinction. You can assume that the code in (*...*) is given.
1 let expect t ts = match ts with
2 | t’ :: ts when t = t’ → ts
3 | _ → failwith " parse ␣ error "
4 let expect_value ts = match ts with
5 | ICON c :: ts → Icon c, ts
6 | BCON c :: ts → Bcon c, ts
7 | _ → failwith " parse ␣ error "
8 let rec parse_expr ts =
9 (* ... *)
10 and parse_match ts = match ts with
11 | ? → ?
12 | _ → failwith " parser ␣ error ␣not␣2␣ match ␣ cases "
13 and parse_simple_expr ts = match ts with
14 (* ... *)
15 | ? → ?
16 (* ... *)
(c) Extend the function elab with the two case match statement.
1 let check_ty env e =
2 let ty_eq t1 t2 = match t1 , t2 with
3 | Some t1 , Some t2 → t1 = t2
4 | _, _ → false in
5 match e with
6 (* ... *)
7 | ? → ?
8 (* ... *)
(d) Extend the function eval for matching with two cases. Use the function val_eq to check if the containt of
the values is identical.
1000 𝑛
1
• 𝒪 (3) • 𝒪
𝑛𝑛
1 2
• 𝒪 • 𝒪 ((𝑛 − 1)!)
fac : N → N
fac 0 = 1
fac 𝑛 = 𝑛 · fac(𝑛 − 1) for 𝑛 ≥ 1
(a) Give the closed form of the running time function of fac with respect to the natural size function 𝜆𝑛 ∈ N. 𝑛.
(b) Give the asymptotic running time of fac in respect to the same natural size function. You do not have to
explain your answer!
blub : N → N
blub 0 = 1 − 1
blub 1 = 1
blub 𝑛 = fac 𝑛 + blub(𝑛 − 1) − blub(𝑛 − 1) for 𝑛 > 1
State the recursive representation of the running time function of blub in respect to the size function 𝜆𝑛 ∈ N. 𝑛.
first : N → N
first (𝑛) = 42 · 𝑛 + 1337 𝑛=0
first (𝑛) = first(𝑛 − 1) + first(𝑛 − 1) 𝑛>0
(ii)
second : L (𝑋) → N
second [] = 1
second (𝑥 :: 𝑥𝑟) = 2 · second(𝑥𝑟)
(iii)
euclid : N2 → N
euclid (𝑥, 𝑦) = if 𝑦 = 0 then 𝑥 else euclid(𝑦, 𝑥 mod 𝑦)
(iv)
funny : N → N
funny (0) = 0
funny (1) = 1
funny (𝑛) = funny(𝑛 − 1) + funny(funny(𝑛 − 1) − funny(𝑛 − 2)) for 𝑛 ≥ 2
f :Z→Z→Z→Z
f (𝑥, 𝑦, 𝑧) = 𝑧 if 𝑥 < 𝑦
f (𝑥, 𝑦, 𝑧) = f (𝑥, 𝑦 + 1, 𝑧 + 𝑥) if 𝑥 ≥ 𝑦
1 1 1 1 𝜋
1− + − + −··· =
3 5 7 9 4
Gambare Gambare! 8 / 11
Exercise 6: Data Structures (0 Points)
Exercise 6.1 (Priority Queues)
Entries in a priority queue are pairs (𝑝, 𝑥), which contain a priority 𝑝 ∈ Z and a value 𝑥. When you add an entry
(𝑝, 𝑥) into a priority queue, it is added in such a way that all present entries with a priority ≥ 𝑝 appear in front
of it and all present entries with a priority < 𝑝 appear after it. You can assume that all priorities are pairwise
different.
The following signature is given for priority queues:
1 module type PQUEUE = sig
2 type 𝛼 queue
3 val empty : 𝛼 queue
4 val insert : 𝛼 queue → int → 𝛼 → 𝛼 queue
5 val union : 𝛼 queue → 𝛼 queue → 𝛼 queue
6 val head : 𝛼 queue → 𝛼 (* Failure *)
7 val tail : 𝛼 queue → 𝛼 queue (* Failure *)
8 end
Gambare Gambare! 9 / 11
30 let insertAfter x value = (* TODO *)
31 let update x v = (* TODO *)
32 end
• yes no The expression: let foo x = fun x → let y = 3 in x*y in x has no free variable.
• yes no With binary search one can find a value in log(𝑛) on any array of length 𝑛.
(d) After the Prog1 lecture, your friend Dieter Schlau seems down. After asking him, he tells you that he
doesn’t like sweet things. Thus you decide to remove constructs in OCaml that makes things sweeter (and
your studies not a complete pain). Find alternatives for the following constructs:
(i) e;e’
(ii) rec