I am going through the exercises of learn-reasonml-workshop, it’s been going well and I feel good about finally getting comfortable with the language after previously only reading about it.
In the 21st exercise the task is two implement Map, Iter and Filter functions, using List.fold_left.
/* Now let’s use List.fold_left to write some other useful List functions. */
I have managed to implement Iter and Filter using List.fold_left, but I can’t find a way to do it for Map, given the defined signature:
let map: ('a => 'b, list('a)) => list('b);
I did manage to write an implementation without fold_left:
let map = (fn: 'a => 'b, xs: list('a)) : list('b) => {
let rec aux = xs =>
switch (xs) {
| [] => []
| [y, ...ys] => [fn(y), ...aux(ys)]
};
aux(xs);
};
But, if someone knows how to do this with fold_left I would like to have a look at the implementation.
.