backup 04/09/2023 15h54
This commit is contained in:
150
Algo/Séminaire/Chapter 1 - CAML basics.md
Normal file
150
Algo/Séminaire/Chapter 1 - CAML basics.md
Normal file
@ -0,0 +1,150 @@
|
||||
CAML is a categorical abstract machine language. It is strongly typed. It was created by INRIA in 1984.
|
||||
|
||||
## 1.1. General idea
|
||||
It's a simple language to write.
|
||||
```Ocaml
|
||||
# 1+2 ;;
|
||||
```
|
||||
|
||||
- **#** is the prompt (we don't have to write it)
|
||||
- **1 + 2** is the program
|
||||
- **;;** is the end of the line
|
||||
|
||||
```Ocaml
|
||||
_: int=3 *That a Caml answer to the previus program*
|
||||
```
|
||||
|
||||
```Ocaml
|
||||
# 1+2.2;; *That an error*
|
||||
|
||||
# 1. +. 2.2;; *This is good*
|
||||
```
|
||||
## 1.2. CAML interaction
|
||||
```Ocaml
|
||||
# 1;; *He can be a simple expression*
|
||||
_: int=1
|
||||
|
||||
# "Hello World";; *He can be a string*
|
||||
_:string = "Hello World"
|
||||
```
|
||||
|
||||
The general form is :
|
||||
```Ocaml
|
||||
# expr ;;
|
||||
_: type of the evaluation = result of the evaluation
|
||||
```
|
||||
|
||||
## 1.3. Definitions
|
||||
### Global definitions
|
||||
```Ocaml
|
||||
# let x=1+2;;
|
||||
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*
|
||||
|
||||
# let x = 2
|
||||
_:bool = false
|
||||
|
||||
# x;;
|
||||
_: int = 2
|
||||
|
||||
# let name = expr;;
|
||||
val name: type of evaluation
|
||||
```
|
||||
### Local definitions
|
||||
```Ocaml
|
||||
# let r = 1. in let pi = 3.14 in 2*pi*r;;
|
||||
_ : float = 6.28
|
||||
|
||||
# let x = 10 in y = 2*x in x+y;;
|
||||
_ : float = 30
|
||||
|
||||
# x;;
|
||||
_ : int =2 *see global definition section*
|
||||
|
||||
# pi;;
|
||||
error: unbound value pi
|
||||
```
|
||||
|
||||
when there is the keyword ``len``and ``in`` that create a local definition. A local definition is something that only exist in our expression and then it disappear. You can define a local definition into a variable : `` let x = let r = 1. in let pi = 3.14 in 2*pi*r;;`` (for example)
|
||||
|
||||
### Multiple definitions
|
||||
```Ocaml
|
||||
# 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"*
|
||||
|
||||
# let x = 2 and y = x + 1 in y + 2;;
|
||||
Error *because x is 1.2 in our current evaluation*
|
||||
```
|
||||
> ⚠️ We have only one `let` and one `in` for the multiple definitions methods
|
||||
|
||||
## 1.4 Basic types
|
||||
- Integer: ``int``
|
||||
- Compatible operator : + ; - ; * ; / ; mod
|
||||
- Same operator priority than in mathematics
|
||||
```Ocaml
|
||||
# 2+3*2;;
|
||||
_: int = 8
|
||||
|
||||
# (2+3)*3;;
|
||||
_: int = 10
|
||||
|
||||
# max_int 1 073 741 823
|
||||
_: int = 2^30-1
|
||||
|
||||
# max_int -1 073 741 824
|
||||
_: int = -2^30
|
||||
```
|
||||
- Float: ``float``
|
||||
- Compatible operator : -. ; +. ; * . ; /.
|
||||
- Booleans: ``bool``
|
||||
- Can be ``true``or ``false``
|
||||
```Ocaml
|
||||
# let x = 2 and y = 1 in x/y > 1;;
|
||||
_: bool = true
|
||||
|
||||
# let x = 2 and y = 0 in x/y > 1;;
|
||||
Exception: Division by zero
|
||||
|
||||
# let x = 2 and y = 0 in y<>0 && x/y> 1;;
|
||||
_: bool = false
|
||||
```
|
||||
|
||||
|
||||
- Char: `char`
|
||||
- 8 bits -> extended ASCII table
|
||||
```Ocaml
|
||||
# 'a'= 'A';;
|
||||
_: bool = false
|
||||
```
|
||||
|
||||
- String: `string`
|
||||
```Ocaml
|
||||
# "ab" < "abc" ;;
|
||||
_: bool = true *because O chars -> 2^24 - 6 chars*
|
||||
|
||||
# "Hello" ^" World"
|
||||
_: string = "Hello World"
|
||||
|
||||
# "abc".[1]
|
||||
_: char = "b"
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Comparaison
|
||||
|
||||
|Mathematics|$=$|$\not=$|$<$|$>$|$\eqslantgtr$|$\eqslantless$|
|
||||
|:----:|:----:|:----:|:----:|:----:|:----:|
|
||||
|CAML|`=`|`<>`|`<`|`>`|`<=`|`>=`|
|
||||
|
||||
|CAML|Maths|English|
|
||||
|:-----|:-----|:-----|
|
||||
|not|$\neg$|negation|
|
||||
|&&|$\land$|and|
|
||||
|//|$\lor$|or (inclusive)|
|
68
Algo/Séminaire/Chapter 2 - Functions.md
Normal file
68
Algo/Séminaire/Chapter 2 - Functions.md
Normal file
@ -0,0 +1,68 @@
|
||||
## 2.1. One parameter function
|
||||
```Ocaml
|
||||
# let successor x=x+1;;
|
||||
val successor: int -> int =<fun>
|
||||
|
||||
# successor;;
|
||||
_ : int -> int =<fun>
|
||||
|
||||
# successor 3;;
|
||||
_: int = 4
|
||||
|
||||
# successor (-1)::
|
||||
_: int = 0
|
||||
```
|
||||
|
||||
>`x` is call a formal parameter and `x+1` is call the body of the function. `3` is an effective parameter (= argument). For the last line, the paratheses are needed, without it, CAML will return an error
|
||||
|
||||
|
||||
#### ⚠️ ``f x = f(x)`` and ``fx+gy = (fx)+(gy)
|
||||
|
||||
```Ocaml
|
||||
# let f x = x+1 and g x = x*2
|
||||
val f: int -> int = <fun>
|
||||
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*
|
||||
_: int = 7
|
||||
```
|
||||
|
||||
```Ocaml
|
||||
# let successor_of_double x=f(gx);;
|
||||
val successor_of_double: int -> int = <fun>
|
||||
|
||||
# successor_of_double (-12);;
|
||||
_: int = -23
|
||||
```
|
||||
|
||||
#### Existing functions
|
||||
```Ocaml
|
||||
# sqrt;;
|
||||
_: float -> float =<fun>
|
||||
|
||||
# int_of_float;;
|
||||
_: float -> int =<fun>
|
||||
|
||||
# length "toto" ;;
|
||||
Error : Unboud
|
||||
# String.lenght "toto";; *String is a library*
|
||||
_: int = 4
|
||||
```
|
||||
|
||||
|
||||
## 2.2. Functions and local definitions
|
||||
```Ocaml
|
||||
# let pred x = x - 1 in pred 3 + pred 4;;
|
||||
_: int = 6
|
||||
|
||||
# pred;;
|
||||
Error
|
||||
|
||||
# let square_of_pred x = let pred_x = x - 1 in pred_x * pred_x;;
|
||||
val square_of_pred: int -> int = <fun>
|
||||
|
||||
# square_of_pred 3;;
|
||||
_: int = 4
|
||||
|
67
Algo/Séminaire/Introduction.md
Normal file
67
Algo/Séminaire/Introduction.md
Normal file
@ -0,0 +1,67 @@
|
||||
## An algorithm is
|
||||
- a set of rules (finite)
|
||||
- sequence (finite)
|
||||
- The goal is to take an input and give an output with the solution
|
||||
|
||||
## How to write an algorithm
|
||||
1) What :
|
||||
- What do you have available ; What is the output that your searching for => this is call specifications
|
||||
- How : find resolution methods and choose the best one
|
||||
- Formalism : shape it like a computer talk
|
||||
- Translate :
|
||||
|
||||
## Compilation and interpretation
|
||||
### Compiler
|
||||
```mermaid
|
||||
flowchart TD
|
||||
|
||||
A[High Level source code] -->|Compiler| B[Machine code]
|
||||
|
||||
B --> C[execution]
|
||||
|
||||
B --> D[execution 2]
|
||||
|
||||
B --> E[execution ...]
|
||||
```
|
||||
The compiler depends on
|
||||
- Language of the source code
|
||||
- The computer it will run the code
|
||||
|
||||
Advantages :
|
||||
- 1 translation
|
||||
- Optimises the code
|
||||
|
||||
> C or C++ language use the compiler
|
||||
### Interpretation
|
||||
The interpretor is a live compiler that translate in realtime.
|
||||
|
||||
Disavantages:
|
||||
- Don't optimize the code
|
||||
|
||||
Avantages:
|
||||
- We can share only the original source code
|
||||
|
||||
> Javascript use interpretation
|
||||
|
||||
## Language families
|
||||
There is two families in the language word, the imperative and the declarative
|
||||
### Imperative
|
||||
- State (of the memory)
|
||||
- Instruction that will modify the state of the memory
|
||||
- and again...
|
||||
- output
|
||||
|
||||
### Declarative
|
||||
- Defines relations between the input and the output
|
||||
|
||||
In the declarative languages we have
|
||||
- The functional languages
|
||||
- $$f:x -> x+1$$
|
||||
- The logical languages
|
||||
$$x\in{N}$$
|
||||
$$x>1$$
|
||||
$$x<3$$
|
||||
$$\text{-> } x=2$$
|
||||
|
||||
|
||||
Latest update 2023-09-04
|
Reference in New Issue
Block a user