vault backup: 2023-09-05 15:07:54

This commit is contained in:
Louis Gallet 2023-09-05 15:07:54 +02:00
parent 4c95cef899
commit 6dfa2cb3bc
Signed by: lgallet
SSH Key Fingerprint: SHA256:qnW7pk4EoMRR0UftZLZQKSMUImbEFsiruLC7jbCHJAY
4 changed files with 24 additions and 25 deletions

View File

@ -148,10 +148,10 @@
}, },
"active": "13c9bfe482ec2d42", "active": "13c9bfe482ec2d42",
"lastOpenFiles": [ "lastOpenFiles": [
"Algo/Séminaire/Introduction.md",
"Algo/Séminaire/Exercices sémaines.md",
"Algo/Séminaire/Chapter 2 - Functions.md",
"Algo/Séminaire/Chapter 1 - CAML basics.md", "Algo/Séminaire/Chapter 1 - CAML basics.md",
"Algo/Séminaire/Chapter 2 - Functions.md",
"Algo/Séminaire/Exercices sémaines.md",
"Algo/Séminaire/Introduction.md",
"Algo/Séminaire/assets/69E2987C-209A-48CD-8964-5A60462966E5.jpg", "Algo/Séminaire/assets/69E2987C-209A-48CD-8964-5A60462966E5.jpg",
"Algo/Séminaire/assets", "Algo/Séminaire/assets",
"Algo/Séminaire/assets/F1D2AA19-E790-4022-AFFF-F778EAB28AB5.jpg", "Algo/Séminaire/assets/F1D2AA19-E790-4022-AFFF-F778EAB28AB5.jpg",

View File

@ -11,20 +11,20 @@ It's a simple language to write.
- **;;** is the end of the line - **;;** is the end of the line
```Ocaml ```Ocaml
_: int=3 *That a Caml answer to the previus program* _: int=3 (*That a Caml answer to the previus program*)
``` ```
```Ocaml ```Ocaml
# 1+2.2;; *That an error* # 1+2.2;; (*That an error*)
# 1. +. 2.2;; *This is good* # 1. +. 2.2;; (*This is good*)
``` ```
## 1.2. CAML interaction ## 1.2. CAML interaction
```Ocaml ```Ocaml
# 1;; *He can be a simple expression* # 1;; (*He can be a simple expression*)
_: int=1 _: int=1
# "Hello World";; *He can be a string* # "Hello World";; (*He can be a string*)
_:string = "Hello World" _:string = "Hello World"
``` ```
@ -38,10 +38,10 @@ _: type of the evaluation = result of the evaluation
### Global definitions ### Global definitions
```Ocaml ```Ocaml
# let x=1+2;; # let x=1+2;;
val x:int = 3 *our current env is x -> 3* val x:int = 3 (*our current env is x -> 3*)
# let y=x/2;; # let y=x/2;;
val y:int = 1 *our current env is x -> 3 ; y -> 1* val y:int = 1 (*our current env is x -> 3 ; y -> 1*)
# let x = 2 # let x = 2
val x : int = 2 val x : int = 2
@ -61,7 +61,7 @@ _ : float = 6.28
_ : float = 30 _ : float = 30
# x;; # x;;
_ : int =2 *see global definition section* _ : int =2 (*see global definition section*)
# pi;; # pi;;
error: unbound value pi error: unbound value pi
@ -71,15 +71,14 @@ when there is the keyword ``len``and ``in`` that create a local definition. A lo
### Multiple definitions ### Multiple definitions
```Ocaml ```Ocaml
# let r = 1. and pi : 3.14 in 2 *. pi *. r;; *local version* # let r = 1. and pi : 3.14 in 2 *. pi *. r;; (*local version*)
_ : float = 6.28 _ : float = 6.28
# let x = 1.2 and y = "Hello";; # let x = 1.2 and y = "Hello";;
val x: float = 1.2 *our current env is x -> 3 ; y -> 1 ; x -> 1.2 * val x: float = 1.2 (*our current env is x -> 3 ; y -> 1 ; x -> 1.2 *)
val y: string = "Hello" *our current env is x -> 3 ; y -> 1 ; x -> 1.2 ; y -> "Hello"* val y: string = "Hello" (*our current env is x -> 3 ; y -> 1 ; x -> 1.2 ; y -> "Hello"*)
# let x = 2 and y = x + 1 in y + 2;; # let x = 2 and y = x + 1 in y + 2;;
Error *because x is 1.2 in our current evaluation* Error (*because x is 1.2 in our current evaluation*)
``` ```
> ⚠️ We have only one `let` and one `in` for the multiple definitions methods > ⚠️ We have only one `let` and one `in` for the multiple definitions methods
@ -129,7 +128,7 @@ _: bool = false
- String: `string` - String: `string`
```Ocaml ```Ocaml
# "ab" < "abc" ;; # "ab" < "abc" ;;
_: bool = true *because O chars -> 2^24 - 6 chars* _: bool = true (*because O chars -> 2^24 - 6 chars*)
# "Hello" ^" World" # "Hello" ^" World"
_: string = "Hello World" _: string = "Hello World"

View File

@ -25,7 +25,7 @@ val g: int -> int = <fun>
# fg 3;; # fg 3;;
Error: was expecting an integer and get a function Error: was expecting an integer and get a function
# f(g 3);; *This way there is no error* # f(g 3);; (*This way there is no error*)
_: int = 7 _: int = 7
``` ```
@ -47,7 +47,7 @@ _: float -> int =<fun>
# length "toto" ;; # length "toto" ;;
Error : Unboud Error : Unboud
# String.lenght "toto";; *String is a library* # String.lenght "toto";; (*String is a library*)
_: int = 4 _: int = 4
``` ```
@ -71,5 +71,5 @@ val square_of_pred: int -> int = <fun>
val pred: int -> int = <fun> val pred: int -> int = <fun>
# square_of_pred 3;; # square_of_pred 3;;
_: int = 4 *same things but with another method* _: int = 4 (*same things but with another method*)
``` ```

View File

@ -1,7 +1,7 @@
## 2.2 (Power) ## 2.2 (Power)
```Ocaml ```Ocaml
(*First version*) (*First version ; 6 multiplications*)
# let power28(x) = # let power28(x) =
let x2 = x + x in let x2 = x + x in
let x4 = x2*x2 in let x4 = x2*x2 in
@ -9,16 +9,16 @@
let x16 = x8*x8 in let x16 = x8*x8 in
x16*x8*x4 ;; x16*x8*x4 ;;
(*Second version*) (*Second version ; 27 multiplications*)
# let power28(x) = x*x*x*x*x*x...*x ;; # let power28(x) = x*x*x*x*x*x...*x ;;
(*Third version*) (*Third version ; 11 multiplications*)
# let power28(x) = # let power28(x) =
let sq(x) = x+x in let sq(x) = x*x in
let pow4(x) = sq(sq(x)) in let pow4(x) = sq(sq(x)) in
pow4(pow4(x))*sq(pow4(x))*pow4(x);; pow4(pow4(x))*sq(pow4(x))*pow4(x);;
(*Fourth version*) (*Fourth version ; 7 multiplications*)
# let power28(x)= # let power28(x)=
let sq(x) = x+x in let sq(x) = x+x in
let p4=sq(sq(x)) in let p4=sq(sq(x)) in