Why is there a let in OCaml's List.map?

functional-programming, ocaml, syntax

Solution

I believe it is there to guarantee an order of function application for the map. The order of evaluation of simple expressions in OCaml is unspecified, so without the `let` the order of applications of `f` to the elements of the list would be unspecified. Since OCaml is not a pure language, you really would like the order to be specified (`f` is called on the head of the list first, and so on recursively).

Problem

In OCaml 3.12.1, `List.map` is written as follows: ``` let rec map f = function [] -> [] | a::l -> let r = f a in r :: map f l ``` I'd expect that last line to be written as `| a::l -> f a :: map f l`, but instead, there is a seemingly useless `let` binding. Why?

Original source