and
) ou réussit
(or
). On utilise une exception (on pourrait aussi écrire la fonction
command_and
directement sans utiliser fold_left
).
exception Retcode of int let rec command_and cmd_arg_list = try List.fold_left (fun acc (cmd, args) -> if acc <> 0 then raise (Retcode acc) else command_wait cmd args) 0 cmd_arg_list with Retcode c -> c;; let command_or cmd_arg_list = try List.fold_left (fun acc (cmd, args) -> if acc = 0 then raise (Retcode 0) else command_wait cmd args) 1 cmd_arg_list with Retcode n -> 0;; |
command_and []
retourne 0 (toutes les commandes ont
réussi) alors que command_or []
retourne 1 (auncune commande n'a réussi).