Reputation: 91
I'm writing a sudoku solver in Prolog. The sudoku itself is given as a vector of vectors:
P =
[[1,_,_, _,_,_, _,_,_],
[_,_,2, 7,4,_, _,_,_],
[_,_,_, 5,_,_, _,_,4],
[_,3,_, _,_,_, _,_,_],
[7,5,_, _,_,_, _,_,_],
[_,_,_, _,_,9, 6,_,_],
[_,4,_, _,_,6, _,_,_],
[_,_,_, _,_,_, _,7,1],
[_,_,_, _,_,1, _,3,_]].
I'm converting this to a matrix with the following code:
convert_to_matrix(P, Puzzle) :-
dim(Puzzle, [9, 9]), % create square matrix
( multifor([I, J], 1, 9), % fill array
param(P, Puzzle)
do
nth1(I, P, RowI),
nth1(J, RowI, Elem),
subscript(Puzzle, [I , J], Elem)
).
This works fine for the first 8 rows and colums, but fails for everytime either I or J is 9. doing nth1(9, P, RowI) makes RowI a brand new variable instead of the 9th vector/row. Doing nth1(9,RowI, Elem) doesn't return the 9th element (for the eight row it should return 1, but returns a new Variable). Is this a bug in Eclipse-clp or am I missing something?
Upvotes: 1
Views: 88
Reputation: 5034
You are absolutely right, this is indeed a bug in nth1/3, introduced and only present in version 7.0#45! You can replace your code with something like
lol_matrix(Xss, M) :-
length(Xss, N),
dim(M, [N,N]),
( foreach(Xs,Xss), foreacharg(Row,M) do
array_list(Row, Xs)
).
However, the easiest way is to write your data directly as a matrix (as done in this example code), then you don't need any conversion at all:
P = []([](1,_,_, _,_,_, _,_,_),
[](_,_,2, 7,4,_, _,_,_),
...
[](_,_,_, _,_,1, _,3,_)).
By the way, ECLiPSe bugs can be reported via [email protected]
Upvotes: 2