en: The first Haskell solution. en: The function `evenSum` returns the sum of all even numbers in a list: fr: La première solution en Haskell. fr: La fonction `evenSum` retourne la somme de tous les nombres pairs d'une liste: > -- Version 1 > evenSum :: [Integer] -> Integer > > evenSum l = accumSum 0 l > > accumSum n l = if l == [] > then n > else let x = head l > xs = tail l > in if even x > then accumSum (n+x) xs > else accumSum n xs en: To test a function you can use `ghci`: fr: Pour tester une fonction nous pouvons utiliser `ghci`:
% ghci GHCi, version 7.0.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Prelude> :load 11_Functions.lhs [1 of 1] Compiling Main ( 11_Functions.lhs, interpreted ) Ok, modules loaded: Main. *Main> evenSum [1..5] 6en: Here is an example of execution[^2]: fr: Voici un exemple d'exécution[^2]: en: [^2]: I know I'm cheating. But I will talk about non-strictness later. fr: [^2]: Je sais que je triche. Mais je parlerais de la non-rigueur plus tard.
*Main> evenSum [1..5] accumSum 0 [1,2,3,4,5] en: 1 is odd fr: 1 est impair accumSum 0 [2,3,4,5] en: 2 is even fr: 2 est pair accumSum (0+2) [3,4,5] en: 3 is odd fr: 3 est impair accumSum (0+2) [4,5] en: 2 is even fr: 4 est pair accumSum (0+2+4) [5] en: 5 is odd fr: 5 est impair accumSum (0+2+4) [] l == [] 0+2+4 0+6 6en: Coming from an imperative language all should seem right. en: In fact, many things can be improved here. en: First, we can generalize the type. fr: En venant d'un langage impératif, tout devrait vous sembler juste. fr: En fait, beaucoup de choses peuvent être améliorées ici. fr: Tout d'abord, nous pouvons généraliser le type.
evenSum :: Integral a => [a] -> a