Exercise 4.57

Read Exercise 4.57 ~ Solution


It took me a while to realise I can’t just create a new rule using (run-query '(rule ...) and that instead I need to use (run-query '(assert! (rule ...) – still once that was out of the way it wasn’t too difficult. The rule is

(run-query '(assert! 
             (rule (can-replace ?person-1 ?person-2)
                   (and (job ?person-1 ?job-1)
                        (job ?person-2 ?job-2)
                        (not (same ?person-1 ?person-2))
                        (or (same ?job-1 ?job-2)
                            (can-do-job ?job-1 ?job-2))))))

Assertion added to data base.

part a

(run-query '(can-replace ?who (Fect Cy D)))
(can-replace (Hacker Alyssa P) (Fect Cy D))
(can-replace (Bitdiddle Ben) (Fect Cy D))

part b

(run-query '(and (can-replace ?x ?y)
                 (salary ?x ?x-salary)
                 (salary ?y ?y-salary)
                 (lisp-value > ?y-salary ?x-salary)))
(and (can-replace (Fect Cy D) (Hacker Alyssa P))
     (salary (Fect Cy D) 35000) 
     (salary (Hacker Alyssa P) 40000) 
     (lisp-value > 40000 35000))
(and (can-replace (Aull DeWitt) (Warbucks Oliver))
     (salary (Aull DeWitt) 25000)
     (salary (Warbucks Oliver) 150000)
     (lisp-value > 150000 25000))

Leave a comment