Стандартная ошибка ML: оператор и операнд не согласуются

Я хочу написать функцию number_before_reaching_sum, которая принимает int с именем sum и возвращает int n так, чтобы первые n элементов списка складывались меньше суммы, но первые 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) эти две строки неверны.


sml
person mooky Fare    schedule 04.08.2016    source источник
comment
В общем, если вы получаете подобные ошибки в Standard ML, это потому, что вы забыли заключить какое-либо выражение в круглые скобки.   -  person Martin Törnwall    schedule 09.08.2016


Ответы (1)


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 хочет кортеж, но вместо этого вы дали ему функцию».

person sepp2k    schedule 04.08.2016