{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# notes15s - [Solutionsv1.0...

This preview shows pages 1–2. Sign up to view the full content.

CS61A Notes 15 – Logic Is What Logic Declares Logic To Be [Solutions v1.0] Lists Again (and again, and again, and again, and again…  Someone changed the Matrix!) QUESTIONS 1. Write a rule for  car  of list.  For example,  (car (1 2 3 4) ?x)  would have  ?x  bound to  1 . (rule (car (?car . ?cdr) ?car)) 2. Write a rule for  cdr  of list.  For example,  (cdr (1 2 3) ?y)  would have  ?y  bound to  (2 3) . (rule (cdr (?car . ?cdr) ?cdr)) 3. Using the above, write a query that would bind  ?x  to the  car  of  my-list .  Write another query  that would bind  ?y  to the  cdr  of  my-list . The temptation is to write (car my-list ?x) or (cdr my-list ?y). This doesn’t work! There is no entry in the database whose first element is “car” and whose second element is the word “my-list”. If you did that, you’re thinking in the old Scheme way – that some “evaluator” will see my-list as a symbol and substitute in (1 2 3 4). This will not happen, since my-list isn’t a variable! What you have to do is this: (and (my-list ?ls) (car ?ls ?x)) First, we match ?ls to be (1 2 3 4), and then match ?x to be 1. 4. Define our old friend,  member , so that  (member 4 (1 2 3 4 5))  would be satisfied, and  (member 3 (4 5 6))  would not, and  (member 3 (1 2 (3 4) 5))  would not. (rule (member ?item (?item . ?cdr))) (rule (member ?item (?car . ?cdr)) (member ?item ?cdr)) 5. Define its cousin,  deep-member , so that  (deep-member 3 (1 2 (3 4) 5))  would be satisfied  as well. (rule (deep-member ?item (?item . ?cdr))) (rule (deep-member ?item (?car . ?cdr)) (deep-member ?item ?car)) (rule (deep-member ?item (?car . ?cdr)) (deep-member ?item ?cdr)) Note how ?item can either be in ?car or ?cdr, so we need three rules. 6. Define another old friend,  reverse , so that  (reverse (1 2 3) (3 2 1))  would be satisfied. (rule (reverse () ()))

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 5

notes15s - [Solutionsv1.0...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online