# Prolog Beginner

2 replies to this topic

New Member

• Members
• 2 posts

Posted 01 January 2008 - 09:23 PM

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.

### #2Trap D

Member

• Members
• 55 posts

Posted 02 January 2008 - 11:27 PM

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).



New Member

• Members
• 2 posts

Posted 03 January 2008 - 12:22 AM

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

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users