# Function Ocaml Generate combination

**URL:** <https://discuss.ocaml.org/t/function-ocaml-generate-combination/3097>\
**Category:** Learning\
**Created:** [December 25, 2018, 10:46am UTC](https://discuss.ocaml.org/t/function-ocaml-generate-combination/3097 "2018-12-25T10:46:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![max](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@max](https://discuss.ocaml.org/u/max)\
**Post date:** [December 25, 2018, 10:46am UTC](https://discuss.ocaml.org/t/function-ocaml-generate-combination/3097/1 "2018-12-25T10:46:42Z")

</div>

Hello,  
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

---

<div class="post-metadata">

**Author:** ![RogerT](https://avatars.discourse-cdn.com/v4/letter/r/5f8ce5/32.png) [@RogerT](https://discuss.ocaml.org/u/RogerT)\
**Post date:** [December 25, 2018, 12:44pm UTC](https://discuss.ocaml.org/t/function-ocaml-generate-combination/3097/2 "2018-12-25T12:44:22Z")

</div>

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

EDIT:  
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?

---

<div class="post-metadata">

**Author:** ![max](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@max](https://discuss.ocaml.org/u/max)\
**Post date:** [January 8, 2019, 1:27pm UTC](https://discuss.ocaml.org/t/function-ocaml-generate-combination/3097/3 "2019-01-08T13:27:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RogerT](https://avatars.discourse-cdn.com/v4/letter/r/5f8ce5/32.png) [@RogerT](https://discuss.ocaml.org/u/RogerT)\
**Post date:** [January 8, 2019, 2:13pm UTC](https://discuss.ocaml.org/t/function-ocaml-generate-combination/3097/4 "2019-01-08T14:13:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![max](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@max](https://discuss.ocaml.org/u/max)\
**Post date:** [January 8, 2019, 2:17pm UTC](https://discuss.ocaml.org/t/function-ocaml-generate-combination/3097/5 "2019-01-08T14:17:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [January 8, 2019, 3:14pm UTC](https://discuss.ocaml.org/t/function-ocaml-generate-combination/3097/6 "2019-01-08T15:14:21Z")

</div>

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` ?

---

<div class="post-metadata">

**Author:** ![RogerT](https://avatars.discourse-cdn.com/v4/letter/r/5f8ce5/32.png) [@RogerT](https://discuss.ocaml.org/u/RogerT)\
**Post date:** [January 8, 2019, 3:28pm UTC](https://discuss.ocaml.org/t/function-ocaml-generate-combination/3097/7 "2019-01-08T15:28:14Z")

</div>

Ok.  
**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](https://try.ocamlpro.com/).

[https://ocaml.org/docs/install.html](https://ocaml.org/docs/install.html) 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](http://opam.ocaml.org/), then install [ocaml-top](https://www.typerex.org/ocaml-top.html) 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](https://caml.inria.fr/pub/docs/manual-ocaml/core.html)

For example, define a string:

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

```

then try the functions available from the [module String](https://caml.inria.fr/pub/docs/manual-ocaml/libref/String.html)

```
String.length st1;;
- : int = 3

```

[https://learnxinyminutes.com/docs/ocaml/](https://learnxinyminutes.com/docs/ocaml/) is a very short and simple introduction.

Then do some of the exercises that are presented in [https://ocaml.org/learn/tutorials/](https://ocaml.org/learn/tutorials/)

Playing with string you should soon need to play also with char type: [Module Char](https://caml.inria.fr/pub/docs/manual-ocaml/libref/Char.html).

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](https://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html)

You will find many pointers. See[Beginner’s guide to OCaml beginner’s guides](http://blog.nullspace.io/beginners-guide-to-ocaml-beginners-guides.html) 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](https://www.seas.upenn.edu/~cis500/cis500-f02/resources/ocaml-intro.pdf). 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](https://realworldocaml.org/) which I consider too much complex for a real beginner.

[Introduction to Functional Programming in OCaml (fun MOOC)](https://www.fun-mooc.fr/courses/course-v1:parisdiderot+56002+session03/about) 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](https://www.seas.upenn.edu/~cis500/cis500-f02/resources/ocaml-intro.pdf).

Have fun!

**OCaml refman**  
[ocaml-4.07-refman.pdf](https://caml.inria.fr/distrib/ocaml-4.07/ocaml-4.07-refman.pdf) offers all in one page.  
[manual-ocaml](https://caml.inria.fr/pub/docs/manual-ocaml/index.html) is the web version.
