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!

Need help!!!

Status
Not open for further replies.

GreatDantone

Programmer
Apr 13, 2010
5
CA
Hi, I ave an assignment in wich we need to code an unification algorithm in prolog. I already wrote a predicate

unifier(E1,E2,S). where S is the unification of E1 and E2 if possible.

Example: unifier(a,'A',S) gives
S = [a,'A']

(variables are in quotes to prevent prolog from interpreting them)

I'm trying to write unifier_liste(E1,E2,S), that would do the same for lists. For example:

unifier_liste([a,b,c],['A,'B','C'],S) would give:
S = [[a,'A'],[b,'B'],[c,'C']]

here is the unifier_liste predicate I wrote, though it's not working:

unifier_liste(E1,E2,Sub) :-
\+empty_list(E1), %empty_list fails if E1 != []
E1 = [X1|Y1],
E2 = [X2|Y2],
unifier(X1,X2,S),
unifier_listes(Y1,Y2,S).

unifier_liste(E1,E2,Sub) :-
\+empty_list(E1), %empty_list fails if E1 != []
E1 = [X1|Y1],
E2 = [X2|Y2],
unifier(X1,X2,S),
append(,,L),
unifier_listes(Y1,Y2,L).

when i run unifier_listes([a,b,c],['A','B','C'],S).
with gprolog, it fails.

If anybody knows what's wrong, it would be appreciated!

Thanks
 
Some comments:

1. Your predicate is called unifier_liste and you make a presumably recursive call but you call unifier_listes, which is not the same name

2. \+empty_list(E1) and E1 = [X1 | Y1] are redundant. It's enough to test E1 against [X1 | Y1]. If it unifies, then E1 is not empty. It not, then it's empty.

3. Your first unifier clause does not compute the result which is Sub, and your second unifier clause does use Sub as an input value, when it's clearly an output value

4. You only have recursive clauses for unifier_liste. You need at least one non-recursive clause, otherwise the process is probably stuck in infinite recursivity.

Here is my idea of your code:

Code:
unifier(A, B, [A, B]).

unifier_liste([], [], []).
unifier_liste([X1|Y1], [X2|Y2], [S1 | S2]) :-
   unifier(X1,X2,S1),
   unifier_liste(Y1, Y2, S2).
 
Thank you very much!!!! I'm new to prolog and I spend two hours yesterday trying to figure this out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top