205 lines
4.4 KiB
Markdown
205 lines
4.4 KiB
Markdown
## 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 = <fun>
|
|
|
|
# (*teatcher version*)
|
|
# let mirror n = 10 *(n mod 10)+n/10;;
|
|
val mirror : int -> int = <fun>
|
|
```
|
|
|
|
```Ocaml
|
|
# let abba(n) = n*100 + mirror(n) ;;
|
|
val abba: int -> int = <fun>
|
|
```
|
|
|
|
```Ocaml
|
|
# let stammer(n) = abba(mirror(n)) * 10 000 + abba(n) ;;
|
|
val stammer: int -> int = <fun>
|
|
```
|
|
|
|
|
|
|
|
## 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 = <fun>
|
|
|
|
(*logical or*)
|
|
# let or_if a b =
|
|
if a then true
|
|
else b;;
|
|
|
|
val or_if: bool -> bool -> bool = <fun>
|
|
|
|
|
|
(*logical implication*)
|
|
# let imply_if a b =
|
|
if a then b
|
|
else true;;
|
|
val imply_if: bool -> bool -> bool = <fun>
|
|
|
|
(*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 = <fun>
|
|
|
|
# let min2 number1 number2 = if number1 > number2 then number2 else number1
|
|
val min2 : 'a -> 'a -> 'a = <fun>
|
|
|
|
# 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 = <fun>
|
|
|
|
# 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 = <fun>
|
|
|
|
# let middle3 x y z =
|
|
x + y + z - min3 x y z - max x y z ;;
|
|
val middle3 = int -> int -> int = <fun>
|
|
|
|
#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 = <fun>
|
|
# 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 = <fun>
|
|
|
|
```
|
|
|
|
## Exercise 3.7
|
|
```Ocaml
|
|
# let rate_eco kg = match kg with
|
|
x when x <= 500 -> 3.40
|
|
| x when x <=1000 -> 4.60
|
|
| x when x <= 2000 -> 5.10
|
|
| x when x <= 3000 -> 6.90
|
|
| _ -> invalid_arg "Cannot be more than 3000g";;
|
|
val rate_eco : int -> float = <fun>
|
|
|
|
# let rate_standard kg = match kg with
|
|
x when x <= 500 -> 4.60
|
|
| x when x <=1000 -> 5.90
|
|
| x when x <= 2000 -> 6.50
|
|
| x when x <= 3000 -> 7.20
|
|
| _ -> invalid_arg "Cannot be more than 3000g";;
|
|
val rate_standart : int -> float = <fun>
|
|
|
|
# let rate_express kg = match kg with
|
|
x when x <= 500 -> 9.10
|
|
| x when x <=1000 -> 11.
|
|
| x when x <= 2000 -> 13.5
|
|
| x when x <= 3000 -> 14.2;
|
|
| _ -> invalid_arg "Cannot be more than 3000g";;
|
|
val rate_express : int -> float = <fun>
|
|
|
|
# let rate rt kg = match rt with
|
|
x when x = "economic" -> rate_eco(kg)
|
|
| x when x = "standard" -> rate_standard(kg)
|
|
| x when x = "express" -> rate_express(kg)
|
|
| _ -> invalid_arg "Bad type of shipping class";;
|
|
val rate : string -> int -> float = <fun>
|