PDA

View Full Version : Prolog list relation help


Praetorian
04-13-2007, 10:05 AM
Ok I need a wee bit of help. I have two lists of unknown length that contain 1s and 0s. They represent states and a valid progression from one to the other is either replacing a 1 with a 0 or replacing two adjacent 1s with 0s

[1,1,0,1,1,1] -> [1,1,0,0,0,1]

but not

[1,1,0,1,1,1] -> [1,1,0,0,1,0]

I need to write a relation that succeeds if the second state is a valid progression from the first, I have been able to do this but it cant produce all possible progressions by backtracking, which is a requirement. If anyone could give me some help of how to write this relation it would be appreciated. Thanks.

Praetorian
04-13-2007, 12:22 PM
Ok I now have this:
Code:

replace(A,B,[A|C],[B|C]). replace(A,B,[D|C],[D|E]) :- replace(A,B,C,E).


When using to replace a 1 with a 0 it will find possible replacements. How do I get it to keep going to find all of the replacements possible (when a second list is supplied it will succeed if performing the replacement on the first list generates the second list)?

Trap D
04-14-2007, 05:02 AM
You should give all the good replacements from [1,1,0,1,1,1] so I can test.

I tried that but I don't know if this is correct :
replace([], []).

replace([1,1 | T1], [1, 1 | T2]) :-
replace(T1,T2).


replace([1,1 | T1], [0, 0 | T2]) :-
!, replace(T1,T2).

replace([1 | T1], [1 | T2]) :-
replace(T1,T2).

replace([1 | T1], [0 | T2]) :-
!, replace(T1,T2).

replace([0 | T1], [0 |T2]) :-
replace(T1,T2).

Produce
8 ?- replace([1,1,0,1,1,1] ,X) .

X = [1, 1, 0, 1, 1, 1] ;

X = [1, 1, 0, 1, 1, 0] ;

X = [1, 1, 0, 0, 0, 1] ;

X = [1, 1, 0, 0, 0, 0] ;

X = [0, 0, 0, 1, 1, 1] ;

X = [0, 0, 0, 1, 1, 0] ;

X = [0, 0, 0, 0, 0, 1] ;

X = [0, 0, 0, 0, 0, 0] ;

No

saminbelize
04-17-2007, 07:03 AM
I am interested by this example of prolog. I have been trying to figure it out myself.

How would you only display the next move possible from a gamestate rather than all of them.

i.e. The query would return true if you put replace([1,1,1,1], [1,0,0,1]) but false if you enter replace([1,1,1,1],[0,0,0,1]) as you cannot reach the state in one move.

Nick
04-17-2007, 08:38 AM
Seriously, what's with all the Prolog questions in the Artificial Intelligence forum lately?

SigKILL
04-17-2007, 09:54 AM
It's quite incredible that these threads aren't moved to the languages forum. I have a friend prolog users can visit : http://www.google.com/search?hl=en&q=prolog+forum

Bigger chance to get your questions answered there.