while
loop belongs to imperative programming part in OCaml.
Basically, you can't modify immutable variables in while
or for
loops or anywhere.
To let a variable to be mutable, you need to define it like let var = ref ...
. ref
is the keyword for mutables.
Read these two chapters:
- https://realworldocaml.org/v1/en/html/a-guided-tour.html#imperative-programming
- https://realworldocaml.org/v1/en/html/imperative-programming-1.html
You can define x,vn,v
as ref
s, but I guess it will be ugly.
I suggest you think your code in a functional way.
Since you haven't placed functions ens
etc here, I can't produce an example refine for u.