Exercise Sheet A
Exercise Sheet A
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.
Which identifiers, constants, operators and keywords occur in the program? To which values does the program
bind the occurring identifiers?
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.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.
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.
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?
Assume 𝑥 and 𝑦 to be positive natural numbers and only use tail recursion.
3/3