Précédent Index Suivant



Impératif/Fonctionnel


let fib = function n ->
  let f_0 = ref(0) 
  and f_1 = ref(1) 
  and temp = ref(0) in
    begin
    for i = 1 to n do
      temp := !f_0;
      f_0 := !f_1;
      f_1 := !temp + !f_1
    done;
    !f_0
    end ;;
let fib =
  let rec f_r = function
    | 0 -> (0, 1)
    | n -> 
      let (f_0,f_1) = 
          f_r(n - 1)
      in (f_1, f_0 + f_1)
  in fun n -> fst(f_r n) ;;
Comment rendre terminal l'appel
f_r(n - 1)


Précédent Index Suivant