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.
It is not finished yet, because the squares still have to be specified manually, but I want to continue to the scala chapter, and maybe will have a look at it later on.
And here are the ideone run results :
conclusion
All in all I liked prolog, but there was a lot of frustration too.
First of all, the bugs in the Mac version(from homebrew on OSX Lion) drove me crazy. But also the programming model of prolog was a real challenge for me. Some things seemed odd, and a lot more difficult than in procedural languages.
But at the end, I liked prolog a lot because for me it was the first time I had the feeling that the computer was solving the problem, instead of me telling the computer how to solve it.
I really would like to apply something like prolog on a real project, but prolog is clearly not a Jack of all trades.
