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?
List. map returns a new list formed from the results of calling the supplied function. List. iter just returns () , which is a specifically uninteresting value.
Lists in OCaml are immutable. So you can't remove things from them. You normally create another list that doesn't have the things you don't want. For this, you would use List.
:: means 2 camel humps, ' means 1 hump!
So fold_left is "folding in" elements of the list from the left to the right, combining each new element using the operator.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With