Finally I’m able to continue the seven languages in seven weeks book. I didn’t have time before because I became father for the second time. My son Thomas was born on the 24rd of december.
Before this I was still contemplating the day 3 exercises of the prolog chapter. I had the solution in about 10 minutes, because you can get a solution to the sudoku solver by copy pasting the code of the sudoku 4×4 solver and adapting it to solve 6×6 sudokus. But this is not quite satisfactory. First of all it is against the DRY-principle, I did not want to repeat my self 3 times to solve 3 different kind of sudokus. This is for me not the most important reason, because the DRY-principle is sometimes overrated and costs you in the clarity of the code. But more importantly, there is no challenge in the exercise if you just mindlessly copy paste the code to a 6 by 6 and 9 by 9 sudoku.
So I was looking for a more general sudoku solve method that could easily be used to solve NxN sudokus. This was much harder than expected. I got frustrated to the point that I quit several times, but at the same point prolog intrigued me and after a couple of days I wanted to take another try. One of the reasons I got frustrated a lot, were all the bugs in the gnu prolog 4.0 version I installed with homebrew. All arithmetic functions would give me a segmentation error, so even 1+1 came up with this error. After a while I decided to use ideone.com and this worked quite well.
Finally I came up with this solution. I’m sure it is not the best Prolog code you will find to solve the problem, but at least it gave me a better understanding of prolog.
Continue Reading →