Prépa Code Connexion Inscription

Exercices de programmation pour classes préparatoires

← Retour aux exercices

Réduction à gauche

ocaml ★★☆☆☆

Écrire une fonction fold_gauche : ('b -> 'a -> 'b) -> 'b -> 'a list -> 'b qui effectue un repliement à gauche (fold left) sur une liste. La fonction prend un accumulateur initial et applique la fonction f de gauche à droite en accumulant le résultat. Sur une liste vide, elle renvoie l'accumulateur initial.

Exemples

AppelRésultat attendu
fold_gauche (fun acc x -> acc + x) 0 [1; 2; 3] 6
fold_gauche (fun acc x -> acc * x) 1 [2; 3; 4] 24

Votre code

Connectez-vous pour soumettre du code.