



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




The purpose of this assignment is for you to gain some experience designing and implementing LISP pro- grams. In this assignment, however, you will explore only a few of the many interesting LISP features. The assignment is broken into three parts. The first part is a fairly straightforward LISP warm-up. The later parts require you to build a pattern-matching program.
The goal of this part of the homework is to familiarize you with the notions of lists, function definitions, and function applications in Lisp. This part requires you to define a number of simple functions:
(all-length ’(a ( b ) c)) 3 (all-length ’(a (b c) (d (e f)))) 6
(range ’(0 7 8 2 3 -1)) (-1 8) (range ’(7 6 5 4 3)) (3 7)
(before ’b ’d ’(a b c d)) (B C D)
If only the first argument appears in the list, before returns a list containing all the elements from the first occurrence of the first argument to the end of the list.
(before ’a ’d ’(a)) (A)
(split-if #’(lambda (x) (> x 4)) ’(1 2 3 4 5 6 7 8 9 10)) ((1 2 3 4) (5 6 7 8 9 10))
(group ’(a b c d e f g) 2) ((A B) (C D) (E F) (G))
(mostn #’length ’((a b) (a b c) (a) (e f g))) ( ((A B C) (E F G)) 3)
Before we start building the pattern-matching function, let us first build a set of routines that will allow us to represent facts, called assertions. For instance, we can define the following assertions:
(this is an assertion) (color apple red) (supports table block1) Here each assertion is represented as a list. The set of assertions can be maintained in a database by representing them in a list. For instance, the following list represents an assertion database containing the above assertions:
((this is an assertion) (color apple red) (supports table block1)) Patterns are like assertions, except that they may contain certain special atoms not allowed in assertions, the single characters? and !, for instance.
(this! assertion) (color? red) Write a function match that compares a pattern and an assertion. When a pattern containing no special atoms is compared to an assertion, the two match only if they are exactly the same, with each corresponding position occupied by the same atom.
(match ’(color apple red) ’(color apple red)) T (match ’(color apple red) ’(color apple green)) NIL
In this example, the pattern variable (? a) matches 2. This results in assigning 2 to a variable called a. Similarly b is assigned 3.
(match-var ’((! u) a (? v)) ’(b c a d)) T u (b c) v d
Here, the pattern variable (! u) matches b c. Hence, u is assigned a value (b c).
man clisp
(load "test.l")
Details regarding the test program will be posted on the class web page.
(setq (^) print-case :downcase)
in that file.
If your program does not fully work, hand in a listing of the last working part along with your attempt at the next part, and indicate clearly which is which. No credit will be given if the last working part is not turned in. Points will be deducted for not following instructions, such as the above.
(pprint (symbol-function foo)) It will print out the definition of the function foo, using indentation to show nesting. This is useful to locate logically incorrect nesting due to, e.g., wrong parenthesizing. - If you cause an error, Common Lisp places you into a mode in which debugging can be performed (LISPcraft section 11.2). To exit any level, except the top level, type :q. To exit the top level, type > (bye)