From a967a60994565faa7a8537bfad55bb6f7b209f3c Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Tue, 2 Mar 2021 16:55:36 -0500 Subject: [PATCH] Implement till 4.63 --- ex-4_61-xx.scm | 42 +++++++++++++++++++++++++++++++++++++++--- misc/sicp-query.scm | 12 ++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/ex-4_61-xx.scm b/ex-4_61-xx.scm index b426f44..898844f 100644 --- a/ex-4_61-xx.scm +++ b/ex-4_61-xx.scm @@ -33,8 +33,44 @@ (display "\nex-4.62 - last-pair\n") -(display "\nex-4.63\n") +(eval-query '(rule (last-pair (?x . ()) (?x . ())))) +(eval-query + '(rule (last-pair (?u . ?v) ?x) + (last-pair ?v ?x))) -;(display "\nex-4.64\n") -;(display "\nex-4.65\n") +(eval-query '(last-pair (1 2 3) ?y)) +(eval-query '(last-pair (3) ?y)) +(eval-query '(last-pair (2 3 4 5 ?x) (3))) +(newline) + +; (eval-query '(last-pair ?x (3))) results in an endless loop because there is +; no definite answer. Any arbitrary number of symbols before the 3 would form a +; solution. + +(display "\nex-4.63 - genesis-family\n") + +; Formulate rules such as ``If S is the son of F, and F is the son of G, then S +; is the grandson of G'' and ``If W is the wife of M, and S is the son of W, +; then S is the son of M'' (which was supposedly more true in biblical times +; than today) that will enable the query system to find the grandson of Cain; +; the sons of Lamech; the grandsons of Methushael. (See exercise 4.69 for some +; rules to deduce more complicated relationships.) + +(eval-query '(rule (grandson ?grandparent ?grandson) + (and (nson ?grandparent ?son) + (nson ?son ?grandson)))) + +(eval-query '(rule (nson ?parent ?son) + (or (son ?parent ?son) + (and (wife ?parent ?wife) + (son ?wife ?son))))) + +(eval-query '(grandson Cain ?x)) +(eval-query '(nson Lamech ?x)) +(eval-query '(grandson Methushael ?x)) +(newline) + +(display "\nex-4.64\n") + +(display "\nex-4.65\n") diff --git a/misc/sicp-query.scm b/misc/sicp-query.scm index 43b31aa..e537d24 100644 --- a/misc/sicp-query.scm +++ b/misc/sicp-query.scm @@ -665,6 +665,18 @@ (or (supervisor ?staff-person ?boss) (and (supervisor ?staff-person ?middle-manager) (outranked-by ?middle-manager ?boss)))) + +; From 4.63 +(son Adam Cain) +(son Cain Enoch) +(son Enoch Irad) +(son Irad Mehujael) +(son Mehujael Methushael) +(son Methushael Lamech) +(wife Lamech Ada) +(son Ada Jabal) +(son Ada Jubal) + )) ;; felixm: for easier use from MIT-Scheme