


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: Artificial Intelligence; Subject: Computer/Information Sciences; University: University of Delaware; Term: Unknown 1989;
Typology: Exams
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Introduction to Artificial Intelligence CISC-481/ Program 0: Simple Lisp Programs
Reading:
Exercise 1 Write a function called palindromep that is given a list and returns t if the list has the same sequence of elements when read left to right as when read right to left. It should return NIL otherwise. This is a 2-line program, if you use the correct two functions. One is an equality function (either EQ, EQL, EQUAL, or =). Please explore and understand those functions—we will use them often in the future.
(palindromep ’(a b c b a)) T (palindromep ’(go spot go)) T (palindromep ’((x y z) (e f g) (e f g) (x y z))) T (palindromep ’(madam im adam)) NIL (palindromep ’(x)) T
Exercise 2 Write a function called presentp that takes two arguments, an atom and a list. it should return non-nil if the atom appears anywhere in the list, and NIL if it does not appear. presentp is like member except that the atom can appear anywhere in the list, not just at top level. You should use recursion in your solution.
(setf formula ’(sqrt (/ (+ (expt x 2) (expt y 3)) 2.0))) (sqrt (/ (+ (expt x 2) (expt y 3)) 2.0)) (presentp ’x formula) T (presentp ’z formula) NIL
Exercise 3 Write a Lisp function duplicate-entries that takes a list and returns true if it contains multiple copies of some entry, and NIL otherwise. Be sure to handle nested lists properly. You can do this recursively, or my using the mapping functions. For example, maplist. Did you know that member takes an optional argument, :TEST, followed by a function to be used for testing equality? For example, (member thing1 thing2 :TEST #’equal). This will often be VERY useful.
(duplicate-entries ’(a b a c d)) T (duplicate-entries ’(a b (a) c d)) NIL (duplicate-entries ’((a b) b c (a b))) T
Exercise 4 Define a function occurrences that takes a list and returns an assoc-list indicating the number of times each eql element appears, sorted form the most common element to the least common element (hint: Common Lisp defines an extremely efficient sort function).
(occurrences ’(a b a d a c d c a)) ((A. 4) (C. 2) (D. 2) (B. 1))
Exercise 5 Why does (member ’(a) ’((a) (b))) return nil?
Exercise 6 What do the global lisp variables print-level and print-length do?
Study $CLASSHOME/prog1.lisp^1. The main function here is generate, and the style of programming used is called data-driven because the data (the list of rewrite rules pointed to by the variable grammar) drives what the program does next. It is a natural and easy-to- use style in Lisp, leading to concise and extensible programs, because it is always possible to add a new piece of data with a new association without having to modify the original program. Here are some examples of generate in use (try this yourself):
(^1) From Peter Norvig’s Paradigms of AI Programming
Excersise 10 Write a Lisp function ask-and-tell that processes simple sentences. It should allow you to enter lists that represent assertions, such as (Mary likes hiking) or (Steve hates pretzels), as well as questions, such as (Does Mary like hiking) or (Does Steve like pretzels). The assertions should all have a proper name (e.g., Mary) in the first position, the word likes or hates in the second position, and either an activity (e.g., hiking) or an object (e.g., pretzels) in the last position. The questions should be similar, except that they begin with the word Does. The function should remember the assertions you give it, and answer your questions appropriately. If you repeat an assertion, it should let you know that; if you enter a contradictory assertion, it should alert you to that, and confirm that you’re sure before making the change. E.g.,
(ask-and-tell ’(mary likes hiking)) "OK" (ask-and-tell ’(steve hates pretzels)) "OK" (ask-and-tell ’(does mary like hiking)) "YES" (ask-and-tell ’(does mary like pretzels)) "I DON’T KNOW" (ask-and-tell ’(mary likes hiking)) "I KNOW THAT ALREADY" (ask-and-tell ’(does steve like pretzels)) "NO" (ask-and-tell ’(mary hates hiking)) "YOU’VE CHANGED YOUR MIND. ARE YOU SURE?" no "OK" (ask-and-tell ’(does mary like hiking)) "YES" (ask-and-tell ’(mary hates hiking)) "YOU’VE CHANGED YOUR MIND. ARE YOU SURE?" yes "OK" (ask-and-tell ’(does mary like hiking)) "NO"
One easy way to do the database is to use Lisp’s built-in hash table operations (see chapter 4.8 in Graham). Put the hashed DB in a global variable.