如何解决为什么在 Let while 循环内部不起作用 SML
我是 Standard ML 的新手,我有一个问题,在下面的代码中 我在没有引入结构的情况下运行,代码运行并给出结果。当我放置引入结构时 它显示语法错误。有人可以帮我吗?
fun findind([],size,value,ans,l,h ) = ~1
| findind(lista,h) =
let
val midval = Real.round((real l + real h) / real 2)
val Nelem = List.nth(lista,midval)
in
if l<=h then
if Nelem <= value then findind(lista,midval,midval+1,h )
else findind(lista,midval+(~1) )
else
ans
end;
let (* <-- this let gives the problem *)
val s = 0
val sum = ref s
val maxlen = 0
val maxlenptr = ref maxlen
val counter_start = 0
val counter = ref counter_start
val arr = [1,5,~58,~1]
val presum = [~53,~52,1,6]
val minInd = [3,2,0
while !counter < List.length(arr) do (
sum := !sum + List.nth(arr,!counter);
if !sum >=0 then maxlenptr := !counter + 1
else
let
val ind = findind(presum,List.length(arr),s,~1,List.length(arr) + (~1) )
val temp = List.nth(minInd,ind)
in
if ind <> ~1 andalso temp < counter_start then maxlenptr := Int.max(!maxlenptr,counter_start + (~temp))
else ()
end;
counter := !counter + 1
);
val m = !maxlenptr
in (* <--- this in *)
m
end;
解决方法
let
的语法是 let <declarations> in <expression>
,while 循环不是声明。您需要将它移到 in
之后(就像您在其他 if
中处理 let
一样)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。