I have not programmed in prolog, or any other logical programming language, much and I'm trying to get the hang of it, so I am working on an Exercise I found.
The Exercise is: You have a 10X10 grid, and you can only move right or up from a location point in the grid (locations represented as a list with 2 elements [2, 3] where 2 is on the x-axis and 3 is on the y-axis, like a regular graph).
The first step is to define the 2 predicates called step that describe the two types of moves you can make.
Here are the correct step predicates:
step([D1, D2], 'right', [Z, D2]) :- Z is D1 + 1.
step([D1, D2], 'up', [D1, Z]) :- Z is D2 + 1.
With the step predicates above, Let's give an example query below:
step([4, 4], 'up', [4, X]).
This example Query above would return the output below:
X = 5.
The next step is to define a path predicate that is recursive and uses the step predicate to determine the path(s) (represented by a list of steps 'up' or 'right' like ['up', 'up', 'right', 'right']) that should be taken to reach the end point location of the 10X10 grid [10, 10].
Below is an example Query of how this path predicate would give proper output bindings for list of steps.
path([4,4],X).
This example query above should return all the possible bindings of X as a list of steps taken to reach [10, 10]. For example is should return something like X = ['up', 'up', 'up', 'up', 'up', 'up', 'right', 'right', 'right', 'right', 'right', 'right'].
So, I tried to write path predicates to serve this function and I don't understand why my predicate code below is not working and always gives errors. Here is my attempt.
path([StartX, StartY], [Move|Rest]) :- StartX =< 8, StartY =< 8, step([StartX, StartY], Move, [NextX, NextY]), path([NextX, NextY], Rest).
If anyone could explain how I should go about this that would be killer!
Also, there is a final step that is to assume that blockages exist at the Grid points implement blockages: (4,1), (4,2), (6,3), (1,2), (7,6) so that you cannot step pass these blocked spots. The Final step is to write predicates that model these blockages.