Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incomplete to difference lists

I want to convert incomplete lists into difference lists and vice versa.

This is code to convert regular list to difference:

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

How do I go the other direction?

like image 870
Cornel Cicai Avatar asked Nov 24 '22 05:11

Cornel Cicai


1 Answers

Incomplete to difference list:

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

Use it as

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 = [].
like image 171
Will Ness Avatar answered Dec 11 '22 03:12

Will Ness