restructure + lecutre 07
This commit is contained in:
96 files changed
+458
-4
No files matched your search
@@ -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.
@@ -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;;
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
echo.ml: Array Printf Sys
|
||||
Binary file not shown.
Binary file not shown.
@@ -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 @@
|
||||
number_file_lines.ml: Array Printf Sys
|
||||
Binary file not shown.
Binary file not shown.
@@ -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;;
|
||||
@@ -0,0 +1 @@
|
||||
number_lines.ml: Printf
|
||||
Binary file not shown.
Binary file not shown.
@@ -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 @@
|
||||
sum_integers.ml: Printf Scanf
|
||||
Binary file not shown.
@@ -0,0 +1,26 @@
|
||||
(* 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()
|
||||
(* cat() *)
|
||||
|
||||
(**** build and test ****)
|
||||
(*
|
||||
ocamlbuild cat.native
|
||||
./cat.native < filename
|
||||
*)
|
||||
@@ -0,0 +1,4 @@
|
||||
123 456
|
||||
-123 -456
|
||||
abc
|
||||
25
|
||||
@@ -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;;
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
(**** RECORDS ****)
|
||||
type student = {id: string; name: string; gpa: float};;
|
||||
|
||||
let s1 = {id = "a12345678"; name = "homer simpson"; gpa = 25.5};;
|
||||
let make_student id name gpa = { id = id; name = name; gpa = gpa };;
|
||||
let s2 = make_student "a000000" "monty burns" 99.9;;
|
||||
|
||||
(* defining a function to return the name field of a record *)
|
||||
let name {id = id; name = name; gpa = gpa} = name;;
|
||||
let name {id; name; gpa} = name;;
|
||||
let name {name} = name;;
|
||||
let name s = s.name;;
|
||||
|
||||
name s2;;
|
||||
|
||||
type instructor = {id: string; name: string; salary: float};;
|
||||
let i1 = {id = "a777777"; name = "lisa simpson"; salary = 100000.};;
|
||||
|
||||
(* name cannot be applied to constructor because name was defined before instructor
|
||||
and it deferred from student *)
|
||||
(* name i1;; *)
|
||||
|
||||
let id x = x.id;;
|
||||
|
||||
(* id s1;; <-- this doesnt work because id infers from the latest defined, such as instructor *)
|
||||
id i1;;
|
||||
|
||||
type isntr = Instructor of {id: string; name: string; salary: float};;
|
||||
let i1 = Instructor {id = "a777777"; name = "lisa simpson"; salary = 100000.};;
|
||||
|
||||
let s1 = make_student "a123456678" "homer simpson" 25.5;;
|
||||
let s2 = s1;; (* s2 is its own student it just shares the same data *)
|
||||
let s2 = {s1 with id = "a22222222"; name = "bart simpson"} (* this allows us to copy data and modify selected data *)
|
||||
|
||||
(**** MUTABILITY - you can mark fields as mutable ****)
|
||||
type student = {id: string; name: string; mutable gpa: float};;
|
||||
let make_student id name gpa = { id = id; name = name; gpa = gpa };;
|
||||
let s1 = make_student "a123456678" "homer simpson" 25.5;;
|
||||
let s2 = s1;; (* refer to the same thing *)
|
||||
|
||||
s2;;
|
||||
|
||||
(* update field for s1 *)
|
||||
s1.gpa <- 15.5;
|
||||
|
||||
(* shows changes applied to s1 since s2 refers to the same data *)
|
||||
s2;;
|
||||
|
||||
(**** REFERENCES & DEREFEREMCES ****)
|
||||
let x = ref 1;;
|
||||
!x;; (* deref *)
|
||||
x := 2;; (* update val of references *)
|
||||
x;;
|
||||
!x;;
|
||||
|
||||
let y = x;;
|
||||
y;;
|
||||
x;;
|
||||
incr x;; (* y is the same as x (ref to int) so updating x also shows in y *)
|
||||
x;;
|
||||
y;;
|
||||
|
||||
let x = ref (Some 1);;
|
||||
let y = ref None;;
|
||||
(* val y : '_wek1 option ref = {contents = None} *)
|
||||
(* curremtly the contents of y are weakly polymorphic
|
||||
because it can be any type. the type inference cannot
|
||||
deduce the value of the optional *)
|
||||
y := Some 2;;
|
||||
y;;
|
||||
(* - : int option ref = {contents = Some 1} *)
|
||||
|
||||
(**** ARRAYS - arrays are mutable ****)
|
||||
[|1;2;3;3;4;5|];; (* arrays have vertical bars unlike lists *)
|
||||
let a = [|1;2;3|];;
|
||||
a.(0);; (* get first element *);;
|
||||
a.(1);;
|
||||
a.(2);;
|
||||
(* a.(3);; (* Invalid_argument excetption *) *)
|
||||
|
||||
a.(0) <- -1;;
|
||||
a;;
|
||||
|
||||
(**** FOR / WHILE LOOPS ****)
|
||||
(* for loops on arrays... yikes *)
|
||||
Array.length a;;
|
||||
for i = 0 to Array.length a - 1 do
|
||||
a.(i) <- 2 * a.(i)
|
||||
done;;
|
||||
|
||||
a;;
|
||||
|
||||
(* you can go in reverse also using downto *)
|
||||
for i = Array.length a - 1 downto 0 do
|
||||
a.(i) <- 2 * a.(i)
|
||||
done;;
|
||||
|
||||
(* while loops also... *)
|
||||
let i = ref 0;;
|
||||
while !i < Array.length a do
|
||||
(* adding semi-colon to end of expression means
|
||||
this is a garbage value, throw it away so we
|
||||
dont return unit() type early *)
|
||||
a.(!i) <- - !i;
|
||||
incr i
|
||||
done;;
|
||||
|
||||
a;;
|
||||
|
||||
(**** IGNORE ****)
|
||||
(* x will be 2 and the expression 1;
|
||||
is ignored because of the semi-colon *)
|
||||
let x = ignore 1; 2;;
|
||||
|
||||
(**** EXCEPTIONS ****)
|
||||
(* defining exceptions and rasing *)
|
||||
(* exception is a variant type and the
|
||||
number of variants can be extended *)
|
||||
(* exception Hell;; *)
|
||||
(* raise Hell;;
|
||||
Failure "hell";; *)
|
||||
|
||||
(* exceptions can have parameters *)
|
||||
exception Hell of int;;
|
||||
Hell 1;;
|
||||
Hell 2;;
|
||||
|
||||
(* List.hd [];; (* raises an exception *) *)
|
||||
|
||||
(* you can pattern match exceptions with try-with *)
|
||||
try
|
||||
List.hd []
|
||||
with
|
||||
| Failure _ -> -1;;
|
||||
|
||||
try
|
||||
List.hd []
|
||||
with
|
||||
| _ -> -1;;
|
||||
|
||||
|
||||
(**** IO ****)
|
||||
print_string "hello world\n";;
|
||||
print_endline "hello world";;
|
||||
print_int 123;;
|
||||
print_float 123.4;;
|
||||
print_newline ();;
|
||||
Printf.printf "%5d\n" 123;;
|
||||
Printf.printf "%05d %s\n" 123 "hello";;
|
||||
Printf.eprintf "%05d %s\n" 123 "hello";; (* print to standard error channel *)
|
||||
|
||||
let x = read_line ();;
|
||||
|
||||
(* read in int - add space at the front, it matches any amount of leading whitespace *)
|
||||
Scanf.scanf " %d" (fun x -> x)
|
||||
|
||||
(* open file *)
|
||||
(* 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;;
|
||||
@@ -0,0 +1 @@
|
||||
hello world
|
||||
@@ -0,0 +1,12 @@
|
||||
(* we can impliment our own ref type *)
|
||||
type 'a ref = { mutable contents: 'a };;
|
||||
|
||||
(* reference and dereference implimentation *)
|
||||
let ref x = { contents = x };;
|
||||
let (!) r = r.contents;;
|
||||
|
||||
(* assign to a mutable field *)
|
||||
let (:=) r x = r.contents <- x;;
|
||||
|
||||
let incr r = r.contents <- r.contents + 1;;
|
||||
let decr r = r.contents <- r.contents - 1;;
|
||||
@@ -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;;
|
||||
|
||||
Reference in new issue
Block a user