1. Michael O'Neill
  2. csci-350-lab1

Commits

Michael O'Neill  committed e1d7e68

Add comments to everything.

  • Participants
  • Parent commits a5b763b
  • Branches default

Comments (0)

Files changed (3)

File ack.ml

View file
  • Ignore whitespace
 print_string("Enter N: ");;
 let n = read_int();;
 
+(* The Ackermann function takes two parameters, m and n *)
 let rec ack = function
+   (* Ensure m and n are non-negative *)
    | m,n when m < 0 || n < 0 -> invalid_arg "Ackermann's function is only defined over the non-negative integers"
+   (* If m = 0 and n is anything *)
    | 0,n -> n+1
+   (* If m is anything and n = 0 *)
    | m,0 -> ack (m-1,1)
+   (* Any other values for m and n *)
    | m,n -> ack (m-1,ack (m,n-1))
 ;;
 

File fib.ml

View file
  • Ignore whitespace
 
 let limit = read_int();;
 
+(* The Fibonacci sequence is generated by the sum of the previous two numbers *)
 let rec fib = function
+   (* If n < 0 throw an error *)
    | n when n < 0 -> invalid_arg "N must be a positive integer"
+   (* The 0th term is defined as 0 *)
    | 0 -> 0
+   (* The 1st term is defined as 1 *)
    | 1 -> 1
+   (* Recurse to find previous values *)
    | n -> fib(n - 1) + fib(n - 2)
 ;;
 

File splitter.ml

View file
  • Ignore whitespace
 print_endline("Splits a list into two based on divisibility by 2.");;
 
+(* File reader takes an input channel and a list to read into.
+ * Contents within list are in reverse order relative to file. *)
 let rec fr ic l =
    try
       fr ic l@[(int_of_string (input_line ic))]
       End_of_file -> l
 ;;
 
+(* Takes an input list and two output lists (one even and one odd). *)
 let rec splitter ll e o = 
    match ll with 
+   (* When the input list is empty, return the output lists *)
      [] -> e,o
+   (* If the list isn't empty, check divisibility by 2, add to the appropriate
+    * list, and recurse with the modified output lists. *)
    | h::t -> if (h mod 2) = 0 then (splitter t (h::e) o) else (splitter t e (h::o))
 ;;
 
+(* Create an input channel. *)
 let ic = open_in "input.dat" in
+(* Read into a list. *)
 let ilist = fr ic [] in 
+(* Call splitter with empty lists; they'll be populated as splitter runs. *)
 let e,o = splitter ilist [] [] in
 
 print_endline ("Input: " ^ String.concat " " (List.map string_of_int ilist));