(* arra1.ml *) let main() = print_string("Enter the number of data: "); let n = read_int() in let data = Array.create n 0 in Printf.printf "Enter %d int data:\n" n; for i = 0 to n-1 do data.(i) <- read_int() done; print_string("Input data is:\n"); for i = 0 to n-1 do Printf.printf "%d " data.(i) done; print_newline();; (* arra1.ml *) let rec print_list l = match l with [] -> () | h::t -> Printf.printf "%d " h ; print_list t;; let main() = print_string("Enter the number of data: "); let n = read_int() and l = ref [] in Printf.printf "Enter %d int data:\n" n; for i = 1 to n do let data = read_int() in l := data :: !l done; print_list !l; print_newline();; (* cTofO.ml *) let cTof c = c*.9.0/.5.0+.32.0;; (* cTofO1.ml *) let main() = print_string("Enter the Celsius value: "); let c = read_float() in let f = c *. 9.0 /. 5.0 +. 32.0 in Printf.printf "%f C = %f F\n" c f (* condExp.ml *) let main() = print_string("Enter three integers: "); let d1 = read_int() and d2 = read_int() and d3 = read_int() in Printf.printf "Largest(%d, %d, %d) = " d1 d2 d3; let l = ref d1 in (* reference *) if d2 > !l then l := d2; (* indirection *) if d3 > !l then l := d3; Printf.printf "%d\n" !l;; (* fact1.ml *) let main() = print_string("Enter a non -ve integer: "); let n = read_int() and fact = ref 1 in for i = 1 to n do fact := !fact*i done; Printf.printf "%d! = %d\n" n !fact;; (* fact1.ml *) let main() = print_string("Enter a non -ve integer: "); let n = read_int() and fact = ref 1 and i = ref 1 in while !i <= n do fact := !fact * !i; i := !i + 1 done; Printf.printf "%d! = %d\n" n !fact;; (* fact2.ml *) let rec fact n = if n = 0 then 1 else n*(fact (n-1));; let main() = print_string("Enter a +ve integer: "); let n = read_int() in Printf.printf "%d! = %d\n" n (fact n) ;; print_string("The first OCAML program\n");; (* insertionSort1.ml *) let insertionSort d n = for i = 0 to n-1 do let temp = d.(i) and j = ref i in while !j > 0 && d.(!j - 1) > temp do d.(!j) <- d.(!j - 1); j := !j - 1 done; d.(!j) <- temp done;; let main() = print_string("Enter the number of data: "); let n = read_int() in let data = Array.create n 0 in Printf.printf "Enter %d int data:\n" n; for i = 0 to n-1 do data.(i) <- read_int() done; print_string("Input data is:\n"); for i = 0 to n-1 do Printf.printf "%d " data.(i) done; print_newline(); insertionSort data n; print_string("Sorted data is:\n"); for i = 0 to n-1 do Printf.printf "%d " data.(i) done; print_newline();; module MyStack = struct type 'a stack = 'a list let create = [] let push stack data = data::stack exception Empty_Stack let pop stack = match stack with [] -> raise Empty_Stack | top::tail -> tail let top stack = match stack with [] -> raise Empty_Stack | top::tail -> top end;; (* testStack.ml is the test program for stack.ml * #use "stack.ml";; * #use "testStack.ml";; *) let s = MyStack.create ;; s;; let s1 = MyStack.push s 20;; s;; s1;; let s2 = MyStack.push s1 40;; s1;; s2;; let s2 = MyStack.push s2 10;; s2;; let s3 = MyStack.pop s2;; s2;; s3;; MyStack1.top s2;; let s3 = MyStack.pop s3;; let s3 = MyStack.pop s3;; let s3 = MyStack.pop s3;;