What's wrong with this imperative version of the factor function in ocaml?

let impfac i = 
  let l = ref i in
  let result = ref 1 in
  let k = ref 2 in
  while !k < !l do
    result := !result * !k
      k:=!k+1
  done;
  !result

      

Error message:

let impfac i = 
  let l = ref i in
  let result = ref 1 in
  let k = ref 2 in
  while !k < !l do
    result := !result * !k
      k:=!k+1
  done;
  !result;;
                Characters 121-123:
      result := !result * !k
                          ^^
Error: This expression is not a function; it cannot be applied
#

      

+3


source to share


1 answer


result := !result * !k
  k:=!k+1

      

You are missing the semicolon at the end of the first line. Because of this, it reads like:

result := !result * (!k k:=!k+1)

      



i.e. it thinks you are trying to call !k

with k:=!k+1

as its argument.

This is why your editor put the line k := !k+1

farther to the right of the line above it. This should have been the first sign that something was wrong with the syntax.

+7


source







All Articles