User Tools

Site Tools


ex-l3-sol
  1. Lista di interi:
     let x = [1;2;3;4;5];;
  2. Aggiunta in testa:
     let f lista = 1::lista;;
  3. .
    let f lista = if lista == [] then 1 else List.hd lista;;
  4. .
    let f n = if(n==0) then [] else [n];;
  5. Minimo di una funzione nell'intervallo [a,b]:
    let rec minFun f a b = 
    if (a = b) then f a else 
    if ( f a  < f  b ) then minFun f a (b-1) 
    else minFun f (a+1) b ;;
  6. .
    let rec test’ f a b = 
    if (a = b) then true else
    	if f a > f b then false
        else test f (a+1) b;;
    let test f a b = 
    if (test’ f a b) then “increasing”
    elsenot increasing”;;
  7. .
     let rec f n acc = if n=0 
    then acc else if (n mod 2) = 0 
    then f (n/10) acc+1 else f (n/10) acc;;
    let contapari n = f n 0;;
  8. .
     let rec f g n acc = if n=0 then acc else if (g (n mod 10) = true)  
    then f g (n/10) (acc+1) else f g (n/10) acc;;
    let contatrue g n = f g n 0;;
  9. .
    let square x = x * x;;
    let rec exp' n x = if n=0 then 1 else if (n mod 2 = 0) 
    then square (exp' (n/2) x ) 
    else x*square (exp' (n/2) x);;  
  10. Fibonacci:
    let fib n = let rec fib' n a b = if n=0 then a else fib' (n-1) b (a+b)
      in fib' n 0 1;;
ex-l3-sol.txt · Last modified: 2015/10/08 15:20 (external edit)