home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / prolog / library / prolo_c / exampl25.pro < prev    next >
Text File  |  1986-10-06  |  1KB  |  38 lines

  1. /* Program 25 */
  2. /*
  3.   Running the goal:
  4.      intersect(X,[1,2,3],[2,3,4,5])
  5.   notice that four answers are returned.  The
  6.   first one is complete and the remaining 3 are
  7.   subsets of the first. In tracing through the
  8.   program notice that once a solution has been
  9.   found, Turbo returns the value and then
  10.   begins backtracking to search for additional
  11.   solutions (see chapter 5: Turbo Prolog's
  12.   Relentless Search for Solutions). To stop
  13.   this back-tracking place a cut (!) at the end
  14.   of the second intersect() clause. Trace the
  15.   program now and see.
  16. */
  17.  
  18. domains
  19.     list = reference integer*
  20. /*
  21.   Reference variables are discussed on page 149
  22. */
  23.  
  24. predicates
  25.     member(integer,list)
  26.     intersect(list,list,list)
  27.  
  28. clauses
  29.     member(X,[X|_]).
  30.     member(X,[_|Y]):- member(X,Y).
  31.  
  32.     intersect([],[],_).
  33.     intersect([X|Y],[X|L1],L2):-
  34.         member(X,L2),
  35.         intersect(Y,L1,L2). /* cut (!) goes here */
  36.  
  37.     intersect(Y,[_|L1],L2):-intersect(Y,L1,L2).
  38.