27/02/1432
1
Programmation fonctionnelle
Cours 1 : les types simples
Licence 1
Année 2010 – 2011
Par N. VINCENT
Licence
1
-
programmation fonctionnelle
2
Présentation du cours
• Cours 18 h ; TD 18 h ; TP 18 h
• 9 groupes de TD et TP
commencent la semaine prochaine
• Contrôle
– Contrôle continu
• 3 CC : 14 mars , 18 avril et 16 mai
• 1 mini-projet sur machine séance 12 en TP
– Pas d’examen
Licence
1
-
programmation fonctionnelle
3
Contenu du cours
• 1er semestre
Structures de contrôle de base en langage C
• Objectif du 2esemestre
– Apprendre un nouveau langage
– programmation fonctionnnelle
– Conforter les éléments d’algorithmie
• Le choix du Caml
– Permet la programmation fonctionnelle et
impérative
Licence
1
-
programmation fonctionnelle
4
Rappels
• Problème à résoudre
• Analyse de la solution choisie
• Écriture de l’algorithme - validation
• Choix d’un langage
– Ici Caml
• Écriture du programme
– Structures
– fonctions
– Instructions
Licence
1
-
programmation fonctionnelle
5
Le langage CAML
• Issu du -calcul, en 1977, le langage ML
• 10 ans plus tard le langage Caml :
Categorical Abstract Machine Language
• Langage fonctionnel :
– déclaration de fonctions,
– les résultats des fonctions étant de tout type
• Langage évolué
• Langage interprété
Licence
1
-
programmation fonctionnelle
6
Caractéristiques du langage
• Types de données très rigoureux
• Les types sont évalués au cours des calculs
– Pas nécessaire de les déclarer
• Une variable ne peut pas être utilisée dans un
calcul sans avoir une valeur
• Un programme est une suite de définitions de
fonctions et de données
• On ne peut utiliser une fonction avant qu’elle
ait été définie
27/02/1432
2
Licence
1
-
programmation fonctionnelle
7
Langage interprété
Écriture d’une expression
Lecture par l’interpréteur
Évaluation par l’interpréteur
Retour du type et de la valeur ou
message d’erreur
2 + 2 ;;
2 + 2
# 2+2 ;;
- : int = 4
#2 + "caml" ;;
Toplevel input:
>2 + "caml" ;;
> ^^^^^^
This expression has type string,
but is used with type int.
Licence
1
-
programmation fonctionnelle
8
L'interpréteur
• Écriture d’une expression
– Après le « prompt » #
– Jusqu'au lancement ;;
• Retour
– Indication de réponse
– Type de l'expression déduit
– Valeur de l'expression
# 1 + 1;;
- : int 2
Licence
1
-
programmation fonctionnelle
9
Types simples
• bool : booléen
• int : entier entre -230 et 230-1
• char : caractère
• string : chaîne de caractères
• float : réel (non compatible avec les entiers)
• unit : unique valeur le vide
• fun : fonction
• 'a : pas encore défini
()
"nom"
true false 2 =2
'c'
#fun x -> x + 1 ;;
- : int -> int = <fun>
2.
228
#let f x y = x > y;;
f : 'a -> 'a -> bool = <fun>
#let f x y = x > y;;
Licence
1
-
programmation fonctionnelle
10
Définition et affectation
#let x = 5 ;;
x : int = 5
#let f = fun x -> x+1 ;;
f : int -> int = <fun>
#f 3 ;;
- : int = 4
#let g x = x + 1 ;;
g : int -> int = <fun>
#g (-8) ;;
- : int = -7
#let y = `c` ;;
y : char = `c`
#let x = 3 and y = 5 ;;
x : int = 3
y : int = 5
#let ... = ... ;;
Ou mieux
La valeur ne peut être modifiée
connue dans la session
#let x = 2 + 5 ;;
x : int = 7
Licence
1
-
programmation fonctionnelle
11
Opérateurs arithmétiques
Liés au type des valeurs
entiers
+ - * / mod abs
réels
+. -. *. /. sqrt exp
A valeur booléenne
Égalité = , comparaison < > <= >=
Les deux arguments doivent être de
même type
Licence
1
-
programmation fonctionnelle
12
Conversions de type
float_of_int
char_of_int
string_of int
int_of_char
int_of_float
int_of_string
#char_of_int 69 ;;
- : char = `E`
#string_of_int 69 ;;
- : string = "69"
#int_of_float 7.3 ;;
- : int = 7
#int_of_float 7.8 ;;
- : int = 7
#int_of_string "452" ;;
- : int = 452
#int_of_string "caml" ;;
Uncaught exception: Failure "int_of_string"
#float_of_int 4 ;;
- : float = 4.0
#int_of_char `E` ;;
- : int = 69
27/02/1432
3
Licence
1
-
programmation fonctionnelle
13
La rigueur des types
#3 + 5 ;;
- : int = 8
#3 + 1.2 ;;
Toplevel input:
>3 + 1.2 ;;
> ^^^
This expression has type float,
but is used with type int.
#3 +. 1.2 ;;
Toplevel input:
>3 +. 1.2 ;;
>^
This expression has type int,
but is used with type float.
#3. +. 1.2 ;;
- : float = 4.2
#x +. float_of_int z ;;
- : float = 4.0
#x + z ;;
Toplevel input:
>x + z ;;
>^
This expression has type float,
but is used with type int.
#x+.y;;
- : float = 8.0
#let x = 3. and y = 5. and z = 1 ;;
x : float = 3.0
y : float = 5.0
z : int = 1
Licence
1
-
programmation fonctionnelle
14
Le type indéterminé
#let f x y = x > y;;
f : 'a -> 'a -> bool = <fun>
#f true false ;;
- : bool = true
#f false true ;;
- : bool = false
#f 25 40 ;;
- : bool = false
#f 3.7 4.9 ;;
- : bool = false
#f `A` `a` ;;
- : bool = false
#f "Caml" "C" ;;
- : bool = true
Licence
1
-
programmation fonctionnelle
15
Opérateurs logiques
Égalité =
Et & &&
Ou or ||
Non not
#2 < 3 && (-3) < 3 ;;
- : bool = true
#not (2 > 1) ;;
- : bool = false
# x < y || y < x ;;
- : bool = true
#(2>1)=(`c`>`a`) ;;
- : bool = true #let x = 5 ;;
x : int = 5
#x = 6 ;;
- : bool = false
#x<y or y<x ;;
- : bool = true
#2<3 & (-3)<3 ;;
- : bool = true
Licence
1
-
programmation fonctionnelle
16
Impression
Une fonction existe pour chaque type
print_int
print_float
print_char
print_string
print_newline ()
#let x = 5 ;;
x : int = 5
#print_int x ;;
5- : unit = () #let x = 5. ;;
x : float = 5.0
#print_float x ;;
5.0- : unit = ()
#let x = `c` ;;
x : char = `c`
#print_char x ;;
c- : unit = () #let x = "caml" ;;
x : string = "caml"
#print_string x ;;
caml- : unit = ()
Licence
1
-
programmation fonctionnelle
17
Déclaration locale
Une variable peut être définie par une
expression 1
dans une
expression
Le type du résultat est celui de
expression
La variable doit avoir un type compatible avec celui de
son occurrence dans expression ou ne pas apparaître
#let x = 12 in x + 5 ;;
- : int = 17
#let x = 12 in x +. 5. ;;
Toplevel input:
>let x = 12 in x +. 5. ;;
> ^
This expression has type int,
but is used with type float.
#let x = 12 in sqrt 12.6 ;;
- : float = 3.54964786986
Licence
1
-
programmation fonctionnelle
18
Déclaration locale
#let x = 3 ;;
x : int = 3
#let x = 5 in x + 2 ;;
- : int = 7
#x;;
- : int = 3
#let x = "caml" ;;
x : string = "caml"
#let x = 12 in x + 5 ;;
- : int = 17
#x ;;
- : string = "caml"
27/02/1432
4
Licence
1
-
programmation fonctionnelle
19
Déclaration d'une fonction simple
#let f x = x * x + 2 * x + 5 ;;
f : int -> int = <fun>
#f 1 ;;
- : int = 8
#let f a b = a + b ;;
f : int -> int -> int = <fun>
#f (2+4) (-8) ;;
- : int = -2
#f 0 ;;
- : int = 5
#f (-1) ;;
- : int = 4
#f 3 7 ;;
- : int = 10
#let x = 5 ;;
x : int = 5
#let f x = x + 1 ;;
f : int -> int = <fun>
#f 7 ;;
- : int = 8
#f x ;;
- : int = 6
Licence
1
-
programmation fonctionnelle
20
La conditionnelle
suit la structure de la forme algorithmique
Ce n'est pas une instruction mais une expression qui
peut prendre 2 valeurs possibles
Définition d'une fonction valeur absolue sur les réels
If condition then expression 1
else expression 2
#let h x = if x < 0. then -. x else x ;;
h : float -> float = <fun>
#h 4. ;;
- : float = 4.0
#h (-45.) ;;
- : float = 45.0
#abs 5. ;;
Toplevel input:
>abs 5. ;;
> ^^
This expression has type float,
but is used with type int.
Licence
1
-
programmation fonctionnelle
21
Conditionnelle
Les deux expressions doivent avoir le
même type
#let x = 5 ;;
x : int = 5
#let y = 2 ;;
y : int = 2
#if x > y then print_int x else y ;;
Toplevel input:
>if x > y then print_int x else y ;;
> ^
This expression has type int,
but is used with type unit.
Licence
1
-
programmation fonctionnelle
22
Les points importants
•Les types
•Les opérateurs
•La conditionnelle
•Les fonctions
1 / 4 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans l'interface ou les textes ? Ou savez-vous comment améliorer l'interface utilisateur de StudyLib ? N'hésitez pas à envoyer vos suggestions. C'est très important pour nous!