ex-l3-sol
Lista di interi:
let x = [1;2;3;4;5];;
Aggiunta in testa:
let f lista = 1::lista;;
.
let f lista = if lista == [] then 1 else List.hd lista;;
.
let f n = if(n==0) then [] else [n];;
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 ;;
.
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”
else “not increasing”;;
.
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;;
.
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;;
.
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);;
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)