The snippets are under the CC-BY-SA license.

Creative Commons Attribution-ShareAlike 3.0


  • The snippets are under the CC-BY-SA license.
  • Please consider keeping a bookmark
  • (instead of printing)
Print a literal string on standard output
print_endline "Hello World!"
Loop to execute some code a constant number of times
let rec n_hellos
    (n : int)
  : unit =
  if n = 0 then
    (print_endline "Hello";
    n_hellos (n-1))

n_hello_worlds 10
Like a function which doesn't return any value, thus has only side effects (e.g. Print to standard output)
let do_something arg1 arg2: unit =
	print_endline "foo"
Create a function which returns the square of an integer
let square x = x*x
Declare a container type for two floating-point numbers x and y
type point = {
	x: float;
	y: float;
Do something with each item x of the list (or array) items, regardless indexes.
items |> List.iter do_something
Print each index i with its value x from an array-like collection items
(* output_elem is a printer for elements of [items] *)
items |> List.iteri (fun i x ->
	printf "%d: %a" i output_elem x
Create a new map object x, and provide some (key, value) pairs as initial content.
module StringMap = Map.Make(String)

let x =
    |> StringMap.add "one" 1
    |> StringMap.add "two" 2
    |> StringMap.add "three" 3
The structure must be recursive because left child and right child are binary trees too. A node has access to children nodes, but not to its parent.
type treenode =
    Node of {
        value: int;
        left: treenode;
        right: treenode
    | Leaf
Check if the list contains the value x.
list is an iterable finite container.
List.mem x list
The structure must be recursive. A node may have zero or more children. A node has access to its children nodes, but not to its parent.
type 'a Tree = {
	value: 'a;
	children: 'a tree list;
Reverse the order of the elements of the list x.
This may reverse "in-place" and destroy the original ordering.
List.rev x
Implement a function search which looks for item x in a 2D matrix m.
Return indices i, j of the matching cell.
Think of the most idiomatic way in the language to return the two values at the same time.
module Arr = Bigarray.Array2

let search array value =
	let x_max = Arr.dim1 array in
	let y_max = Arr.dim2 array in
	let rec loop x y = 
		(* End of array *)
		if x = x_max then
			raise Not_found
		(* End of row, go to next *)
		else if y = y_max then
			loop (x+1) 0
			(* If found, return it *)
			if array.{x,y} = value then
			(* Otherwise go to next col *)
				loop x (y+1)
	loop 0 0

Calculate the sum s of the integer list or array x.
List.fold_left (+) 0 l
Declare the floating point number y and initialize it with the value of the integer x .
let y = float_of_int(x)
Create an enumerated type Suit with 4 possible values SPADES, HEARTS, DIAMONDS, CLUBS.
type suit = Spades | Hearts | Diamonds | Clubs
Create t consisting of 3 values having different types.

Explain if the elements of t are strongly typed or not.
let t = 1, 'a', "b"