difference list (Prolog)(Logic Programming) -


i have problem solving difference list problem paper-pencil not helping of swi prolog ( in these days preparing logic programming exam).

here question:

q(x) :- p(x - []). p([x|y] - y). p([x|y] - z) :- p(y - [x|z]). 

explicitly give set of ground terms t query ?- q(t). succeeds. state formal definition of set in closed form (i.e., not use recursive definition).

answer :

{[t1, . . . , tn−1, tn, tn−1, . . . , t1] | n > 0, ti ground term ∈ {1, . . . , n}}

i used [1,2,3,2,1] , gave me true expected answer.

i not understand steps in order solve this?

let t = [t1,t2,t3, ... , tn] list.

q(t). succeed if p(t - []). succeeds.

p(t - []). succeeds if p([t2,t3,...,tn] - [t1]). succeeds (third rule).

p([t2,t3,...,tn] - [t1]). succeeds if p([t3,...,tn] - [t2,t1]) succeeds, etc.

this end succeeding if @ 1 point second rule gets applied (otherwise end calling p([] - _). not match rule available), if have p([ti,ti+1,...,tn] - [ti+1,...,tn])..

the second list [ti+1,...,tn] [ti-1,ti-2,...,t2,t1] according way constructed in third rule.

therefore q(t). succeeds iff ti-1 = ti+1, ti-2 = ti+2, ..., t2 = tn-1, t1 = tn, means t = [t1,t2,...,ti-1,ti,ti-1,...,t2,t1].


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -