Advertisement
Tavi33

CFLP 12 v2

May 26th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.81 KB | None | 0 0
  1. Problema [1]
  2. # type 'tipelement nod = Empty
  3.     | Nod of ('tipelement nod*'tipelement*'tipelement nod);;
  4.  
  5. # type nod_int = int nod;;
  6.  
  7. # let rec inser n = function
  8.                 | Empty -> Nod(Empty, n, Empty)
  9.                 | Nod(l, x, r) as node ->
  10.                     if n < x then Nod((inser n l), x, r)
  11.                     else if n > x then Nod(l, x, (inser n r))
  12.             else node;;
  13. # let a = inser 5;;
  14. # let a = inser 1;;
  15. # let a = inser 10;;
  16.            
  17. # let rec search n = function
  18.                 | Nod(l, x, r) -> if n < x then (search n l) else if n > x then (search n r)
  19.                 | Empty -> 0;;
  20.  
  21.  
  22. # let rec preorder = function
  23.   | Empty -> []
  24.   | Nod (l, x, r) -> x::preorder l@preorder r;;
  25.  
  26.  # preorder a;;
  27. - : int list = [5; 1; 10]
  28.  
  29. # let rec inorder = function
  30.   | Empty -> []
  31.   | Nod (l, x, r) -> inorder l@x::inorder r;;
  32.  
  33.   # inorder a;;
  34. - : int list = [1; 5; 10]
  35.  
  36. Problema [2]
  37. # type expresie = Valoare of float
  38.    | Adunare of (expresie*expresie)
  39.    | Scadere of (expresie* expresie)
  40.    | Inmultire of (expresie*expresie)
  41.    | Impartire of (expresie* expresie)
  42.    | Fc1 of ((float->float)*expresie)
  43.    | Fc2 of ((float->float->float)*expresie*expresie);;
  44.    
  45. # let rec eval = function
  46.             | Valoare(x) -> x
  47.             | Adunare(x,y) -> (eval x) +. (eval y)
  48.             | Scadere(x,y) -> (eval x) -. (eval y)
  49.             | Inmultire(x,y) -> (eval x) *. (eval y)
  50.             | Impartire(x,y) -> (eval x) /. (eval y)
  51.             | Fc1(f, x) -> (f (eval x))
  52.             | Fc2(f, x, y) -> (eval x) ** (eval y);;
  53.            
  54. //s
  55. # let power x y = x ** y;;
  56.  
  57. //6/2-2^3+cos(2*asin 1)
  58.  
  59. # 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))));;
  60.  
  61. # eval a;;
  62. - : float = -6.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement