标准ML错误:运算符和操作数不一致

moo*_*are 3 sml

我想编写一个函数number_before_reaching_sum,它接受一个名为sum的int,并返回一个int n,使得列表的前n个元素加上小于sum,但列表的前n + 1个元素加上sum或更多.这是我的代码

 fun number_before_reaching_sum(sum:int,lists:int list)=
 let
 val sum_list=0
 val n=0
 in
     let fun list_compute(sum_list:int,lists2:int list,n:int)=
           let val sum_list2=sum_list+(hd lists2)
           in if sum_list2>=sum
                  then (sum_list2,n+1)
              else (#1 list_compute(sum_list2,tl lists2,n+1),#2 list_compute(sum_list2,tl lists2,n+1))
               end
     in   #2 list_compute(sum_list,lists,n)
     end
 end
Run Code Online (Sandbox Code Playgroud)

打印出错误消息:

    hw1_1.sml:67.14-67.97 Error: operator and operand don't agree [type mismatch]
  operator domain: {1:'Y; 'Z}
  operand:         int * int list * int -> 'X
  in expression:
    (fn {1=1,...} => 1) list_compute
hw1_1.sml:67.14-67.97 Error: operator and operand don't agree [type mismatch]
  operator domain: {2:'Y; 'Z}
  operand:         int * int list * int -> 'X
  in expression:
    (fn {2=2,...} => 2) list_compute
hw1_1.sml:69.11-69.44 Error: operator and operand don't agree [type mismatch]
  operator domain: {2:'Y; 'Z}
  operand:         int * int list * int -> int * int
  in expression:
    (fn {2=2,...} => 2) list_compute
Run Code Online (Sandbox Code Playgroud)

我想不通为什么(#1 list_compute(sum_list2,tl lists2,n+1),#2 list_compute(sum_list2,tl lists2,n+1))#2 list_compute(sum_list,lists,n) 这2条线是错误的.

sep*_*p2k 5

f g(x,y)被解析为(f g) (x,y),而不是f (g (x,y)).所以你想添加如下括号:

#1 (list_compute (sum_list2,tl lists2,n+1))
Run Code Online (Sandbox Code Playgroud)

否则它会尝试应用于#1该功能list_compute.错误消息是编译器告诉你" #1想要一个元组,但你给了它一个函数".