0% found this document useful (0 votes)
20 views11 pages

Mock Exam Endterm

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views11 pages

Mock Exam Endterm

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Mock Exam for Endterm

Programming 1 (WS 2023/24)

On demand

Name:

Matriculation Number:

Seat:

Do not open this exam booklet before we ask you to do so :)


Do read this page carefully.
You may not use additional materials or tools. This mock exam covers the second half of the lecture. For the first
half, refer to the mock Midterm. The contents of this mock exam are neither relevant nor not relevant for the
endterm and are thought of by the tutors.
We recommend you to try this exam after revisiting the topics on the exercise and tutorial sheets. The difficulty
of the exercises are estimations of the tutors and can differ from what you may experience.
Generally: Keep calm when writing the Prog1 endterm or other exams in the future. You can do it!
The exam should have a length of around 90 minutes. We omitted the points to not confuse you. Some exercises
may have suggest points in the solution.
Note that the appendix may be incomplete at the beginning.
The exam language is English. For the endterm you can choose if you want a bilingual exam in the CMS. These
best-effort translations are there to provide additional help to you; however, in case of contradictions, the English
version takes precedence. You may submit answers in English or German at your convenience.

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 → 𝛼 → 𝛼

• iterup: (int * 𝛼 → 𝛼 ) → int → int → 𝛼 → 𝛼

• first: (int → bool) → int → int

• map: ( 𝛼 → 𝛽 ) → 𝛼 list → 𝛽 list

• append: 𝛼 list → 𝛼 list → 𝛼 list (available as @)

• rev: 𝛼 list → 𝛼 list

• rev_append : 𝛼 list → 𝛼 list → 𝛼 list

• length: 𝛼 list → int

• seq: int → int → int list

• flatten : 𝛼 list list → 𝛼 list

• filter: ( 𝛼 → bool) → 𝛼 list → 𝛼 list

• mem: 𝛼 → 𝛼 list → bool

• exists: ( 𝛼 → bool) → 𝛼 list → bool

• forall: ( 𝛼 → bool) → 𝛼 list → bool

• hd: 𝛼 list → 𝛼 (raises Not_found in case list is empty)

• tl: 𝛼 list → 𝛼 list (raises Not_found in case list is empty)

• fold: ( 𝛼 list → 𝛼 ) → tree → 𝛼

Constructor Types:

• type 𝛼 option = None | Some of 𝛼

• type tree = T of tree list

• 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, [])])]

Hint: You may split the tree into smaller parts.

Exercise 1.2 (ToyCaml extension)


We extend ToyCaml by Match statements with exactly 2 cases. Because our ToyOcaml only supports integers,
booleans and variables, we only allow to match on integers and boolean in the matchcases so that we do not have to
worry about the variable-defining property of the match statement.
Example:
1 let m = 1 in
2 match m with
3 | 1 → true
4 | 10 → false

Use the following extended abstract syntax:


1 type token = (* ... *)| ARROW | MATCH | WITH | PIPE
2 type exp = (* ... *) | Match of exp * (exp*exp*exp*exp)

(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.

May the recursion be 3 / 11


with you(with you(with you(. . . ))).
1 let eval f e =
2 let val_eq v1 v2 = match v1 , v2 with
3 | Ival x, Ival y → x = y
4 | Bval x, Bval y → x = y
5 | _ → failwith "not␣ values "
6 in match e with
7 (* ... *)
8 | ? → ?
9 (* ... *)

May you fold the exam 4 / 11


and not let the exam fold you.
Exercise 2: Runnung Time (0 Points)
Exercise 2.1 (Tontaubenschießen)
Declare a function f: int → int that has a running time of 𝑂(𝑛 2 · log 𝑛) and that returns the given argument.
Hint: You are allowed to declare helper functions.

Exercise 2.2 (Hierachy)


Sort according to the asymptotic behavior:

1000 𝑛
1

• 𝒪 (3) • 𝒪

𝑛𝑛
1 2

• 𝒪 • 𝒪 ((𝑛 − 1)!)

Exercise 2.3 (F-ing factorials)


Let the following function definition be given:

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!

Exercise 2.4 (Fishing simulator)


Let the following function definition be given:

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. 𝑛.

Sorting is like life: sometimes ascending, 5 / 11


sometimes descending
Exercise 3: Termination (0 Points)
Exercise 3.1 (Stop, he’s already dead!)
(a) How do you reason about termination? Why does the approach work?
(b) Show that the following functions are terminating.
(i)

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

If this exam is not your type, 6 / 11


try to be more polymorphic!
Exercise 4: Induction (0 Points)
Exercise 4.1 (Oh, yeah. It’s all coming together.)
Consider the following function definition:

f :Z→Z→Z→Z
f (𝑥, 𝑦, 𝑧) = 𝑧 if 𝑥 < 𝑦
f (𝑥, 𝑦, 𝑧) = f (𝑥, 𝑦 + 1, 𝑧 + 𝑥) if 𝑥 ≥ 𝑦

(a) Prove that f terminates.


(b) Consider the following claim ∀𝑥, 𝑦 ∈ Z2 . 𝑥 ≥ 𝑦 =⇒ 𝑓 (𝑥, 𝑦, 0) = (𝑥 − 𝑦 + 1) · 𝑥.
(i) What kind of induction can you use to proof this and on what argument of the function?
(ii) Do you need strengthening? If yes, give the strengthened term. If not, explain why you claim is
sufficiently strong.
(iii) Now prove the claim.

What do generators do? 7 / 11


They generate good results!
Exercise 5: Arrays and persistent Memory (0 Points)
Exercise 5.1 (The footnote is funny though ;))
Write a prime generator. You may assume that nextprime : int → int is already declared for you. First, give
the type of the generator. Your generator should be efficient, i.e., not calculate all primes up to the number in
each step.

