home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / prolog / 1517 < prev    next >
Encoding:
Internet Message Format  |  1992-08-12  |  2.3 KB

  1. Path: sparky!uunet!mcsun!uknet!edcastle!aiai!ken
  2. From: ken@aiai.ed.ac.uk (Ken Johnson)
  3. Newsgroups: comp.lang.prolog
  4. Subject: Portray clauses for difference lists
  5. Message-ID: <7169@skye.ed.ac.uk>
  6. Date: 12 Aug 92 13:48:26 GMT
  7. Followup-To: comp.lang.prolog
  8. Organization: Bugs-R-Us
  9. Lines: 68
  10.  
  11.  
  12. The following code may be of interest to people who are programming in
  13. Prolog using difference lists (aka open lists).  The test
  14. is_difference_list/1 determines whether a structured term whose
  15. principal functor is -/2 is a properly constructed difference list or
  16. not.  If it is, then show_difference_list/1 will display it as a list
  17. between braces, e.g.  [a,b,c|X]-X displays as {a,b,c}. 
  18.  
  19.         If you don't know what a difference list is, please
  20.         consult a Prolog wiz at your site or a decent Prolog
  21.         book *before* you mail me about it.  The explanation
  22.         would run to a couple of pages and I don't want to have
  23.         to sit and think of one right now.  In essence, a
  24.         difference list is a Prolog structure which allows
  25.         slightly easier and faster appending and concatenation
  26.         than an ordinary list.  The cost of using it is an
  27.         increased complexity of programming. 
  28.  
  29. You may use this code freely etc., but if you sell copies of it at a
  30. profit, I want a share. 
  31.  
  32. %% -----------------------------------------------------------------------
  33. %% Portray clause for Difference List
  34. %% Ken Johnson 12 August 1992
  35.     
  36. portray(X-Y) :-
  37.     is_difference_list(X-Y),
  38.     show_difference_list(X-Y).
  39.  
  40. %%
  41.  
  42. is_difference_list(X-Y) :-
  43.     var(X),
  44.     X == Y.
  45.  
  46. is_difference_list(X-Y) :-
  47.     X \== Y,
  48.     nonvar(X),
  49.     X = [_|T],
  50.     is_difference_list(T-Y).
  51.  
  52. %%
  53.  
  54. show_difference_list(X-Y) :-
  55.     X == Y,
  56.     write('{ }').
  57.  
  58. show_difference_list(X-Y) :-
  59.     X \== Y,
  60.     write('{'),
  61.     show_difference_list_1(X-Y).
  62.  
  63. show_difference_list_1([H|X]-Y) :-
  64.     X == Y,
  65.     write(H), write('}').
  66.  
  67.  
  68. show_difference_list_1([H1,H2|X]-Y) :-
  69.     [H1,H2|X] \== Y,
  70.     write(H1), write(','),
  71.     show_difference_list_1([H2|X]-Y).
  72.  
  73. %% Ends ------------------------------------------------------------------
  74. -- 
  75. ////  Advice to dieters:          ////  Ken Johnson, A I Applications Institute
  76. ////    Never eat more than       ////       80 South Bridge, EDINBURGH EH1 1HN
  77. ////    you can carry.            ////                 E-mail ken@aiai.ed.ac.uk
  78. ////               -- Miss Piggy  ////     phone 031-650 2756  fax 031-650 6513
  79.