restructure + lecutre 07

This commit is contained in:
SowinskiBraeden committed 2026-02-19 11:59:58 -08:00
1 parent b8c19aa341
commit 7c3198fee4
96 files changed
+458 -4

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="Start" href="index.html">
<link rel="Up" href="index.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Length" rel="Chapter" href="Length.html"><title>Length</title>
</head>
<body>
<div class="navbar">&nbsp;<a class="up" href="index.html" title="Index">Up</a>
&nbsp;</div>
<h1>Module <a href="type_Length.html">Length</a></h1>
<pre><span id="MODULELength"><span class="keyword">module</span> Length</span>: <code class="code">sig</code> <a href="Length.html">..</a> <code class="code">end</code></pre><div class="info module top">
<div class="info-desc">
<p>returns the number of elements in the list <code class="code">l</code> - non tail recursive</p>
</div>
</div>
<hr width="100%">
<pre><span id="VALlength"><span class="keyword">val</span> length</span> : <code class="type">'a list -&gt; int</code></pre>
<pre><span id="VALtest_length"><span class="keyword">val</span> test_length</span> : <code class="type">unit -&gt; unit</code></pre>
<pre><span id="VALlength_tr"><span class="keyword">val</span> length_tr</span> : <code class="type">'a list -&gt; int</code></pre><div class="info ">
<div class="info-desc">
<p>returns the number of elements in the list <code class="code">l</code> - tail recursive</p>
</div>
</div>
<pre><span id="VALtest_length_tr"><span class="keyword">val</span> test_length_tr</span> : <code class="type">unit -&gt; unit</code></pre></body></html>
+27
View File
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="Start" href="index.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Length" rel="Chapter" href="Length.html"><title></title>
</head>
<body>
<div class = "index-list">
<ul class="indexlist">
<li><a href="index_values.html">Index of values</a></li>
<li><a href="index_modules.html">Index of modules</a></li>
</ul>
</div>
<table class="indextable module-list">
<tr><td class="module"><a href="Length.html">Length</a></td><td><div class="info">
returns the number of elements in the list <code class="code">l</code> - non tail recursive
</div>
</td></tr>
</table>
</body>
</html>
+18
View File
@@ -0,0 +1,18 @@
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="Start" href="index.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Length" rel="Chapter" href="Length.html"><title>Index of class attributes</title>
</head>
<body>
<div class="navbar">&nbsp;<a class="up" href="index.html" title="Index">Up</a>
&nbsp;</div>
<h1>Index of class attributes</h1>
<table>
</table>
</body>
</html>
+18
View File
@@ -0,0 +1,18 @@
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="Start" href="index.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Length" rel="Chapter" href="Length.html"><title>Index of class types</title>
</head>
<body>
<div class="navbar">&nbsp;<a class="up" href="index.html" title="Index">Up</a>
&nbsp;</div>
<h1>Index of class types</h1>
<table>
</table>
</body>
</html>
+18
View File
@@ -0,0 +1,18 @@
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="Start" href="index.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Length" rel="Chapter" href="Length.html"><title>Index of classes</title>
</head>
<body>
<div class="navbar">&nbsp;<a class="up" href="index.html" title="Index">Up</a>
&nbsp;</div>
<h1>Index of classes</h1>
<table>
</table>
</body>
</html>
+18
View File
@@ -0,0 +1,18 @@
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="Start" href="index.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Length" rel="Chapter" href="Length.html"><title>Index of exceptions</title>
</head>
<body>
<div class="navbar">&nbsp;<a class="up" href="index.html" title="Index">Up</a>
&nbsp;</div>
<h1>Index of exceptions</h1>
<table>
</table>
</body>
</html>
+18
View File
@@ -0,0 +1,18 @@
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="Start" href="index.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Length" rel="Chapter" href="Length.html"><title>Index of extensions</title>
</head>
<body>
<div class="navbar">&nbsp;<a class="up" href="index.html" title="Index">Up</a>
&nbsp;</div>
<h1>Index of extensions</h1>
<table>
</table>
</body>
</html>
+18
View File
@@ -0,0 +1,18 @@
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="Start" href="index.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Length" rel="Chapter" href="Length.html"><title>Index of class methods</title>
</head>
<body>
<div class="navbar">&nbsp;<a class="up" href="index.html" title="Index">Up</a>
&nbsp;</div>
<h1>Index of class methods</h1>
<table>
</table>
</body>
</html>
+18
View File
@@ -0,0 +1,18 @@
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="Start" href="index.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Length" rel="Chapter" href="Length.html"><title>Index of module types</title>
</head>
<body>
<div class="navbar">&nbsp;<a class="up" href="index.html" title="Index">Up</a>
&nbsp;</div>
<h1>Index of module types</h1>
<table>
</table>
</body>
</html>
+24
View File
@@ -0,0 +1,24 @@
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="Start" href="index.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Length" rel="Chapter" href="Length.html"><title>Index of modules</title>
</head>
<body>
<div class="navbar">&nbsp;<a class="up" href="index.html" title="Index">Up</a>
&nbsp;</div>
<h1>Index of modules</h1>
<table>
<tr><td align="left"><div>L</div></td></tr>
<tr><td><a href="Length.html">Length</a> </td>
<td><div class="info">
returns the number of elements in the list <code class="code">l</code> - non tail recursive
</div>
</td></tr>
</table>
</body>
</html>
+18
View File
@@ -0,0 +1,18 @@
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="Start" href="index.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Length" rel="Chapter" href="Length.html"><title>Index of types</title>
</head>
<body>
<div class="navbar">&nbsp;<a class="up" href="index.html" title="Index">Up</a>
&nbsp;</div>
<h1>Index of types</h1>
<table>
</table>
</body>
</html>
+31
View File
@@ -0,0 +1,31 @@
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="Start" href="index.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Length" rel="Chapter" href="Length.html"><title>Index of values</title>
</head>
<body>
<div class="navbar">&nbsp;<a class="up" href="index.html" title="Index">Up</a>
&nbsp;</div>
<h1>Index of values</h1>
<table>
<tr><td align="left"><div>L</div></td></tr>
<tr><td><a href="Length.html#VALlength">length</a> [<a href="Length.html">Length</a>]</td>
<td></td></tr>
<tr><td><a href="Length.html#VALlength_tr">length_tr</a> [<a href="Length.html">Length</a>]</td>
<td><div class="info">
returns the number of elements in the list <code class="code">l</code> - tail recursive
</div>
</td></tr>
<tr><td align="left"><div>T</div></td></tr>
<tr><td><a href="Length.html#VALtest_length">test_length</a> [<a href="Length.html">Length</a>]</td>
<td></td></tr>
<tr><td><a href="Length.html#VALtest_length_tr">test_length_tr</a> [<a href="Length.html">Length</a>]</td>
<td></td></tr>
</table>
</body>
</html>
+43
View File
@@ -0,0 +1,43 @@
.keyword { font-weight : bold ; color : Red }
.keywordsign { color : #C04600 }
.comment { color : Green }
.constructor { color : Blue }
.type { color : #5C6585 }
.string { color : Maroon }
.warning { color : Red ; font-weight : bold }
.info { margin-left : 3em; margin-right: 3em }
.param_info { margin-top: 4px; margin-left : 3em; margin-right : 3em }
.code { color : #465F91 ; }
.typetable { border-style : hidden }
.paramstable { border-style : hidden ; padding: 5pt 5pt}
tr { background-color : White }
td.typefieldcomment { background-color : #FFFFFF ; font-size: smaller ;}
div.sig_block {margin-left: 2em}
*:target { background: yellow; }
body {font: 13px sans-serif; color: black; text-align: left; padding: 5px; margin: 0}
h1 { font-size : 20pt ; text-align: center; }
h2 { font-size : 20pt ; text-align: center; }
h3 { font-size : 20pt ; border: 1px solid #000000; margin-top: 5px; margin-bottom: 2px;text-align: center; background-color: #90BDFF ;padding: 2px; }
h4 { font-size : 20pt ; border: 1px solid #000000; margin-top: 5px; margin-bottom: 2px;text-align: center; background-color: #90DDFF ;padding: 2px; }
h5 { font-size : 20pt ; border: 1px solid #000000; margin-top: 5px; margin-bottom: 2px;text-align: center; background-color: #90EDFF ;padding: 2px; }
h6 { font-size : 20pt ; border: 1px solid #000000; margin-top: 5px; margin-bottom: 2px;text-align: center; background-color: #90FDFF ;padding: 2px; }
div.h7 { font-size : 20pt ; border: 1px solid #000000; margin-top: 5px; margin-bottom: 2px;text-align: center; background-color: #90BDFF ; padding: 2px; }
div.h8 { font-size : 20pt ; border: 1px solid #000000; margin-top: 5px; margin-bottom: 2px;text-align: center; background-color: #E0FFFF ; padding: 2px; }
div.h9 { font-size : 20pt ; border: 1px solid #000000; margin-top: 5px; margin-bottom: 2px;text-align: center; background-color: #F0FFFF ; padding: 2px; }
div.h10 { font-size : 20pt ; border: 1px solid #000000; margin-top: 5px; margin-bottom: 2px;text-align: center; background-color: #FFFFFF ; padding: 2px; }
a {color: #416DFF; text-decoration: none}
a:hover {background-color: #ddd; text-decoration: underline}
pre { margin-bottom: 4px; font-family: monospace; }
pre.verbatim, pre.codepre { }
.indextable {border: 1px #ddd solid; border-collapse: collapse}
.indextable td, .indextable th {border: 1px #ddd solid; min-width: 80px}
.indextable td.module {background-color: #eee ; padding-left: 2px; padding-right: 2px}
.indextable td.module a {color: #4E6272; text-decoration: none; display: block; width: 100%}
.indextable td.module a:hover {text-decoration: underline; background-color: transparent}
.deprecated {color: #888; font-style: italic}
.indextable tr td div.info { margin-left: 2px; margin-right: 2px }
ul.indexlist { margin-left: 0; padding-left: 0;}
ul.indexlist li { list-style-type: none ; margin-left: 0; padding-left: 0; }
ul.info-attributes {list-style: none; margin: 0; padding: 0; }
div.info > p:first-child { margin-top:0; }
div.info-desc > p:first-child { margin-top:0; margin-bottom:0; }
+11
View File
@@ -0,0 +1,11 @@
<html><head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="Start" href="index.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Length" rel="Chapter" href="Length.html"><title>Length</title>
</head>
<body>
<code class="code"><span class="keyword">sig</span>&nbsp;<span class="keyword">end</span></code></body></html>
+187
View File
@@ -0,0 +1,187 @@
(*
if everything is fine, test functions
will return the unit value
*)
(** [length l] returns the number of elements in the list [l]; non tail recursive *)
let rec length l =
match l with
| [] -> 0
| x :: xs -> 1 + length xs;;
(**/**)
let test_length () =
assert (length [] = 0);
assert (length [2] = 1);
assert (length [5; 7; 8;] = 3)
(**/**)
(** [length_tr l] returns the number of elements in the list [l]; tail recursive *)
let length_tr l =
let rec lenth_tr' acc l =
match l with
| [] -> acc
| _ :: xs -> lenth_tr' (acc + 1) xs
in
lenth_tr' 0 l;;
(**/**)
let test_length_tr () =
assert (length_tr [] = 0);
assert (length_tr [2] = 1);
assert (length_tr [5; 7; 8;] = 3)
(**/**)
(** [reverse l] returns the reverse order of list [l]; non tail recursive *)
let rec reverse l =
match l with
| [] -> []
| x :: xs -> reverse xs @ [x];;
(**/**)
let test_reverse () =
assert (reverse [] = []);
assert (reverse [1] = [1]);
assert (reverse [1; 2] = [2; 1])
(**/**)
(** [reverse_tr l] returns the reverse order of list [l]; tail recursive *)
let reverse_tr l =
let rec reverse_tr' acc l =
match l with
| [] -> acc
| x :: xs -> reverse_tr' (x :: acc) xs
in
reverse_tr' [] l;;
(**/**)
let test_reverse_tr () =
assert (reverse_tr [] = []);
assert (reverse_tr [1] = [1]);
assert (reverse_tr [1; 2] = [2; 1])
(**/**)
(* list.rev is a built in reverse function *)
(** [take n l] returns a list containing the first [n] elements of [l];
* return [] if (n <= 0); return [l] if [l has fewer elements than [n]];
* non tail recursive *)
let rec take n l =
if n <= 0 then []
else
match l with
| [] -> []
| x :: xs -> x :: take (n - 1) xs
(**/**)
let test_take () =
assert (take 0 [1; 2; 3;] = []);
assert (take 3 [4; 5; 6; 7; 8; 9] = [4; 5; 6]);
assert (take 5 [1; 2; 3] = [1; 2; 3])
(**/**)
(** [take_tr n l] returns a list containing the first [n] elements of [l];
* return [] if (n <= 0); return [l] if [l has fewer elements than [n]];
* tail recursive *)
let take_tr n l =
let rec take_tr' acc n l =
if n <= 0 then reverse_tr(acc)
else
match l with
| [] -> acc
| x :: xs -> x :: take_tr' (x :: acc) (n - 1) xs
in
take_tr' [] n l;;
(**/**)
let test_take_tr () =
assert (take_tr 0 [1; 2; 3;] = []);
assert (take_tr 3 [4; 5; 6; 7; 8; 9] = [4; 5; 6]);
assert (take_tr 5 [1; 2; 3] = [1; 2; 3])
(**/**)
(** [every_other l] returns a list consisting of every other element of [l]
* starting from the first element; non tail recursive *)
let rec every_other l =
match l with
| x :: _ :: xs -> every_other xs
| _ -> l;;
(**/**)
let test_every_other () =
assert (every_other [] = []);
assert (every_other [1] = []);
assert (every_other [1; 2] = [2]);
assert (every_other [1; 2; 3] = [2]);
assert (every_other [1; 2; 3; 4] = [2; 4])
(**/**)
(** [every_other_tr l] returns a list consisting of every other element of [l]
* starting from the first element; tail recursive *)
let every_other_tr l =
let rec every_other_tr' acc l =
match l with
| x :: _ :: xs -> every_other_tr' (x :: acc) xs
| _ -> reverse_tr acc
in
every_other_tr' [] l;;
(**/**)
let test_every_other_tr () =
assert (every_other_tr [] = []);
assert (every_other_tr [1] = []);
assert (every_other_tr [1; 2] = [2]);
assert (every_other_tr [1; 2; 3] = [2]);
assert (every_other_tr [1; 2; 3; 4] = [2; 4])
(**/**)
(** [sum l1 l2] returns a list consisting of the sum of corresponding integers
* in [l1] and [l2]; non tail recursive *)
let rec sum l1 l2 =
match l1, l2 with (* this is a tuple of (l1, l2) *)
| [], _ | _, [] -> [] (* if l1 is empty or l2 is empty, return empty *)
| x1 :: xs1, x2 :: xs2 ->
(x1 + x2) :: sum xs1 xs2;;
(**/**)
let test_sum () =
assert (sum [] [] = []);
assert (sum [1] [] = []);
assert (sum [] [1] = []);
assert (sum [7] [8] = [15]);
assert (sum [7; 3] [8; 8] = [15; 11]);
assert (sum [7] [8; 8] = [15])
(**/**)
(** [sum_tr l1 l2] returns a list consisting of the sum of corresponding integers
* in [l1] and [l2]; tail recursive *)
let sum_tr l1 l2 =
let rec sum_tr' acc l1 l2 =
match l1, l2 with
| [], _ | _, [] -> reverse_tr acc
| x1 :: xs1, x2 :: xs2 ->
sum_tr' ((x1 + x2) :: acc) xs1 xs2
in
sum_tr' [] l1 l2;;
(**/**)
let test_sum_tr () =
assert (sum_tr [] [] = []);
assert (sum_tr [1] [] = []);
assert (sum_tr [] [1] = []);
assert (sum_tr [7] [8] = [15]);
assert (sum_tr [7; 3] [8; 8] = [15; 11]);
assert (sum_tr [7] [8; 8] = [15])
(**/**)
(** [count_change amt denoms] returns the number of ways of breaking up [amt]
* into currencies with denominations specified by [denoms];
* Require: elements of [denoms] must be positive *)
let rec count_change amt denoms =
if amt < 0 then 0
else if amt = 0 then 1
else
match denoms with
| [] -> 0
| d :: ds ->
count_change (amt - d) denoms + count_change amt ds;; (* use/not-use d *)
+135
View File
@@ -0,0 +1,135 @@
(*
***** TUPLES *****
has a fixed number of elements
*)
let x = (1, 2.1, "hello");;
(*
this is also a tuple, but this tuple
is a different type to the tuple above
*)
let y = (1, 2.1, "hello", "goodbye");;
(*
function with tuple argument
accepts a tuple with 3 elements of int
*)
let add_tuple (x, y, z) = x + y + z;;
(*
***** PATTERN MATCHING *****
where x gets deconstructed into a, b, and c
*)
let (a, b, c) = x;;
a;;
b;;
c;;
(*
***** LISTS *****
lists are semi-colon seperated values of the same type
lists are a recursive data type
*)
let nums = [1; 2; 3];; (* this is a valid list *)
(*
warning -
this is valid syntax but this is a list with a single
element that is a tuple of 3 ints
*)
let nums = [1, 2, 3];;
(* results in - list : [(1, 2, 3)] *)
(*
since lists are recursive data structures
we can add elements to the front of the list
as seen below
this operation is called cons
*)
2 :: (1 :: []);;
2 :: 1 :: [];;
(*
[1; 2; 3] === 1 :: 2 :: 3 :: [];;
*)
(* list of lists *)
[[1; 2]; [3; 4]];;
(* list concatonation *)
[1; 2] @ [3; 4];;
[];;
(*
results in - : 'a list = []
where 'a means its a type variable
since lists are generic
*)
let l = [[1; 2]; [3]; []];;
(*
pattern matching lists to extract 3
pattern matching must be exhaustive
*)
let [_; [x]; _] = l;;
let data = [(1, 2, 'a'); (5, 3, 'b'); (4, 1, 'c'); (5, 0, 'd')];;
let _ :: (_, _, x) :: _ = data;;
x;;
(*
recursive function
with pattern matching to find
length of list
*)
(**
* {length l} returns the number of elements
* in the list {l} - non tail recursive
*)
let rec length l =
match l with
| [] -> 0
| x :: xs -> 1 + length xs;;
(**
* {length_tr l} returns the number of elements
* in the list {l} - tail recursive
*)
let length_tr l =
let rec aux acc l =
match l with
| [] -> acc
| _ :: xs -> aux (acc + 1) xs
in
aux 0 l;;
(*
GENERATE WEB DOCUMENTATION
mkdir html
ocamldoc -html -d html main.ml
*)
(*
there is no void in ocaml,
everything has to return a value
all expressions have a value;
if your function has no value to
return, you still need to return something
so return the unit value
example:
printf is like a utility to print to the
console but does not need to return any value
so it returns the unit value
*)
Printf.printf "Hello, world\n";;