prolog program to find equality of two lists in any order

Posted by Happy Mittal on Stack Overflow See other posts from Stack Overflow or by Happy Mittal
Published on 2010-04-26T00:11:44Z Indexed on 2010/04/26 0:23 UTC
Read the original article Hit count: 614

Filed under:
|
|
|
|

I wanted to write a prolog program to find equality of two lists, the order of elements
doesn't matter.
So I wrote following:
del( _ , [ ] , [ ] ) . del( X , [ X|T ] , T ).
del( X , [ H|T ] , [ H|T1 ] ) :- X \= H , del( X , T , T1 ).
member( X, [ X | _ ] ) .
member( X, [ _ | T ] ) :- member( X, T ).
equal( [ ], [ ] ).
equal( [X], [X] ).
equal( [H1|T], L2 ) :- member( H1, L2 ), del( H1, L2, L3), equal(T , L3 ).

But when I give input like equal([1,2,3],X)., it doesn't show all possible values of X.
instead the program hangs in the middle.
What could be the reason?

© Stack Overflow or respective owner

Related posts about prolog

Related posts about lists