标准ML错误:操作员和操作员不同意

时间:2016-08-04 19:04:19

标签: sml

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

 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

打印出错误消息:

    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

我无法弄清楚为什么(#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) 这两行是错误的。

1 个答案:

答案 0 :(得分:5)

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

#1 (list_compute (sum_list2,tl lists2,n+1))

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