Programmation fonctionnelle

Transcription

Programmation fonctionnelle
12/01/17
Présentation du cours
•  Cours 18 h ; TD 18 h ; TP 18 h
•  9 groupes de TD et TP
Programmation fonctionnelle
commencent la semaine prochaine
Cours 1 : les types simples
Licence 1
Année 2016 – 2017
•  Contrôle
– Contrôle continu
•  3 CC : février , mars et fin avril
•  1 mini-projet sur machine séance 12 en TP
– Pas d’examen
Par N. VINCENT
Licence 1 - programmation fonctionnelle
Contenu du cours
Le processus général
•  1er semestre
Structures de contrôle de base en langage C
•  Objectif du 2e semestre
– Apprendre un nouveau langage
– Comprendre la programmation fonctionnelle
– Conforter les éléments d’algorithmie
•  Le choix du Caml
3
Le langage CAML
– Ici Caml
•  Écriture du programme
Licence 1 - programmation fonctionnelle
4
Caractéristiques du langage
•  Issu du λ-calcul, en 1977, le langage ML
•  10 ans plus tard le langage Caml :
Categorical Abstract Machine Language
•  Langage fonctionnel :
•  Types de données très rigoureux
•  Les types sont évalués au cours des calculs
–  Pas nécessaire de les déclarer
– Définitions de fonctions,
– Les résultats des fonctions étant de tout type
•  Langage évolué
•  Langage interprété
Licence 1 - programmation fonctionnelle
•  Problème à résoudre
•  Analyse de la solution choisie
•  Écriture de l’algorithme - validation
•  Choix d’un langage
– Structures
– fonctions
– Instructions
– Permet la programmation fonctionnelle et
impérative
Licence 1 - programmation fonctionnelle
2
5
•  Une variable ne peut pas être utilisée dans un
calcul sans avoir une valeur définie
•  Un programme est une suite de définitions,
de fonctions et de données
•  On ne peut utiliser une fonction avant qu’elle
n’ait été définie
Licence 1 - programmation fonctionnelle
6
1
12/01/17
Langage interprété
l 
l 
l 
l 
L'interpréteur
Écriture d’une expression 2 + 2 2 = 2
Lecture par l’interpréteur
2 + 2 ;;
Évaluation par l’interpréteur
Retour du type et de la valeur ou Déclencheur
de calcul
message d’erreur
# 2+2 ;;
- : int = 4
# 2 + "caml" ;;
Error: This expression has type string but an
expression was expected of type
int
# 2 = 2 ;;
- : bool = true
7
Licence 1 - programmation fonctionnelle
# 3 < 0 ;;
•  bool : booléen true false 2 =2
- : bool = false
30
30
•  int : entier entre -2 et 2 -1 # 228 ;;
- : int = 228
;;
•  char : caractère #- :'c'char
= 'c'
;;
•  string : chaîne de caractères #- :"nom"
string = "nom"
•  float : réel (non compatible avec les entiers)
# 2. ;;
- : float = 2.
•  unit : unique valeur le vide ()
#fun x -> x + 1 ;;
•  fun : fonction
- : int -> int = <fun>
# fun x y -> x > y ;;
# fun x y -> x > y ;;
- : 'a -> 'a -> bool = <fun>
Licence 1 - programmation fonctionnelle
Une comparaison
résultat booléen
9
– Indication de réponse : - :
– Type de l'expression déduit =
– Valeur de l'expression
# () ; 2+4 ;;
- : int = 6
# let x=3<0 ;;
val x : bool = false
- : int = 2
# 2+3 ;; 2+4 ;;
- : int = 5
# let x=3<0 ; 2+4 ;;
Warning 10: this expression should have type unit.
val x : int = 6
Licence 1 - programmation fonctionnelle
8
#let ... = ... ;;
interprété
val ... : ... =
La valeur est connue dans toute la session
# let x = 5 ;;
val x : int = 5
#let y = `c` ;;
val y : char = `c`
# let x = -3 ;;
val x : int = -3
#let x = 2 + 5 ;;
val x : int = 7
# let x = -3 and y = 5 ;;
val x : int = -3
val y : int = 5
# let x = 5 ;;
val x : int = 5
# x ;;
- : int = 5
# let x = "bonjour" ;;
val x : string = "bonjour"
# x ;;
- : string = "bonjour"
10
Licence 1 - programmation fonctionnelle
Opérateurs
= fournit un
# let x = "bonjour" ;;
val x : string = "bonjour"
Liés au type des valeurs
entiers : + - * / mod abs
réels : +. -. *. /. sqrt exp **
Les deux arguments doivent être de même type
# x = 3 ;;
Error: This expression has type int
but an expression was expected of
type string
Le résultat a le même type
Ou à valeur booléenne
Égalité = <>
# x = "janvier" ;;
- : bool = false
Licence 1 - programmation fonctionnelle
•  Retour
La valeur peut être modifiée
Affectation ≠ comparaison
#let ... = ... ;;
# 1 + 1;;
– Après le « prompt » #
– Jusqu'au lancement ;;
Définition et affectation
Types simples
•  'a : pas encore défini
•  Écriture d’une expression
Comparaison < > <= >=
Les deux arguments doivent être de même type
11
Licence 1 - programmation fonctionnelle
12
2
12/01/17
La rigueur des types
Résultat des opérateurs
Le type des opérandes est déduit de
l’opérateur
#3 + 5 ;;
- : int = 8
# 3 + 1.2 ;;
Error: This expression has type float but
an expression was expected of type
int
# 3 +. 1.2 ;;
Error: This expression has type int but
an expression was expected of type
float
#3. +. 1.2 ;;
- : float = 4.2
13
Licence 1 - programmation fonctionnelle
# let x = 3. and y = 5. and z = 1 and u = "bonjour" ;;
val x : float = 3.
val y : float = 5.
val z : int = 1
val u : string = "bonjour"
# x + u ;;
Error: This expression has type float but an expression
was expected of type int
# x +. u ;;
Error: This expression has type string but an expression
was expected of type float
Licence 1 - programmation fonctionnelle
Conversions de type
float_of_int
char_of_int
#float_of_int 4 ;;
- : float = 4.0
#string_of_int 69 ;;
string_of_int - : string = "69"
int_of_char
int_of_float
int_of_string
#x +. float_of_int z ;;
- : float = 4.0
#char_of_int 69 ;;
- : char = `E`
# char_of_int 691 ;;
Exception: Invalid_argument
"char_of_int".
# int_of_float (-7.3) ;;
- : int = -7
# int_of_float (-7.8) ;;
- : int = -7
#int_of_string "452" ;;
- : int = 452
#int_of_string "caml" ;;
15
Uncaught
exception:
Failure "int_of_string"
Licence 1 - programmation
fonctionnelle
# 2. ** 3 ;;
Error: This expression has type int but an expression was
expected of type float
# 2. ** (-1.) ;;
- : float = 0.5
Licence 1 - programmation fonctionnelle
16
Priorité des opérateurs
#(2>1)=(`c`>`a`) ;;
- : bool = true
#let x = 5 ;;
val x : int = 5
#x = 6 ;;
- : bool = false
Et & &&
#2 < 3 && (-3) < 3 ;;
- : bool = true
#2<3 & (-3)<3 ;;
- : bool = true
Ou or ||
# x < y || y < x ;;
- : bool = true
Non not
#not (2 > 1) ;;
- : bool = false
#x<y or y<x ;;
- : bool = true
Licence 1 - programmation fonctionnelle
# 2 ** 3 ;;
Error: This expression has type int but an expression was
expected of type float
# 2. ** 3. ;;
- : float = 8.
Opérateurs logiques
Égalité =
14
La rigueur des types
#int_of_char `E` ;;
- : int = 69
#int_of_float 7.3 ;;
- : int = 7
#int_of_float 7.8 ;;
- ; int = 7
# x + z ;;
Error: This expression has type float but
an expression was expected of type
int
#x+.y;;
- : float = 8.0
* Prioritaire sur +
# 2 + 5 * -3 + 4 ;;
- : int = -9
# (2 + 5) * (-3 + 4) ;;
- : int = 7
not est prioritaire sur le && qui lui-même est
prioritaire sur le or
# not 2 = 3 = 2 = 2 ;;
Error: This expression has type int but an expression was expected of type
bool
# not (2 = 3) = 2 = 2 ;;
Error: This expression has type int but an expression was expected of type
bool
# not (2 = 3) = (2 = 2) ;;
- : bool = true
17
Licence 1 - programmation fonctionnelle
18
3
12/01/17
Définition locale
Opérateurs polymorphes
Une variable peut être définie par une expression1 dans
une expression
Certains opérateurs : = <>
< > <= >=
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
Utilisables avec les nombres entiers, réels, les
caractères et les chaînes
# 5 > 10 ;;
- : bool = false
# 6. < 23. ;;
- : bool = true
# 'a' < 'm' ;;
- : bool = true
# "java" > "caml" ;;
- : bool = true
# true > false ;;
- : bool = true
Licence 1 - programmation fonctionnelle
#let x = 12 in x + 5 ;;
#let x = 12 in sqrt 12.6 ;;
- : int = 17
- : float = 3.54964786986
# let x = 1 ;;
val x : int = 1
# let x = 12 in x + 5 ;;
- : int = 17
# x + 1 ;;
- : int = 2
19
# let x = 12 in x +. 5. ;;
Error: This expression has type int but
an expression was expected of type
float
Licence 1 - programmation fonctionnelle
Définition locale
La conditionnelle
suit la structure de la forme algorithmique
Ce n'est pas une instruction mais une expression qui
peut prendre 2 valeurs possibles
If condition then expression 1
else expression 2
Définition d'une fonction valeur absolue sur les réels
#let x = "caml" ;;
val x : string = "caml"
#let x = 12 in x + 5 ;;
- : int = 17
#x ;;
- : string = "caml"
# let x = 4. ;;
# abs 5. ;;
Error: This expression has type
# let a = if x < 0. then -. x else x ;;
val a : float = 4.
expected of type
# let x = - 3. ;;
21
val x : float = -3.
# let a = if x < 0. then -. x else x ;;
val a : float
= 3.
Licence 1 - programmation
fonctionnelle
Conditionnelle
22
Impression
Les deux expressions doivent avoir le
même type
Une fonction existe pour chaque type
print_int
# let x = 5 ;;
val x : int = 5
# let y = 2 ;;
val y : int = 2
# if x > y then print_int x else y ;;
Error: This expression has type int but an
expression was expected of type
unit
Licence 1 - programmation fonctionnelle
val x : float = 4.
float but an expression was
int
Licence 1 - programmation fonctionnelle
20
#let x = 5 ;;
val x : int = 5
#print_int x ;;
5- : unit = ()
print_float
print_char
#let x = `c` ;;
val x : char = `c`
#print_char x ;;
c- : unit = ()
#let x = 5. ;;
val x : float = 5.0
#print_float x ;;
5.0- : unit = ()
#let x = "caml" ;;
val x : string = "caml"
#print_string x ;;
# print_int 3 ; print_newline () ;;
caml- : unit = ()
3
print_newline Licence
() 1 - programmation fonctionnelle
24
- : unit = ()
print_string
23
4
12/01/17
Les points importants
•  Les types
•  Les opérateurs
•  La conditionnelle
Licence 1 - programmation fonctionnelle
25
5

Documents pareils