0
101 Jan 01, 2008 at 21:23

Hi,
I am battling with prolog like a weary adversary, and i’d like some help or even just to tell me where i’m going wrong. I’ve got a list of 6 items which i then turn into 3 pairs of colours. I then try and run a check on it to see if it identifies the pair of colours within the list. When i run it, it converts the pairs, does the check, but only finds the pair when they are the first item in the list. I am thinking my tail recursion is wrong. If anyone could help, greatly appreciated. Thanks. (Code below)

makepairs([],[]).
makepairs([X1,X2|T],[[X1,X2]|B]):-makepairs(T,:).

isredwhite([], 0).
isredwhite([X1|_], XP):-member(red,X1),member(white,X1), XP is 1.
isredwhite([_|T], 0):-isredwhite(T, 0).

:- dynamic cube/6.
initialize:-
makepairs([red,whitea,red,white,red,green],Xnew),
assert(cube(1,Xnew,0,0,0,0)),write(Xnew),nl,
isredwhite(Xnew, Xhasredwhiter),write(Xhasredwhiter),nl,
cube(2,Xnew,0,0,0,0)=L,
write(L),nl.

sortcubes:- initialize.

Thanks again.

2 Replies

0
101 Jan 02, 2008 at 23:27

Hi.

isredwhite([_|T], 0):-isredwhite(T, 0).


You don’t know the result of the check, but you say that it’s 0.
You should write

isredwhite([_|T], XP):-isredwhite(T, XP).


You can write

isredwhite([X1|_], 1):-
member(red,X1),member(white,X1), !.


The cut is to stop bactracking because you have found.
Try this :

initialize(Xhasredwhiter) :-
makepairs([red,whitea,red,white,red,green],Xnew),
isredwhite(Xnew, Xhasredwhiter).

0
101 Jan 03, 2008 at 00:22

Ah yes that works now, thanks, quite some time i’ve been puzzled over this. Thankyou