# unicaml

### Site Tools

ex-l4-sol
##### Esercizi sulle liste:
1. .
`let liststr = ["foo";"bar";"baz"];;`
2. .
`let hdadd lista = 1::2::3::lista;;`
3. .
`let tladd lista = lista@[1;2;3];;`
4. 1)
```let strbool = function
["foo"] -> false
| _ ->true;;```

2)

`let f l = "Anna"::l = ["Anna"] ;;`
5. .
`let f b = if b then [1] else [2];;`
6. 1) Senza pattern matching
`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;;```
7. .
```let rec intToList n = if n < 10 then [n]
else let cifra = n mod 10 in  (listaCifreRec (n / 10)) @[cifra] ;;```
##### Esercizi su funzioni passate come parametro:
1. .
```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);;```
2. .
```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;;```
3. .
```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";;```
4. .
```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;;```
5. .
```let rec f pred n = if n < 10 then (if pred n = true then 1 else 0 )
else let cifra = n mod 10
in if pred cifra = true then 1 + f pred (n/10)
else f pred (n/10);; ```
6. Esempio senza pattern matching
```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);;```