Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Problema [1]
- # type 'tipelement nod = Empty
- | Nod of ('tipelement nod*'tipelement*'tipelement nod);;
- # type nod_int = int nod;;
- # let rec inser n = function
- | Empty -> Nod(Empty, n, Empty)
- | Nod(l, x, r) as node ->
- if n < x then Nod((inser n l), x, r)
- else if n > x then Nod(l, x, (inser n r))
- else node;;
- # let a = inser 5;;
- # let a = inser 1;;
- # let a = inser 10;;
- # let rec search n = function
- | Nod(l, x, r) -> if n < x then (search n l) else if n > x then (search n r)
- | Empty -> 0;;
- # let rec preorder = function
- | Empty -> []
- | Nod (l, x, r) -> x::preorder l@preorder r;;
- # preorder a;;
- - : int list = [5; 1; 10]
- # let rec inorder = function
- | Empty -> []
- | Nod (l, x, r) -> inorder l@x::inorder r;;
- # inorder a;;
- - : int list = [1; 5; 10]
- 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 rec eval = function
- | Valoare(x) -> x
- | Adunare(x,y) -> (eval x) +. (eval y)
- | Scadere(x,y) -> (eval x) -. (eval y)
- | Inmultire(x,y) -> (eval x) *. (eval y)
- | Impartire(x,y) -> (eval x) /. (eval y)
- | Fc1(f, x) -> (f (eval x))
- | Fc2(f, x, y) -> (eval x) ** (eval y);;
- //s
- # let power x y = x ** y;;
- //6/2-2^3+cos(2*asin 1)
- # let a = Adunare(Scadere(Impartire(Valoare 6.0, Valoare 2.0), Fc2(power, Valoare 2.0, Valoare 3.0)), Fc1(cos, Inmultire(Valoare 2.0, Fc1(asin, Valoare 1.0))));;
- # eval a;;
- - : float = -6.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement