0% found this document useful (0 votes)
4 views3 pages

Exercise Sheet A

This document is an exercise sheet for an Introduction to OCaml course at Saarland University. It contains a series of programming exercises aimed at familiarizing students with OCaml syntax, functions, and recursion. The exercises range from warm-up tasks to more complex problems involving Boolean logic, integer operations, and function tracing.

Uploaded by

toukeaparfait
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)
4 views3 pages

Exercise Sheet A

This document is an exercise sheet for an Introduction to OCaml course at Saarland University. It contains a series of programming exercises aimed at familiarizing students with OCaml syntax, functions, and recursion. The exercises range from warm-up tasks to more complex problems involving Boolean logic, integer operations, and function tracing.

Uploaded by

toukeaparfait
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/ 3

Saarland University Programming 1

Prof. Martina Maggio, Robert Pietsch, Lena Becker https://cms.sic.saarland/prog1_24/

Exercise Sheet A
Introduction to OCaml

Hint: This exercise sheet serves as preparation for the tutorial on 23.10.2024.

Note: Do include explicit specifications of argument types and return types in any function you declare.

Exercise EA.1 (Warmup I)


Read the lecture notes of Gert Smolka up to (and including) Chapter 1.7: “More about Mathematical Functions”.
Visit the office hours and the forum for questions.

Exercise EA.2 (Warmup II)


Visit the interpreter at https://cdltools.cs.uni-saarland.de/soocaml/ and play around with it. Write a
program, evaluate it, and then generate a sharing link using the Share button at the top. If you have problems
with the interpreter, you can use our forum at https://prog1.sic.saarland to get help.

Exercise EA.3 (OCaml Anatomy)


Consider the following program:
1 let x = 7 + 4
2 let y = x * (x − 1)
3 let z = x * (y − 2)

Which identifiers, constants, operators and keywords occur in the program? To which values does the program
bind the occurring identifiers?

Exercise EA.4 (What’s the Magic Word?)


Is the word int in OCaml a keyword, an identifier, a constant, or an operator? Does your opinion change
considering that you can introduce new identifiers in OCaml?

Exercise EA.5 (Looking Beyond the Interpreter)


Identifiers, constants, operators, and keywords do not only occur in OCaml but are part of all programming
languages. Consider the following code snippet in the programming language C.
1 void main () {
2 int a = 5, b = 6;
3 int decision ;
4
5 if ( decision == 0) {
6 b = a * a;
7 } else {
8 b = −5 + (8 − a);
9 }
10 }

Which words are identifiers, constants, operators, and keywords? Justify your statements. (Note: This is not
about consulting a C reference or the internet, but about applying the definitions given in the first chapter.)

1/3
Exercise EA.6 (Machine Integers)
Is it possible for the expression x + 1 < x to evaluate to true? Why?

Exercise EA.7 (To Be or ¬ to Be)


Declare a function neg: bool → bool that implements Boolean negation. Do not use any operators.

Exercise EA.8 (Booleantastic Simplifications)


Use boolean operators ||, &&, and not to simplify complex as much as possible.
1 let complex (a: bool) (b: bool) (c: bool) : bool = if not a then false
2 else if c then b
3 else true

Exercise EA.9 (Give me a Sign!)


Declare a function signum: int → int that evaluates to −1 for negative arguments, 1 for positive arguments,
and 0 for 0. Additionally, give its defining equations.

Exercise EA.10 (Multiplyin’ like it’s 1984)


Declare a recursive function mult: int → int → int that multiplies two natural numbers 𝑥 and 𝑦 using
repeated addition, without the * operator. Give both its defining equations as well as its OCaml declaration.

Exercise EA.11
Declare a function divNeg: int → int that doubles a negative number and diverges if the argument is a
positive number. If you pass zero as an argument, the function should return 42.

Exercise EA.12 (Fair and Sometimes Square)


We want to make sure that the following two functions always compute the same result in our OCaml interpreter:
1 let foo (a: int) : int = a * a
2 let foo ’ (b: int) : int = b * (b − 1) + b

For this, declare a recursive function check: int → bool such that check 𝑥 evaluates to true if for every
𝑦 ≤ 𝑥, it holds that foo 𝑦 = foo′ 𝑦.
Using the check function, specify an expression that evaluates to true if and only if foo and foo′ indeed compute
the same results for all integers available in your OCaml interpreter.

Exercise EA.13 (Sisyphean Recursion)


Consider this declaration of bar. For which values of 𝑥 and 𝑦 does it terminate? For which does it diverge?
Assume the ideal interpreter (i.e., an interpreter that can work with arbitrarily large integers without overflowing).
1 let rec bar (x: int) (y: int) : int = if x = 5 then y else bar (x − 2) y

Exercise EA.14 (May contain traces of elementary school mathematics.)


Consider the following declarations.
1 let rec f (x: int) (y: int) : int = if x = 0 then y else f (x − 1) (x + y)
2 let g (a: int) : int = f a 0

Give a full execution trace of g 1, as well as reduced execution traces of f 3 8 and g 5.


What function does g compute?

2/3
Exercise EA.15 (Tail Recursion)
Consider the following declarations.
1 let rec bee (x: int) : int =
2 if x = 0 then 0 else 4 + bee (x − 1)
3
4 let fox (x: int) : int =
5 let rec fox ’ (x: int) (a: int) : int =
6 if x = 0 then a else fox ’ (x − 1) (a + 4)
7 in fox ’ x 0

Give complete execution traces for bee 2 and fox 2. If you can evaluate more than one expression at the same
time, start with the leftmost expression. Which of bee, fox, and fox’ are tail-recursive?

Exercise EA.16 (New Divide)


Declare a function div: int → int → int that performs integer division 𝑥/𝑦 without using the / operator.
 

Assume 𝑥 and 𝑦 to be positive natural numbers and only use tail recursion.

Exercise EA.17 (Greatest Common Divisor)


In Section 1.5 of the lecture notes, the gcd: int → int → int function is declared. Give reduced execution
traces for the applications gcd 105 126, gcd 250 108, and gcd 42 13.

3/3

You might also like