I'm currently trying to write a program in Prolog which prints out a table like
x x x x
x x x x
x x x x
5 6 7 8
x x x x
x x x x
x x x x
1 2 3 4
You can imagine like there are shelves at a supermarket, and x
s are empty shelves and shelves that do have goods are assigned an number. So 1
represents the shelf whose shelfID is 1, 6
is the shelf whose shelfID is 6 etc.
I want to write a function that iterates through an array where all the shelf locations and shelfIDs are stored and prints out the shelfID. In case it's an empty shelf that doesn't have an ID it should printed out as x
, just like displayed above.
The program successfully prints out the shelfID when the exact match is provided, but it doesn't handle the case scenarios where the shelf is empty and doesn't have an ID.
How do I do it?
I've already checked the tutorial video https://www.youtube.com/watch?v=SykxWpFwMGs&t=352s and another stack overflow post 'if' in prolog?, but they don't seem to be applicable to my case, because this involves more than 2 possibilities (when printing out the line (5, 6, 7, 8) the line (1, 2, 3, 4), and the lines that print (x, x, x, x), not just if-then-else
, which essentially handles just two cases.
The code that I've written so far looks like this. (I know the syntax may be completely messed up. I haven't found a good documentation)
% shelves locations
map([point(1,1), point(1,2), point(1,3), point(1,4),
point(2,1), point(2,2), point(2,3), point(2,4),
point(3,1), point(3,2), point(3,3), point(3,4),
point(4,1), point(4,2), point(4,3), point(4,4),
point(5,1), point(5,2), point(5,3), point(5,4),
point(6,1), point(6,2), point(6,3), point(6,4),
point(7,1), point(7,2), point(7,3), point(7,4),
point(8,1), point(8,2), point(8,3), point(8,4)]).
print_shelf(1,4):-
ShelfID is 5,
write(ShelfID).
print_shelf(2,4):-
ShelfID is 6,
write(ShelfID).
print_shelf(3,4):-
ShelfID is 7,
write(ShelfID).
print_shelf(4,4):-
ShelfID is 8,
write(ShelfID).
print_shelf(1,8):-
ShelfID is 1,
write(ShelfID).
print_shelf(2,8):-
ShelfID is 2,
write(ShelfID).
print_shelf(3,8):-
ShelfID is 3,
write(ShelfID).
print_shelf(4,8):-
ShelfID is 4,
write(ShelfID).
print_shelf(Other, Hoka):-
Hoka =/= 4,
Hoka =/= 8,
NonID is 'x',
write(NonID).
%print_map... yet to be implemented...
NOTE: If you are to reply to this post, please kindly write your answer by posting an answer with some concrete examples that I can relate to, not just writing a comment on my question post. If you feel like writing a comment like "Just visit www.learnprolognow.org", DO NOT write a comment in the first place. I'm not asking for such a reply. The whole point of writing this post is to find an explanation that I couldn't find in the existing online resources. Please write a comment instead of posting an answer only if you are trying to clarify what my question is or what I meant in a particular part in the code that I provided before you post an answer. Thanks in advance