let liststr = ["foo";"bar";"baz"];;
let hdadd lista = 1::2::3::lista;;
let tladd lista = lista@[1;2;3];;
let strbool = function ["foo"] -> false | _ ->true;;
2)
let f l = "Anna"::l = ["Anna"] ;;
let f b = if b then [1] else [2];;
let complist l1 l2 = (l1 = l2);;
2) Con pattern matching
let rec complist l1 l2 = match (l1,l2) with ([],[]) -> true | (l,[]) -> false | ([], l) -> false | (hd1::tl1, hd2::tl2) -> if( hd1=hd2) then complist tl1 tl2 else false;;
let rec intToList n = if n < 10 then [n] else let cifra = n mod 10 in (listaCifreRec (n / 10)) @[cifra] ;;
let min a b = if a < b then a;; let max a b = if a < b then b;; let rec minFun f a b = if (a - b = 0) then f a else min (f a) (minFun f (a+1) b);; let rec maxFun f a b = if (a - b = 0) then f a else max (f a) (maxFun f (a+1) b);; let minMax f a b = if (a < b ) then (minFun f a b , maxFun f a b) else (minFun f b a, maxFun f b a);;
let rec countZeri f a b = if (a>b) then 0 else let isZero = (if f a = 0.0 then 1 else 0) in isZero + countZeri f (a+. 0.5) b;;
let rec sdecreasing f a b = if b<a then true else if b=a+1 then (f a) < (f b) else (f a) < (f (a+1)) && (sdecreasing f (a+1) b);; let test f a b = if sdecreasing f a b then "strictly decreasing" else "unknown";;
let isMult n = if n mod 3 = 0 then 1 else 0;; let rec multTreRec n = if n = 0 then 0 else let cifra = n mod 10 in isMult cifra + multTreRec ( n / 10) ;; let multTre n = if (n=0) then 1 else multTreRec n;;
let rec gcd n m = if n=0 then m else if m=0 then n else if n>m then gcd (n-m) m else gcd n (m-n);;
Alternativa con pattern matching
let rec gcd n m = match (n,m) with (0,m) -> m | (n,0) -> n | (n,m) when n>m -> gcd (n-m) m | _ -> gcd n (m-n);;