Reputation: 649
I am new to OCaml.
I am working on some code in which the following function is nested inside another function, which must return an int type value.
Asn(x,e) ->
let v = eval e in
Array.set vararr x+1 v
vararr is a global array already declared and initialised above.
In this function, I want to set an element of the array using "Array.set vararr x+1 v", but I want the function to return the value v. How can I do that?
Update:
I changed it to:
Asn(x,e) ->
let v = eval e in
Array.set vararr x+1 v;
v
The error for "Array.set vararr x+1 v;" is:
This expression has type int -> unit
but an expression was expected of type int
I think the problem isn't what Asn returns. The problem is with what "Array.set vararr x+1 v;" returns. This is very strange! Why does "Array.set vararr x+1 v;" have to return an int type value?
Upvotes: 0
Views: 970
Reputation: 66793
I think you want this:
let v = eval e in
Array.set vararr (x + 1) v;
v
Update
Don't forget parentheses around (x + 1)
.
The expression:
Array.set vararr x+1 v
Is parsed like this:
(Array.set vararr x) + (1 v)
Function application in OCaml has very high precedence.
The +
operator wants an int at the left but you have a function there.
Generally speaking, you just need to get used to OCaml syntax. It's not so bad after a little experience.
Upvotes: 4