my problem is: While learning Prolog i wanted to make a NxN Sudoku solver. This solver will get the input like
[[1,2,3,4],[3,4,1,2],[2,3,4,1],[4,1,2,3]]
Where some of them might be variables. The solver has to solve that Sudoku. The problem is way smaller:
firstElementsOf([],_).
firstElementsOf([[X|_]|Rest2],Y) :-
firstElementsOf(Rest2,Y2),
append([X],[Y2],NotFlat),
flatten(NotFlat,Y).
This should be the beginning of checking, if every column has distinct numbers. The Y
from firstElementsOf
should contain only the first elements of the given rows. In the Example:
[1,3,2,4]
Sadly, thanks to append, it always adds another empty space to the Y
list.
It gives:
[1,3,2,4,_1320]
Question1: Is there a way to get rid of that _1320
?
Question2: Is this even right? Will there be a way to get the 2nd and 3rd elements of the Input with that?
For question 1: I suppose the error is in
I think should be
Off topic: are you sure that you can't simply write the other clause as follows?
For question 2: I propose a more general predicate: the following
getPosList/3
with support ofgetPosElem/3
It extract a list of all elements in position
Pos
, soit's equivalent to
firstElementOf([[1,2,3,4],[3,4,1,2],[2,3,4,1],[4,1,2,3]], L)
and extract[1, 3, 2, 4]
,extract
[2, 4, 3, 1]
,extract
[3, 1, 4, 2]
,extract
[4, 2, 1, 3]
andor a number greather than 5, return false