1-8 ocaml
OCaml: functional, statically-typed programming language
will use for property-based testing to check whether programs meet specs
helps you program with modularity (breaking programming tasks into smaller ones that can
be built independently) and abstraction (ensuring that these smaller tasks are independent)
allows you to apply property-based testing to larger systems by breaking them into
smaller ones with manageable specs
open ocaml's repl with utop command in terminal (quit with CTRL-D or "#quit;;"):
end all commands with ;; (newlines are ignored)
responses will contain a type and resulting value
ex: "2 + 2;;" would output "int = 4" (int types support addition( + ), multiplication( * ),
integer division( / ) (rounds down), and mod (gets remainder))
floating-point numbers cannot be evaluated with integers and can use the same
operators as int but must include a "." after the operator (ex: "3.2 +. 5.;;" returns
"float=8.2")
booleans (true, false) use OR (||), AND (&&), and not operators
not false || true gets interpreted as (not false) || true but you can use
parenthesis to group the OR statement together first
use "if then else" statements
ex: if false then 2 else 3 returns int = 3
strings can be concatenated using ^ operator
ex: hi " ^ "there";; returns "string = "hi there""
let expressions (used to define variables):
locally define variables (scope is just within the expression) and use them in
expressions using "let x = .. in .."
ex: let x = 8 in x / 2;; returns int = 4
can be nested and sequenced:
let a = 2+2 in let b = 2 in
a + b;; returns int = 6`
let x = (let a = 1 in a) * (let x = 2 in x + 1) in
100 / x
;;
returns int = 33
defining without using "in" makes the scope broaden to the utop session (you can
always redefine it within the session, but the variable will stay defined through
multiple commands)
let x = 3;;
returns val x : int = 3
x;;
returns int = 3
let x = x + 1;; ⟵ re-defining variable, not modifying or mutating
returns val x : int = 4
equality expressions
let x = 2 + 2 in
let y = 2 * 2 in
if x = y then 3 else 4;;
returns int = 3
false = true;;
returns bool = false
type conversions:
ex: converting int to float: float_of_int 3;; returns float = 3.