Online CS University
Advanced functional programming with OCaml
Homework & Practice Quiz
Exercise 01: Predefined functions
1- Explain what the following predefined functions do in Ocaml:
Int_of_float , float_ of_int, int_of_char, ceil, floor, sqrt, sin, cos, exp, log.
2- The Ocaml language has a rich string module. Explain the following functions related to this
module:
Length, get, create, sub, index, compare
3- Explain the following functions in Ocaml's List module: map, iter, for_all, exists
Exercise 02: Natural numbers
4- The following type is defined in OCAML to represent natural integers:
5- type nat = O | S of nat ;;
6- According to this definition, a natural number is either a zero, represented by the O
constructor, or the successor of a natural number, represented by the S constructor.
, 7- Evaluate the following expressions : S(S O) ;; O ;; S O ;; S(S(S(O))) ;;
Part 01: Calculations on the defined type
8- Write the recursive function addn to add two elements of type nat (the result is of type nat).
9- Write the recursive function multn multiplication of two elements of type nat (the result is of
type nat).
10-Write a factn function that calculates the factorial of a nat number (the result is a nat
number).
Part 02: Property checks on the defined type
11-Write a function that checks whether a number of type nat is zero.
12-Write a function that checks whether a number of type nat is even.
Part 03: Transformation from/to the defined type
13-Write a recursive int_of_nat function to obtain the integer value (type int) corresponding
to a nat value.
14-Write a nat_of_int function to obtain the nat value corresponding to a positive or zero integer.
Advanced functional programming with OCaml
Homework & Practice Quiz
Exercise 01: Predefined functions
1- Explain what the following predefined functions do in Ocaml:
Int_of_float , float_ of_int, int_of_char, ceil, floor, sqrt, sin, cos, exp, log.
2- The Ocaml language has a rich string module. Explain the following functions related to this
module:
Length, get, create, sub, index, compare
3- Explain the following functions in Ocaml's List module: map, iter, for_all, exists
Exercise 02: Natural numbers
4- The following type is defined in OCAML to represent natural integers:
5- type nat = O | S of nat ;;
6- According to this definition, a natural number is either a zero, represented by the O
constructor, or the successor of a natural number, represented by the S constructor.
, 7- Evaluate the following expressions : S(S O) ;; O ;; S O ;; S(S(S(O))) ;;
Part 01: Calculations on the defined type
8- Write the recursive function addn to add two elements of type nat (the result is of type nat).
9- Write the recursive function multn multiplication of two elements of type nat (the result is of
type nat).
10-Write a factn function that calculates the factorial of a nat number (the result is a nat
number).
Part 02: Property checks on the defined type
11-Write a function that checks whether a number of type nat is zero.
12-Write a function that checks whether a number of type nat is even.
Part 03: Transformation from/to the defined type
13-Write a recursive int_of_nat function to obtain the integer value (type int) corresponding
to a nat value.
14-Write a nat_of_int function to obtain the nat value corresponding to a positive or zero integer.