Incomplete lists of differences

I want to convert partial lists to difference lists and vice versa.

This is the code to convert a regular list to a difference:

reg2diff(L,X-Y):-append(L,Y,X).

How do I go in another direction?

+5
source share
2 answers

Incomplete list of differences:

inc2diff(L,Z):- 
  (  nonvar(L) 
  -> ( L=[_|T] -> inc2diff(T,Z) ; L=[] -> Z=[] )
  ;  L=Z
  ).

Use it like

23 ?- L=[1,2,3|_],inc2diff(L,X).
L = [1, 2, 3|X].

24 ?- L=[1,2,3|Z],inc2diff(L,X).
L = [1, 2, 3|X],
Z = X.

25 ?- L=[1,2,3],inc2diff(L,X).
L = [1, 2, 3],
X = [].
+1
source
| ?- listing(dl_list),dl_list([a,b,c,d|X]-X,L).

% file: ...util.pg

dl_list(A - [], A).

L = [a,b,c,d]
X = []

yes
-1
source

All Articles