Exercice: Evaluer dans cet ordre les expressions suivantes:
let x = 2 in x * x;;
x;;
let x = 12;;
x;;
let x = 4 and y = 5 in x + y;;
x;;
y;;
let y = (let x = 3 and z = 8 in x * z);;
x;;
y;;
z;;
let z = (if x=y then "XEgaleY" else "XDifferentY");;
z;;
x;;
y;;
let x = (if y>0 then "YPositif" else "YNonPositif");;
x;;
let w1 = (let a1=("12", "Douze") and a2=("13", "Treize") and a3=("14", "Quatorze") in
(fst a1 ^ fst a2 ^ fst a3, snd a1 ^ " " ^ snd a2 ^ " " ^ snd a3));;
let w2 = (let a1=("12", "Douze") and a2=("13", "Treize") and a3=("14", "Quatorze") in
(fst a1 ^ snd a2 ^ fst a3, snd a1 ^ fst a2 ^ fst a3, fst a1 ^ fst a2 ^ snd a3));;
let w3 = (let a1=12 and a2=13 and a3=14 in (string_of_int a1 ^ string_of_int a2 ^ string_of_int a3, max a1 (min a2 a3)));;
w3;;
w1;;
let w4 = (let l = string_length (fst w1 ^ snd w1) and w5 = (fst w1 ^ snd w1) in (w5, l));;
let w5 = (let l = string_length (fst w4) and w6 = (fst w4) in (w6.[0], w6.[l - 1]));;
w5;;
let w6 = (let l = string_length (snd w1) and w5 = snd w1 in
if l>10 then sub_string w5 10 (l - 10) else sub_string w5 0 (l - 1));;
w6;;
let w7 = (let l = string_length (fst w1) and w5 = fst w1 in
if l>10 then sub_string w5 10 (l - 10) else sub_string w5 0 (l - 1));;
w7;;
let x1 = 3 and x2 = x1 + 1 in x1 + x2;;
let x1 = 3 in let x2 = x1 + 1 in x1 + x2;;
let x1 = 11;;
let x1 = 3 and x2 = x1 + 1 in x1 + x2;;
Solution
#let x = 2 in x * x;;
- : int = 4
#x;;
Toplevel input:
>x;;
>^
The value identifier x is unbound.
#let x = 12;;
x : int = 12
#x;;
- : int = 12
#let x = 4 and y = 5 in x + y;;
- : int = 9
#x;;
- : int = 12
#y;;
Toplevel input:
>y;;
>^
The value identifier y is unbound.
#let y = (let x = 3 and z = 8 in x * z);;
y : int = 24
#x;;
- : int = 12
#y;;
- : int = 24
#z;;
Toplevel input:
>z;;
>^
The value identifier z is unbound.
#
let z = (if x=y then "XEgaleY" else "XDifferentY");;
z : string = "XDifferentY"
#z;;
- : string = "XDifferentY"
#
x;;
- : int = 12
#y;;
- : int = 24
#let x = (if y>0 then "YPositif" else "YNonPositif");;
x : string = "YPositif"
#x;;
- : string = "YPositif"
#
let w1 = (let a1=("12", "Douze") and a2=("13", "Treize") and a3=("14", "Quatorze") in
(fst a1 ^ fst a2 ^ fst a3, snd a1 ^ " " ^ snd a2 ^ " " ^ snd a3));;
w1 : string * string = "121314", "Douze Treize Quatorze"
#
w1;;
- : string * string = "121314", "Douze Treize Quatorze"
#let w2 = (let a1=("12", "Douze") and a2=("13", "Treize") and a3=("14", "Quatorze") in
(fst a1 ^ snd a2 ^ fst a3, snd a1 ^ fst a2 ^ fst a3, fst a1 ^ fst a2 ^ snd a3));;
w2 : string * string * string = "12Treize14", "Douze1314", "1213Quatorze"
#w2;;
- : string * string * string = "12Treize14", "Douze1314", "1213Quatorze"
#
#let w3 = (let a1=12 and a2=13 and a3=14 in (string_of_int a1 ^ string_of_int a2 ^ string_of_int a3, max a1 (min a2 a3)));;
w3 : string * int = "121314", 13
#w3;;
- : string * int = "121314", 13
#
#w1;;
- : string * string = "121314", "Douze Treize Quatorze"
#
#let w4 = (let l = string_length (fst w1 ^ snd w1) and w5 = (fst w1 ^ snd w1) in (w5, l));;
w4 : string * int = "121314Douze Treize Quatorze", 27
#w4
;;
- : string * int = "121314Douze Treize Quatorze", 27
#
let w5 = (let l = string_length (fst w4) and w6 = (fst w4) in (w6.[0], w6.[l - 1]));;
w5 : char * char = `1`, `e`
#w5;;
- : char * char = `1`, `e`
#
let w6 = (let l = string_length (snd w1) and w5 = snd w1 in
if l>10 then sub_string w5 10 (l - 10) else sub_string w5 0 (l - 1));;
w6 : string = "ze Quatorze"
#w6;;
- : string = "ze Quatorze"
#let w7 = (let l = string_length (fst w1) and w5 = fst w1 in
if l>10 then sub_string w5 10 (l - 10) else sub_string w5 0 (l - 1));;
w7 : string = "12131"
#w7;;
- : string = "12131"
#
#let x1 = 3 and x2 = x1 + 1 in x1 + x2;;
Toplevel input:
>let x1 = 3 and x2 = x1 + 1 in x1 + x2;;
> ^^
The value identifier x1 is unbound.
#let x1 = 3 in let x2 = x1 + 1 in x1 + x2;;let x1 = 11;;
- : int = 7
#let x1 = 11;;
#x1 : int = 11
#- : int = 15
#
#let x1 = 3 and x2 = x1 + 1 in x1 + x2;;
- : int = 15
#
