home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / prolog / pdprolog / trees.pro < prev    next >
Text File  |  1986-05-05  |  2KB  |  68 lines

  1. /* A Prolog solution to Family Trees by Virginia McCarthy.
  2.             Tom Sullivan
  3.             5415 Grand Ave.
  4.             Western Springs, IL 60558  -  October 30,1985.
  5. */
  6.  
  7. northside (larch).
  8. northside (catalpa)    :- northside ('Grandes').
  9. northside ('Grandes')  :- have ('Grandes', larch).
  10. have ('Grandes',larch) :- not_own ('Crewes',larch),
  11.                           not_own ('Dews',larch),
  12.                           not_own ('Lands',larch).
  13.  
  14. southside ('Crewes').
  15. southside ('Dews').
  16. southside (dogwood) :- northside (larch), northside (catalpa).
  17. southside (ginko)   :- northside (larch), northside (catalpa).
  18.  
  19. here ('Grandes').
  20. there (catalpa).
  21.  
  22. /* belongs_to provides a recursive definition of membership in a list
  23.    see Clocksin & Mellish p. 53.  */
  24.  
  25. belongs_to (X,[X|_]).
  26. belongs_to (X,[Y|Z]) :- belongs_to (X,Z).
  27.  
  28. same_first_letter (['Dews', dogwood]).
  29. same_first_letter (['Grandes',ginko]).
  30. same_first_letter (['Lands',larch]).
  31. same_first_letter (['Crewes',catalpa]).
  32.  
  33. human (['Grandes','Crewes','Dews','Lands']).
  34. plant ([catalpa,ginko,dogwood,larch]).
  35.  
  36. person (X) :- human (Y), belongs_to (X,Y).
  37. tree (X)   :- plant (Y), belongs_to (X,Y).
  38.  
  39. not_own (X,Y) :- same_first_letter (Z),
  40.                  belongs_to (X,Z), belongs_to (Y,Z).
  41. not_own (X,Y) :- here (X), there (Y).
  42. not_own (X,Y) :- (person (X), person (Y));
  43.                  (tree (X), tree (Y)).
  44. not_own (X,Y) :- (northside (X),southside (Y));
  45.                  (southside (X),northside (Y)).
  46. not_own ('Crewes', X) :- owns ('Dews', X).
  47. not_own ('Lands', X)  :- owns ('Crewes', X).
  48. not_own ('Lands', X)  :- owns ('Dews',X).
  49.  
  50.  
  51. owns (X,Y) :- person (X), tree (Y), not (not_own (X,Y)).
  52. hello :- owns(Person,Tree),print (Person,' owns the ',Tree).
  53.  
  54. /* query with "owns (Person,Tree), write (Person,Tree)."
  55.    or just say "hello."  */
  56.  
  57.  
  58. /* The puzzle - "Family Trees" by Virginia McCarthy as found in
  59.    Dell Champion Variety Puzzles, November, 1985
  60.  
  61.    The Crewes, Dews, Grandes, and Lands of Bower Street each have 
  62.    a front-yard tree -- a catalpa, dogwood, gingko, and larch.  The
  63.    Grandes' tree and the catalpa are on the same side of the street.
  64.    The Crewes live across the street from the larch, which is across
  65.    the street from the Dews' house.  If no tree starts with the same 
  66.    letter as its owner's name, who owns which tree?     
  67. */
  68.