Laborator 11

Laborator 11

Problema 1

type 'tipelement nod = Empty

| Nod of ('tipelement nod*'tipelement*'tipelement nod);;

# let rec adaug = function y -> function Empty -> (Nod(Empty,y,Empty))

| Nod(st,x,dr) -> if (st=Empty) then (Nod(Nod(Empty,y,Empty),x,dr))

else

if (dr=Empty) then (Nod(st,x,Nod(Empty,y,Empty)))

else

(Nod((adaug y st),x,dr));;

val adaug : 'a -> 'a nod -> 'a nod = <fun>

# let n= Empty;;

val n : 'a nod = Empty

# let n = adaug 8 n;;

val n : int nod = Nod (Empty, 8, Empty)

# let n = adaug 2 n;;

val n : int nod = Nod (Nod (Empty, 2, Empty), 8, Empty)

# let n = adaug 1 n;;

val n : int nod = Nod (Nod (Empty, 2, Empty), 8, Nod (Empty, 1, Empty))

# let n = adaug 4 n;;

val n : int nod = Nod (Nod (Nod (Empty, 4, Empty), 2, Empty), 8, Nod (Empty, 1, Empty))

-Cauta:

# let rec caut = function y -> function Empty -> false

| Nod(s,x,d) -> if (x=y) then true

else ((caut y s) or (caut y d));;

val caut : 'a -> 'a nod -> bool = <fun>

# caut 1 n;;

- : bool = true

# caut 9 n;;

- : bool = false

-Afisare inordine:

# let rec inordine = function Empty -> []

| Nod(s,x,d) -> (inordine s)@x::[]@(inordine d);;

val inordine : 'a nod -> 'a list = <fun>

# inordine n;;

- : int list = [4; 2; 8; 1]

-Afisare preordine:

# let rec preordine = function Empty -> []

| Nod(s,x,d) -> x::[]@(preordine s)@(preordine d);;

val preordine : 'a nod -> 'a list = <fun>

#preordine n;;

- : int list = [8; 2; 4; 1]

Problema 2.

# type expresie = Valoare of float

| Adunare of (expresie*expresie)

| Scadere of (expresie*expresie)

| Inmultire of (expresie*expresie)

| Impartire of (expresie*expresie)

| Fc1 of ((float->float)*expresie)

| Fc2 of ((float->float->float)*expresie*expresie);;

let x1 = Valoare(6.);;

let x2 = Valoare(2.);;

let imp = Impartire(x1,x2);;

let x1 = Valoare(2.);;

let x2 = Valoare(3.);;

let rec putere = function x -> function y -> if (y=1.) then x else

x*.(putere x (y-.1.));;

let pp = Fc2 (putere,x1,x2);;

let scad = Scadere(imp,pp);;

let x = Valoare(1.);;

let a = Fc1(asin,x);;

let x = Valoare(2.);;

let inm = Inmultire(x,a);;

let cc = Fc1(cos,inm);;

let suma = Adunare(scad,cc);;

let rec fct = function Valoare(v) -> v

| Adunare(e1,e2) -> ((fct e1)+.(fct e2))

| Scadere(e1,e2) -> ((fct e1)-.(fct e2))

| Inmultire(e1,e2) -> ((fct e1)*.(fct e2))

| Impartire(e1,e2) -> ((fct e1)/.(fct e2))

| Fc1(f,e) -> (f (fct e))

| Fc2(f,e1,e2) -> (f (fct e1) (fct e2));;

fct suma;;

- : float = -6.