Adrien Durier PRO
Enseignant-Chercheur en informatique @ Université Paris-Saclay
Polytech Paris Saclay
2024-2025
Adrien Durier
OCaml
OCaml
(suite)
Types Simples - Rappel
let z = 4 + 1 - 2 * 2let x = 4.3e4 +. 1.2 *. -2.3let x = false || true
let x = 3 <= 1
let x = not (0=2) && 1 >= 3
let x = if 2 < 0 then 2.0 else (4.6 *. 1.2)intfloatboolTypes Simples
let z = 'a'let x = "hello"charstringTypes Simples - strings
let z = 'a'charOn a les fonctions int_of_char et char_of_int:
# int_of_char 'a' ;;
- : int = 97
# char_of_int 100 ;;
- : char = 'd'/!\ L'apostrophe ' indique un caractère
Le double apostrophe " une chaîne de caractères
Code ASCII
Types Simples
let z = 'a'let x = "hello"charstringTypes Simples
let x = "hello"string^ concatène les chaînes .[i] accède au caractère i (commence à 0)
# "" ;;
- : string = ""
# "hello".[1] ;;
- : char = 'e'
# string_of_int 123 ;;
- : string = "123"Types Simples
Printf est de type unit
unit sert pour les fonctions qui n'ont que des effets de bord:
void
unit, notée ()
unit sont égales !!!
# () ;;
- : unit = ()
# Print.printf "bonjour\n" ;;
bonjour
- : unit = ()Types Simples
(1,2) est de type int * int
(1,"WTF") est de type int * string
let u = (1, 2)
let uu = (1,"AAAAAAAAAh")
let f (x, y, z) w = if x + y < 0 then w^"NON" else z
let fst x = let (w,z) = x in w
let snd x = let (w,z) = x in wTypes Simples
(1,2) est de type int * int
(1,"WTF") est de type int * string
f, fst, snd ?
let f (x, y, z) w = if x + y < 0 then w^"NON" else z
let fst x = let (w,z) = x in w
let snd x = let (w,z) = x in wTypes Simples
(1,2) est de type int * int
(1,"WTF") est de type int * string
f, fst, snd ?
let f (x, y, z) w = if x + y < 0 then w^"NON" else z
let fst x = let (w,z) = x in w
let snd x = let (w,z) = x in zRéponse:
f:(int * int * string) -> string -> string
fst est de type ('a * 'b) -> 'a
snd est de type ('a * 'b) -> 'b
By Adrien Durier
Programmation Fonctionnelle: Cours 2 - Complément du cours 1 sur le typage