User Tools

Site Tools


ex-l2-plus-sol
  1. Successioni per ricorrenza e mutua ricorsione
    1. Coefficiente binomiale:
      let rec cb n k = if(k=0 || k=n) then 1 else cb (n-1) (k-1) + 
      cb (n - 1) (k);;
    2. f:
      let rec f n  = if (n = 0) then 0 else if (n = 1) then 1 else 2 * f (n-2) + 
      f (n-1) ;;
    3. Hofstadter Female and Male Sequences:
      let rec f n = if (n=0) then 1 else n-m(f(n-1)) 
      and
      m n = if (n=0) then 0 else n-f(m(n-1));;
  2. Tail recursion:
    1. Fattoriale:
      let fact n = let rec fact' n acc = if (n=0) then acc else 
      fact' (n-1) acc*n in fact' n 1;; 
    2. Esponenziale:
      let exp x n = let rec exp' x n acc = if (n=0) then 1 
      else exp' x (n-1) x*acc in exp' x n x;;
    3. Fibonacci:
      let fib n = let rec fib' n a acc = if(n=0) then a 
      else fib' (n-1) acc (a+acc) in fib' n 0 1;;
    4. Real:
      let real n = let rec real' n acc = if(n=0) then acc 
      else real' (n-1) (1.0+.acc) in real' n 0.0;;
  3. Tuple:
    1. Esempio con esponenziale:
      let exp5 a b c d e = 
      (exp a 2, exp b 2, exp c 2, exp d 2, exp e 2);;
    2. Ordine lessicografico:
      let lessic a b = 
      if ((fst a < fst b) || ((fst a = fst b) && (snd a <= snd b))) 
      then (a,b) else (b,a);;
ex-l2-plus-sol.txt · Last modified: 2015/10/08 15:20 (external edit)