It's currying. Essentially, List.fold_right
has a type of ('a -> 'b -> 'b) -> 'a list -> 'b
, and this definition uses that operation with a function type for 'b
.
You can see how this can produce "four arguments" for List.fold_right
if you substitute 'c -> 'd
for 'b
, skipping redundant parens:
('a -> 'c -> 'd -> ('c -> 'd)) -> 'a list -> 'c -> 'd
So the List.fold_right
folds over the list to produce a function, which is applied to one argument to produce the final result. To make that clearer, let's rewrite it with a named temporary:
let foldl f init list =
let fold_result =
List.fold_right
(fun elt g acc -> g (f acc elt))
list
(fun x -> x) in
fold_result init