vault backup: 2023-09-05 15:07:54
This commit is contained in:
parent
4c95cef899
commit
6dfa2cb3bc
6
.obsidian/workspace.json
vendored
6
.obsidian/workspace.json
vendored
@ -148,10 +148,10 @@
|
||||
},
|
||||
"active": "13c9bfe482ec2d42",
|
||||
"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 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",
|
||||
"Algo/Séminaire/assets/F1D2AA19-E790-4022-AFFF-F778EAB28AB5.jpg",
|
||||
|
@ -11,20 +11,20 @@ It's a simple language to write.
|
||||
- **;;** is the end of the line
|
||||
|
||||
```Ocaml
|
||||
_: int=3 *That a Caml answer to the previus program*
|
||||
_: int=3 (*That a Caml answer to the previus program*)
|
||||
```
|
||||
|
||||
```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
|
||||
```Ocaml
|
||||
# 1;; *He can be a simple expression*
|
||||
# 1;; (*He can be a simple expression*)
|
||||
_: int=1
|
||||
|
||||
# "Hello World";; *He can be a string*
|
||||
# "Hello World";; (*He can be a string*)
|
||||
_:string = "Hello World"
|
||||
```
|
||||
|
||||
@ -38,10 +38,10 @@ _: type of the evaluation = result of the evaluation
|
||||
### Global definitions
|
||||
```Ocaml
|
||||
# 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;;
|
||||
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
|
||||
val x : int = 2
|
||||
@ -61,7 +61,7 @@ _ : float = 6.28
|
||||
_ : float = 30
|
||||
|
||||
# x;;
|
||||
_ : int =2 *see global definition section*
|
||||
_ : int =2 (*see global definition section*)
|
||||
|
||||
# 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
|
||||
```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
|
||||
|
||||
# let x = 1.2 and y = "Hello";;
|
||||
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 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"*)
|
||||
# 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
|
||||
|
||||
@ -129,7 +128,7 @@ _: bool = false
|
||||
- String: `string`
|
||||
```Ocaml
|
||||
# "ab" < "abc" ;;
|
||||
_: bool = true *because O chars -> 2^24 - 6 chars*
|
||||
_: bool = true (*because O chars -> 2^24 - 6 chars*)
|
||||
|
||||
# "Hello" ^" World"
|
||||
_: string = "Hello World"
|
||||
|
@ -25,7 +25,7 @@ val g: int -> int = <fun>
|
||||
|
||||
# fg 3;;
|
||||
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
|
||||
```
|
||||
|
||||
@ -47,7 +47,7 @@ _: float -> int =<fun>
|
||||
|
||||
# length "toto" ;;
|
||||
Error : Unboud
|
||||
# String.lenght "toto";; *String is a library*
|
||||
# String.lenght "toto";; (*String is a library*)
|
||||
_: int = 4
|
||||
```
|
||||
|
||||
@ -71,5 +71,5 @@ val square_of_pred: int -> int = <fun>
|
||||
val pred: int -> int = <fun>
|
||||
|
||||
# square_of_pred 3;;
|
||||
_: int = 4 *same things but with another method*
|
||||
_: int = 4 (*same things but with another method*)
|
||||
```
|
||||
|
@ -1,7 +1,7 @@
|
||||
## 2.2 (Power)
|
||||
|
||||
```Ocaml
|
||||
(*First version*)
|
||||
(*First version ; 6 multiplications*)
|
||||
# let power28(x) =
|
||||
let x2 = x + x in
|
||||
let x4 = x2*x2 in
|
||||
@ -9,16 +9,16 @@
|
||||
let x16 = x8*x8 in
|
||||
x16*x8*x4 ;;
|
||||
|
||||
(*Second version*)
|
||||
(*Second version ; 27 multiplications*)
|
||||
# let power28(x) = x*x*x*x*x*x...*x ;;
|
||||
|
||||
(*Third version*)
|
||||
(*Third version ; 11 multiplications*)
|
||||
# let power28(x) =
|
||||
let sq(x) = x+x in
|
||||
let sq(x) = x*x in
|
||||
let pow4(x) = sq(sq(x)) in
|
||||
pow4(pow4(x))*sq(pow4(x))*pow4(x);;
|
||||
|
||||
(*Fourth version*)
|
||||
(*Fourth version ; 7 multiplications*)
|
||||
# let power28(x)=
|
||||
let sq(x) = x+x in
|
||||
let p4=sq(sq(x)) in
|
||||
|
Loading…
x
Reference in New Issue
Block a user