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

Creative Commons Attribution-ShareAlike 3.0

Logo

Programming-Idioms.org

  • The snippets are under the CC-BY-SA license.
  • Please consider keeping a bookmark
  • (instead of printing)
Caml
1
Class Def:
creates a new subclass called SudokuSolver with board as a declared variable, as well as the package it will be under.
print_endline "Hello World!"
2
This method sets the internal board to the given 2D array aBoard.
let rec n_hellos
    (n : int)
  : unit =
  if n = 0 then
    ()
  else
    (print_endline "Hello";
    n_hellos (n-1))

n_hello_worlds 10
3
Returns the value at a specific cell. Smalltalk arrays are 1-indexed, so we add 1 to both row and col.
let do_something arg1 arg2: unit =
	print_endline "foo"
4
Creates a copy of the board. Sets the value at the specified cell. Returns the new board (does not mutate the original).
let square x = x*x
5
Checks if num is already present in the specified row
type point = {
	x: float;
	y: float;
}
6
Iterates over all rows in the column. Returns true if num is found.
items |> List.iter do_something
7
Calculates the top-left corner of the 3×3 box. Iterates through all 9 cells in the box. Returns true if num is found.
(* output_elem is a printer for elements of [items] *)
items |> List.iteri (fun i x ->
	printf "%d: %a" i output_elem x
)
8
Combines the three checks. Returns true if num is not in the row, column, or box.
module StringMap = Map.Make(String)

let x =
    StringMap.empty
    |> StringMap.add "one" 1
    |> StringMap.add "two" 2
    |> StringMap.add "three" 3
9
Scans the board linearly (0 to 80). Converts each index to (row, col) using integer division and modulo.Returns the first cell with value 0 (empty), or nil if none found.
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"