views:

435

answers:

3

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.

+1  A: 

After you wrote let foo = bar, you must write then in and write one more "assignment". Compiler expects in but encounters done, which is "unexpected".

You should remove let or add in, depending on what your aim is.

Pavel Shved
+3  A: 

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.

newacct
A: 

Are you doing a programming course at McMaster University? I think I'm doing the same thing you are and I'm encountering similar problems.

Roger Davis
Yup, I solved this problem, im having trouble with recursion now , remulos_rec
Faisal Abid