Sundaze123
Sundaze123

Reputation: 23

Prolog dealing with 2D arrays and unbound variables

I'm familiar with the language java, but I'm having trouble understanding the language prolog. I want to create a function matrix(N,M) that should return true only if M is a 2D-array of N x N & each cell in M contains an unbound variable. Also, I'm not sure I understand what an unbound variable is as well & how would my method differ if I wanted each cell to contain a specific sequence.

Any help would be appreciated.

Upvotes: 0

Views: 432

Answers (1)

lurker
lurker

Reputation: 58244

This answer is an attempt to give you some ideas to get you started without just handing over a complete solution.

To learn Prolog, forget all about Java, C, C++, or any other imperative language. Prolog is different. It's best not to try to map what you know in those languages to Prolog. Prolog doesn't have function but predicates. These are rules that describe something. An unbound variable is a variable that has not been unified (or bound) to a specific value.

Run Prolog and get to a Prolog prompt and try some things to begin seeing how Prolog works.

For example, suppose you are interested in lists with length N. Prolog has a predicate called length/2 (length has 2 arguments):

1 ?- length([a,b,c], N).
N = 3.

The list [a,b,c] has 3 elements. I could also use variables:

2 ?- length([X,Y,Z], N).
N = 3.

The variables X, Y, and Z are unbound. What makes length/2 a predicate and not a function is that it is just describing a relationship between the length and the list. You could also write:

3 ?- length(L, 3).
L = [_5112, _5118, _5124].

Prolog finds the solution, L, for a list of length 3. The variables _xxxx are anonymous unbound variables.

Another example:

4 ?- length([X,Y,Z], N), Y = 2, write([X,Y,Z]).
[_5850,2,_5862]
Y = 2,
N = 3.

Here, I am saying I want a list [X,Y,Z], length N, value of Y unifiable with 2. I write it out. When I write the list, I get [_xxxx, 2, _xxxx], so the middle value is bound and the other two are not. You also want to be careful here. Notice I did not say, assign 2 to Y. That's not what = does. =/2 is a unification operator in Prolog. It succeeds if the arguments are unifiable. If there are variables involved, Prolog will bind the variables to values that make it unifiable, if possible. In this case, Y = 2 will bind Y with value 2 to make it succeed, then move on to the next statement. =/2 is completely reflexive, so I could just as well have written 2 = Y and there would be absolutely nothing wrong with writing it that way. Our imperative programming brains seem to be more comfortable seeing it as Y = 2, although it can be misleading.

Now extend this to your NxN matrix. It's a list of length N whose elements are lists of length N. One element can be determined by, length(Row, N). If you say matrix(N, M) then you also know that length(M, N) should be true.

To solve the problem totally, you'll need to enumerate the rows of M (the elements of list M) and enforce the rule of length on that row. You can also have a look at maplist/3 which can make this easier.

Notice that I did not include any bindings for variables, so this will all be unbound variables. If you want to bind the variables, I can't offer any more input than the above without having more information about what your rules are for binding the specific values (e.g., are you given a full bound matrix, or are you just wanting to bind specific entries? etc).

If you want more examples of list processing, I highly recommend perusing the problems and solutions given here: 99 Prolog Problems

Upvotes: 2

Related Questions