Advertisement
Tavi33

CFLP12v2233

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