1:- module(
    2  dlist,
    3  [
    4    dappend/2,         % +Ls:list(dlist), -L:dlist
    5    dappend/3,         % +L1:dlist, +L2:dlist, -L3:dlist
    6    dappend_to_list/2, % +Ls:list(dlist), -L:list
    7    dappend_to_list/3, % +L1:dlist, +L2:dlist, -L3:list
    8    dlist_to_list/2,   % +L1:dlist, -L2:list
    9    is_dlist/1         % @Term
   10  ]
   11).

Difference lists

*/

   17:- use_module(library(apply)).
 dappend(+Ls:list(dlist), -L:dlist) is det
   23dappend([], []):- !.
   24dappend([L], L):- !.
   25dappend([L1,L2], L):- !,
   26  dappend(L1, L2, L).
   27dappend([L1,L2|T], L):-
   28  dappend(L1, L2, L3),
   29  dappend([L3|T], L).
 dappend(+L1:dlist, +L2:dlist, -L3:dlist) is det
   35dappend(L1-H1, H1-H2, L1-H2).
 dappend_to_list(+DLs:list(dlist), -L:list) is det
Append an arbitrary number of difference lists into one regular list.
   44dappend_to_list(DLs, L) :-
   45  dappend(DLs, DL),
   46  dlist_to_list(DL, L).
 dappend_to_list(+DL1:dlist, +DL2:dlist, -L:list) is det
Append two difference lists into one regular list.
   54dappend_to_list(DL1, DL2, L):-
   55  dappend(DL1, DL2, DL),
   56  dlist_to_list(DL, L).
 dlist_to_list(+DL:dlist, -L:list) is det
Converts a difference list into a reggular list.
   64dlist_to_list(L1, L):-
   65  dappend(L1, []-[], L-[]).
 is_dlist(@Term) is semidet
Succeeds if `Term' is a difference list.
   73is_dlist(L1-L2):-
   74  maplist(is_list, [L1,L2])