;; Note: You will need logic.py in order to run these examples. ;; Make sure to put logic.py in the same directory as your Scheme project. ;; Since you have already completed up to Q6, that should be enough to use ;; the logic interpreter. ;; Demo: Facts and Queries (color sun yellow) help (fact (color sun yellow)) (fact (color sky blue)) (query (color ?what yellow)) (query (color sky ?color)) (fact (grass color green)) (query (color grass ?what)) (query (grass color ?what)) (query (color ?thing ?color)) (fact (color clouds red)) (query (color clouds ?color)) (fact (color clouds white)) (query (color clouds ?color)) ;; Demo: A Larger Database (load database.logic) (query (job ?x (computer ?y))) (query (job ?x (computer . ?y))) (query (can-do-job ?y (computer programmer))) (fact (can-do-job (computer . ?r) (computer . ?r))) (query (can-do-job ?y (computer programmer))) (fact (can-do-job (computer . ?r) (computer . ?s))) (query (can-do-job ?y (computer programmer))) (query (can-do-job ?y (computer programmer)) (job ?x ?y)) (query (address (bitdiddle ben) ?where)) (query (address (fect cy d) ?where)) (fact (lives-near ?person-1 ?person-2) (address ?person-1 (?town . ?rest-1)) (address ?person-2 (?town . ?rest-2))) (query (lives-near ?who (bitdiddle ben))) (query (lives-near ?p1 ?p2)) (query (?relation (hacker alyssa p) (bitdiddle ben))) (query (?relation (hacker alyssa p) (fect cy d))) ;; Demo: List Manipulations (fact (car (?x . ?y) ?x)) (query (car (1 2 3) ?what)) ; expect Success! ; what: 1 (query (car ((4 5) 6 7) ?what)) ; expect Success! ; what: (4 5) (query (car 3 ?what)) ; expect Failed. (fact (cdr (?x . ?y) ?y)) (fact (evens (2 4 6 8))) ;; Use your car rule to write a query that gets the 2 out of evens. ;; You are not allowed to have any numbers hard-coded into your query. ;; You must use the car rule (even though there is a simpler solution). ;; Bonus: find the simpler solution. ;; Your code here (query (evens ?list) (car ?list ?what)) ; expect Success! ; list: (2 4 6 8) what: 2 (query (evens (?what . ?rest))) ; expect Success! ; what: 2 rest: (4 6 8) ;; Use your car and cdr rules to write a query that gets the 4 out of evens. ;; You are not allowed to have any numbers hard-coded into your query. ;; You must use the car and cdr rules (even though there is a simpler solution). ;; Bonus: find the simpler solution. ;; Your code here (query (evens ?list) (cdr ?list ?x) (car ?x ?what)) ; expect Success! ; list: (2 4 6 8) x: (4 6 8) what: 4 (query (evens (?first ?what . ?rest))) ; expect Success! ; first: 2 what: 4 rest: (6 8) ;; Demo: Recursive Rules (fact (append () ?y ?y)) (fact (append (?first . ?x) ?y (?first . ?z)) (append ?x ?y ?z)) ;; Write the "base case" for the append fact. ;; Your code here (query (append (1 2) (3 4 5) ?what)) ; expect Success! ; what: (1 2 3 4 5) (query (append ?what (1 4) (s u 1 4))) ; expect Success! ; what: (s u) (query (append ?x ?y (1 2 3 4 5))) ; expect Success! ; x: () y: (1 2 3 4 5) ; x: (1) y: (2 3 4 5) ; x: (1 2) y: (3 4 5) ; x: (1 2 3) y: (4 5) ; x: (1 2 3 4) y: (5) ; x: (1 2 3 4 5) y: () (fact (member ?item (?item . ?cdr))) (fact (member ?item (?car . ?cdr)) (member ?item ?cdr)) ;; This is a fully functioning member rule. ;; It seems like we are always breaking the list into two parts... ;; Where is the base case then? ;; What happens when you do the following, and why: (query (member ?what (1 2 3 1 2 3))) (query (member 1 ?what)) (query (member 3 (1 2 ?what))) ; expect Success! ; what: 3 ;; Demo: Permutations Time! (fact (remove-one ?item (?item . ?new-lst) ?new-lst)) (fact (remove-one ?item (?car . ?cdr) (?car . ?new-lst)) (remove-one ?item ?cdr ?new-lst)) (fact (permutation () ())) (fact (permutation (?first . ?rest) ?lst) (remove-one ?first ?lst ?new-lst) (permutation ?rest ?new-lst)) (query (permutation ?what (1 2 3))) (query (permutation ?what (1 1 2))) ; Beware of this one ; (query (permutation (1 2 3) ?what))