0
votes

How can you write a predicate in Prolog, which takes 3 lists, L1 is a list that contains constants and variables, L2 contains constants, and L3 is a some list. The predicate should return true iff the variables of the first list substituted with the corresponding element of the second list equals the third list.

Like if L1_i is a variable, it needs to be substituted with L2_i, and then if it needs to be equal to L3_i.

How can this be done?

Thanks.

1
Have you tried anything? - Daniel Lyons

1 Answers

0
votes

Unification (i.e. =/2) does that, actually.

?- [X, a, Y, b, Z] = [1, A, 2, B, 3].
X = 1,
Y = 2,
Z = 3,
A = a,
B = b.

Oh, but you need three lists:

?- [X,a,Y,b,Z] = [1,A,2,B,3], [1,A,2,B,3] = [1,a,2,b,3].
X = 1,
Y = 2,
Z = 3,
A = a,
B = b.

Sorry, let's make it look more like your assignment:

?- L1 = [X,a,Y,b,Z], L2 = [1,A,2,B,3], L3 = [1,a,2,b,3], L1 = L2, L2 = L3.
L1 = [1, a, 2, b, 3],
X = 1,
Y = 2,
Z = 3,
L2 = [1, a, 2, b, 3],
A = a,
B = b,
L3 = [1, a, 2, b, 3].

:)