Jump to content


Prolog list relation help


5 replies to this topic

#1 Praetorian

    New Member

  • Members
  • Pip
  • 3 posts

Posted 13 April 2007 - 05:05 PM

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.

#2 Praetorian

    New Member

  • Members
  • Pip
  • 3 posts

Posted 13 April 2007 - 07: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)?

#3 Trap D

    Member

  • Members
  • PipPip
  • 55 posts

Posted 14 April 2007 - 12:02 PM

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

Quote

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


#4 saminbelize

    New Member

  • Members
  • Pip
  • 1 posts

Posted 17 April 2007 - 02:03 PM

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.

#5 Nick

    Senior Member

  • Members
  • PipPipPipPip
  • 1225 posts

Posted 17 April 2007 - 03:38 PM

Seriously, what's with all the Prolog questions in the Artificial Intelligence forum lately?

#6 SigKILL

    Valued Member

  • Members
  • PipPipPip
  • 200 posts

Posted 17 April 2007 - 04:54 PM

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.co...&q=prolog+forum

Bigger chance to get your questions answered there.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users