; Recursive function asks questions until resolution ; On failure, asks for the unknown person and a fact to discrimate that person (defun QA_System (db) (cond ; Terminating condition when a resolution is reached ; If not at a resolution, ask a question ((not (endp (cadr db))) (format t "~a~%>> " (car db)) (if (eq 'yes (read)) (QA_System (cadr db)) (QA_System (caddr db)))) ; On apparent resolution (if an atom is reached), ask for confirmation (t (format t "~&Your person must be ~a! Am I right?~%>> " (car db)) (if (eq 'yes (read)) ; If resolution is correct, function finally exits (format t "~&Resolution Complete.~%") ; If not, the database must not have the atom you were looking for (let ((name (input "~&Please enter the name of your person:~%>> "))) ; And a fact to distinguish your new individual from the rest (format t "~&Enter a question to distinguish ~a and ~a:~%" (car db) name) ; Insert this new information into the database, for future queries (insert db name (input "~&>> ") (let () ; Before entered, program asks if the fact is true or false for your individual (format t "If thinking of ~a, how would someone respond to your question?~%>> " name) (eq 'yes (read))))))))) ; Initial db initialization (no facts, no students) (setf db '("nobody" () ())) ; Function to insert a new fact in the database (defun insert (node item question no_is_no) (if (null no_is_no) (rplacd node (list (cons (car node) '(() ()))(cons item '(() ())))) (rplacd node (list (cons item '(() ()))(cons (car node) '(() ()))))) (rplaca node question) (format t "~&Thank you! This fact has been learned.~%")) ; Function to correct for (read-line) bug (defun input (prompt) (format t prompt) (let ((answer (read-line))) (if (zerop (length answer)) (read-line) answer))) ; Example database loaded with EE students ("Is the person a student?" ("Is your student female?" ("Is your student blonde?" ("Tara" NIL NIL) ("Lynnette" NIL NIL)) ("Is your student a graduate?" ("Did your student graduate FIU?" ("William" NIL NIL) ("Does your student have black hair?" ("Is he a student in EEL5840?" ("Does your student have straight hair?" ("Vinh" NIL NIL) ("Israel" NIL NIL)) ("Seth" NIL NIL)) ("Is your student a TA?" ("Stuart" NIL NIL) ("Cabbage" NIL NIL)))) ("Does he work at Hungry Howie's?" ("Matt" NIL NIL) ("Jonathan" NIL NIL)))) ("Arroyo" NIL NIL))