Искам да напиша функция number_before_reaching_sum
, която приема int, наречено sum, и връща 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
Отпечатва се съобщението за грешка:
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)
тези 2 реда са грешни.