Adloun

OCaml (option informatique) · Chapitre 17 · Application directe du cours

Nombre de variables

Logique propositionnelle · niveau 1 (application)

Énoncé

Écrire nb_variables et l'appliquer à f = Et (Ou (Var 0, Non (Var 1)), Var 2).

Corrigé

let rec var_max f =
  match f with
  | Var i -> i
  | Non g -> var_max g
  | Et (g, h) | Ou (g, h) ->
      let a = var_max g and b = var_max h in if a >= b then a else b

let nb_variables f = var_max f + 1

La plus grande variable de f est Var 2, donc nb_variables f = 3. On suppose les variables numérotées sans trou de 0 à n-1.

Exercices semblables

Le cours du chapitre · Tous les exercices du chapitre