gpt4 book ai didi

sml - 标准 ML 错误 :operator and operand don't agree

转载 作者:行者123 更新时间:2023-12-01 08:13:44 25 4
gpt4 key购买 nike

我想编写一个函数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)这两行是错误的。

最佳答案

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 想要一个元组,但您给了它一个函数”。

关于sml - 标准 ML 错误 :operator and operand don't agree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38775436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com