mirror of
https://github.com/Fechin/reference.git
synced 2026-11-21 05:24:10 +03:00
4.2 KiB
4.2 KiB
title, date, background, tags, categories, intro, plugins
| title | date | background | tags | categories | intro | plugins | ||||
|---|---|---|---|---|---|---|---|---|---|---|
| OCaml | 2024-08-06 00:00:00 | bg-[#ec670f] |
|
|
The [OCaml](https://www.ocaml.org/) cheat sheet is a one-page reference sheet for the OCaml programming language. |
|
Getting Started
hello.ml
# let () =
let message = "Hello, World!" in
Printf.printf "%s\n" message
Compile and Run
$ ocamlc -o hello.byte hello.ml
$ ./hello.byte
Build and Run with Dune
$ dune build hello.exe
$ _build/default/hello.exe
Imports
Global Open
# open Hex
Local Open
# Hex.of_string "hex string"
# let open Hex in
of_string "to hex"
Comments
(* A single line comment *)
(* A multi-line comment
* where we want to explain
* something complex *)
Documentation Comments
val sum : int -> int -> int
(** [sum x y] returns the sum
of two integers [x] and [y] *)
Data Types
Predefined Types
Unit
# () (* eqivalent to void *)
- : unit = ()
Basic Types
# 5 (* int *)
# 3.14 (* float *)
# true (* bool *)
# false
# 'a' (* char *)
# "a string" (* string *)
# String.to_bytes "hello" (* bytes *)
- : bytes = Bytes.of_string "hello"
# (3, 5) (* tuple *)
# ref 0 (* reference *)
Options & Results
# Some 42
- : int option = Some 42
# Ok 42
# Error "404"
Arrays & Lists
Arrays
# [|0; 1; 2; 3|]
# [|'u'; 's'; 'c'|].(1) (* array access *)
- char = 's'
Arrays are mutable
# let scores = [|97; 85; 99|]
# scores.(2) <- 89 (* update an element *)
- unit = ()
# scores
- : int array = [|97; 85; 89|]
Lists
# [1; 2; 3]
- : int list = [1; 2; 3;]
# ["a"; "str"; "lst"]
- : string list = ["a"; "str"; "lst"]
Lists are immutable
# let lst = [1; 2; 3]
# let new_lst = lst :: 4 (* append to a new list *)
- : int list = [1; 2; 3; 4]
User-Defined Types
Records
Bundle related data
# type person = {
name: string;
age: int
}
# let zeno = {name = "Zeno"; age = 30};;
val zeno : person = {name = "Zeno"; age = 30}
Variants
Several different, but related types
type shape =
| Circle of float
| Rectangle of float * float
let my_shape = Circle 5.0
- : shape = Circle 5.
Aliases
Provide meaningful name to complex or commonly used types
# type point = float * float
# let origin: point = (0.0, 0.0)
val origin : point = (0., 0.)
Functions
Functions
# let sum x y =
let result = x + y in
result
# sum 2 3
-: int = 5
Recursive Functions
# let rec factorial n =
if n < 1 then 1 else n * factorial (n - 1)
Chaining
Application Operator
# log @@ float_of_int @@ sum 2 3
Pipeline
(* find log((x + y)!) *)
# sum 2 3
|> factorial
|> float_of_int
|> log
- : float = 4.787...
Control Flow
If Statements
If Statement
# let is_pos x =
if x > 0 then "positive" else "negative"
If else if
# let f x =
if x > 3 then "gt 3"
else if x < 3 then "lt 3"
else "eq 3"
Pattern Matching
let is_pos x =
match x > 0 with
| true -> "positive"
| false -> "negative"
Loops
For loop
for i = 1 to 5
do print_int i
done
While loop
let i = ref 0 in
while !i < 5 do
print_int !i;
i := !i + 1
done
Operators
Comparison Operators
= (* equal to *)
<> or != (* not equal to *)
> (* greater than *)
< (* less than *)
>= (* greater than or eq to *)
<= (* less than or eq to *)
Arithmatic Operators
(* int operator float operator *)
+ +. (* addition *)
- -. (* subtraction *)
* *. (* multiplication *)
/ /. (* division *)
** (* power *)
List Library
(* an int list*)
# let lst = [1;2;3]
(* map a function to each elem *)
# List.map (fun x -> x + x) lst
- : int list = [2; 4; 6]
# List.mem 4 lst
- : bool = false
# List.find (fun x -> x = 4) lst
Exception: Not_found
Associaton Lists
# let scores =
[("math", 91); ("phil", 89); ("stats", 94)]
# List.assoc "stats" scores;;
- : int = 94