Testing Prolog Difference Lists

I read about how large the lists of differences are, and I was hoping to check out some examples from the books. But it seems that you cannot pass lists as input in the same way as, for example, append ([1,2,3], [4,5], X), where X = [1,2,3,4, 5]. Oddly enough, not a single book that I consulted with ever mentions this.

I am running the code on swipl, and I'm interested in testing the difference append predicate:

dapp(AB,BC,AC). 

and the predicate "rotate the first element of the list":

 drotate([H|T]-T1,RS) :- dapp(T-T1,[H|L]-L,RS). 

Any ideas how I can check these predicates in swipl?

+7
source share
2 answers

Try:

 dapp([1,2,3|X] - X,[4,5,6] - [],Y - []). drotate([1,2,3|X] - X,Y - []). 

Y is the answer for both predicates.

+7
source

The definition of drotate can be simplified:

 dapp(AB,BC,AC). drotate([H|T]-T1,RS) :- % dapp(T-T1,[H|L]-L,RS). %% use the definition of dapp: T=R, T1=[H|L], L=S. 

IOW, just

 drotate([H|R]-[H|L],RL). 

Now any list of differences is usually written out as a pair, AB . Thus, a call to drotate can be drotate([1,2,3|Z]-Z,RL) with the intention of seeing the output in RL variables. But comparing this call with the last definition, we get Z=[1|L] , that is, the logarithm of Z, presumably not created before the call, gets an instance of it, actually adding 1 to the end of the open list [1,2,3|Z]-Z turning it into [1,2,3,1|L]-L . R simply points to the second elt of the recently enlarged list, matching [H|R] with the list.

 ?- drotate([1,2,3|Z]-Z,RL). Z = [1|_G345] R = [2, 3, 1|_G345] L = _G345 Yes 

But it can also be called with true circular data, AA=[1,2,3|Z]-Z, drotate(AZ,RL) :

 ?- AA=[1,2,3|Z]-Z, drotate(AZ,RL). A = [1, 2, 3, 1, 2, 3, 1, 2, 3|...] Z = [1, 2, 3, 1, 2, 3, 1, 2, 3|...] R = [2, 3, 1, 2, 3, 1, 2, 3, 1|...] L = [2, 3, 1, 2, 3, 1, 2, 3, 1|...] Yes 
+2
source

All Articles