I executed the following command in utop with Jane street Base library installed
open Base;;
List.fold;;
It prints
- : 'a list -> init:'accum -> f:('accum -> 'a -> 'accum) -> 'accum = <fun>
I interpret this as the fold function first parameter is a list of values. second is the initial value and 3rd is the fold function. The folder function whose first parameter is the accumulator and second parameter is the item from the list being folded.
With this interpretation I write this code
List.fold [Some 1; None; None; Some 2] 0 (fun accm x -> match x with | None -> accum | Some x -> accum + x)
This doesn't work and it prints something weird on the screen
- : init:(int -> (int -> int option -> int) -> '_weak2) ->
f:((int -> (int -> int option -> int) -> '_weak2) ->
int option -> int -> (int -> int option -> int) -> '_weak2) ->
'_weak2
= <fun>
Now If I change my code in utop to use named parameters
List.fold [Some 1; None; Some 2] ~init:0 ~f:(fun accum x -> match x with | None -> accum | Some x -> accum + x);;
Now it works. What's going on? I passed the parameters in the same order as the documented signature suggested? then why am I being forced to use named parameters?
Argument labels can only be omitted in non-ambiguous full applications.
In particular, this is never the case for labelled functions with a polymorphic return type like fold
. Indeed, for such function, there is no clear notion of full application
.
For instance, if you look at your initial application:
List.fold [Some 1; None; None; Some 2] 0
(fun accm x -> match x with | None -> accum | Some x -> accum + x)
it could be completed into the following full application:
List.fold
[Some 1; None; None; Some 2] 0
(fun acc x -> match x with None -> acc | Some x -> acc + x)
~init:(fun x f -> f x None)
~f:(fun f _ -> f )
Consequently, your initial code
List.fold [Some 1; None; None; Some 2] 0
(fun accm x -> match x with | None -> accum | Some x -> accum + x)
is not a full application, but a valid partial application where the labelled arguments are still missing.