Exercise 5.2 (Warum Tauben?)


Write a generator pigen: unit → float that approximates 𝜋 with each call. Use the fact that

1 1 1 1 𝜋
1− + − + −··· =
3 5 7 9 4

Hint: It could be helpful to store the sign separately.

Exercise 5.3 (A Gourmet)


Write an OCaml function fillarray : 𝛼 array → (unit → 𝛼 ) → unit that fills an array (in order from
left to right) with the generated values of the passed generator.

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

Implement the priority queue as a sorted list.

Exercise 6.2 (Doubly linked list)


A doubly linked list is a data structure where each element has a link to the previous and next element. The
signature and a code structure for the implementation is given below.
Complete the following functions:
(a) get x should return the value saved in the doubly linked list element x.
(b) getNext x and getPrev x returns the next/previous element of x.
(c) getNth x n should return the value saved in the n-th element after x.
(d) remove x removes x from the doubly linked list.
(e) insertAfter x v inserts an element with v as the successor of x.
(f) update x v sets the value of the element x to v.
Hint: Remember to update the links to the previous and next element, when inserting or removing elements.
For invalid cases the exception should be thrown.
1 module type DOUBLYLINKEDLIST = sig
2 type 𝛼 dllelement
3 val make : 𝛼 → 𝛼 dllelement ref
4 val get : 𝛼 dllelement ref → 𝛼
5 val getNth : 𝛼 dllelement ref → int → 𝛼
6 val getNext : 𝛼 dllelement ref → 𝛼 dllelement ref
7 val getPrev : 𝛼 dllelement ref → 𝛼 dllelement ref
8 val remove : 𝛼 dllelement ref → unit
9 val insertAfter : 𝛼 dllelement ref → 𝛼 → unit
10 val update : 𝛼 dllelement ref → 𝛼 → unit
11 end
12
13 module DoublyLinkedList : DOUBLYLINKEDLIST = struct
14 type 𝛼 dllelement = Nil | Elem of 𝛼 dllelement ref * 𝛼 * 𝛼 dllelement ref
15 exception InvalidElement
16 let make x = ref (Elem(ref Nil ,x,ref Nil))
17 let get x = (* TODO *)
18 let getNext x = (* TODO *)
19 let getPrev x = (* TODO *)
20 let getNth x n = (* TODO *)
21 (* helper function : *)
22 let setNext x next= match !x with
23 | Nil → ()
24 | Elem(p,v,n) → x B Elem(p,v,next)
25 (* helper function : *)
26 let setPrev x prev= match !x with
27 | Nil → ()
28 | Elem(p,v,n) → x B Elem(prev ,v,n)
29 let remove x = (* TODO *)

Gambare Gambare! 9 / 11
30 let insertAfter x value = (* TODO *)
31 let update x v = (* TODO *)
32 end

Irgendwann kommt jeder mal an einen Tiefpunkt, 10 / 11


dann geht es nur noch auf oder abwärts
Exercise 7: Puzzel Corner (0 Points)
Exercise 7.1 (¬⊥! It’s funny because it’s ⊤)

(a) • yes no The list [1,2] has length 2.

• yes no A pre-projection is associated with exactly one tree.

• yes no There is a value of type ∀𝛼.𝛼.

• yes no The expression: let foo x = fun x → let y = 3 in x*y in x has no free variable.

• yes no Given an array a with a.(0)=1. The expression (fun x y → y) (a.(0)<−a.(0)*2)



(a.(0) <− a.(0)+1) has a deterministic evaluation.

• yes no With binary search one can find a value in log(𝑛) on any array of length 𝑛.

(b) Consider the following code:


1 let a f x = f x
2 let rec f x = f (x+1)
3 let b = a f

What is the running time of calculating b.


Hint: An informal argument is enough
(c) In the following code are 2 errors, find and correct them.
1 let rec nth_opt l n =
2 match l with
3 | [] → None
4 | [x :: l] → if n < 1 then x else nth_opt l (n−1)

(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

Irgendwann kommt jeder mal an einen Tiefpunkt, 11 / 11


dann geht es nur noch auf oder abwärts

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy