Search code examples
functional-programmingsml

Why inside Let while loop does not work SML


i am new at Standard ML and i have a question , at the code below when i run without the let-in structure , the code runs and gives result .When i put the the let-in it shows syntax errors . Can anyone help me ?

fun findInd([],size, value , ans , l , h ) =  ~1
 |  findInd(lista ,size, value , ans , l , 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,size,value,midval,midval+1,h )     
       else                       findInd(lista,size,value,ans,l,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,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 ,0 , 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;

Solution

  • The syntax of let is let <declarations> in <expression> and a while loop is not a declaration. You'll need to move it after the in (like you did with the ifs in the other lets).