Fiche de Révision Haskell
1. Variables & Types
x = 5
nom = "Alice"
-- Type
age :: Int
age = 20
2. Fonctions
add x y = x + y
carre x = x * x
3. Fonctions pures
-- même entrée = même sortie
double x = x * 2
4. Listes
liste = [1,2,3]
-- ajouter
newList = 0 : liste
-- concat
liste2 = liste ++ [4,5]
5. Fonctions utiles
map (+1) [1,2,3] -- [2,3,4]
filter (>2) [1,2,3,4] -- [3,4]
sum [1,2,3] -- 6
6. Récursion
factorielle 0 = 1
factorielle n = n * factorielle (n-1)
7. Conditions
maxi a b = if a > b then a else b
8. Pattern Matching
longueur [] = 0
longueur (_:xs) = 1 + longueur xs
9. Lambda (fonction anonyme)
map (\x -> x * 2) [1,2,3]
10. Composition
double x = x * 2
plus1 x = x + 1
result = (double . plus1) 3
11. Lazy Evaluation
infini = [1..]
take 5 infini -- [1,2,3,4,5]
12. Exemple complet
result =
sum (map (^2) (filter even [1..10]))