0% found this document useful (0 votes)
8 views2 pages

Tutorial 0 Introduction

This document is a tutorial sheet for Programming 1 at Saarland University, containing exercises designed to familiarize students with OCaml programming. It includes tasks on basic operations, function definitions, type annotations, debugging code, and logical operations. The exercises aim to enhance students' understanding of programming concepts and their application in OCaml.

Uploaded by

funa teek
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)
8 views2 pages

Tutorial 0 Introduction

This document is a tutorial sheet for Programming 1 at Saarland University, containing exercises designed to familiarize students with OCaml programming. It includes tasks on basic operations, function definitions, type annotations, debugging code, and logical operations. The exercises aim to enhance students' understanding of programming concepts and their application in OCaml.

Uploaded by

funa teek
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

Saarland University Programming 1

Team of Tutors https:// [Link]/ prog1_25/

Tutorial Sheet 0
Introduction

Hint: These exercises were created by the tutors for the tutorials. They are neither relevant nor irrelevant for the exam.
The evaluation of the difficulty corresponds to the assessment of the tutors.

Exercise T0.1 (Getting started with OCaml)


Familiarize yourself with the interpreter at https:// [Link]/ . For that, do the following:
(a) Declare an identifier x and bind the integer value 10 to it.
(b) Declare an identifier y and bind the integer value -421 to it.
(c) Add the previously declared identifiers x and y and bind the result to the identifier z.
(d) Declare a function div that, given two integers, divides them and returns the result. Use the function to calculate
x / y and bind the result to the identifier div_res.

(e) Declare a function that, given two integers and a boolean value b, calculates the sum of the two integer values if b
is true and the quotient of the two integers if b is false.
(f) Declare a function that does something of your choice based on provided inputs. Then, show your function to
your neighbor and let them explain what the function does.

Exercise T0.2 (So much fun)


Consider the following functions. Describe in one sentence what each function does depending on its inputs.
(a) let foo (x : int) = x + 62
(b) let bar ((x : int), (y : int)) = x * y
(c) let fum (a : bool) = if a then 104 else 2339
(d) let baz ((x : int), (t : int)) = 5 * (bar (t, t))
(e) let qux ((s : int), (a : int), (b : int)) = if s mod 2 = 0 then 2 * a else b / 1

Exercise T0.3 (Types, Types everywhere)


Dieter Schlau is screwed. He was asked to put explicit type annotations in his code, but he does not know where to put
in which type. Can you help him?
(a) let square (a : ) : = a * a

(b) let check (b : ) : = if b / 21 = 0 then false else true

(c)
1 let login ((id : ), (pin : )) : = if id = 101010
2 then if pin <> 1610
3 then false
4 else true
5 else false

(d) let test (a : ) : = a mod 2 = 0

1/2
Exercise T0.4 (Inspector Camel)
In the following program, we made five mistakes. Can you find and correct them?
1 let add ((a : bool), (b : int)) : int = a + b
2 let subtract ((a : int), (b : int)) : bool = a - b
3
4 let sum_or_sub ((t : int), (a : int), (b : int)) : int = if t =< 500 then add (a b) else (subtract (a, b))
5
6 let x : int = 5000
7 let y : bool = true
8 let z : int = 1234
9
10 let result : int = sum_or_sub (500, x, y)

Exercise T0.5 (And and or)


Declare two functions band : bool * bool -> bool and bor : bool * bool -> bool using a conditional such
that for a : bool and b : bool:
• band (a, b) returns true if a and b are true, and false otherwise.
• bor (a, b) returns true if a or b is true, and false otherwise.

Exercise T0.6 (Fuzz around and find out)


Consider the following program. Provide inputs for each function, such that the desired value is computed.
1 let f00l (a : int) : int = a * (-1)
2
3 let why ((y : bool), (z : bool)) : bool = if y then (if y = z then true else z) else true
4
5 let magic ((a : int), (b : int), (c : int)) : int = if band (b = c, a < 0) then a * b * c else b * c
6
7 let wow ((x : int), (y : bool)) : int = if bor (y, (f00l x) < 0) then x else 900
8
9 let what ((x : int), (y : int), (z : int), (b : bool)) : int = if b
10 then if x = y
11 then magic (z, x, y) * -1
12 else f00l 599
13 else f00l 1337

(band and bor are declared as in the exercise above.)


(a) Provide an input for f00l such that the function evaluates to 100.
(b) Find inputs for why such that the function evaluates to false.
(c) Find values such that magic evaluates to 64.
(d) Find inputs for wow such that the function returns a negative number.
(e) Provide inputs for what such that a positive number is computed.

2/2

You might also like