|
@@ -0,0 +1,209 @@
|
|
|
+# PFONC
|
|
|
+
|
|
|
+Programmation fonctionnelle
|
|
|
+
|
|
|
+# Cours
|
|
|
+
|
|
|
+## Bases
|
|
|
+
|
|
|
+Évaluation gardée :
|
|
|
+
|
|
|
+```hs
|
|
|
+signe n
|
|
|
+ | n < 0 = -1
|
|
|
+ | n > 0 = 1
|
|
|
+ | otherwise = 0
|
|
|
+```
|
|
|
+
|
|
|
+## Types
|
|
|
+
|
|
|
+### Somme
|
|
|
+
|
|
|
+```hs
|
|
|
+data Bool = True | False
|
|
|
+```
|
|
|
+
|
|
|
+### Produit
|
|
|
+
|
|
|
+```hs
|
|
|
+data Vector3 = Vec3 Float Float Float
|
|
|
+```
|
|
|
+
|
|
|
+### Algébrique
|
|
|
+
|
|
|
+Somme de produit
|
|
|
+
|
|
|
+```hs
|
|
|
+data Maybe a = Nothing | Just a
|
|
|
+```
|
|
|
+
|
|
|
+### Alias
|
|
|
+
|
|
|
+```hs
|
|
|
+type String = [Char]
|
|
|
+```
|
|
|
+
|
|
|
+### Tuple
|
|
|
+
|
|
|
+```hs
|
|
|
+(a, b)
|
|
|
+```
|
|
|
+
|
|
|
+### Enregistrement
|
|
|
+
|
|
|
+```hs
|
|
|
+data Canard = Coin { nom::String, enverg::Double }
|
|
|
+
|
|
|
+let c1 = Coin "Mr. Coin" 0.8 in
|
|
|
+let name = nom c1
|
|
|
+```
|
|
|
+
|
|
|
+### Listes on compréhension
|
|
|
+
|
|
|
+```hs
|
|
|
+evens n = [x+1 | x <- [1..(n-2)], odd x]
|
|
|
+
|
|
|
+odd3Products = [m*n | m <- [101, 103..999],
|
|
|
+ n <- [101, 103..999]]
|
|
|
+```
|
|
|
+
|
|
|
+## Fonctions d'ordre supérieur
|
|
|
+
|
|
|
+Fonction de **première classe** : type de base
|
|
|
+
|
|
|
+Fonctions d'**ordre supérieur** : **ensemble de fonction** dans le domaine ou le codomaine
|
|
|
+
|
|
|
+_Exemple : map_
|
|
|
+
|
|
|
+### Fold left
|
|
|
+
|
|
|
+From left to right
|
|
|
+
|
|
|
+```hs
|
|
|
+foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn
|
|
|
+```
|
|
|
+
|
|
|
+### Fold right
|
|
|
+
|
|
|
+From right to left
|
|
|
+
|
|
|
+```hs
|
|
|
+foldr f z [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn `f` z)...)
|
|
|
+```
|
|
|
+
|
|
|
+## Méthodes d'évaluation
|
|
|
+
|
|
|
+Listes infinies :
|
|
|
+- `foldr` **fonctionne** mais NON récursive terminale
|
|
|
+- `foldl` ne fonctionne pas mais est **récursive terminale**
|
|
|
+- `foldl'` **fonctionne** en forçant l'**évaluation stricte**
|
|
|
+
|
|
|
+`seq::a -> b -> b` évalue son premier argument et donne le second comme résultat
|
|
|
+
|
|
|
+## Entrées, sorties
|
|
|
+
|
|
|
+```hs
|
|
|
+putStr :: String - > IO ()
|
|
|
+putStrLn :: String - > IO ()
|
|
|
+```
|
|
|
+
|
|
|
+```hs
|
|
|
+print ::( Show a ) = > a -> IO ()
|
|
|
+print = putStrLn . show
|
|
|
+```
|
|
|
+
|
|
|
+Lire au clavier
|
|
|
+```hs
|
|
|
+getLine :: IO String
|
|
|
+getChar :: IO Char
|
|
|
+```
|
|
|
+
|
|
|
+Dual de Show
|
|
|
+```hs
|
|
|
+read ::( Read a ) = > String - > a
|
|
|
+```
|
|
|
+
|
|
|
+Opérateur **bind**
|
|
|
+```hs
|
|
|
+(>>=) :: IO a -> (a -> IO b) -> IO b
|
|
|
+```
|
|
|
+
|
|
|
+Simplifie les séquences de `bind`
|
|
|
+```hs
|
|
|
+echo2 ’ :: IO ()
|
|
|
+echo2 ’ = do
|
|
|
+ x <- getLine
|
|
|
+ y <- getLine
|
|
|
+ putStrLn ( y ++ " " ++ x )
|
|
|
+```
|
|
|
+
|
|
|
+```hs
|
|
|
+return :: a -> IO a
|
|
|
+```
|
|
|
+
|
|
|
+## Généricité avancée
|
|
|
+
|
|
|
+`Eq` (in)égalité
|
|
|
+
|
|
|
+`Ord` comparaisons
|
|
|
+
|
|
|
+`Enum` exemple : `[x..y]`
|
|
|
+
|
|
|
+`Bounded`
|
|
|
+
|
|
|
+`Show` implémentant `show::a -> String`
|
|
|
+
|
|
|
+`Read` implémentant `read::String -> a`
|
|
|
+
|
|
|
+# Utilities
|
|
|
+
|
|
|
+## Fonctions de haut niveau
|
|
|
+
|
|
|
+```hs
|
|
|
+
|
|
|
+map::(a -> b) -> [a] -> [b]
|
|
|
+
|
|
|
+flip::(a -> b -> c) -> b -> a -> c
|
|
|
+
|
|
|
+filter::(a -> Bool) -> [a] -> [a]
|
|
|
+
|
|
|
+foldl::(b -> a -> b) -> b -> [a] -> b
|
|
|
+
|
|
|
+foldr::(a -> b -> b) -> b -> [a] -> b
|
|
|
+
|
|
|
+foldl1, foldr1::(a -> a -> a) -> [a] -> a
|
|
|
+
|
|
|
+zip::[a] -> [b] -> [(a, b)]
|
|
|
+
|
|
|
+zipWith::(a -> b -> c) -> [a] -> [b] -> [c]
|
|
|
+
|
|
|
+take::Int -> [a] -> [a]
|
|
|
+
|
|
|
+takeWhile::(a -> Bool) -> [a] -> [a]
|
|
|
+
|
|
|
+drop::Int -> [a] -> [a]
|
|
|
+
|
|
|
+dropWhile::(a -> Bool) -> [a] -> [a]
|
|
|
+
|
|
|
+any::(a -> Bool) -> [a] -> Bool
|
|
|
+
|
|
|
+```
|
|
|
+
|
|
|
+## List operations
|
|
|
+
|
|
|
+```hs
|
|
|
+tail::[a] -> [a]
|
|
|
+
|
|
|
+head::[a] -> a
|
|
|
+
|
|
|
+concat::[[a]] -> [a]
|
|
|
+```
|
|
|
+
|
|
|
+
|
|
|
+# Tricks
|
|
|
+
|
|
|
+## Valeurs en zéro de fonction
|
|
|
+
|
|
|
+```hs
|
|
|
+map ($ 0) [f, g, h]
|
|
|
+```
|