Rdhao
Rdhao

Reputation: 107

I have a Strange output i do not expect. its similar but.. well odd

So ive taken up Prolog and im trying to learn the ropes of it. Right now im working on a assignment set by the book and ive come quite far. My output nearly matches that which is required but somehow its makes the last few lests that come out come out in a head|tail format in which the tail is a row of dots?

The code ive written looks like this:

%prefix methode. E1 + L = L1
% It takes L, adds E1 in front of it and makes it L1.
prefix(_,[],[]).
prefix(El,[LHead|LTail],[Q|L1]) :- Q = [El|LHead], prefix(El,LTail,L1).


gray(0,[[]]).
gray([],[]).

% this should generate a list of all the gray
% code possibilities with the defined length

gray(N,Lijst) :-  N > 0, X is N - 1, gray(X,Prevlist), 
reverse(Prevlist, RevPrevlist), prefix(0,Prevlist,Deel1),
prefix(1,RevPrevlist,Deel2), append(Deel1,Deel2,Lijst).

Now the thing it should do is if I give him for example -?gray(3,List). It should tell me:

List = [[0,0,0],[0,0,1],[0,1,1],[0,1,0],[1,1,0],[1,1,1],[1,0,1],[1,0,0]].

However what it tells me instead is:

List = [[0, 0, 0], [0, 0, 1], [0, 1, 1], [0, 1, 0], [1, 1, 0], [1, 1, 1], [1, 0|...], [1|...]].

As you can see with the output I get my last two lasts have a head|tail construction with a tail consisting of dots. However I do not know what this means or what I did wrong in my code.

Can anybody elaborate why I am getting the dots, and maybe steer me in the right direction to help me solve the problem?

P.S. my apologies if I might've been unclear on certain parts. If I've been so, please say so and I'll try to edit/elaborate.

Upvotes: 0

Views: 88

Answers (1)

magus
magus

Reputation: 1357

I just ran this through on swi-prolog:

2 ?- grey(3, List).
Correct to: "gray(3,List)"? yes
List = [[0, 0, 0], [0, 0, 1], [0, 1, 1], [0, 1, 0], [1, 1, 0], [1, 1, 1], [1, 0|...], [1|...]] [write]
List = [[0, 0, 0], [0, 0, 1], [0, 1, 1], [0, 1, 0], [1, 1, 0], [1, 1, 1], [1, 0, 1], [1, 0, 0]]

Notice where it said [write].. I press the 'w' button at that point which forces swi-prolog to dump out the whole list, not just appreviate it with the ...

Not sure if that helps, or if the answer is correct.

If not, suggest explaining the problem/assignment itself in a bit more detail. You've explained your solution in some detail, but it's not clear what the problem is ? To me, anyway.

Edit I see you have edited to clarify.. yep, just looks like prolog was abbreviating the results visually, but was actually holding the correct result. A 'display only' issue.

Upvotes: 3

Related Questions