🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.prolog
Article #619

Portray clauses for difference lists

#619
From: Ken Johnson
Date: Wed, 12 Aug 1992 13:48
68 lines
2465 bytes
The following code may be of interest to people who are programming in
Prolog using difference lists (aka open lists).  The test
is_difference_list/1 determines whether a structured term whose
principal functor is -/2 is a properly constructed difference list or
not.  If it is, then show_difference_list/1 will display it as a list
between braces, e.g.  [a,b,c|X]-X displays as {a,b,c}. 

        If you don't know what a difference list is, please
        consult a Prolog wiz at your site or a decent Prolog
        book *before* you mail me about it.  The explanation
        would run to a couple of pages and I don't want to have
        to sit and think of one right now.  In essence, a
        difference list is a Prolog structure which allows
        slightly easier and faster appending and concatenation
        than an ordinary list.  The cost of using it is an
        increased complexity of programming. 

You may use this code freely etc., but if you sell copies of it at a
profit, I want a share. 

%% -----------------------------------------------------------------------
%% Portray clause for Difference List
%% Ken Johnson 12 August 1992
	
portray(X-Y) :-
	is_difference_list(X-Y),
	show_difference_list(X-Y).

%%

is_difference_list(X-Y) :-
	var(X),
	X == Y.

is_difference_list(X-Y) :-
	X \== Y,
	nonvar(X),
	X = [_|T],
	is_difference_list(T-Y).

%%

show_difference_list(X-Y) :-
	X == Y,
	write('{ }').

show_difference_list(X-Y) :-
	X \== Y,
	write('{'),
	show_difference_list_1(X-Y).

show_difference_list_1([H|X]-Y) :-
	X == Y,
	write(H), write('}').


show_difference_list_1([H1,H2|X]-Y) :-
	[H1,H2|X] \== Y,
	write(H1), write(','),
	show_difference_list_1([H2|X]-Y).

%% Ends ------------------------------------------------------------------
-- 
////  Advice to dieters:          ////  Ken Johnson, A I Applications Institute
////    Never eat more than       ////       80 South Bridge, EDINBURGH EH1 1HN
////    you can carry.            ////                 E-mail ken@aiai.ed.ac.uk
////               -- Miss Piggy  ////     phone 031-650 2756  fax 031-650 6513
Technical Details
Message-ID: <7169@skye.ed.ac.uk>
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!uknet!edcastle!aiai!ken

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!uknet!edcastle!aiai!ken
From: ken@aiai.ed.ac.uk (Ken Johnson)
Newsgroups: comp.lang.prolog
Subject: Portray clauses for difference lists
Message-ID: <7169@skye.ed.ac.uk>
Date: 12 Aug 92 13:48:26 GMT
Followup-To: comp.lang.prolog
Organization: Bugs-R-Us
Lines: 68