Common Lisp Program - Programming Languages | ECS 140A, Assignments of Programming Languages

Material Type: Assignment; Class: Programming Languages; Subject: Engineering Computer Science; University: University of California - Davis; Term: Winter 2006;

Typology: Assignments

Pre 2010

Uploaded on 07/30/2009

koofers-user-z23
koofers-user-z23 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Homework 4: Common Lisp Program
Deadline: 28th Feb, 2006
Overview
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.
Part 1: Simple function definitions
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 land a number n. It returns a new list in which the elements
of lare 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))
ECS 140A -1- Winter 2006
pf3
pf4

Partial preview of the text

Download Common Lisp Program - Programming Languages | ECS 140A and more Assignments Programming Languages in PDF only on Docsity!

Homework 4: Common Lisp Program Deadline: 28th Feb, 2006

Overview

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.

Part 1: Simple function definitions

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)

Part 2: Assertion and simple pattern matching

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.

  • When developing your program, you might find it easier to test your functions first interactively before using the test program. You might find trace, step, print, etc,. functions useful in debugging your functions.
  • You must develop your program in parts as outlined above. Grading will be divided as follows:

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.

  • A message giving exact details of what to turn in, where the provided test files are, etc, will be posted to the newsgroup and the home page.
  • A few points to help the novice LISP programmer: - Watch your use of (, ), ", and ’. Be sure to quote things that need to be quoted. - To see how lisp reads your function, use pretty printing. For example, > (pprint (symbol-function ’foo)) will print out the definition of 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)
  • Get started now to avoid the last minute rush.