Outils pour utilisateurs

Outils du site


nsi:langages:ocaml:listes

Ceci est une ancienne révision du document !



Warning: Undefined array key 1 in /home/goupillf/wiki.goupill.fr/lib/plugins/codeprettify/syntax/code.php on line 172

Warning: Undefined array key 1 in /home/goupillf/wiki.goupill.fr/lib/plugins/codeprettify/syntax/code.php on line 172

Warning: Undefined array key 2 in /home/goupillf/wiki.goupill.fr/lib/plugins/codeprettify/syntax/code.php on line 214

Warning: Undefined array key 2 in /home/goupillf/wiki.goupill.fr/lib/plugins/codeprettify/syntax/code.php on line 214

Warning: Undefined array key 2 in /home/goupillf/wiki.goupill.fr/lib/plugins/codeprettify/syntax/code.php on line 214

Warning: Undefined array key 2 in /home/goupillf/wiki.goupill.fr/lib/plugins/codeprettify/syntax/code.php on line 214

Listes

On dispose d'une structure de liste.

let x = [4;5;17];;
let y = 15::x;; (* liste [15;4;5;17] *)
let z = 12::13;; (* erreur, pas autorisé *)

Avec une fonction on peut accéder au premier élément d'une liste head et à la queue tail – éléments suivants. Voici un exemple donnant la somme des éléments d'une liste.

let rec somme = function
  | [] -> 0                (* si la liste est vide *)
  | hd::tl -> hd + (somme tl);;(* hd = head = 1er élément
                                tl = tail = éléments suivants.*)
somme x;; (* renvoie 26 *)                                
nsi/langages/ocaml/listes.1647984405.txt.gz · Dernière modification : de goupillwiki