sicp-ex-4.69



<< Previous exercise (4.68) | Index | Next exercise (4.70) >>


meteorgan

  
  
  
 (rule (end-in-grandson (grandson))) 
 (rule (end-in-grandson (?x . ?rest)) 
       (end-in-grandson ?rest)) 
  
 (rule ((grandson) ?x ?y) 
       (grandson ?x ?y)) 
 (rule ((great . ?rel) ?x ?y) 
       (and (end-in-grandson ?rel) 
            (son ?x ?z) 
            (?rel ?z ?y))) 

codybartfast




Rules:
======

  (rule (son-of ?parent ?son)
        (or (son ?parent ?son)
            (and (son ?mother ?son)
                 (wife ?parent ?mother))))

  (rule (grandson ?grand-parent ?grand-son)
        (and (son-of ?parent ?grand-son)
             (son-of ?grand-parent ?parent)))
  
  (rule (same ?x ?x))

  (rule ((great . ?rel) ?g ?ggs)
    (or
      (and (same ?rel (grandson))
           (grandson ?i ?ggs)
           (son-of ?g ?i))
      (and (same ?rel (great . ?rest))
           ((great . ?rest) ?i ?ggs)
           (son-of ?g ?i))))


Output:
=======

  ;;; Query input:
  ((great great grandson) Adam ?who)

  ;;; Query results:
  ((great great grandson) Adam Mehujael)


  ;;; Query input:
  ((great grandson) ?g ?ggs)

  ;;; Query results:
  ((great grandson) Mehujael Jubal)
  ((great grandson) Mehujael Jabal)
  ((great grandson) Irad Lamech)
  ((great grandson) Enoch Methushael)
  ((great grandson) Cain Mehujael)
  ((great grandson) Adam Irad)


  ;;; Query input:
  (?rel Adam Jabal)

  ;;; Query results:
  ((great great great great great grandson) Adam Jabal)