Programmation fonctionnelle et python ATTAL Jean-Philippe (1) (1) [email protected] June 22, 2016 Contents 1 Programmation fonctionnelle 2 Map,filter, reduce ATTAL Jean-Philippe Programmation fonctionnelle et python 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 Programmation fonctionnelle et python Contents 1 Programmation fonctionnelle 2 Map,filter, reduce ATTAL Jean-Philippe Programmation fonctionnelle et python Programmation fonctionnelle La propgrammation fonctionnelle offre certaines spécificités: • map() • filter() • reduce() • fonction anonymes • La récursion ATTAL Jean-Philippe Programmation fonctionnelle et python Programmation fonctionnelle: Fonctions intégrées ATTAL Jean-Philippe Programmation fonctionnelle et python Programmation fonctionnelle, fonction map() Appliquer une fonction f qui transforme chaque élément d’un iterable pour en obtenir un nouveau. ATTAL Jean-Philippe Programmation fonctionnelle et python Programmation fonctionnelle, fonction filter() Appliquer une fonction pour filtrer chaque élément d’un iterable et en obtenir un nouveau. ATTAL Jean-Philippe Programmation fonctionnelle et python Programmation fonctionnelle, composition de fonctions La composition consiste en "filter(map)". ATTAL Jean-Philippe Programmation fonctionnelle et python Programmation fonctionnelle, composition de fonctions La composition consiste en "filter(filter)". La composition filter(filter) est commutative en terme de résultat, mais n’a pas le même coût en terme de complexité algorithmique. ATTAL Jean-Philippe Programmation fonctionnelle et python Programmation fonctionnelle, composition de fonctions La composition consiste en "map(map)". La composition map(map) n’est pas commutative. ATTAL Jean-Philippe Programmation fonctionnelle et python Programmation fonctionnelle, fonction reduce() La fonction reduce(func, seq) applique de manière répétitive la fonction func à la séquence seq. Il ne retourne qu’une simple valeur. ATTAL Jean-Philippe Programmation fonctionnelle et python Programmation fonctionnelle, Exercices Question 1 Déterminez le maximum d’une liste de valeurs numériques en utilisant un reduce. Question 2 En considérant une structure de tuple, écrivez deux fonctions, fahrenheit et celsius permettant de retourner une liste avec conversion. ATTAL Jean-Philippe Programmation fonctionnelle et python