我试图将一个函数作为参数传递给另一个函数但是在编译时遇到错误。
Error: This function has type 'a -> 'a -> int
It is applied to too many arguments; maybe you forgot a `;'.
我的代码是:
let sort to_do my_list =
let rec change to_do my_list verify newlist = match my_list with
| Empty -> if verify = newlist then newlist else
change to_do newlist newlist Empty
| Item (elem, next) -> change to_do my_list verify (compare to_do elem newlist)
in
change to_do my_list my_list Empty
;;
let rec compare to_do elem my_list = match my_list with
| Empty -> (elem, my_list)
| Item (elem1, Empty) -> if to_do elem elem1 > 0 then Item (elem, elem1) else
Item (elem1, elem)
| Item (elem1, next) -> compare to_do elem next
;;
答案 0 :(得分:2)
在sort
中,您指的是名为compare
的函数。由于其他函数compare
位于下方,因此将解析为Pervasives.compare : 'a -> 'a -> int
(需要两个值并进行比较)。
将compare
功能移到sort
以上可能会解决问题。
答案 1 :(得分:0)
对于有趣的人,我的最终代码是:
let rec comp to_do elem my_list memlist = match my_list with
| Empty -> Item (elem, memlist)
| Item (elem1, Empty) -> if to_do elem1 elem > 0 then Item (elem, memlist) else
rev (Item (elem, (rev memlist)))
| Item (elem1, next) -> comp to_do elem next memlist
;;
let sort to_do my_list =
let rec change to_do my_list verify newlist = match my_list with
| Empty -> if verify = newlist then newlist else
change to_do newlist newlist Empty
| Item (elem, next) -> change to_do next verify (comp to_do elem newlist newlist)
in
change to_do my_list my_list Empty
;;