Programmation fonctionnelle
Évaluation gardée :
signe n
| n < 0 = -1
| n > 0 = 1
| otherwise = 0
data Bool = True | False
data Vector3 = Vec3 Float Float Float
Somme de produit
data Maybe a = Nothing | Just a
type String = [Char]
(a, b)
data Canard = Coin { nom::String, enverg::Double }
let c1 = Coin "Mr. Coin" 0.8 in
let name = nom c1
evens n = [x+1 | x <- [1..(n-2)], odd x]
odd3Products = [m*n | m <- [101, 103..999],
n <- [101, 103..999]]
Fonction de première classe : type de base
Fonctions d'ordre supérieur : ensemble de fonction dans le domaine ou le codomaine
Exemple : map
From left to right
foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn
From right to left
foldr f z [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn `f` z)...)
Listes infinies :
foldr
fonctionne mais NON récursive terminalefoldl
ne fonctionne pas mais est récursive terminalefoldl'
fonctionne en forçant l'évaluation stricteseq::a -> b -> b
évalue son premier argument et donne le second comme résultat
putStr :: String - > IO ()
putStrLn :: String - > IO ()
print ::( Show a ) = > a -> IO ()
print = putStrLn . show
Lire au clavier
getLine :: IO String
getChar :: IO Char
Dual de Show
read ::( Read a ) = > String - > a
Opérateur bind
(>>=) :: IO a -> (a -> IO b) -> IO b
Simplifie les séquences de bind
echo2 ’ :: IO ()
echo2 ’ = do
x <- getLine
y <- getLine
putStrLn ( y ++ " " ++ x )
return :: a -> IO a
Eq
(in)égalité
Ord
comparaisons
Enum
exemple : [x..y]
Bounded
Show
implémentant show::a -> String
Read
implémentant read::String -> a
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
tail::[a] -> [a]
head::[a] -> a
concat::[[a]] -> [a]
map ($ 0) [f, g, h]