1
votes

I found following exercise on www.learnprolognow.org, I tried solving it but haven't been able to:

Write a predicate swap12(List1,List2) which checks whether List1 is identical to List2, except that the first two elements are exchanged.

What I have now:

swap12([X,Y],[Y,X]).
swap12([X,Y|T],[Y,X|Z]) :- 
1
What is the issue you are having? You don't specify any. - APerson
@APerson I added what I currently have - Stanko
You're very close. Why are the two tails (T and Z) different in your second predicate clause if they're the same list other than the first two elements swapped? - lurker

1 Answers

0
votes

You're almost there: you just need to establish that the elements after the first two in each list are the same (hint: if two things can both be represented by the same variable, they are the same). In fact, you only need one rule.