Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to implement all_different in SWI-Prolog

Status
Not open for further replies.

Shaitan00

Programmer
Jul 21, 2006
5
CA
I am writing a swi-prolog program to solve a sudoku puzzle, the program is working perfectly when using the built-in "all_different" predicate but I was now instructed to actually write my own and not take advantage of the available function. Therefore I am attempting to write my own customized version "alldifferent(L):-" that will achieve the same results.

Code:
  alldifferent([A1,B1,C1,D1]), 
  alldifferent([A2,B2,C2,D2]), 
  alldifferent([A3,B3,C3,D3]), 
  alldifferent([A4,B4,C4,D4]), 
  ... 

  alldifferent([_]).		% Base Case
  alldifferent(H,T) :- H\=T.	% Compare Head and Tail
  alldifferent([H|T]) :- alldifferent(H,T), alldifferent(T).
[/oOde] 

As you can see I pass in a list of 4 variables and I need to ensure that none of them are the same. I've been trying to implement this for a while now and can't seem to figure it out (I've written my current attempt) - I was wondering if anyone could provide some help. MY current attempt doesn't work (not sure how you can compare the head of a list [H] with the tail [T] and am doubtful about my base-case.

Any hints would be greatly appreciated. 
Thanks,
 
I think your base case is OK
The next line needs squared brackets to make it a list format.
The third line - you should not mix lists [H|T] with non-lists H,T. The third line also needs reworking. You could look at using the "member" predicate to look for duplication.
Cheers
John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top