Coming from a Java and C background, grasping some concepts of Ocaml has been quite interesting. One such is, getting a/multiple statement to run inside a for loop.
let test_method (x:vector list)(vec:vector) =
if List.length x != 0 then
{a=0.;b=0.} (* Return a vector of 0,0 *)
else
for i = 0 to List.length x do
let key = {a=(List.nth x i).a;b=(List.nth x i).b}
done;;
the error that i get is done;; is an unexpected token. Why is this happening? this happens even if i do this
let test_method (x:vector list)(vec:vector) =
if List.length x != 0 then
{a=0.;b=0.} (* Return a vector of 0,0 *)
else
for i = 0 to List.length x do
let key = {a=(List.nth x i).a;b=(List.nth x i).b} in
let value = vec_neg key;
done;;
where vec_neg is a method that works fine that negates the vector type.
Any help would be greatly appreciated.
let expressions must have a body. (i.e. let var = val in body) (except let definitions at the top level of a module, which implicitly use the rest of the module as the body) let creates a local binding which is in scope inside the body. What's the point of doing that if you're not using it (i.e. don't have a body)? Plus every expression in the language has to evaluate to a value. A let expression evaluates to whatever the body evaluates to. That's why it needs a body.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With