Part 2 - Expressions and functionsExercisesType-inference by yourselfDocumentation and user's manualTable of contentsOCaml programs

Type-inference by yourself

 1 
  All the following Caml expressions are syntactically correct. Consider each of them. Has it a value? If it has a value then which one and of which type? Else why?

5 * 4 * 3 * 2 * 1 ;;
true ;;
if 2 > 0 then "true"
         else "false" ;;
if 2 > 0 then true
         else false ;;
if 2 > 0 then "yes"
         else "no" ;;
if 2 > 0 then yes
         else no ;;
if 2 > 0 then true
         else "false" ;;
3.14 +. 2. ;;
3.14 +. 2 ;;
3.14 + 2.0 ;;
3.14 > 2. ;;
3.14 > 2 ;;

int_of_float (3.14) > 2 ;;
3.14 > float_of_int (2) ;;
3 > 2 ;;
"three" > "two" ;;
"two" > "one" ;;  
5 / 2 ;;
5. /. 2. ;;
sqrt (4.) ;;
sqrt (4) ;;
sqrt ;;
exp (1.) ;;
log (exp (1.)) ;;
5 / 0 ;;
if true then 5 / 0
        else 3 ;;
if true then 3
        else 5 / 0 ;;

if false then 5 / 0
         else 3 ;;
if false then 3
         else 5 / 0 ;;
"s" ^  "i" ;;
`s` ^  "i" ;;
`s` ^  `i` ;;
"s" ^ i ;;
si ;;
"if" ;;
function x -> x > 0 ;;
(function x -> x > 0) (12) ;;
function x -> 2 * x ;;
(function x -> 2 * x) (7) ;;

function (x, y) -> (x + y, x - y) ;;
function x -> x / 0 ;;
(function x -> x / 0) (12) ;;
(function x -> x) (5) ;;
(function x -> x) ("yes") ;;
function x -> x ;;
(function x -> 1) (5) ;;
(function x -> 1) (5.) ;;
function x -> 1 ;;
(function (x, y) -> (y, x)) (1, 2) ;;
(function (x, y) -> (y, x)) (1, "yes") ;;
(function (x, y) -> (y, x)) ("yes", "yes") ;;
function (x, y) -> (y, x) ;;
function x -> (x, x) ;;
(function x -> (x, x)) (1) ;;

 2 
  All the following Caml phrases are syntactically correct. They are assumed to be evaluated in order. Consider each of them. Is it a value definition let ... = e or an expression e? Has e a value? If it has a value then which one and of which type? Else why?

let absolute = function x -> if x >= 0 then x else - x ;;
absolute (absolute (-12)) ;;
let comp = function (x, y, z) -> (x <= y, y <= z) ;;
(comp (3, 2, 4), comp (2, 3, 4)) ;;
comp ("yellow", "red", "green") ;;  
let approx = function (a, b) -> let (b1, b2) = comp (a - 1, b, a + 1) in b1 & b2 ;;
approx (12, 13) ;;
approx ("purple", "violet") ;;
let ff = function x -> x + 1 in ff ;;
ff (5) ;;
let ff = function x -> x + 1 ;;
ff (5) ;;
let ff = function x -> x + 1. ;;
ff (5) ;;
let ff = function x -> x +. 1. ;;
ff (5) ;;
(let ff = function x -> x + 1 in ff) (5) ;;
ff (5) ;;
ff ;;

 3 
  Consider each of the following types. Write a value having this types (naming it is useless), evaluate it, then apply it:

int -> int
int * int -> bool
string -> bool

float * float -> string
bool * bool -> bool
int -> int * bool

 4 
  Write values named  f  and  g  in such a manner that the following expression has a value:

f (f (g (2, true), 4))

 5 
  All the following Caml phrases are syntactically correct. They are assumed to be evaluated in order (those in the left column being evaluated first). Consider each of them. Is it a value definition let ... = e or an expression e? Has e a value? If it has a value then which one and of which type? Else why?

let a = 5
in 2 * a ;;
a ;;
let a = 7
in (a, a) ;;
let a = 1 and b = 3 and c = 4
in a + b + c ;;
let (a, b, c) = (1, 3, 4)
in a + b + c ;;
let a = 5 ;;
a ;;
let a = 3
in 2 * a ;;
a ;;

let a = "yes" ;;
a ;;
let x = 5 ;;
let x = 3
in 2 * x ;;
let y = x + 1 ;;
let x = 4 and y = 2
in x * y ;;
y ;;
let y = let x = 7 and y = 9
        in y - x ;;
let y = y = 9 ;;
failwith ;;

 6 
  Same question.

let f = function 
  x -> if x <> 0 then x / abs (x)
                 else failwith ("f : requires a non-zero value as argument") ;;
(f (-7), f (5)) ;;
f (0) ;;
let rec times = function (a, b) -> if b = 0 then 0
                                            else a + times (a, b - 1) ;;
times (5, 7) ;;
let rec modulo_8 = function n -> if n < 0 then modulo_8 (n + 8)
                                         else if n >= 8 then modulo_8 (n - 8)
                                                        else n ;;
modulo_8 (-13) ;;
let rec repeat = function (word, n) -> if n = 0 then "!"
                                                else word ^ repeat (word, n - 1) ;;
repeat ("Ah ", 5) ;;
let rec f = function x -> f (x) ;;
f (3) ;;
let rec power = function (x, n) ->
  if n = 0 then 1.0
           else if n < 0 then 1.0 /. power (x, - n)
                         else x *. power (x, n - 1) ;;
power (1.2, 4) ;;
power (5.0, -3) ;;
let rec is_even = function n -> if n = 0 then true
                                         else is_even (n - 2) ;;
is_even (12) ;;
is_even (11) ;;


Latest update : October 5, 2006
This document was translated from LaTeX by Hyperlatex 2.5, which is not the latest version of Hyperlatex.

Part 2 - Expressions and functionsExercisesType-inference by yourselfDocumentation and user's manualTable of contentsOCaml programs