


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: Assignment; Class: Programming Languages; Subject: Engineering Computer Science; University: University of California - Davis; Term: Winter 2006;
Typology: Assignments
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Homework 4: Common Lisp Program Deadline: 28th Feb, 2006
The purpose of this assignment is for you to gain some experience designing and implementing LISP programs. This assignment, however, explores only a few of the many interesting LISP features. The assignment is broken into three parts. The first part is fairly straightforward LISP warmup. The later parts requires 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 application in Lisp. This part requires you to define a number of simple functions: (a) The lisp function length counts the number of elements in the top level of a list. Write a function, all-length, that takes a list and counts the number of atoms that occur in a list at all levels.
(all-length ’(a ( b ) c)) 3 (all-length ’(a (b c) (d (e f)))) 6
(b) Define a function range that takes a list of numbers (at least one long) and returns a list of length 2 of the smallest and largest numbers.
(range ’(0 7 8 2 3 -1)) (-1 8) (range ’(7 6 5 4 3)) (3 7)
(c) Write a function, before that searches a list and tells if one object is found before another in a list.
(before ‘b ’d ’(a b c d)) (B C D)
Note that before returns true if we encounter a first argument before encountring the second. Thus, it will return true if the second argument does not occur in the list at all.
(before ’a ’d ’(a)) (a)
(d) Write a function, split-if, that returns a list into two parts. It takes two arguments: a function (f) and a list. All memebers for which f is true go into one list, and the rest go into another list.
> (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))
(e) Write a function, group, that takes arguments: a list l and a number n. It returns a new list in which the elements of l are grouped into sublists of length n. The remainder is put in a final sublist.
(group ’(a b c d e f g) 2) ((A B) (C D) (E F) (G))
(f) Write a function, mostn, that takes two arguments: a function f and a list l. It returns a list of all elements for which the function yields the highest score (along with the score itself), where score the value returned from the given function:
(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 which 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
The special atom ’?’ matches any single atom.
(match ’(color apple ?) ’(color apple red)) T (match ’(color? red) ’(color apple red)) T (match ’(color? red) ’(color apple green)) NIL
In the last example, color? red and (color apple green) do not match because red and green do not match. The special symbol ’!’ expands the capability of match by matching any one or more atoms.
(match ’(! table !) ’(this table supports a block)) T
Here, the first symbol ’! matches this, table matches table, and second symbol ’! matches supports a block.
(match ’(this table !) ’(this table supports a block)) T (match ’(! brown) ’(green red brown yellow)) NIL
(setq print-case :downcase)
in that file.
Part Percentage 1 35 2 30 3 35 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.