day = {0, 1, 2, 3, 4, 5, 6} C intx is a valid day if $0 \le x \le 6$is_week_end: day -> bool informally means that integers between 0 and 6 are the only valid inputs for this function
In [2]:
type color = int;;
let red: color = 0;;
let white: color = 1;;
let blue: color = 2;;
Out[2]:
Out[2]:
Out[2]:
Out[2]:
In [11]:
type positive = int;;
let abs (x: int) = (if x<0 then -x else x: positive);;
let abs' (x: int): positive = if x<0 then -x else x;;
Out[11]:
Out[11]:
Out[11]:
let x: some_type = some_expression;;let f (x: some_type) = some_expression;;let f x: some_type = some_expression;;let f x = (some_expression: some_type);;type t = int and x be a value of type t, then x is also of type intt is represented as a value of type int in the machinetype positive = int. The type synonym positive provides no more static guarantee about positivity than int.let x: positive = -1;;
In [17]:
let origin = (0, 0);;
let x_positive_limit = (max_int, 0);;
let x_negative_limit = (min_int, 0);;
type point2D = int * int;;
let origin: point2D = (0, 0);;
Out[17]:
Out[17]:
Out[17]:
Out[17]:
Out[17]:
* constructs tuple types: some_type * ... * some_typesome_expr, some_expr, ..., some_expr
In [29]:
let (x, _) = (6*3, 2) in x;; (* observe 6*3 by naming it x, and ignore 2 *)
let a = (3*6, 4*6);;
let (x, _) = a;;
let abscissa (x, _) = x;;
let ordinate (_, x) = x;;
Out[29]:
Out[29]:
Out[29]:
Out[29]:
Out[29]:
(some_pattern, ..., some_pattern)let p = (1, 2, 3);; and let q = (p, 0);; the p in q is a pointer to the memory address of tuple p.let p = (1, 2, 3);; and let q = (p, p);;= implements structural equality.== implements physical equality.
In [30]:
let x = (1, 2);;
let y = (1, 2);;
let z = x;;
x = y;;
x == y;;
x == z;;
Out[30]:
Out[30]:
Out[30]:
Out[30]:
Out[30]:
Out[30]:
let (x, _) = (1, 2, 3);; and let (x, x, y) = (1, 2, 3);;let abscissa (x, y) = y;; and let ordinate (x, y) = x;; are syntactically correct, but compiler cannot understand that abscissa and ordinate are swapped.
In [3]:
type point2D = {x: int; y: int};;
let origin = {x=0; y=0};;
let from_tuple (x, y) = {x; y};; (* {x=x; y=y};; *)
let a: point2D = from_tuple (4, 2);;
let b: point2D = from_tuple (10, 5);;
type box = {left_upper_corner: point2D; right_lower_corner: point2D;};;
let the_box = {left_upper_corner=a; right_lower_corner=b};;
Out[3]:
Out[3]:
Out[3]:
Out[3]:
Out[3]:
Out[3]:
Out[3]:
type some_type_identifier = {field_name: some_type; ...; field_name: some_type}{field_name=some_expr; ...; field_name=some_expr}some_expr.field_name{field_name: some_pattern; ...; field_name: some_pattern}type point2D = {x: int; y: int};; | let stuff = {x=0};; $\rightarrow$ Errortype person = {name: String; age: int};; | let luke = {name="Sky walker"; age="26"};; $\rightarrow$ Errortype t = {x: bool};; | type u = {x: int};; | {x: true} $\rightarrow$ Error
In [4]:
let p = [|1; 2; 3|];;
let square x = x*x;;
let squares n = Array.init n square;;
let s1 = squares 5;;
let s2 = squares 10;;
Out[4]:
Out[4]:
Out[4]:
Out[4]:
Out[4]:
[|some_expr; ...; some_expr|]Array.make expects an integer representing size of the array and a value to initialize each component of the array.Array.init expects an integer representing size of the array and a function to initialize each component of the array.Array.length returns size of an array.some_expr.(some_expr)'a' are taken between 0 to Array.length a - 1.[|some_pattern; ...; some_pattern|]
In [15]:
let swap a = [|a.(1); a.(0)|];; (* Polymorphic typed *)
swap [|1; 0|];;
swap [|"Bose"; "Anirudha"|];;
Out[15]:
Out[15]:
Out[15]:
In [21]:
let swap a = [|a.(1); a.(0)|];;
swap [|1; 2; 3|];;
let swap [|x; y|] = [|y; x|];;
swap [|1; 2; 3|];;
Out[21]:
Out[21]:
Out[21]:
Out[21]:
In [1]:
type phone_number = int * int * int * int;;
type contact = {
name: string;
phone_number: phone_number;
};;
let nobody = {name=""; phone_number=(0, 0, 0, 0)};;
type database = {
number_of_contacts: int;
contacts: contact array;
};;
let make max_number_of_contacts = {
number_of_contacts = 0;
contacts = Array.make max_number_of_contacts nobody;
};;
type query = {
code: int;
contact: contact;
};;
Out[1]:
Out[1]:
Out[1]:
Out[1]:
Out[1]:
Out[1]:
In [2]:
let search db contact =
let rec aux idx =
if idx >= db.number_of_contacts
then
(false, db, nobody)
else
if db.contacts.(idx).name = contact.name
then
(true, db, db.contacts.(idx))
else
aux (idx+1)
in
aux 0;;
Out[2]:
In [3]:
let insert db contact =
if db.number_of_contacts = Array.length db.contacts - 1
then
(false, db, nobody)
else
let (status, db, _) = search db contact
in
if status
then
(false, db, nobody)
else
let cells i =
if i = db.number_of_contacts
then
contact
else
db.contacts.(i)
in
let _db = {
number_of_contacts = db.number_of_contacts + 1;
contacts = Array.init (Array.length db.contacts) cells;
}
in
(true, _db, contact)
;;
Out[3]:
In [4]:
let delete db contact =
let (status, db, contact) = search db contact
in
if not status
then
(false, db, contact)
else
let cells i =
if db.contacts.(i).name = contact.name
then
nobody
else
db.contacts.(i)
in
let _db = {
number_of_contacts = db.number_of_contacts - 1;
contacts = Array.init (Array.length db.contacts) cells;
}
in
(true, _db, contact)
;;
Out[4]:
In [5]:
let engine db (code, contact) =
if code = 0 then insert db contact
else if code = 1 then delete db contact
else if code = 2 then search db contact
else (false, db, nobody)
;;
Out[5]:
In [6]:
let db = make 5;; (* Creating the database *)
Out[6]:
In [7]:
let (status, db, contact) = engine db (0, {name="luke"; phone_number=(1, 2, 3, 4)});;
Out[7]:
database -> query -> status * database * contact.d and for all contact c, if insert db c = (true, db', _) then search db' c = (true, db', c).