我正在試驗Core的List.fold_left
。使用無標籤的Core.Std.List.fold_left
# List.fold_left;;
- : 'a Core.Std.List.t -> init:'b -> f:('b -> 'a -> 'b) -> 'b = <fun>
當我指定的標籤,它工作正常:
# List.fold_left [1;2;3] ~init:0 ~f:(+);;
- : int = 6
,但我得到了不同的結果,當我不指定標籤:
# List.fold_left [1;2;3] 0 (+);;
- : init:(int -> (int -> int -> int) -> '_a) ->
f:((int -> (int -> int -> int) -> '_a) ->
int -> int -> (int -> int -> int) -> '_a) ->
'_a
= <fun>
與其他部分應用程序也屈服非直覺類型。爲什麼我可以在列表參數後添加任意數量的0?
# List.fold_left [1;2;3] 0;;
- : init:(int -> '_a) -> f:((int -> '_a) -> int -> int -> '_a) -> '_a = <fun>
# List.fold_left [1;2;3] 0 0;;
- : init:(int -> int -> '_a) ->
f:((int -> int -> '_a) -> int -> int -> int -> '_a) -> '_a
= <fun>
一些其他功能表現出相同的行爲:
# let app ~f ~x = f x;;
val app : f:('a -> 'b) -> x:'a -> 'b = <fun>
# app (fun x -> x + 1) 1;;
- : f:('a -> (int -> int) -> int -> 'b) -> x:'a -> 'b = <fun>
但是,當沒有給出標籤應用了一些標記功能能夠返回預期的結果。例如:
# List.map;;
- : 'a Core.Std.List.t -> f:('a -> 'b) -> 'b Core.Std.List.t = <fun>
# List.map [1;2;3] (fun x -> x + 1);;
- : int Core.Std.List.t = [2; 3; 4]
爲什麼當沒有給出標籤應用了一些函數返回值是非直觀的,而其他工作如預期?