Function Ocaml Generate combination


I do not find how to do a function that generates all possible combinations from a list of digits contained in an X variable.
For example, my variable X: [1; 2; 3; 4] and I would like it to generate me the combinations (in list form) such as: [[1; 1; 1; 1] [2; 1; 1; 1] [1; 2; 1; 1]… [4;4;4;4] etc …] I would like all possible combinations from a list. Thank you for your help


first, have you an idea of the algorithm that could be used?

Hint: how can you get the successor of an item such as

  • [1; 1; 1; 1]
  • [4; 1; 1; 1]
    then of some item with a 4 inside at any other position in the list:
  • [1; 4; 1; 1] …

Can you also move this topic from Community to Learning?


I don’t have any idea… I don’t understand…


From these definitions:
let a1 = [1; 1; 1; 1]
let a2 = [2; 1; 1; 1]
let a3 = [3; 1; 1; 1]
let a4 = [4; 1; 1; 1]

Can you write an OCaml function f such that:

f a1 = a2
f a2 = a3
f a3 = a4

What is your current knowledge about OCaml programming language?


I have a very bad level in Ocaml, sorry…
I don’t know how to do this


A simpler start might be to make first a pair function. If you have two lists l and l2
, can you make a function that lists all pairs of one element of l and one element of l2 ?


First, tools.
do you have OCaml available in some way? (CLI, emacs, vim)
If this is not the case, you can start to play with tryocaml. gives installation instructions but if you don’t know anything about setting up an OCaml environment, you will probably get small difficulties.

If you are on Linux, you can get OCaml quickly installed with your package manager. However this “Toplevel” is very basic. If you can install opam, then install ocaml-top from opam. This tool intended for education will be a good basecamp.

If you are on Windows, ocaml-top is also available and easy to install.

Second, the OCaml language itself.
Start to discover OCaml playing with its basics constructs.
Play with the let construct and with built-in types: int, char, string, bool (then with bytes, float, unit, exn) and see how the type system works. See Core Library

For example, define a string:

let st1 = "abc"
val st1 : string = "abc"

then try the functions available from the module String

String.length st1;;
- : int = 3 is a very short and simple introduction.

Then do some of the exercises that are presented in

Playing with string you should soon need to play also with char type: Module Char.

Then you will also need to understand list type which is intensively used, and that is related to your first question (int list).
See Module List

You will find many pointers. See Beginner’s guide to OCaml beginner’s guides which is fine and quite funny.

If you want a first complete guide to OCaml: this one is fine Introduction to the Objective Caml Programming Language. You will find all that you need and it only depends on OCaml stlib so no dependencies headache. It should help you organize your learning in a progressive and coherent way.

After that, you will be prepared to go to the next level with RWO which I consider too much complex for a real beginner.

Introduction to Functional Programming in OCaml (fun MOOC) is great but also too complex for a beginner. And surprisingly, the course is only open two months a year (which is just incredible regarding people who wants to learn OCaml…). As with RWO, you should consider this mooc after having completed Introduction to the Objective Caml Programming Language.

Have fun!

OCaml refman
ocaml-4.07-refman.pdf offers all in one page.
manual-ocaml is the web version.