let a = 5;;
;; terminates ocaml phrases.
let is not an assignment. It identifies new identifier with new scope.
let rec defines a recursive function.
;; terminates ocaml phrases.
let is not an assignment. It identifies new identifier with new scope.
let rec defines a recursive function.
Data Types
int, float, bool, char, string
int, float, bool, char, string
Pattern Matching
let rec doNothing list = match list with
[] -> []
| head::tail -> head::doNothing(tail);;
the | defines different cases to match with.
[] case implies that list is a list and this is the case if it's empty.
head::tail case also implies that list is a list and head is the first element of the list and tail is the sublist without that head.
Ocaml can't modify in-place a list after it's built they are immutable data structures.
A function with a list as input will build and return a new list with or without the elements of the input list.
let rec doNothing list = match list with
[] -> []
| head::tail -> head::doNothing(tail);;
the | defines different cases to match with.
[] case implies that list is a list and this is the case if it's empty.
head::tail case also implies that list is a list and head is the first element of the list and tail is the sublist without that head.
Ocaml can't modify in-place a list after it's built they are immutable data structures.
A function with a list as input will build and return a new list with or without the elements of the input list.
Typing
let deriv f dx = function x -> ( f (x +. dx) -. f x ) /. dx;;
This phrase evaluates to
(float -> float) -> float -> float -> float = <fun>
1 2 3 4 5
1. (float -> float) is the type of f
The braces mean that f is a function. It takes in a float and outputs a float
since in f (x +. dx) we are passing in a floating point number to f because +. is a floating point addition.
And ( f (x +. dx) -. f x ) indicates that f outputs a floating point number because -. is floating point subtraction.
2. float is what dx type is.
3. float is what x's type is.
4. float is what the function deriv's output type is.
5. <fun> indicates that it's a function.
let deriv f dx = function x -> ( f (x +. dx) -. f x ) /. dx;;
This phrase evaluates to
(float -> float) -> float -> float -> float = <fun>
1 2 3 4 5
1. (float -> float) is the type of f
The braces mean that f is a function. It takes in a float and outputs a float
since in f (x +. dx) we are passing in a floating point number to f because +. is a floating point addition.
And ( f (x +. dx) -. f x ) indicates that f outputs a floating point number because -. is floating point subtraction.
2. float is what dx type is.
3. float is what x's type is.
4. float is what the function deriv's output type is.
5. <fun> indicates that it's a function.
User Defined Types
type ratio = {num: int; denom: int};;
Here is a user defined type to represent a fraction.
let add_ratio r1, r2 = {
num = r1.num * r2.denom + r2.num * r1.denom;
denom = r1.denom * r2.denom};;
ratio -> ratio -> ratio
Here we can return a ratio using the curly braces and setting the num and denom, separated by a ;.
We can also access the denom and num of r1 and r2 using the . operator.
type ratio = {num: int; denom: int};;
Here is a user defined type to represent a fraction.
let add_ratio r1, r2 = {
num = r1.num * r2.denom + r2.num * r1.denom;
denom = r1.denom * r2.denom};;
ratio -> ratio -> ratio
Here we can return a ratio using the curly braces and setting the num and denom, separated by a ;.
We can also access the denom and num of r1 and r2 using the . operator.
Variant Type
Lists the possible shapes for values of that type.
type sign = Positive | Negative;;
Good for trees, pattern matching.
Lists the possible shapes for values of that type.
type sign = Positive | Negative;;
Good for trees, pattern matching.
Imperative Programming Features
for ... to ... do
...
done;
while ... do
....
done;
for ... to ... do
...
done;
while ... do
....
done;
Arrays
Two ways of creation
1. Array.create
let arr = Array.create len 0.0;;
len = length
0.0 = initial values of the array
2. [| |]
let arr = [|1;2;3;|];;
Two ways of creation
1. Array.create
let arr = Array.create len 0.0;;
len = length
0.0 = initial values of the array
2. [| |]
let arr = [|1;2;3;|];;
References
ref j
defines j as a reference
j := !j;;
:= assigns to the contents of j.
! accesses the contents of j.
ref j
defines j as a reference
j := !j;;
:= assigns to the contents of j.
! accesses the contents of j.
No comments:
Post a Comment