Reputation: 1
I was working on this algorithm to make a magic matrix (of uneven size) but i'm stuck here:
let constr mat =
let n = Array.length mat in
let b = ref (n-1)
and c = ref (n/2) in
for i = 1 to (n*n) do
if mat.(!b).(!c) <> 0 then b:= ((!b-1) mod n)
else b:= ((!b+1) mod n) ; c:= ((!c+1) mod n) ;
mat.(!b).(!c) = i done;
mat;;
It's using a common method of magic matrix creation. The issue is that i don't know the syntax I have to use in order to give a value to a box of my matrix... Can anybody help me? The error message is :
Line 8, characters 2-19:
Warning 10 [non-unit-statement]: this expression should have type unit.
Upvotes: 0
Views: 276
Reputation: 36536
To expand slightly on Jeffrey's answer, the for
loop in OCaml is an imperative feature, and it's all about side effects rather than generating new values. A for loop doesn't return a value to the program.
As such, expressions which do not have side-effects are allowed in, but also useless in a for loop. That's why you get a warning about what you've done, but the code does compile.
mat.(!b).(!c) = i
Is a boolean expression comparing mat.(!b).(!c)
to i
for equality. In its current context in the for loop, the program would do exactly the same thing if that expression were commented out or removed altogether.
Warnings like this can assist you with identifying code that's probably "wrong" in terms of solving your problem, but still valid code.
Upvotes: 2