restructure + lecutre 07
This commit is contained in:
96 files changed
+458
-4
No files matched your search
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -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"
|
||||
@@ -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.
|
||||
Binary file not shown.
@@ -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 <filename>\"\n" Sys.argv.(0) Sys.argv.(0)
|
||||
else
|
||||
let ic = open_in Sys.argv.(1) in
|
||||
print_records @@ sort_records @@ read_file [] ic;;
|
||||
@@ -0,0 +1 @@
|
||||
records.ml: Array Printf Scanf Sys
|
||||
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -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
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -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"
|
||||
@@ -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.
|
||||
Binary file not shown.
@@ -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
|
||||
*)
|
||||
@@ -0,0 +1 @@
|
||||
cat.ml:
|
||||
Binary file not shown.
Binary file not shown.
File renamed without changes.
@@ -0,0 +1 @@
|
||||
echo.ml: Array Printf Sys
|
||||
Binary file not shown.
Binary file not shown.
File renamed without changes.
@@ -0,0 +1 @@
|
||||
number_file_lines.ml: Array Printf Sys
|
||||
Binary file not shown.
Binary file not shown.
File renamed without changes.
@@ -0,0 +1 @@
|
||||
number_lines.ml: Printf
|
||||
Binary file not shown.
Binary file not shown.
File renamed without changes.
@@ -0,0 +1 @@
|
||||
sum_integers.ml: Printf Scanf
|
||||
Binary file not shown.
File renamed without changes.
File renamed without changes.
@@ -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;;
|
||||
|
||||
@@ -158,4 +158,3 @@ Scanf.scanf " %d" (fun x -> x)
|
||||
(* open_in "filename";; *)
|
||||
let ic = open_in "output";;
|
||||
input_line ic;;
|
||||
|
||||
@@ -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;;
|
||||
|
||||
@@ -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;;
|
||||
File renamed without changes.
File renamed without changes.
@@ -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;;
|
||||
|
||||
@@ -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"
|
||||
@@ -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.
|
||||
File renamed without changes.
@@ -0,0 +1 @@
|
||||
two_list_queue.ml: Fun List
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -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;;
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
(* bstree with records *)
|
||||
type 'a t = L | N of {v: 'a; l: 'a t; r: 'a t};;
|
||||
@@ -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);;
|
||||
@@ -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;;
|
||||
@@ -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);;
|
||||
@@ -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;;
|
||||
@@ -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);;
|
||||
Reference in new issue
Block a user