Rappels
Types algébriques
Type synonyme : type <Id> = <type>
Exemples :
type Name = String type Age = Int type Personne = (Name, Age)
Type synonyme polymorphe : type <Id> <typeGenerique>...<typeGenerique> = <type>
Exemple :
type HashTable a b = [(a,b)]
Type énuméré : data <Id> = <Constr> | ... | <Constr>
Exemple :
data Jour = Lundi | ... | Dimanche
Type union : data <Id> = <Constr> <type>... <type> | ... | <Constr> <type> ... <type>
Exemple :
data Fete = Anniversaire Personne | Mariage Personne Personne | JourFerie {date :: String, nomFete :: String}
Type union polymorphe : data <Id> <typeGenerique>...<typeGenerique> = <Constr> <type> ... <type> | ... | <Constr> <type> ... <type>
Exemple :
data Maybe a = Just a | Nothing
Pattern matching sur les types algébriques : case ... of.
Exemple :
felicitation :: Fete -> String
felicitation fete = case fete of
Anniversaire (nom, age) -> "Bon anniversaire de " ++ nouvelAge ++ " ans " ++ nom
where nouvelAge = (show (age + 1))
Mariage (nom1, _) (nom2, _) -> "Felicitations " ++ nom1 ++ " et " ++ nom2 ++ " !"
Dérivation automatique de certaines opérations : deriving (Eq, Show, Ord, Enum, ...)
Monades
data Maybe a = Just a | Nothing data Either a b = Left a | Right b
INGInious