Pouknouki

Automates

Dec 7th, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.93 KB | None | 0 0
  1. type Etat == int;;
  2. type automate = {
  3.     initial : Etat;
  4.     delta : Etat -> char -> Etat;
  5.     vals : Etat -> int;
  6. };;
  7.  
  8. (*Question 1*)
  9.  
  10. let execute a m =
  11.     let e = ref(a.initial) in
  12.     for i = 0 to string_length(m) - 1 do
  13.         e := a.delta !e (m.[i]);
  14.     done;
  15.     a.vals !e;;
  16.  
  17. let valsExemple e = match e with
  18.     | 4 -> 1;
  19.     | n -> 0;;
  20.  
  21. let deltaExemple e c = match e, c with
  22.     | 1, `a` -> 2;
  23.     | 2, `a` -> 3;
  24.     | 3, `a` -> 3;
  25.     | 3, `b` -> 4;
  26.     | 4, `a` -> 3;
  27.     | n, c -> -1;;
  28.    
  29. let exemple = { initial = 1; delta = deltaExemple; vals = valsExemple };;
  30.  
  31. let rec int2string n =
  32.     let resultat = ref("") in
  33.     let refn = ref(n) in
  34.     while !refn > 0 do
  35.         if !refn mod 2 = 0 then
  36.             resultat := !resultat ^ (make_string 1 `0`)
  37.         else
  38.             resultat := !resultat ^ (make_string 1 `1`);
  39.         refn := !refn / 2;
  40.     done;
  41.     let temp = (make_string (string_length !resultat) `0`) in
  42.     for i = 0 to string_length(!resultat) - 1 do
  43.         temp.[i] <- !resultat.[(string_length !resultat) - 1 - i];
  44.     done;
  45.     temp;;
  46.  
  47. let deltaDiv3 e c = match e, c with
  48.     | 0, `0` -> 0
  49.     | 0, `1` -> 1
  50.     | 1, `1` -> 0
  51.     | 1, `0` -> 2
  52.     | 2, `0` -> 1
  53.     | 2, `1` -> 2
  54.     | e, c   -> -1;;
  55.  
  56. let valsDiv3 e = match e with
  57.     | 0 -> 1
  58.     | n -> 0;;
  59.  
  60. let div3 = { initial = 0; delta = deltaDiv3; vals = valsDiv3 };;
  61.  
  62. print_int(execute div3 (int2string 9));;
  63.  
  64. let rec insere e l = match e, l with
  65.     | e, [] -> [e]
  66.     | e, t::q ->
  67.         if t < e then
  68.             t::e::q
  69.         else
  70.             t::(insere e q);;
  71.            
  72. let rec egal E1 E2 = match E1, E2 with
  73.     | [], [] -> true
  74.     | l, [] -> false
  75.     | [], l -> false
  76.     | t1::q1, t2::q2 -> (t1 = t2 && egal q1 q2);;
  77.    
  78. let rec estDans e l = match l with
  79.     | [] -> false
  80.     | t::q -> t = e || (e < t && estDans e q);;
  81.  
  82. type afnd = {
  83.     ei    : Etat list;
  84.     transitions : (Etat * char * Etat) list;
  85.     vals  : Etat -> int
  86. };;
  87.  
  88. let etatsAFND a =
  89.     let insererSiNonPresent liste element = if !estDans
  90.     let rec tousLesEtats liste acc = match liste with
  91.         | [] -> acc
  92.         | t::q ->
Advertisement
Add Comment
Please, Sign In to add comment