Due by 11:59pm on Monday, 8/10

Instructions

Download hw11.zip. Inside the archive, you will find a file called hw11.logic, along with a copy of the Logic interpreter.

Submission: To receive full credit for this homework, you only need to fill out the survey in Question 1. The rest of the homework consists of practice problems for Logic.

Readings: You might find the following references useful:

Required questions

Question 1: A survey

To get credit for this homework (3 points), all you have to do is fill out this survey (it should take about 15 minutes). The survey is not anonymous, since we will be using your response to give you credit for the assignment. We value any feedback you can give us!

Logic practice

The following questions are not required for submission. They are meant as extra practice for Logic.

You can test your solutions by running logic on hw11.logic:

python3 logic hw11.logic

Question 2: Add to all

Write facts for add-to-all, which declares a relation between an item, a list of lists, and a second list whose elements are the elements of the first list with the item added to the front of each:

; YOUR CODE HERE

(query (add-to-all a ((b) (c d)) ((a b) (a c d))))
; expect Success!
(query (add-to-all a ((b c) (b) (foo)) ?what))
; expect Success! ; what: ((a b c) (a b) (a foo))
(query (add-to-all ?what ((c) (d e) ()) ((b c) (b d e) (b))))
; expect Success! ; what: b
(query (add-to-all ?what ?list ((b c) (d e) (b))))
; expect Failed.

Question 3: Sublists

Write facts for sublists, which relates a list of items to another list that contains every possible sublist of the first. A sublist of a list is a list that contains zero or more items from the original list, in the same order that they appear in the original list:

(fact (append () ?a ?a))
(fact (append (?x . ?r) ?b (?x . ?c))
      (append ?r ?b ?c))

; YOUR CODE HERE

(query (sublists (1 2 3) ?subs))
; expect Success! ; subs: (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3))

Question 4

Below is a list of fruits. Write facts for fruits-tail, which relates sublists of fruits starting at a given fruit:

(fact (fruits apple banana cherry date elderberry fig guava))

; YOUR CODE HERE

(query (fruits-tail date elderberry fig guava))
; expect Success!
(query (fruits-tail banana . ?after-banana))
; expect Success! ; after-banana: (cherry date elderberry fig guava)
(query (fruits-tail ?e fig guava))
; expect Success! ; e: elderberry