Programmation fonctionnelle et python - Jean

publicité
Programmation fonctionnelle et python
Attal Jean-Philippe
EISTI : Campus de Cergy
October 16, 2016
Attal Jean-Philippe (EISTI)
Python
October 16, 2016
1 / 12
Plan
1
Programmation fonctionnelle
2
Map,filter, reduce
Attal Jean-Philippe (EISTI)
Python
October 16, 2016
2 / 12
Programmation fonctionnelle
Définition
La programmation fonctionnelle est un paradigme de programmation qui
considère le calcul en tant qu’évaluation de fonctions mathématiques.
• Lisp, créé en 1958 par McCarthy.
• Scheme (1975)
• Common Lisp (1984)
• ML (1973)
• Haskell (1987)
• OCaml (1996, Inria )
• Erlang (Ericsson)
• Clean et Oz (Gert Smolka, Christian Schulte, Peter Van Roy )
• CDuce
• Scala (Martin Odersky, 2003)
• F# (Don Syme, Microsoft Research)
Attal Jean-Philippe (EISTI)
Python
October 16, 2016
3 / 12
Programmation fonctionnelle
La programmation fonctionnelle offre certaines spécificités:
• map()
• filter()
• reduce()
• fonction anonymes
• La récursion
Attal Jean-Philippe (EISTI)
Python
October 16, 2016
4 / 12
Programmation fonctionnelle: Fonctions intégrées
Attal Jean-Philippe (EISTI)
Python
October 16, 2016
5 / 12
Programmation fonctionnelle, fonction map()
Appliquer une fonction f qui transforme chaque élément d’un iterable
pour en obtenir un nouveau.
Attal Jean-Philippe (EISTI)
Python
October 16, 2016
6 / 12
Programmation fonctionnelle, fonction filter()
Appliquer une fonction pour filtrer chaque élément d’un iterable et en
obtenir un nouveau.
Attal Jean-Philippe (EISTI)
Python
October 16, 2016
7 / 12
Programmation fonctionnelle, composition de fonctions
La composition consiste en ”filter(map)”.
Attal Jean-Philippe (EISTI)
Python
October 16, 2016
8 / 12
Programmation fonctionnelle, composition de fonctions
La composition consiste en ”filter(filter)”.
La composition filter(filter) est commutative en termes de résultat, mais
n’a pas le même coût en termes de complexité algorithmique.
Attal Jean-Philippe (EISTI)
Python
October 16, 2016
9 / 12
Programmation fonctionnelle, composition de fonctions
La composition consiste en ”map(map)”.
La composition map(map) n’est pas commutative.
Attal Jean-Philippe (EISTI)
Python
October 16, 2016
10 / 12
Programmation fonctionnelle, fonction reduce()
La fonction reduce(func, seq) applique de manière répétitive la fonction
func la séquence seq. Elle ne retourne qu’une simple valeur.
Attal Jean-Philippe (EISTI)
Python
October 16, 2016
11 / 12
Programmation fonctionnelle, Exercices
Question 1
Déterminer le maximum d’une liste de valeurs numériques en utilisant un
reduce.
Question 2
En considérant une structure de tuple, écriver deux fonctions, fahrenheit et
celsius permettant de retourner une liste avec conversion.
Question 3
Calculer la somme des nombres entre 1 et 100 en utilisant la fonction
reduce.
Attal Jean-Philippe (EISTI)
Python
October 16, 2016
12 / 12
Téléchargement