vault backup: 2023-11-13 13:50:53
This commit is contained in:
154
Algo/B1/Séminaire/Chapter 1 - CAML basics.md
Normal file
154
Algo/B1/Séminaire/Chapter 1 - CAML basics.md
Normal file
@ -0,0 +1,154 @@
|
||||
|
||||
<center><img src="https://gitea.louisgallet.fr/lgallet/epicours/raw/branch/main/Algo/S%C3%A9minaire/assets/basics-meme.png " width=auto height=400 /> </center>
|
||||
|
||||
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 previous 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
|
||||
val x : int = 2
|
||||
|
||||
# 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
|
||||
|
||||
# 'a'
|
||||
- : char = 'a'
|
||||
```
|
||||
|
||||
- String: `string`
|
||||
```Ocaml
|
||||
# "ab" < "abc" ;;
|
||||
_: bool = true (*because O chars -> 2^24 - 6 chars*)
|
||||
|
||||
# "Hello" ^" World"
|
||||
_: string = "Hello World"
|
||||
|
||||
# "abc".[1]
|
||||
_: char = "b"
|
||||
```
|
||||
⚠️ Les minuscules passent avant les majuscules
|
||||
|
||||
### Comparaison
|
||||
|
||||
|Mathematics|$=$|$\not=$|$<$|$>$|$\eqslantgtr$|$\eqslantless$|
|
||||
|:----:|:----:|:----:|:----:|:----:|:----:|
|
||||
|CAML|`=`|`<>`|`<`|`>`|`<=`|`>=`|
|
||||
|
||||
|CAML|Maths|English|
|
||||
|:-----|:-----|:-----|
|
||||
|not|$\neg$|negation|
|
||||
|&&|$\land$|and|
|
||||
|// (vertical bar)|$\lor$|or (inclusive)|
|
Reference in New Issue
Block a user