Skip to content

Emrio/ocaml-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ocaml Cheatsheet

Table of contents

Index

Tests

All of these test operators are generally O(1) in Space and Time complexity

Complexity may differ depending on the provided type 'a

(=) : 'a -> 'a -> bool

Value equality

(<>) : 'a -> 'a -> bool

Value inequality

(==) : 'a -> 'a -> bool

Identity equality

(!=) : 'a -> 'a -> bool

Identity inequality

(<) : 'a -> 'a -> bool

Strictly smaller comparator

(>) : 'a -> 'a -> bool

Strictly greater comparator

(<=) : 'a -> 'a -> bool

Smaller or equal comparator

(>=) : 'a -> 'a -> bool

Greater or equal comparator

Global scope builtins

All of these functions are generally O(1) in Space and Time complexity unless other specified

Complexity may differ depending on the provided type 'a

Polymorphic comparators

compare : 'a -> 'a -> int

Returns a positive integer when x > y, negative if x < y, and 0 if x = y

max : 'a -> 'a -> 'a

Returns the largest element in a set of two

min : 'a -> 'a -> 'a

Returns the smallest element in a set of two

Stdout

print_int : int -> unit

Print an integer

print_float : float -> unit

Print a real number

print_string : string -> unit

Print a string

print_newline : unit -> unit

Print a line return character

Type Casting

'a_of_string functions are O(N) with N length of the string. This should be considered O(1) for most applications

ignore : 'a -> unit

Cast value to unit

float_of_int : int -> float

Cast integer to float

int_of_float : float -> int

Cast a real number to the integer of its truncated decimal representation

string_of_int : int -> string

Integer to string

int_of_string : string -> int

Parse a string as an integer

string_of_float : float -> string

Float to string

float_of_string : string -> float

Parse a string as a floating number

Unit unit

Only type member: ()

Warning: Tests for unit should use = and <>, not == and !=

Boolean bool

All of these operators are O(1) in Space and Time complexity

Boolean operators

(not) : bool -> bool

Formal negation

(&&) : bool -> bool -> bool

Formal logical AND

(||) : bool -> bool -> bool

Formal logical OR

Integer int

Integer operators

(+) : int -> int -> int

Integer addition

(-) : int -> int -> int

Integer difference

( * ) : int -> int -> int

Integer multiplication

(/) : int -> int -> int

Integer division quotient

(mod) : int -> int -> int

Integer division remainder

Integer global functions

abs : int -> int

Integer absolute value (canonical distance to 0)

Float float

Float operators

(+.) : flaot -> flaot -> flaot

Real number addition

(-.) : float -> float -> float

Real number difference

( *.) : float -> float -> float

Real number multiplication

(/.) : float -> float -> float

Real number division

( ** ) : float -> float -> float

Real number power

Float global functions

sqrt : float -> float

Real square root

exp : float -> float

Natural real exponentiation

log : float -> float

Natural real logarithm

log10 : float -> float

Base 10 real logarithm

sin : float -> float

Sine function

cos : float -> float

Cosine function

tan : float -> float

Tangent function

asin : float -> float

Sine's reciprocal function

acos : float -> float

Cosine's reciprocal function

atan : float -> float

Tangent's reciprocal function

Tuple

All of these functions are O(1) in Space and Time complexity

Couple 'a * 'b global functions

fst : ('a, 'b) -> 'a

Get first element of a couple

snd : ('a, 'b) -> 'b

Get second element of a couple

String string

String operators

(^) : string -> string -> string

String concatenation

O(N + M) with N, M lengths of the given strings

str.[idx] -> char

Equivalent to String.get str idx

O(1)

String module functions

String.length : string -> int

Get string length

O(1)

String.get : string -> int -> char

Get char at given index

O(1)

String.sub : string -> start: int -> lenght: int -> string

Create substring given start index and length of sub-string

O(N) with N being created string length

String.make : int -> char -> string

Create a string with given length and filled with given character

O(N) with N string length

String.init : int -> (int -> char) -> string

Create a string of given length and by using characters returned by given function

O(N) with N string length

List 'a list

List operators

(::) : 'a -> 'a list -> 'a list

List push to head or pull head

O(1)

(@) : 'a list -> 'a list -> 'a list

List concatenation

O(N) with N length of the first list

List module functions

List.length : 'a list -> int

Get list's size

Warning: Never use, this is linear in time

O(N) with N length of the list

List.hd : 'a list -> 'a

Get list's head (first element)

O(1)

List.tl : 'a list -> 'a list

Get list's tail (list minus list head)

O(1)

List.nth : 'a list -> int -> 'a

Get list's nth element

Note: Lists are 0-indexed. First element is at index 0.

Warning: Avoid using, this is linear in time

O(N) with N index of sought element

List.mem : 'a list -> 'a -> bool

Test whether second argument is an element of given list

O(N) with N list length

List.rev : 'a list -> 'a list

Create a list with the same elements but with reversed order

O(N) with N list length

List.iter : ('a -> unit) -> 'a list -> unit

Applies given function to all elements of the list, from head to tail

O(N) with N list length

List.map : ('a -> 'b) -> 'a list -> 'b list

Applies given function to all elements of the list and returns the results as a list

O(N) with N list length

List.fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b

Reduces a list with given function and initial value from tail to head

O(N) with N list length

List.fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a

Same as List.fold_right but from head to tail and with different argument order

O(N) with N list length

Array 'a array

Array module functions

Array.length : 'a array -> int

Get array length

O(1)

Array.make : int -> 'a -> 'a array

Create an array with given length and filled with given element

O(N) with N array length

Array.make_matrix : int -> int -> 'a -> 'a array array

Create a two dimensional array with given width, height and filled with given element

Warning: Watch your steps as filler value is passed by reference and use Array.init instead if necessary

O(N * M) with N, M width and height

Array.init : int -> (int -> 'a) -> 'a array

Create an array of given length and by using values returned by given function

O(N) with N array length

Function <fun>

Function definition

let foo = function x -> x + x;;
let foo = fun x -> x + x;;
let foo x = x + x;;

Warning: Only function syntax allows for matching

Warning: Recursivity requires rec keyword

let bar = function
  | 42 -> "yay"
  | _  -> "nay";;

Warning: Only function syntax allows for matching

let rec factorial = function
  | 0 -> 1
  | n -> n * fact (n - 1);;

Useful implementations

Math

let rec gcd a = function
  | 0 -> a
  | b -> gcd b (a mod b);;

Greatest common denominator of two integers

Quality of life

let string_of_char chr = String.make 1 chr;;

let char_of_string str = String.get str 0;;

let string_of_array t = String.init (Array.length t) (Array.get t);;

Cast a char array into a string

let array_of_string s = Array.init (String.length s) (String.get s);;

Cast a string into a char array

About

Documentation for some Ocaml features

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published