EDIT: See my answer below for why I am a fool. There is still a riddle in this that I would love to have answered though.
I've been stuck on this for far too long. I'm trying to print out a sudoku solution in a nice looking grid.
I think I'm running into problems because I don't understand some key part of how pattern matching works in Prolog.
Without further ado, my code:
prettier_print([]).
prettier_print([Puzzle]) :- prettier_print(0, [Puzzle]).
prettier_print(0, Puzzle) :-
writeln('┌───────┬───────┬───────┐'),
prettier_print(1, Puzzle).
prettier_print(4, Puzzle) :-
writeln('│───────┼───────┼───────│'),
prettier_print(5, Puzzle).
prettier_print(8, Puzzle) :-
writeln('│───────┼───────┼───────│'),
prettier_print(9, Puzzle).
prettier_print(12, []) :-
writeln('└───────┴───────┴───────┘').
prettier_print(N, [Col1, Col2, Col3, Col4, Col5, Col6, Col7, Col8, Col9 | Puzzle]) :-
member(N, [1,2,3,5,6,7,9,10,11]), % tried this when the line below did not work
% N =\= 0, N =\= 4, N =\= 8, N =\= 13,
format('│ ~d ~d ~d │ ~d ~d ~d │ ~d ~d ~d │~n', [Col1, Col2, Col3, Col4, Col5, Col6, Col7, Col8, Col9]),
succ(N, N1),
prettier_print(N1, Puzzle).
Here is the call:
prettier_print(0, [1,2,3,4,5,6,7,8,9,
2,2,3,4,5,6,7,8,9,
3,2,3,4,5,6,7,8,9,
4,2,3,4,5,6,7,8,9,
5,2,3,4,5,6,7,8,9,
6,2,3,4,5,6,7,8,9,
7,2,3,4,5,6,7,8,9,
8,2,3,4,5,6,7,8,9,
9,2,3,4,5,6,7,8,9]).
And here is the output:
┌───────┬───────┬───────┐
│ 1 2 3 │ 4 5 6 │ 7 8 9 │
│ 2 2 3 │ 4 5 6 │ 7 8 9 │
│ 3 2 3 │ 4 5 6 │ 7 8 9 │
│───────┼───────┼───────│
│ 4 2 3 │ 4 5 6 │ 7 8 9 │
│ 5 2 3 │ 4 5 6 │ 7 8 9 │
│ 6 2 3 │ 4 5 6 │ 7 8 9 │
│───────┼───────┼───────│
│ 7 2 3 │ 4 5 6 │ 7 8 9 │
│ 8 2 3 │ 4 5 6 │ 7 8 9 │
│ 9 2 3 │ 4 5 6 │ 7 8 9 │
└───────┴───────┴───────┘
true ;
false.
The problem is it doesn't JUST return with true, I have to press ;, and then it returns false. This in turn means that my prettier_print/1 rule doesn't work properly.
I think I know enough to figure out that this means:
Prolog is backtracking and trying another interpretation of my rules (seeing if there is anything else it can unify with, if I'm understanding my terms correctly). This finds one other thing to unify with that, but that fails immediately. Is that right?
I want there to be only one possible interpretation. How can I fix my function to mean that?
Thank you for any help, this has me feeling like such a fool!