## Exercise 2.2 (Power) ```Ocaml (*First version ; 6 multiplications*) # let power28(x) = let x2 = x + x in let x4 = x2*x2 in let x8 = x4*x4 in let x16 = x8*x8 in x16*x8*x4 ;; (*Second version ; 27 multiplications*) # let power28(x) = x*x*x*x*x*x...*x ;; (*Third version ; 11 multiplications*) # let power28(x) = let sq(x) = x*x in let pow4(x) = sq(sq(x)) in pow4(pow4(x))*sq(pow4(x))*pow4(x);; (*Fourth version ; 7 multiplications*) # let power28(x)= let sq(x) = x+x in let p4=sq(sq(x)) in sq(sq(p4))*sq(p4)*p4;; ``` ## Exercise 2.3 ```Ocaml # (*my verison*) # let mirror(n) = let diz = n/10 and uni = n mod 10 in uni*10 + diz;; val mirror : int -> int = # (*teatcher version*) # let mirror n = 10 *(n mod 10)+n/10;; val mirror : int -> int = ``` ```Ocaml # let abba(n) = n*100 + mirror(n) ;; val abba: int -> int = ``` ```Ocaml # let stammer(n) = abba(mirror(n)) * 10 000 + abba(n) ;; val stammer: int -> int = ``` ## Exercice 2.6 ```Ocaml let sec_of_time h m s = h*3600 + m*60 + s ;; let time_of_sec s = let hours = s/3600 in let minutes = (s - hours*3600)/60 in let seconds = s - hours *3600 - minutes * 60 in (hours, minutes, seconds);; let add_times h1 m1 s1 h2 m2 s2 = let sec1 = sec_of_time h1 m1 s1 and sec2 = sec_of_time h2 m2 s2 in let resultsec = sec1 + sec2 in time_of_sec resultsec ;; ``` ## Exercise 3.1 ```Ocaml let add a b = if a > b then a-b + a/b else b-a + b/a let test x y = (if x> y then x else y ) *(x+y);; let f a b c = let g x y = (x+y)*(x-y) in if a > b then if b > c then (a+b)*(a-b) else (c+a) *(a-c) else if a > c then (a+b)*(a-b) else (b+c) * (b-c);; let f a b c = (if a > b && if b > c then a + b else c + a else if a > c then a + b else b + c)* (if a > b && b > c then a - b else a - c else if a > c then a-b else b-c);; ``` ### Exercise 3.2 ```OCaml (*logical and*) # let and_if a b = if a then b else false;; val and_if: bool -> bool -> bool = (*logical or*) # let or_if a b = if a then true else b;; val or_if: bool -> bool -> bool = (*logical implication*) # let imply_if a b = if a then b else true;; val imply_if: bool -> bool -> bool = (*logical exclusive or*) let xor a b = if a then if b then false else true else if b then true else false (*logical equivalence = a b*) let equiv a b = if a then b else if b then false else true (*not b*) ``` ### Exercise 3.3 ```Ocaml # let max2 number1 number2 = if number1 > number2 then number1 else number2 val max2 : 'a -> 'a -> 'a = # let min2 number1 number2 = if number1 > number2 then number2 else number1 val min2 : 'a -> 'a -> 'a = # let max3 x y z = let high = if x > y then x else y in if high > z then high else z;; val max3 : 'a -> 'a -> 'a -> 'a = # let min3 x y z = let low = if x < y then x else y in if low < z then low else z;; val min3 : 'a -> 'a -> 'a -> 'a = # let middle3 x y z = x + y + z - min3 x y z - max x y z ;; val middle3 = int -> int -> int = #let max4 number1 number2 number3 number4 = let nb1 = max2(number1 number2) and nb2 = max2(number3 number4) in max2(nb1 nb2) # let max4 x y z t= max2(max2 x y)(max2 z t) val max4 : 'a -> 'a -> 'a -> 'a -> 'a = # let min4 x y z t= min2(min2 x y)(min2 z t) ``` ### Exercise 3.4 ```Ocaml let highest_square_sum x y z = let sq x = x*x in sq(max3 x y z) + sq(middle3 x y z) val highest_square_sum : int -> int -> int -> int = ```