List.iteri and Array.iteri in OCaml¶
This is a small article about two very convenient function in the OCaml language.
If the name OCaml does not ring a bell for you, this article will probably not interest you at all!
These functions are iterators of the following type:
val List.iter : ('a -> unit) -> 'a list -> unit val Array.iter : ('a -> unit) -> 'a array -> unit
They allow to apply a certain function f : “a -> unit which acts by side effects (e.g., printing) to each element of a list or an array.
A better version?¶
But… quite often, we need more than just the value of
x of each element, we might need their index
(for instance to print something like « the ith value contains x[i] »).
So, we would like to have two other iterator functions with the following signature:
val List.iteri : (int -> 'a -> unit) -> 'a list -> unit val Array.iteri : (int -> 'a -> unit) -> 'a array -> unit
Now, f accepts as first argument the index
i corresponding to its second argument the value
That will be very convenient to avoid for loops!
In Python… ?
The common syntax for every iterator is already doing this, e.g. :
for i in [2*i for i in range(8)]: print i
And the enumerate function also allows to iterate on both index and value,
Two functions that took their time to appear¶
So what’s the issue?¶
These two functions are very useful, and so they are used a lot.
During some programming projects and exercises that were asked to me during some courses at ENS Cachan, I have been ask to make my OCaml code work on at least a machine in room 411, machines which were old at this time!
And that’s why in December 2013, we lost a few minutes (during an oral exam) trying to find the source of a bug, and I included a tiny « patch » v3.12 → v4.01.0 which adds manually the code for List.iteri in my project 5.
In particular, this link 6 shows the different modifications on my code, after I saw this bug.
Anyway, we would have liked to get them before!¶
That’s basically what this (useless) article was about.
Array.iter in the doc of the Array module;
List.iter in the doc of the List module;
the diff on array.ml which introduced the Array.iteri function (on the INRIA’s svn repository);
the diff on list.ml which introduced the List.iteri function (on the INRIA’s svn repository);
the written report (in French) for the tiny Laby project implemented in December 2013, as a training for the computer-science oral exam for the highly competitive french national exam to become a prep” school professor (« agrégation »);
the commit reporting compatibility with OCaml v3.12.1 for my Laby project (private).
« But OCaml is lame isn’t it? »
No, do not listen to people saying this, OCaml is awesome!