我正在尝试在ReasonML中创建一个程序来执行以下操作:
scanRight(add, [1, 1 , 2, 3, 5], 0);
/* Intermediate Steps
[(add 1 (add 1 (add 2 (add 3 (add 5 0))))),
(add 1 (add 2 (add 3 (add 5 0)))),
(add 2 (add 3 (add 5 0))),
(add 3 (add 5 0)),
(add 5 0),
0] */
/* Final Output */
[12, 11, 10, 8, 5, 0]
这是我编写的过程:
let rec scanRight: (('a, 'b) => 'b, list('a), 'b) => list('b) = (proc, lst, base) =>
switch(lst){
|[] => [base]
|[hd, ... tl] => List.flatten([proc(hd, scanRight(proc, tl, base)), scanRight(proc, tl, base)])
};
但是,它一直给我这个错误:
2 │ switch(lst){
3 │ |[] => [base]
4 │ |[hd, ... tl] => **List.flatten([proc(hd, scanRight(proc, tl, base)),
scanRight(proc, tl, base)])**
5 │ };
6 │
This has type:
list('a)
But somewhere wanted:
list(list('a))
The type variable 'a occurs inside list('a)
我不确定在list(list('a))
的确切位置,因为我的类型签名指示输出应为list('a)
类型。
编辑:当我取出List.flatten()
时会引发此错误:
1 │ let rec scanRight: (('a, 'b) => 'b, list('a), 'b) => list('b) = **(proc,
lst, base) =>
2 │ switch(lst){
3 │ |[] => [base]
4 │ |[hd, ... tl] => [proc(hd, scanRight(proc, tl, base)), scanRight(pr
oc, tl, base)]
5 │ };**
This has type:
(('a, list('b)) => list('b), list('a), list('b)) => list(list('b))
But somewhere wanted:
(('a, list('b)) => list('b), list('a), list('b)) => list('b)
The type variable 'b occurs inside list('b)