diff --git a/lab01/expo.ml b/labs/01/expo.ml similarity index 100% rename from lab01/expo.ml rename to labs/01/expo.ml diff --git a/lab01/lab1.ml b/labs/01/lab1.ml similarity index 100% rename from lab01/lab1.ml rename to labs/01/lab1.ml diff --git a/lab02/lab2.ml b/labs/02/lab2.ml similarity index 100% rename from lab02/lab2.ml rename to labs/02/lab2.ml diff --git a/lab03/kvtree.ml b/labs/03/kvtree.ml similarity index 100% rename from lab03/kvtree.ml rename to labs/03/kvtree.ml diff --git a/lab03/part1.ml b/labs/03/part1.ml similarity index 100% rename from lab03/part1.ml rename to labs/03/part1.ml diff --git a/labs/04/_build/_digests b/labs/04/_build/_digests new file mode 100644 index 0000000..bc54875 --- /dev/null +++ b/labs/04/_build/_digests @@ -0,0 +1,5 @@ +"Rule: ocaml dependencies ml (%=records )": "\168\187\158\232\128I\157\015\216j\244\004\243\234\011\217" +"Rule: ocaml: ml & cmi -> cmx & o (%=records )": "5\144\205\021B\003\025\204\145\159\255\166t\136\140^" +"Resource: /home/flami/Projects/comp3958/lab04/records.ml": "\133\242qUUy2\188\b\134=\163c9\2142" +"Rule: ocaml: ml -> cmo & cmi (%=records )": "\026zi!\193g\255\238<\162\135s\021\142\001\012" +"Rule: ocaml: cmx* & o* -> native (%=records )": "C\176\141F\181\127\251\240\211V\156\t\167\208\129\177" diff --git a/labs/04/_build/_log b/labs/04/_build/_log new file mode 100644 index 0000000..4f9a364 --- /dev/null +++ b/labs/04/_build/_log @@ -0,0 +1,12 @@ +### Starting build. +# Target: /home/flami/.opam/default/bin/ocamlc.opt -config, tags: { } +/home/flami/.opam/default/bin/ocamlc.opt -config +# Target: records.ml.depends, tags: { extension:ml, file:records.ml, ocaml, ocamldep, quiet } +/home/flami/.opam/default/bin/ocamldep.opt -modules records.ml > records.ml.depends +# Target: records.cmo, tags: { byte, compile, extension:cmo, extension:ml, file:records.cmo, file:records.ml, implem, ocaml, quiet } +/home/flami/.opam/default/bin/ocamlc.opt -c -o records.cmo records.ml +# Target: records.cmx, tags: { compile, extension:cmx, extension:ml, file:records.cmx, file:records.ml, implem, native, ocaml, quiet } +/home/flami/.opam/default/bin/ocamlopt.opt -c -o records.cmx records.ml +# Target: records.native, tags: { dont_link_with, extension:native, file:records.native, link, native, ocaml, program, quiet } +/home/flami/.opam/default/bin/ocamlopt.opt records.cmx -o records.native +# Compilation successful. diff --git a/labs/04/_build/records.cmx b/labs/04/_build/records.cmx new file mode 100644 index 0000000..8fcc2a8 Binary files /dev/null and b/labs/04/_build/records.cmx differ diff --git a/labs/04/_build/records.ml b/labs/04/_build/records.ml new file mode 100644 index 0000000..5a7aa97 --- /dev/null +++ b/labs/04/_build/records.ml @@ -0,0 +1,143 @@ +(** [record] of a person with a first name, last name, and score *) +type record = {firstname: string; lastname: string; score: int};; + +(** [print_records r] takes a list of records [r] and prints + * each record neatly to the console *) +let rec print_records r = + match r with + | [] -> () + | x :: xs -> Printf.printf "%3d %s %s\n" x.score x.lastname x.firstname; print_records xs;; + +(** [compare_records r1 r2] compares records [r1] and [r2] based + * on score, then by lastname, then by firstname. + *) +let compare_records r1 r2 = + compare + (r2.score, r2.lastname, r2.firstname) + (r1.score, r1.lastname, r1.firstname);; + +(** [records_insert x l] inserts a record element [x] into + * list of records [l] + * Requires: [l] is in ascending order. *) +let rec records_insert x l = + match l with + | y :: ys when compare_records x y > 0 -> y :: records_insert x ys + | _ -> x :: l;; + +(**/**) +let test_records_insert () = + assert (records_insert + {firstname = "a"; lastname = "b"; score = 1} [] = + [{firstname = "a"; lastname = "b"; score = 1}] + ); + assert (records_insert + {firstname = "a"; lastname = "b"; score = 1} + [{firstname = "c"; lastname = "d"; score = 2}] = + [ + {firstname = "c"; lastname = "d"; score = 2}; + {firstname = "a"; lastname = "b"; score = 1} + ]); + assert (records_insert + {firstname = "e"; lastname = "f"; score = 2} + [ + {firstname = "c"; lastname = "d"; score = 3}; + {firstname = "a"; lastname = "b"; score = 1} + ] = + [ + {firstname = "c"; lastname = "d"; score = 3}; + {firstname = "e"; lastname = "f"; score = 2}; + {firstname = "a"; lastname = "b"; score = 1} + ]);; +(**/**) + +(** [records_insertion_sort l] sorts list of records [l] in ascending order *) +let rec sort_records l = + match l with + | [] -> [] + | x :: xs -> records_insert x (sort_records xs);; + +(**/**) +let test_sort_records () = + assert (sort_records [] = []); + assert (sort_records + [{firstname = "a"; lastname = "b"; score = 1}] = + [{firstname = "a"; lastname = "b"; score = 1}]); + + assert (sort_records [ + {firstname = "c"; lastname = "d"; score = 3}; + {firstname = "a"; lastname = "b"; score = 1}; + {firstname = "e"; lastname = "f"; score = 2}; + ] = [ + {firstname = "c"; lastname = "d"; score = 3}; + {firstname = "e"; lastname = "f"; score = 2}; + {firstname = "a"; lastname = "b"; score = 1}; + ]);; +(**/**) + +(** [parse l] takes in a line [l] and parses the line to extract + * firstname, lastname, and score, returning Some record if successful + * or None if the input line [l] contains bad data *) +let rec parse l = + try + Scanf.sscanf l " %s %s %s" (fun f l s -> + match int_of_string_opt s with + | Some v when v >= 0 && v <= 100 -> Some {firstname = f; lastname = l; score = v} + | _ -> None + ) + with + | Scanf.Scan_failure _ -> None;; + +(**/**) +let test_parse () = + assert (parse "" = None); + assert (parse "Bart simpson 5abc" = None); + assert (parse "Lisa simpson 130" = None); + assert (parse "Homer Simpson -5" = None); + assert (parse "Homer Simpson 5" = + Some {firstname = "Homer"; lastname = "Simpson"; score = 5}); + assert (parse "Homer Simpson 5 blah blah blah" = + Some {firstname = "Homer"; lastname = "Simpson"; score = 5});; +(**/**) + +(** [read_file acc ic] takes in an accumulator [acc] and an input channel [ic] + * and oterates over it input channel [ic] to parse each line and store the + * parsed result into the accumulator [acc] *) +let rec read_file acc ic = + try + match parse @@ input_line ic with + | None -> read_file acc ic + | Some v -> read_file (v :: acc) ic + with + | End_of_file -> close_in ic; acc;; + +(**/**) +let test_read_file () = + assert (read_file [] @@ open_in "data.txt" = [ + {firstname = "gary"; lastname = "chalmers"; score = 5}; + {firstname = "waylon"; lastname = "smithers"; score = 100}; + {firstname = "homer"; lastname = "simpson"; score = 25}; + ]); + assert (read_file [] @@ open_in "data1.txt" = [ + {firstname = "gary"; lastname = "chalmers"; score = 5}; + {firstname = "waylon"; lastname = "smithers"; score = 100}; + {firstname = "ned"; lastname = "flanders"; score = 12}; + {firstname = "homer"; lastname = "simpson"; score = 25}; + ]); + assert (read_file [] @@ open_in "data2.txt" = []);; +(**/**) + +(**/**) +let run_all_tests () = + test_parse(); + test_read_file(); + test_records_insert(); + test_sort_records();; +(**/**) + +(** main program entry from cli *) +let () = + if Array.length Sys.argv = 1 then + Printf.printf "%s: expects 1 file input.\nUsage: \"%s \"\n" Sys.argv.(0) Sys.argv.(0) + else + let ic = open_in Sys.argv.(1) in + print_records @@ sort_records @@ read_file [] ic;; diff --git a/labs/04/_build/records.ml.depends b/labs/04/_build/records.ml.depends new file mode 100644 index 0000000..b0ac031 --- /dev/null +++ b/labs/04/_build/records.ml.depends @@ -0,0 +1 @@ +records.ml: Array Printf Scanf Sys diff --git a/labs/04/_build/records.o b/labs/04/_build/records.o new file mode 100644 index 0000000..371a671 Binary files /dev/null and b/labs/04/_build/records.o differ diff --git a/lab04/data.txt b/labs/04/data.txt similarity index 100% rename from lab04/data.txt rename to labs/04/data.txt diff --git a/lab04/data1.txt b/labs/04/data1.txt similarity index 100% rename from lab04/data1.txt rename to labs/04/data1.txt diff --git a/lab04/data2.txt b/labs/04/data2.txt similarity index 100% rename from lab04/data2.txt rename to labs/04/data2.txt diff --git a/lab04/records.ml b/labs/04/records.ml similarity index 100% rename from lab04/records.ml rename to labs/04/records.ml diff --git a/lab05/kvtree/kvtree.ml b/labs/05/kvtree/kvtree.ml similarity index 97% rename from lab05/kvtree/kvtree.ml rename to labs/05/kvtree/kvtree.ml index a1e6ea5..aac971f 100644 --- a/lab05/kvtree/kvtree.ml +++ b/labs/05/kvtree/kvtree.ml @@ -82,14 +82,14 @@ module Make(Ord: OrderedType) = struct match t with | L -> L | N (k', v, l, r) when Ord.compare k k' < 0 -> - N (k', v, delete k l, r) + N (k', v, delete k l, r) | N (k', v, l, r) when Ord.compare k k' > 0 -> - N (k', v, l, delete k r) + N (k', v, l, delete k r) | N (_, _, l, L) -> l | N (_, _, L, r) -> r | N (_, _, l, r) -> let (ks, vs) = largest l in - N (ks, vs, delete ks l, r);; + N (ks, vs, delete ks l, r);; (** [of_list l] takes a list of pairs [l] and creates a * kvtree in the order specefied by the comparator [] to compare diff --git a/lab05/kvtree/kvtree.mli b/labs/05/kvtree/kvtree.mli similarity index 100% rename from lab05/kvtree/kvtree.mli rename to labs/05/kvtree/kvtree.mli diff --git a/lab05/kvtree/tests.ml b/labs/05/kvtree/tests.ml similarity index 100% rename from lab05/kvtree/tests.ml rename to labs/05/kvtree/tests.ml diff --git a/lecture01/notes.ml b/lectures/01/notes.ml similarity index 100% rename from lecture01/notes.ml rename to lectures/01/notes.ml diff --git a/lecture01/tinker.ml b/lectures/01/tinker.ml similarity index 100% rename from lecture01/tinker.ml rename to lectures/01/tinker.ml diff --git a/lecture02/html/Length.html b/lectures/02/html/Length.html similarity index 100% rename from lecture02/html/Length.html rename to lectures/02/html/Length.html diff --git a/lecture02/html/index.html b/lectures/02/html/index.html similarity index 100% rename from lecture02/html/index.html rename to lectures/02/html/index.html diff --git a/lecture02/html/index_attributes.html b/lectures/02/html/index_attributes.html similarity index 100% rename from lecture02/html/index_attributes.html rename to lectures/02/html/index_attributes.html diff --git a/lecture02/html/index_class_types.html b/lectures/02/html/index_class_types.html similarity index 100% rename from lecture02/html/index_class_types.html rename to lectures/02/html/index_class_types.html diff --git a/lecture02/html/index_classes.html b/lectures/02/html/index_classes.html similarity index 100% rename from lecture02/html/index_classes.html rename to lectures/02/html/index_classes.html diff --git a/lecture02/html/index_exceptions.html b/lectures/02/html/index_exceptions.html similarity index 100% rename from lecture02/html/index_exceptions.html rename to lectures/02/html/index_exceptions.html diff --git a/lecture02/html/index_extensions.html b/lectures/02/html/index_extensions.html similarity index 100% rename from lecture02/html/index_extensions.html rename to lectures/02/html/index_extensions.html diff --git a/lecture02/html/index_methods.html b/lectures/02/html/index_methods.html similarity index 100% rename from lecture02/html/index_methods.html rename to lectures/02/html/index_methods.html diff --git a/lecture02/html/index_module_types.html b/lectures/02/html/index_module_types.html similarity index 100% rename from lecture02/html/index_module_types.html rename to lectures/02/html/index_module_types.html diff --git a/lecture02/html/index_modules.html b/lectures/02/html/index_modules.html similarity index 100% rename from lecture02/html/index_modules.html rename to lectures/02/html/index_modules.html diff --git a/lecture02/html/index_types.html b/lectures/02/html/index_types.html similarity index 100% rename from lecture02/html/index_types.html rename to lectures/02/html/index_types.html diff --git a/lecture02/html/index_values.html b/lectures/02/html/index_values.html similarity index 100% rename from lecture02/html/index_values.html rename to lectures/02/html/index_values.html diff --git a/lecture02/html/style.css b/lectures/02/html/style.css similarity index 100% rename from lecture02/html/style.css rename to lectures/02/html/style.css diff --git a/lecture02/html/type_Length.html b/lectures/02/html/type_Length.html similarity index 100% rename from lecture02/html/type_Length.html rename to lectures/02/html/type_Length.html diff --git a/lecture02/lists.ml b/lectures/02/lists.ml similarity index 100% rename from lecture02/lists.ml rename to lectures/02/lists.ml diff --git a/lecture02/notes02.ml b/lectures/02/notes02.ml similarity index 100% rename from lecture02/notes02.ml rename to lectures/02/notes02.ml diff --git a/lecture03/notes03.ml b/lectures/03/notes03.ml similarity index 100% rename from lecture03/notes03.ml rename to lectures/03/notes03.ml diff --git a/lecture04/bstree.ml b/lectures/04/bstree.ml similarity index 100% rename from lecture04/bstree.ml rename to lectures/04/bstree.ml diff --git a/lecture04/bstree.mli b/lectures/04/bstree.mli similarity index 100% rename from lecture04/bstree.mli rename to lectures/04/bstree.mli diff --git a/lecture04/notes.ml b/lectures/04/notes.ml similarity index 100% rename from lecture04/notes.ml rename to lectures/04/notes.ml diff --git a/lectures/05/_build/_digests b/lectures/05/_build/_digests new file mode 100644 index 0000000..de7371b --- /dev/null +++ b/lectures/05/_build/_digests @@ -0,0 +1,25 @@ +"Resource: /home/flami/Projects/comp3958/lecture05/number_lines.ml": "n\239\025\\<\011\028#\000A>\249\204\182\170r" +"Rule: ocaml dependencies ml (%=cat )": "\210V\221`\251\191B\198\232\184\146#\164\015\176," +"Rule: ocaml: cmx* & o* -> native (%=number_file_lines )": "R|\021U[G\226zs:\214\154\1910a\177" +"Rule: ocaml: ml & cmi -> cmx & o (%=cat )": "\025\244\2354\235@{lBk\211\031$\237><" +"Rule: ocaml: cmx* & o* -> native (%=number_lines )": "\180K\170\211^n:g\196\220\030 \233\127*\149" +"Rule: ocaml: cmx* & o* -> native (%=echo )": "\196\206\211\186\185&f\154\217p\215\195\248p\1485" +"Resource: /home/flami/Projects/comp3958/lecture05/echo.ml": "h\001\167\215xe\171\150p\151\004p>\004%\181" +"Rule: ocaml dependencies ml (%=sum_integers )": "\207\140\220\182.\186\127\200\228\226\152\002dAJ\134" +"Rule: ocaml dependencies ml (%=echo )": "\237\219\161\187\134\217\180\030\148\240\0166\176W\2222" +"Resource: /home/flami/Projects/comp3958/lecture05/sum_integers.ml": "#b[\196\162u!\202\205~\135\239\025i\195\215" +"Rule: ocaml: ml -> cmo & cmi (%=sum_integers )": "\015x\165\015\217\024\189(\143ud\232#\158Um" +"Rule: ocaml: cmx* & o* -> native (%=sum_integers )": "q.\011\170\190\228da(\218\172\231\192\136\236+" +"Rule: ocaml: ml -> cmo & cmi (%=cat )": "\248U3\213\015A\001\180 \023\180>\223}\142\188" +"Rule: ocaml: ml -> cmo & cmi (%=number_file_lines )": "\218\210>\023\150\016u\174\235\179&6\234\147\248\016" +"Rule: ocaml: ml & cmi -> cmx & o (%=echo )": "\1770\n\142p\235j\205HX\163\208\005\133n\t" +"Rule: ocaml: ml & cmi -> cmx & o (%=sum_integers )": "\024\022\151\178THu\229\251\017\175G\230\179rD" +"Rule: ocaml dependencies ml (%=number_lines )": "\023O8\158;\213,z\t`\236\204\250\168\140W" +"Resource: /home/flami/Projects/comp3958/lecture05/number_file_lines.ml": ":\233\133ai\245 \000\199f8\022\140<\199\174" +"Resource: /home/flami/Projects/comp3958/lecture05/cat.ml": "^\135\t\199Rr\210]\197\127HpY\r\147\170" +"Rule: ocaml: ml -> cmo & cmi (%=echo )": "\224\\C\014\150\004=\191p\132*-\017jj\242" +"Rule: ocaml dependencies ml (%=number_file_lines )": "\142\151\154\251\184\227\201\196\243\150\003\242\017\222\229X" +"Rule: ocaml: ml & cmi -> cmx & o (%=number_lines )": "l\154\251\180\134\026\206s\139\030!A\155\t\198\161" +"Rule: ocaml: ml -> cmo & cmi (%=number_lines )": "\029\1849 z\004\n\025\253\155]\017\244\252\141\129" +"Rule: ocaml: cmx* & o* -> native (%=cat )": "j\019\018\218,\202\215\161 &5SE\134\223/" +"Rule: ocaml: ml & cmi -> cmx & o (%=number_file_lines )": "|\132>\000@\222\195L,\1271\1424\141wJ" diff --git a/lectures/05/_build/_log b/lectures/05/_build/_log new file mode 100644 index 0000000..57df0fc --- /dev/null +++ b/lectures/05/_build/_log @@ -0,0 +1,12 @@ +### Starting build. +# Target: /home/flami/.opam/default/bin/ocamlc.opt -config, tags: { } +/home/flami/.opam/default/bin/ocamlc.opt -config +# Target: number_lines.ml.depends, tags: { extension:ml, file:number_lines.ml, ocaml, ocamldep, quiet } +/home/flami/.opam/default/bin/ocamldep.opt -modules number_lines.ml > number_lines.ml.depends +# Target: number_lines.cmo, tags: { byte, compile, extension:cmo, extension:ml, file:number_lines.cmo, file:number_lines.ml, implem, ocaml, quiet } +/home/flami/.opam/default/bin/ocamlc.opt -c -o number_lines.cmo number_lines.ml +# Target: number_lines.cmx, tags: { compile, extension:cmx, extension:ml, file:number_lines.cmx, file:number_lines.ml, implem, native, ocaml, quiet } +/home/flami/.opam/default/bin/ocamlopt.opt -c -o number_lines.cmx number_lines.ml +# Target: number_lines.native, tags: { dont_link_with, extension:native, file:number_lines.native, link, native, ocaml, program, quiet } +/home/flami/.opam/default/bin/ocamlopt.opt number_lines.cmx -o number_lines.native +# Compilation successful. diff --git a/lectures/05/_build/cat.cmx b/lectures/05/_build/cat.cmx new file mode 100644 index 0000000..964d9cd Binary files /dev/null and b/lectures/05/_build/cat.cmx differ diff --git a/lectures/05/_build/cat.ml b/lectures/05/_build/cat.ml new file mode 100644 index 0000000..0ca6d82 --- /dev/null +++ b/lectures/05/_build/cat.ml @@ -0,0 +1,25 @@ +(* display content of file read via stdin *) +let rec cat () = + try + let line = read_line () in + print_endline line; + cat() + with + | _ -> ();; (* EOF return unit *) + +(* similar to main function *) +let () = + let rec aux () = + try + print_endline @@ read_line (); + aux () + with + | _ -> () + in + aux() + +(**** build and test ****) +(* + ocamlbuild cat.native + ./cat.native < filename +*) diff --git a/lectures/05/_build/cat.ml.depends b/lectures/05/_build/cat.ml.depends new file mode 100644 index 0000000..832688d --- /dev/null +++ b/lectures/05/_build/cat.ml.depends @@ -0,0 +1 @@ +cat.ml: diff --git a/lectures/05/_build/cat.o b/lectures/05/_build/cat.o new file mode 100644 index 0000000..ef4132e Binary files /dev/null and b/lectures/05/_build/cat.o differ diff --git a/lectures/05/_build/echo.cmx b/lectures/05/_build/echo.cmx new file mode 100644 index 0000000..34c2dfd Binary files /dev/null and b/lectures/05/_build/echo.cmx differ diff --git a/lecture05/echo.ml b/lectures/05/_build/echo.ml similarity index 100% rename from lecture05/echo.ml rename to lectures/05/_build/echo.ml diff --git a/lectures/05/_build/echo.ml.depends b/lectures/05/_build/echo.ml.depends new file mode 100644 index 0000000..741a082 --- /dev/null +++ b/lectures/05/_build/echo.ml.depends @@ -0,0 +1 @@ +echo.ml: Array Printf Sys diff --git a/lectures/05/_build/echo.o b/lectures/05/_build/echo.o new file mode 100644 index 0000000..770a8a3 Binary files /dev/null and b/lectures/05/_build/echo.o differ diff --git a/lectures/05/_build/number_file_lines.cmx b/lectures/05/_build/number_file_lines.cmx new file mode 100644 index 0000000..9911453 Binary files /dev/null and b/lectures/05/_build/number_file_lines.cmx differ diff --git a/lecture05/number_file_lines.ml b/lectures/05/_build/number_file_lines.ml similarity index 100% rename from lecture05/number_file_lines.ml rename to lectures/05/_build/number_file_lines.ml diff --git a/lectures/05/_build/number_file_lines.ml.depends b/lectures/05/_build/number_file_lines.ml.depends new file mode 100644 index 0000000..ae47d2a --- /dev/null +++ b/lectures/05/_build/number_file_lines.ml.depends @@ -0,0 +1 @@ +number_file_lines.ml: Array Printf Sys diff --git a/lectures/05/_build/number_file_lines.o b/lectures/05/_build/number_file_lines.o new file mode 100644 index 0000000..498f5ee Binary files /dev/null and b/lectures/05/_build/number_file_lines.o differ diff --git a/lectures/05/_build/number_lines.cmx b/lectures/05/_build/number_lines.cmx new file mode 100644 index 0000000..b309989 Binary files /dev/null and b/lectures/05/_build/number_lines.cmx differ diff --git a/lecture05/number_lines.ml b/lectures/05/_build/number_lines.ml similarity index 100% rename from lecture05/number_lines.ml rename to lectures/05/_build/number_lines.ml diff --git a/lectures/05/_build/number_lines.ml.depends b/lectures/05/_build/number_lines.ml.depends new file mode 100644 index 0000000..8142677 --- /dev/null +++ b/lectures/05/_build/number_lines.ml.depends @@ -0,0 +1 @@ +number_lines.ml: Printf diff --git a/lectures/05/_build/number_lines.o b/lectures/05/_build/number_lines.o new file mode 100644 index 0000000..f825a76 Binary files /dev/null and b/lectures/05/_build/number_lines.o differ diff --git a/lectures/05/_build/sum_integers.cmx b/lectures/05/_build/sum_integers.cmx new file mode 100644 index 0000000..f564928 Binary files /dev/null and b/lectures/05/_build/sum_integers.cmx differ diff --git a/lecture05/sum_integers.ml b/lectures/05/_build/sum_integers.ml similarity index 100% rename from lecture05/sum_integers.ml rename to lectures/05/_build/sum_integers.ml diff --git a/lectures/05/_build/sum_integers.ml.depends b/lectures/05/_build/sum_integers.ml.depends new file mode 100644 index 0000000..55eb6ca --- /dev/null +++ b/lectures/05/_build/sum_integers.ml.depends @@ -0,0 +1 @@ +sum_integers.ml: Printf Scanf diff --git a/lectures/05/_build/sum_integers.o b/lectures/05/_build/sum_integers.o new file mode 100644 index 0000000..f8a1a49 Binary files /dev/null and b/lectures/05/_build/sum_integers.o differ diff --git a/lecture05/cat.ml b/lectures/05/cat.ml similarity index 100% rename from lecture05/cat.ml rename to lectures/05/cat.ml diff --git a/lecture05/data.txt b/lectures/05/data.txt similarity index 100% rename from lecture05/data.txt rename to lectures/05/data.txt diff --git a/lectures/05/echo.ml b/lectures/05/echo.ml new file mode 100644 index 0000000..b2774fd --- /dev/null +++ b/lectures/05/echo.ml @@ -0,0 +1,6 @@ +let () = + let len = Array.length Sys.argv - 1 in + for i = 1 to len do + Printf.printf (if i <> len then "%s " else "%s\n") Sys.argv.(i) + done;; + diff --git a/lecture05/notes.ml b/lectures/05/notes.ml similarity index 99% rename from lecture05/notes.ml rename to lectures/05/notes.ml index bdf4153..2711f6e 100644 --- a/lecture05/notes.ml +++ b/lectures/05/notes.ml @@ -158,4 +158,3 @@ Scanf.scanf " %d" (fun x -> x) (* open_in "filename";; *) let ic = open_in "output";; input_line ic;; - diff --git a/lectures/05/number_file_lines.ml b/lectures/05/number_file_lines.ml new file mode 100644 index 0000000..477043c --- /dev/null +++ b/lectures/05/number_file_lines.ml @@ -0,0 +1,16 @@ +let rec number acc ic = + try + Printf.printf "%5d| %s\n" acc @@ input_line ic; + number (acc + 1) ic + with + | _ -> if ic <> stdin then close_in ic else () + +let () = + if Array.length Sys.argv = 1 then + number 1 stdin + else + try + number 1 @@ open_in Sys.argv.(1) + with + | Sys_error s -> Printf.eprintf "%s\n" s;; + diff --git a/lectures/05/number_lines.ml b/lectures/05/number_lines.ml new file mode 100644 index 0000000..4028e41 --- /dev/null +++ b/lectures/05/number_lines.ml @@ -0,0 +1,9 @@ +let () = + let rec aux acc = + try + Printf.printf "%5d| %s\n" acc @@ read_line (); + aux (acc + 1) + with + | _ -> () + in + aux 1;; diff --git a/lecture05/output b/lectures/05/output similarity index 100% rename from lecture05/output rename to lectures/05/output diff --git a/lecture05/ref.ml b/lectures/05/ref.ml similarity index 100% rename from lecture05/ref.ml rename to lectures/05/ref.ml diff --git a/lectures/05/sum_integers.ml b/lectures/05/sum_integers.ml new file mode 100644 index 0000000..d9b5fac --- /dev/null +++ b/lectures/05/sum_integers.ml @@ -0,0 +1,12 @@ +let () = + let rec aux sum = + try + aux @@ Scanf.scanf " %d" (fun x -> sum + x) + with + | Scanf.Scan_failure _ -> + Scanf.scanf " %s" (fun _ -> ()); + aux sum + | End_of_file -> sum + in + Printf.printf "%d\n" @@ aux 0;; + diff --git a/lectures/06/_build/_digests b/lectures/06/_build/_digests new file mode 100644 index 0000000..a316040 --- /dev/null +++ b/lectures/06/_build/_digests @@ -0,0 +1,3 @@ +"Rule: ocaml dependencies ml (%=two_list_queue )": "\153s\252\225.\205\197\1597\030\142\254Q\213.\135" +"Resource: /home/flami/Projects/comp3958/lecture06/two_list_queue.ml": "\174\189*\255(\166==\251\006\232\150\141\1714\130" +"Rule: ocaml: ml -> cmo & cmi (%=two_list_queue )": "9\165\179\031F'#6\127\149\136<\242y\019t" diff --git a/lectures/06/_build/_log b/lectures/06/_build/_log new file mode 100644 index 0000000..9d0e2de --- /dev/null +++ b/lectures/06/_build/_log @@ -0,0 +1,8 @@ +### Starting build. +# Target: /home/flami/.opam/default/bin/ocamlc.opt -config, tags: { } +/home/flami/.opam/default/bin/ocamlc.opt -config +# Target: two_list_queue.ml.depends, tags: { extension:ml, file:two_list_queue.ml, ocaml, ocamldep, quiet } +/home/flami/.opam/default/bin/ocamldep.opt -modules two_list_queue.ml > two_list_queue.ml.depends +# Target: two_list_queue.cmo, tags: { byte, compile, extension:cmo, extension:ml, file:two_list_queue.cmo, file:two_list_queue.ml, implem, ocaml, quiet } +/home/flami/.opam/default/bin/ocamlc.opt -c -o two_list_queue.cmo two_list_queue.ml +# Compilation successful. diff --git a/lecture06/two_list_queue.ml b/lectures/06/_build/two_list_queue.ml similarity index 100% rename from lecture06/two_list_queue.ml rename to lectures/06/_build/two_list_queue.ml diff --git a/lectures/06/_build/two_list_queue.ml.depends b/lectures/06/_build/two_list_queue.ml.depends new file mode 100644 index 0000000..0d870ba --- /dev/null +++ b/lectures/06/_build/two_list_queue.ml.depends @@ -0,0 +1 @@ +two_list_queue.ml: Fun List diff --git a/lecture06/bstree.ml b/lectures/06/bstree.ml similarity index 100% rename from lecture06/bstree.ml rename to lectures/06/bstree.ml diff --git a/lecture06/bstree.mli b/lectures/06/bstree.mli similarity index 100% rename from lecture06/bstree.mli rename to lectures/06/bstree.mli diff --git a/lecture06/bstree2.ml b/lectures/06/bstree2.ml similarity index 100% rename from lecture06/bstree2.ml rename to lectures/06/bstree2.ml diff --git a/lecture06/functor/_build/_digests b/lectures/06/functor/_build/_digests similarity index 100% rename from lecture06/functor/_build/_digests rename to lectures/06/functor/_build/_digests diff --git a/lecture06/functor/_build/_log b/lectures/06/functor/_build/_log similarity index 100% rename from lecture06/functor/_build/_log rename to lectures/06/functor/_build/_log diff --git a/lecture06/functor/_build/bstree.ml b/lectures/06/functor/_build/bstree.ml similarity index 100% rename from lecture06/functor/_build/bstree.ml rename to lectures/06/functor/_build/bstree.ml diff --git a/lecture06/functor/_build/bstree.ml.depends b/lectures/06/functor/_build/bstree.ml.depends similarity index 100% rename from lecture06/functor/_build/bstree.ml.depends rename to lectures/06/functor/_build/bstree.ml.depends diff --git a/lecture06/functor/_build/bstree.mli b/lectures/06/functor/_build/bstree.mli similarity index 100% rename from lecture06/functor/_build/bstree.mli rename to lectures/06/functor/_build/bstree.mli diff --git a/lecture06/functor/_build/bstree.mli.depends b/lectures/06/functor/_build/bstree.mli.depends similarity index 100% rename from lecture06/functor/_build/bstree.mli.depends rename to lectures/06/functor/_build/bstree.mli.depends diff --git a/lecture06/functor/bstree.ml b/lectures/06/functor/bstree.ml similarity index 100% rename from lecture06/functor/bstree.ml rename to lectures/06/functor/bstree.ml diff --git a/lecture06/functor/bstree.mli b/lectures/06/functor/bstree.mli similarity index 100% rename from lecture06/functor/bstree.mli rename to lectures/06/functor/bstree.mli diff --git a/lecture06/notes.ml b/lectures/06/notes.ml similarity index 100% rename from lecture06/notes.ml rename to lectures/06/notes.ml diff --git a/lecture06/notes.txt b/lectures/06/notes.txt similarity index 100% rename from lecture06/notes.txt rename to lectures/06/notes.txt diff --git a/lecture06/queue.ml b/lectures/06/queue.ml similarity index 100% rename from lecture06/queue.ml rename to lectures/06/queue.ml diff --git a/lectures/06/two_list_queue.ml b/lectures/06/two_list_queue.ml new file mode 100644 index 0000000..48eff0c --- /dev/null +++ b/lectures/06/two_list_queue.ml @@ -0,0 +1,42 @@ +(* queue is empty iff the first list is empty *) +type 'a t = 'a list * 'a list;; + +exception Empty;; + +let empty = ([], []);; + +let is_empty (l, _) = l = [];; + +let enqueue x (l1, l2) = + match l1 with + | [] -> [x], l2 + | _ -> (l1, x :: l2);; + +let dequeue (l1, l2) = + match l1 with + | [] -> raise Empty + | [_] -> (List.rev l2, []) + | _ :: xs -> (xs, l2);; + +let dequeue_opt q = + try + Some (dequeue q) + with + | Empty -> None;; + +let front (l, _) = + match l with + | [] -> raise Empty + | x :: _ -> x;; + +let front_opt (l, _) = + match l with + | [] -> None + | x :: _ -> Some x;; + +let of_list l = + List.fold_left (Fun.flip enqueue) empty l;; + +let to_list (l1, l2) = + l1 @ List.rev l2;; + diff --git a/lectures/07/bstree.ml b/lectures/07/bstree.ml new file mode 100644 index 0000000..83ebf68 --- /dev/null +++ b/lectures/07/bstree.ml @@ -0,0 +1,2 @@ +(* bstree with records *) +type 'a t = L | N of {v: 'a; l: 'a t; r: 'a t};; diff --git a/lectures/07/counter.ml b/lectures/07/counter.ml new file mode 100644 index 0000000..402a668 --- /dev/null +++ b/lectures/07/counter.ml @@ -0,0 +1,7 @@ +let counter = + let c = ref 0 in + (fun () -> incr c; !c);; + +let make_counter init = + let c = ref init in + (fun () -> let x = !c in incr c; x);; diff --git a/lectures/07/frequency.ml b/lectures/07/frequency.ml new file mode 100644 index 0000000..d00826d --- /dev/null +++ b/lectures/07/frequency.ml @@ -0,0 +1,12 @@ +module F = Map.Make(String) + +let rec count map = + let word = Scanf.scanf " %s" (fun w -> w) in + if word = "" then map + else count (F.update word (function + | None -> Some 1 + | Some n -> Some (n + 1)) + map);; + +let () = + List.iter (fun (s, n) -> Printf.printf "%s: %d\n" s n) @@ count F.empty;; diff --git a/lectures/07/leftist.ml b/lectures/07/leftist.ml new file mode 100644 index 0000000..2ac4afa --- /dev/null +++ b/lectures/07/leftist.ml @@ -0,0 +1,46 @@ +(** rank of node = shortest distance to a leaf (empty node) + * hence, rank of node = 1 + min(rnak of left, rank of right) + * leftist tree has 2 properties: + * For ever node + * * leftist: rnak(left) >= rank(right) + * * min-heap: value(parent) <= value(node) + *) + +type 'a t = L | N of int * 'a * 'a t * 'a t;; + +exception Empty;; + +let rank = function + | L -> 0 + | N (rk, _, _, _) -> rk;; + +let empty = L;; + +let is_empty t = t = L;; + +let rec merge t1 t2 = + match t1, t2 with + | t, L | L, t -> t + | N (_, x1, _, _), N (_, x2, _, _) when x2 < x1 -> + merge t2 t1 + | N (_, x, l, r), t -> + let t' = merge r t in + if rank t' > rank l then N (1 + rank l, x, t', l) + else N (1 + rank t', x, l, t');; + +let insert x t = + merge t (N (1, x, L, L));; + +let of_list l = List.fold_left (Fun.flip insert) L l;; + +let get_min = function + | L -> raise Empty + | N (_, x, _, _) -> x;; + +let delete_min = function + | L -> L + | N (_, _, l, r) -> merge l r;; + +let rec to_list t = + if is_empty t then [] + else get_min t :: to_list (delete_min t);; diff --git a/lectures/07/modules.ml b/lectures/07/modules.ml new file mode 100644 index 0000000..6ffc1c9 --- /dev/null +++ b/lectures/07/modules.ml @@ -0,0 +1,47 @@ +(* + - a module can contain other modules as well as module type definitions + - a module type/sig (signature) can not contain modules but can contain other sigs + - for a .ml/.mli pair, + * anything not specified in .mli file is hidden + * everything specified in the .mli file must be defined in the .ml file +*) + +module M = struct + let f x = 2 * x +end;; + +module type S = module type of M;; + +module M = struct + let f x = 2 * x;; + let g x = 3 * x;; + module N = struct + type t = int + end +end;; + +(* S signature is the same as M if loaded in utop *) +module type S = module type of M;; + +(***** EXTENDED MODULES *****) + +(* lets extend List module *) +module ListExt = struct + include List;; (* include module to extend *) + let to_array l = Array.of_list l;; +end;; + +ListExt.to_array [3;2;7;6;8];; + +module F = Map.Make(String);; + +(* returns abstract type *) +let m = F.empty |> F.add "hello" 1 |> F.add "hello" 2;; +F.to_list m;; + +let incr key m = + F.update key (function | None -> Some 1 | Some x -> Some (x + 1)) + m;; + +let m = incr "hello" m;; +let m = incr "world" m;; diff --git a/lectures/07/sieve.ml b/lectures/07/sieve.ml new file mode 100644 index 0000000..0c409b0 --- /dev/null +++ b/lectures/07/sieve.ml @@ -0,0 +1,16 @@ +let isqrt n = n |> float_of_int |> sqrt |> int_of_float;; + +let sieve n = + let is_prime = Array.make n true in + for i = 2 to isqrt n do + if is_prime.(i) then + let j = ref i in + while i * !j < n do + is_prime.(i * !j) <- false; + incr j + done + done; + is_prime.(0) <- false; + is_prime.(1) <- false; + is_prime |> Array.to_list |> List.mapi (fun i b -> (i, b)) |> + List.filter_map (fun (i, b) -> if b then Some i else None);;