Je joue avec du code Haskell. J'ai défini deux fonctions:La chaîne associative de Haskell est déroutante
count :: [a] -> Int
count [] = 0
count (x:xs) = 1 + (count xs)
-- 03. Write a function that computes the mean of a list, i.e., the sum of all
-- elements in the list divided by its length. (You may need to use the
-- fromIntegralfunction to convert the length of the list from an integer
-- into a floating-point number.)
-- I've guessed this type definition, but it's incorrect:
-- listMean :: [a] -> Double
-- The following is the same inferred by hgci
listMean :: (Fractional a) => [a] -> a
listMean lst = (foldl1 (+) lst)/(fromIntegral (count lst))
Pourquoi [a] -> Double n'est pas correct? Il semble que je donne à lst une liste générique de type a et que listMean renvoie un double. Qu'est-ce que je fais mal?
Merci, Alfredo
+1, très belle explication. –
Parfait !! Merci beaucoup! –