SHARE
TWEET

Untitled

a guest Sep 20th, 2019 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (* Recursion *)
  2.  
  3. let rec pow (a : int ) (n : int) : int =
  4.     match n with
  5.         0 -> 1
  6.     |   1 -> a
  7.     |   other -> a * pow a (n - 1)
  8. ;;
  9.  
  10. let rec float_pow (a : float) (n : int) : float =
  11.     if ((a < 0.0) && (n mod 2 != 0)) then
  12.         -.float_pow (abs_float a) n
  13.     else if((a < 0.0) && (n mod 2 == 0)) then
  14.         float_pow (abs_float a) n
  15.     else if (n = 0) then
  16.         1.0
  17.     else if (n = 1) then
  18.         a
  19.     else
  20.         a *. float_pow a (n - 1)
  21. ;;
  22.  
  23. (* When testing negative a, I did float_pow (-.a) n *)
  24.  
  25. let rec print_list = function (* Function taken from Stack Overflow to help print lists *)
  26.     [] -> ()
  27.     | head::tail -> print_int head ; print_string " " ; print_list tail ;
  28. ;;
  29.  
  30.  
  31. let rec reverse list =
  32.     reverse_helper [] list
  33. and reverse_helper acc = function
  34.     | [] -> acc
  35.     | head::tail -> reverse_helper (head::acc) tail
  36.     (* (head::acc) puts the first elemen of the current list at the "back" until the last element of the list is put first *)
  37. ;;
  38.  
  39. let rec compress = function
  40.     | [] -> []
  41.     | head::(next:: _ as tail) -> compress_helper head next tail
  42.     | (_:: _ as tail) -> tail
  43. and compress_helper head next tail =
  44.     if (head = next) then
  45.         compress tail
  46.     else
  47.         head::compress tail
  48. ;;
  49.  
  50. let rec cluster list =  
  51.     cluster_helper [] [] list
  52. and cluster_helper biglist smalllist = function
  53.     | [] -> []    (* Can only be reached if original list is empty *)
  54.     | [singleton] -> (singleton :: biglist) :: smalllist
  55.     | head :: (next :: _ as tail) -> if head = next then cluster_helper (head :: biglist) smalllist tail else cluster_helper [] ((head :: biglist) :: smalllist) tail
  56. ;;
  57.  
  58.     (* NOTE: Can also use accumulator for this function. Check adjacent elements, create new list if they are not the same. *)
  59.  
  60. let slice list i j = function
  61.     | [] -> []
  62.     | _::_ -> []
  63. ;;
  64. (* Couldn't do slice *)
  65.  
  66. (* pow tests *)
  67. print_int(pow 3 10);;
  68. print_string("\n");;
  69. print_int(pow 2 8);;
  70. print_string("\n");;
  71.  
  72. (* float_pow tests *)
  73. print_float(float_pow (-2.2) 2);;
  74. print_string("\n");;
  75. print_float(float_pow (-27.1) 3);;    
  76. print_string("\n");;
  77. print_float(float_pow (27.1) 2);;    
  78. print_string("\n");;
  79. print_float(float_pow 4.0 2);;
  80. print_string("\n");;
  81.  
  82. (* List tests *)
  83. let a_list = [1;2;3;4;5];;
  84. let b_list = [1;2;3;4;5;6;7;8;9;10];;
  85. let uncompressed_list = [10;10;20;30;40;40;40;50];;
  86. let unclustered_list = [1;1;1;2;3;3;1;1];;
  87. print_list a_list;;
  88. let reversed_list = reverse a_list;;
  89. print_string("\n");;
  90. print_list reversed_list;;
  91. print_string("\nUncompressed List: ");;
  92. print_list uncompressed_list;;
  93. let compressed_list = compress uncompressed_list;;
  94. print_string("\nCompressed List: ");;
  95. print_list compressed_list;;
  96. print_string("\n");;
  97. (* let sliced_list = slice b_list 3 2;;
  98. print_list sliced_list;;
  99. print_string("\n");; *)
  100.  
  101. (* Higher-order functions *)
  102.  
  103. let composition f g x =
  104.     f (g x)
  105. ;;
  106.  
  107. let rec equiv_on f g lst : bool =
  108.     match lst with
  109.     | [] -> true
  110.     | head::tail -> (f head = g head) && (equiv_on f g tail); (* Checks if all corresponding indexes from lst are the same with functions f and g applied to them *)
  111. ;;
  112.  
  113. let rec pairwisefilter cmp lst =
  114.     match lst with
  115.     | [] -> []
  116.     | head::next::tail -> cmp head next :: pairwisefilter cmp tail
  117.     | _ as remaining -> remaining
  118. ;;
  119.  
  120.  
  121. let square x = x * x;;
  122. let increment x = x + 1;;
  123. let f i = i * i;;
  124. let g i = 3 * i;;
  125.  
  126. let square_of_increment = composition square increment;;
  127. print_int(square_of_increment 4);;
  128. print_string("\n");;
  129. equiv_on f g [1;2;3];; (* booleans are not printed*)
  130. equiv_on f g [3];; (* booleans are not printed*)
  131.  
  132. let min a b = if a > b then b else a;;
  133. let pairwisefilter_list = pairwisefilter min [14; 11; 20; 25; 10; 11; 12];;
  134. print_list pairwisefilter_list;;
  135. print_string("\n");;
  136.  
  137.  
  138. let polynomial lst x =
  139.     let rec aux lst x =
  140.         match lst with
  141.         | [] -> 0
  142.         | head::tail -> match head with (a, b) -> a * pow x b + aux tail x
  143.         in aux lst x
  144.     ;;
  145.  
  146. let f = polynomial [3, 3; -2, 1; 5, 0];;
  147. print_int(f 3);;
  148.  
  149. (* Data types *)
  150.  
  151. (* Truth Table *)
  152. type bool_expr =
  153.     | Lit of string
  154.     | Not of bool_expr
  155.     | And of bool_expr * bool_expr
  156.     | Or of bool_expr * bool_expr
  157.     ;;
  158.  
  159. let rec evaluate_truth_table a a_val b b_val = function
  160.     | Lit str -> if str = a then a_val else b_val
  161.     | Not expr -> not(evaluate_truth_table a a_val b b_val expr)
  162.     | Or(val_1, val_2) -> evaluate_truth_table a a_val b b_val val_1 || evaluate_truth_table a a_val b b_val val_2
  163.     | And(val_1, val_2) -> evaluate_truth_table a a_val b b_val val_1 && evaluate_truth_table a a_val b b_val val_2
  164.     ;;
  165.  
  166. let truth_table a b bool_expr =
  167.     [(true, true, evaluate_truth_table a true b true bool_expr);
  168.     (true, false, evaluate_truth_table a true b false bool_expr);
  169.     (false, true, evaluate_truth_table a false b true bool_expr);
  170.     (false, false, evaluate_truth_table a false b false bool_expr)]
  171.     ;;
  172.  
  173. let truth_table_test = truth_table "a" "b" (Or(And(Lit("a"), Lit("b")), Not(Lit("a"))));;
  174. (* (bool * bool * bool) list = [(true, true, true); (true, false, false); (false, true, true); (false, false, true)] *)
  175. (* End of Truth Table *)
  176.  
  177.  
  178. type 'a binary_tree =
  179.     | Empty
  180.     | Node of 'a * 'a binary_tree * 'a binary_tree
  181.     ;;
  182.    
  183. let rec tree2str = function
  184.     | Empty -> ""
  185.     | Node(root, child1, child2) -> tree2str_helper root child1 child2
  186. and tree2str_helper root child1 child2 =
  187.     match child1, child2 with
  188.     | (Empty, Empty) -> string_of_int root
  189.     | (_, _) -> string_of_int root ^ "(" ^ tree2str child1 ^ "," ^ tree2str child2 ^ ")"
  190.     ;;
  191.  
  192. let intList : list = [1;3;4;5;6];;
  193. let clusterIntList : list = cluster intList;;
  194. print_list (intList);;
  195.  
  196. let a_tree = Node(1, Node(2, Node(4, Empty, Empty), Node(5, Empty, Empty)),
  197. Node(3, Node(6, Empty, Empty), Node(7, Empty, Empty)));;
  198. print_string("\n");;
  199. let tree2str_test = tree2str a_tree;;
  200. print_string(tree2str_test);;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top