Advertisement
kator

tree_lib

Mar 27th, 2019
879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.50 KB | None | 0 0
  1. (* tree.ml *)
  2. (* definicja drzewa *)
  3. type btree =
  4.   Leaf of int
  5. | Node of btree * btree ;;
  6.  
  7. (* val print_tree : btree -> unit;; *)
  8. let print_tree d =
  9.   let rec string_of_tree d =
  10.     match d with
  11.     Leaf f -> string_of_int f
  12.     | Node (n1,n2) -> " N("^string_of_tree n1^";"^string_of_tree n2^") "
  13.   in Printf.printf "TREE -> [%s]\n" (string_of_tree d);;
  14.  
  15.  
  16.  
  17. (* val sum : btree -> int *)
  18. let rec sum = function
  19.   Leaf f -> f
  20.   | Node (n1,n2) -> (sum n1) + (sum n2);;
  21.  
  22.   (* let sum tree  =
  23.     let rec sum_tree tree = function
  24.     Leaf f -> f
  25.     | Node (n1,n2) -> (sum_tree n1) + (sum_tree n2)
  26.     in (sum_tree tree);; *)
  27.  
  28. (* Dla kazdego liscia zaaplikuj funkcje *)
  29. (* val map : *)
  30. let map fx tree =
  31.     let rec map_t= function
  32.     Leaf l -> Leaf (fx l)
  33.   | Node (n1,n2) -> Node((map_t n1),( map_t n2))
  34.   in (map_t tree);;
  35.  
  36. (* let rec map fx = function
  37.     Leaf l -> Leaf(fx l)
  38. |   Node(n1,n2) -> Node(map fx n1) *)
  39.  
  40. (* val fold_left: (int -> int) -> int -> btree -> int;; *)
  41. let fold_left (red: int -> int -> int) seed tree =
  42.     let rec reduce seed = function
  43.         Leaf l -> red seed l
  44.     |   Node(n1, n2) -> reduce (reduce seed n1) n2
  45.     in reduce seed tree;;
  46.  
  47. (* val mount: int -> btree -> btree -> btree;;     *)
  48. let mount v t1 t2 =
  49.     let rec searchV v = function
  50.     Leaf f -> if f=v then t2 else Leaf f
  51.     | Node(n1, n2) -> Node((searchV v n1), (searchV v n2))
  52.     in (searchV v t1);;
  53.  
  54.  
  55. (* pr_tree.ml *)
  56. open Printf;;
  57. open Tree;;
  58. let t3 = Node(Leaf 1, Leaf 2)
  59. let t1 = Node ( Node(Leaf 3, Leaf 3),
  60.                   Leaf 3);;
  61. let t2 = Node (
  62.     Node(
  63.         Node(Leaf 2, Leaf 4),
  64.         Leaf 3
  65.     ), Node(Leaf 1, Leaf 5)
  66. );;
  67.  
  68. (* print_tree t1;; *)
  69. (* printf "%d\n" (sum t1);; *)
  70. (* printf "%d\n" (sum t2);; *)
  71.  
  72. let fx = fun x -> x *10 + 1;;
  73. let red = fun x y -> x-y;;
  74. (* let mapped = map fx t1;; *)
  75. print_tree (map fx t2);;
  76. printf "%d\n" (fold_left red 10 t1);;
  77. print_tree t1;;
  78. print_tree t2;;
  79. print_tree (mount 3 t1 t3);;
  80.  
  81. (* tree.mli *)
  82. (* definicja drzewa *)
  83. type btree =
  84.   Leaf of int
  85. | Node of btree * btree ;;
  86.  
  87. val print_tree : btree -> unit;;
  88. val sum : btree -> int;;
  89. val map : (int -> int) -> btree -> btree;;
  90. val fold_left: (int -> int -> int) -> int -> btree -> int;;
  91. val mount: int -> btree -> btree -> btree;;
  92.  
  93.  
  94. # Makefile tree
  95. all: pr_tree
  96. tree.cmi: tree.mli
  97.     ocamlc -c tree.mli
  98. tree.cmo: tree.ml tree.cmi
  99.     ocamlc -c tree.ml
  100. pr_tree: tree.cmi tree.cmo
  101.     ocamlc -o tree tree.cmo pr_tree.ml
  102. clean:
  103.     rm -r *.cmo *cmi tree
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement