0
votes

I would like to write a predicate in prolog to find the common suffix of two lists and also return the position that the common suffix begins, counting from the end of the list. Furthermore, given the common suffix, clicking more should return the lower length common suffix, as described in the following examples. The only available embedded functions for this exercise should be length, member and of course, append.

1) ?- common_suffix([1, b, c], [a, b, c], Suffix, Pos).
Suffix = [b, c]
Pos = 2
Yes
Suffix = [c]
Pos = 1
Yes
Suffix = []
Pos = 0
Yes

2) ?- common_suffix([a, b, c], [a, b, c], Suffix, Pos).
Suffix = [a, b, c]
Pos = 3
Yes
Suffix = [b, c]
Pos = 2
Yes
Suffix = [c]
Pos = 1
Yes
Suffix = []
Pos = 0
Yes

3) ?- common_suffix([1, b, 3], [a, b, c], Suffix, Pos).
Suffix = []
Pos = 0
Yes 
1
so, you posted your homework. Please also show us what you have tried.tiffi

1 Answers

0
votes

common_suffix(L1,L2,Suffix,Pos):- append(,Suffix,L1), append(,Suffix,L2), length(Suffix,Pos).

ook, I tried this and it seems working.