title: SML/NJ while loop
body: <p>I am really new to SML and I can't figure out how to get answer for the same;</p>
  
  <p>It goes something like:  3^4 &lt; 32 but 3^5 > 32 so my answer is 4 (power of 3), similarly if I have numbers 4 and 63 then 4^2&lt;63 but 4^3>63 so my answer is 2(power of 4).</p>
  
  <p>I have come up with the following code</p>
  
  <pre><code>val log (b, n) =
      let
          val counter = ref b
          val value = 0
      in
          while !counter &gt; n do 
          ( counter := !counter*b 
            value := !value + 1)
      end;
  </code></pre>
  
  <p>So here value is what I need as my answer but I get a lot of errors. I know I am wrong at many places. Any help would be appreciated.</p>
  
  <p>I can perhaps do this the normal ML way but I want to learnt impure ML also...</p>
  
  <pre><code>fun loghelper(x,n,b) = if x&gt;n then 0 else (1+loghelper((x*b),n,b)); 
  fun log(b,n) = loghelper(b,n,b);
  </code></pre>
  
  <p>ok so finally here is the correct code for the while loop and it works as well;</p>
  
  <pre><code>      fun log (b, n) =
              let
                 val counter = ref b
                 val value = ref 0
              in
                 while (!counter &lt;= n) do 
                 (counter := !counter*b; 
                  value := !value + 1);
                 !value
              end;
  </code></pre>