I need to write a predicate bapos(L1,L2,E1,E2)that is true when L1 and L2 are lists such that E1 has a position in L1 that is one position before or after the position E2 has in L2.
my solution:
bapos([_|_],[L2|_], _, L2).
bapos([_|L1],[_|L2],E1,E2) :- bapos(L1,L2,E1,E2). ```
this works but not as expected.
expected output.
bapos([1,2,3], L2, 2, b).
L2 = [b|_G1869] ;
L2 = [_G1868, _G1871, b|_G1875] ;
false.
My output:
bapos([1,2,3],L2,2,b).
L2 = [b|_34474] ;
L2 = [_35132, _35138|_35140] ;
L2 = [_35132, b|_35800] ;
L2 = [_35132, _36458, b|_36466] ;
false.
I think I got what you were trying to do, but expressed in the wrong way. This should work:
bapos([E1|_], [_,E2|_], E1, E2).
bapos([_,E1|_], [E2|_], E1, E2).
bapos([_|L1], [_|L2], E1, E2):-
bapos(L1, L2, E1, E2).
The first two clauses check whether E1 and E2 are just before or just after the other element. The third clause is the recursive step.
Sample runs:
?- bapos([1,2,3],L2,2,b).
L2 = [b|_1542] ;
L2 = [_1540, _1546, b|_1554] ;
false.
?- bapos([1,2,3],[a,b,c],1,a).
false.