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

# Programming-Idioms.org

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

n_hello_worlds 10``````
3
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"``````
4
Create a function which returns the square of an integer
``let square x = x*x``
5
Declare a container type for two floating-point numbers x and y
``````type point = {
x: float;
y: float;
}``````
6
Do something with each item x of the list (or array) items, regardless indexes.
``items |> List.iter do_something``
7
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
)``````
8
Create a new map object x, and provide some (key, value) pairs as initial content.
``````module StringMap = Map.Make(String)

let x =
StringMap.empty
9
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``````
12
Check if the list contains the value x.
list is an iterable finite container.
``List.mem x list``
17
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;
}``````
19
Reverse the order of the elements of the list x.
This may reverse "in-place" and destroy the original ordering.
``List.rev x``
20
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
else
(* If found, return it *)
if array.{x,y} = value then
(x,y)
(* Otherwise go to next col *)
else
loop x (y+1)
in
loop 0 0

``````
54
Calculate the sum s of the integer list or array x.
``List.fold_left (+) 0 l``
79
Declare the floating point number y and initialize it with the value of the integer x .
``let y = float_of_int(x)``
122
Create an enumerated type Suit with 4 possible values SPADES, HEARTS, DIAMONDS, CLUBS.
``type suit = Spades | Hearts | Diamonds | Clubs``
220
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"``