| <html> |
| <head> |
| <title>LISP Tutorial 1: Basic LISP Programming</title> |
| </head> |
| <body BGCOLOR=FFFFFF> |
| <h1>LISP Tutorial 1: Basic LISP Programming</h1> |
|
|
|
|
|
|
| <h2>LISP Expressions</h2> |
|
|
| <p>When you start up the Common LISP environment, you should |
| see a prompt, which means that LISP is waiting for you to enter |
| a LISP expression. In the environment I am using, it looks like |
| the following: |
| <pre> |
| USER(1): |
| </pre> |
| The Common LISP environment follows the algorithm below |
| when interacting with users: |
| <pre> |
| loop |
| read in an expression from the console; |
| evaluate the expression; |
| print the result of evaluation to the console; |
| end loop. |
| </pre> |
| Common LISP reads in an expression, evaluates it, |
| and then prints out the result. For example, if you want to |
| compute the value of <em>(2 * cos(0) * (4 + 6))</em>, you type in: |
| <pre> |
| USER(1): (* 2 (cos 0) (+ 4 6)) |
| </pre> |
| Common LISP replies: |
| <pre> |
| 20.0 |
| </pre> |
| before prompting you to enter the next expression. |
| Several things are worth noting: |
| <ul> |
| <li>LISP expressions are composed of <em>forms</em>. |
| The most common LISP form is <em>function application</em>. |
| LISP represents a function call <em>f(x)</em> |
| as <tt>(f x)</tt>. For example, <em>cos(0)</em> |
| is written as <tt>(cos 0)</tt>. |
| <li>LISP expressions are case-insensitive. It makes |
| no difference whether we type <tt>(cos 0)</tt> or |
| <tt>(COS 0)</tt>. |
| <li>Similarly, "<tt>+</tt>" is the name of the |
| addition function that returns the sum of |
| its arguments. |
| <li>Some functions, like "<tt>+</tt>" and "<tt>*</tt>", |
| could take an arbitrary number of arguments. In our |
| example, "<tt>*</tt>" took three arguments. It could |
| as well take 2 arguments, as in "<tt>(* 2 3)</tt>", or |
| 4 arguments, as in "<tt>(* 2 3 4 5)</tt>". |
| <li>In general, |
| a function application form looks like |
| <tt>(<em>function</em> |
| <em>argument<sub>1</sub></em> <em>argument<sub>2</sub></em> |
| ... <em>argument<sub>n</sub></em>)</tt>. |
| As in many programming languages (e.g. C/C++), |
| LISP evaluates function calls in <em>applicative order</em>, which |
| means that all the argument forms are evaluated |
| before the function is invoked. That is to say, the |
| argument forms <tt>(cos 0)</tt> |
| and <tt>(+ 4 6)</tt> are respectively evaluated to the |
| values <em>1</em> and |
| <em>10</em> before they are passed as arguments to the <tt>*</tt> |
| function. |
| Some other forms, like |
| the <em>conditionals</em> we will see later, are not evaluated in |
| applicative order. |
| <li>Numeric values like <tt>4</tt> and <tt>6</tt> are |
| called <em>self-evaluating forms</em>: they evaluate |
| to themselves. To evaluate <tt>(+ 4 6)</tt> in applicative order, |
| the forms <tt>4</tt> and <tt>6</tt> are respectively evaluated |
| to the values <em>4</em> and <em>6</em> before they are passed |
| as arguments to the <tt>+</tt> function. |
| </ul> |
|
|
| <p>Complex arithmatic expressions can be constructed from built-in |
| functions like the following: |
| <p><table border=1> |
| <tr> |
| <th>Numeric Functions</th><th>Meaning</th> |
| </tr> |
| <tr> |
| <td><tt>(+ <em>x<sub>1</sub></em> <em>x<sub>2</sub></em> ... <em>x<sub>n</sub></em>)</tt></td> |
| <td>The sum of <em>x<sub>1</sub></em>, <em>x<sub>2</sub></em>, ..., <em>x<sub>n</sub></em></td> |
| </tr> |
| <tr> |
| <td><tt>(* <em>x<sub>1</sub></em> <em>x<sub>2</sub></em> ... <em>x<sub>n</sub></em>)</tt></td> |
| <td>The product of <em>x<sub>1</sub></em>, <em>x<sub>2</sub></em>, ..., <em>x<sub>n</sub></em></td> |
| </tr> |
| <tr> |
| <td><tt>(- <em>x</em> <em>y</em>)</tt></td> |
| <td>Subtract <em>y</em> from <em>x</em></td> |
| </tr> |
| <tr> |
| <td><tt>(/ <em>x</em> <em>y</em>)</tt></td> |
| <td>Divide <em>x</em> by <em>y</em></td> |
| </tr> |
| <tr> |
| <td><tt>(rem <em>x</em> <em>y</em>)</tt></td> |
| <td>The remainder of dividing <em>x</em> by <em>y</em></td> |
| </tr> |
| <tr> |
| <td><tt>(abs <em>x</em>)</tt></td> |
| <td>The absolute value of <em>x</em></td> |
| </tr> |
| <tr> |
| <td><tt>(max <em>x<sub>1</sub></em> <em>x<sub>2</sub></em> ... <em>x<sub>n</sub></em>)</tt></td> |
| <td>The maximum of <em>x<sub>1</sub></em>, <em>x<sub>2</sub></em>, ..., <em>x<sub>n</sub></em></td> |
| </tr> |
| <tr> |
| <td><tt>(min <em>x<sub>1</sub></em> <em>x<sub>2</sub></em> ... <em>x<sub>n</sub></em>)</tt></td> |
| <td>The minimum of <em>x<sub>1</sub></em>, <em>x<sub>2</sub></em>, ..., <em>x<sub>n</sub></em></td> |
| </tr> |
| </table> |
| <p>Common LISP has a rich set of pre-defined numerical functions. |
| For a complete coverage, consult Chapter 12 of the book, <em>Common |
| LISP, The Language (2nd Edition)</em> (CLTL2) by Guy Steele. In |
| general, we will not be able to cover all aspects of Common LISP |
| in this tutorial. |
| Adventurous readers should consult CLTL2 frequently for more |
| in-depth explanation of various features of the language. |
|
|
| <p><hr> |
| <b>Exercise:</b> Look up pages 376-378 of CLTL2 and find out |
| what the functions <tt>floor</tt> and <tt>ceiling</tt> are |
| for. Then, find out the subtle difference between <tt>mod</tt> |
| and <tt>rem</tt>. |
| <hr> |
|
|
| <h2>Defining Functions</h2> |
|
|
| <p>Evaluating expressions is not very interesting. We would like to |
| build expression abstractions that could be reused in the future. |
| For example, we could type in the following: |
| <pre> |
| USER(2): (defun double (x) (* x 2)) |
| DOUBLE |
| </pre> |
| In the above, we define a function named <tt>double</tt>, which |
| returns two times the value of its input argument <em>x</em>. We can then |
| test-drive the function as below: |
| <pre> |
| USER(3): (double 3) |
| 6 |
| USER(4): (double 7) |
| 14 |
| </pre> |
|
|
| <h2>Editing, Loading and Compiling LISP Programs</h2> |
|
|
| <p>Most of the functions we would like to write is going to |
| be a lot longer than the <tt>double</tt> function. |
| When working with complex programs, it is usually desirable |
| to edit the program with an editor, fully debug the code, |
| and then compile it for faster performance. Use |
| your favorite text editor (mine is <tt>emacs</tt>) to key in the |
| following function definition: |
| <pre> |
| ;;; testing.lisp |
| ;;; by Philip Fong |
| ;;; |
| ;;; Introductory comments are preceded by ";;;" |
| ;;; Function headers are preceded by ";;" |
| ;;; Inline comments are introduced by ";" |
| ;;; |
|
|
| ;; |
| ;; Triple the value of a number |
| ;; |
|
|
| (defun triple (X) |
| "Compute three times X." ; Inline comments can |
| (* 3 X)) ; be placed here. |
|
|
| ;; |
| ;; Negate the sign of a number |
| ;; |
|
|
| (defun negate (X) |
| "Negate the value of X." ; This is a documentation string. |
| (- X)) |
| </pre> |
| Save the above in the file <tt>testing.lisp</tt>. Now load |
| the definition into the LISP environment by typing: |
| <pre> |
| USER(5): (load "testing.lisp") |
| ; Loading ./testing.lisp |
| T |
| </pre> |
| Let us try to see if they are working properly. |
| <pre> |
| USER(6): (triple 2) |
| 6 |
| USER(7): (negate 3) |
| -3 |
| </pre> |
| When functions are fully debugged, we can also |
| compile them into binaries: |
| <pre> |
| USER(8): (compile-file "testing.lisp") |
| </pre> |
| Depending on whether your code is well-formed, and what system |
| you are using, some compilation messages will be generated. |
| The compiled code can be loaded into the LISP environment later by |
| using the following: |
| <pre> |
| USER(9): (load "testing") |
| ; Fast loading ./testing.fasl |
| T |
| </pre> |
|
|
| <h2>Control Stuctures: Recursions and Conditionals</h2> |
|
|
| <p>Now that we are equipped with all the tools for |
| developing LISP programs, let us venture into |
| something more interesting. Consider the definition of |
| factorials: |
| <table> |
| <tr> |
| <td> </td><td><em>n! = 1</em></td><td> </td><td>if <em>n = 1</em></td> |
| </tr> |
| <tr> |
| <td> </td><td><em>n! = n * (n - 1)!</em></td><td> </td><td>if <em>n > 1</em></td> |
| </tr> |
| </table> |
| We can implement a function to compute factorials using |
| recursion: |
| <pre> |
| (defun factorial (N) |
| "Compute the factorial of N." |
| (if (= N 1) |
| 1 |
| (* N (factorial (- N 1))))) |
| </pre> |
| The <tt>if</tt> form checks if <tt>N</tt> is one, and |
| returns one if that is the case, or else returns |
| <em>N * (N - 1)!</em>. |
|
|
| Several points are worth noting: |
| <ul> |
| <li>The condition expression <tt>(= N 1)</tt> is |
| a relational expression. It returns boolean |
| values <tt>T</tt> or <tt>NIL</tt>. In fact, LISP |
| treats <tt>NIL</tt> as false, and everything else |
| as true. Other relational operators include the |
| following: |
| <p><table border=1> |
| <tr> |
| <th>Relational Operators</th><th>Meaning</th> |
| </tr> |
| <tr> |
| <td><tt>(= <em>x</em> <em>y</em>)</tt></td> |
| <td><em>x</em> is equal to <em>y</em></td> |
| </tr> |
| <tr> |
| <td><tt>(/= <em>x</em> <em>y</em>)</tt></td> |
| <td><em>x</em> is not equal to <em>y</em></td> |
| </tr> |
| <tr> |
| <td><tt>(< <em>x</em> <em>y</em>)</tt></td> |
| <td><em>x</em> is less than <em>y</em></td> |
| </tr> |
| <tr> |
| <td><tt>(> <em>x</em> <em>y</em>)</tt></td> |
| <td><em>x</em> is greater than <em>y</em></td> |
| </tr> |
| <tr> |
| <td><tt>(<= <em>x</em> <em>y</em>)</tt></td> |
| <td><em>x</em> is no greater than <em>y</em></td> |
| </tr> |
| <tr> |
| <td><tt>(>= <em>x</em> <em>y</em>)</tt></td> |
| <td><em>x</em> is no less than <em>y</em></td> |
| </tr> |
| </table> |
| <p> |
| <li>The <tt>if</tt> form is not a <em>strict function</em> |
| (strict functions evaluate their arguments in applicative |
| order). |
| Instead, the <tt>if</tt> form evaluates |
| the condition <tt>(= N 1)</tt> before further |
| evaluating the other two arguments. |
| If the condition evaluates to true, then only the |
| second argument is evaluated, and its value is |
| returned as the value of the <tt>if</tt> form. |
| Otherwise, the third argument is evaluated, and its |
| value is returned. Forms that are not strict |
| functions are called |
| <em>special forms</em>. |
| <li>The function is recursive. The definition of <tt>factorial</tt> |
| involves invocation of itself. Recursion is, for now, our only |
| mechanism for producing looping behavior. Specifically, |
| the kind of recursion we are looking at is called <em>linear |
| recursion</em>, in which the function may make at most one |
| recursive call from any level of invocation. |
| </ul> |
|
|
| <p>To better understand the last point, |
| we can make use of the debugging facility |
| <tt>trace</tt> (do not compile your code if you want to use |
| <tt>trace</tt>): |
| <pre> |
| USER(11): (trace factorial) |
| (FACTORIAL) |
| USER(12): (factorial 4) |
| 0: (FACTORIAL 4) |
| 1: (FACTORIAL 3) |
| 2: (FACTORIAL 2) |
| 3: (FACTORIAL 1) |
| 3: returned 1 |
| 2: returned 2 |
| 1: returned 6 |
| 0: returned 24 |
| 24 |
| </pre> |
| Tracing <tt>factorial</tt> allows us to examine |
| the recursive invocation of the function. As you |
| can see, at most one recursive call is made from |
| each level of invocation. |
|
|
| <p><hr><b>Exercise:</b> |
| The <em>N</em>'th <em>triangular number</em> is |
| defined to be <em>1 + 2 + 3 + ... + N</em>. |
| Alternatively, we could give a recursive definition |
| of triangular number as follows: |
| <table> |
| <tr> |
| <td></td> |
| <td><em>T(n) = 1</em></td> |
| <td></td> |
| <td>if <em>n = 1</em></td> |
| </tr> |
| <tr> |
| <td></td> |
| <td><em>T(n) = n + T(n-1)</em></td> |
| <td></td> |
| <td>if <em>n > 1</em></td> |
| </tr> |
| </table> |
| Use the recursive definition to |
| help you implement |
| a linearly recursive function <tt>(triangular <em>N</em>)</tt> |
| that returns the <em>N</em>'th triangular number. |
| Enter your function definition into a text file. Then load |
| it into LISP. Trace the execution of <tt>(triangular 6)</tt>. |
| <hr> |
|
|
| <p><hr><b>Exercise:</b> |
| Write down a recursive definition of <em>B<sup>E</sup></em> |
| (assuming that |
| both <em>B</em> and <em>E</em> are non-negative integers). |
| Then implement a linearly |
| recursive function <tt>(power <em>B</em> <em>E</em>)</tt> that |
| computes <em>B<sup>E</sup></em>. |
| Enter your function definition into a text file. Then load |
| it into LISP. Trace the execution of <tt>(power 2 6)</tt>. |
| <hr> |
|
|
| <h2>Multiple Recursions</h2> |
|
|
| <p>Recall the definition of Fibonacci numbers: |
| <table> |
| <tr> |
| <td> </td> <td><em>Fib(n) = 1</em></td> <td> </td> |
| <td>for <em>n = 0</em> or <em>n = 1</em></td> |
| </tr> |
| <tr> |
| <td> </td> <td><em>Fib(n) = Fib(n-1) + Fib(n-2)</em></td> <td> </td> |
| <td>for <em>n > 1</em></td> |
| </tr> |
| </table> |
| This definition can be directly translated to the |
| following LISP code: |
| <pre> |
| (defun fibonacci (N) |
| "Compute the N'th Fibonacci number." |
| (if (or (zerop N) (= N 1)) |
| 1 |
| (+ (fibonacci (- N 1)) (fibonacci (- N 2))))) |
| </pre> |
|
|
| <p>Again, several observations can be made. |
| First, the function call <tt>(zerop N)</tt> tests if <tt>N</tt> is |
| zero. It is merely a shorthand for <tt>(= N 0)</tt>. As such, |
| <tt>zerop</tt> returns either <tt>T</tt> or <tt>NIL</tt>. We |
| call such a boolean function a <em>predicate</em>, as indicated by the |
| suffix <tt>p</tt>. Some other built-in shorthands and predicates |
| are the following: |
| <p><table border=1> |
| <tr> |
| <th>Shorthand</th><th>Meaning</th> |
| </tr> |
| <tr> |
| <td><tt>(1+ <em>x</em>)</tt></td> |
| <td><tt>(+ <em>x</em> 1)</tt></td> |
| </tr> |
| <tr> |
| <td><tt>(1- <em>x</em>)</tt></td> |
| <td><tt>(- <em>x</em> 1)</tt></td> |
| </tr> |
| <tr> |
| <td><tt>(zerop <em>x</em>)</tt></td><td><tt>(= <em>x</em> 0)</tt></td> |
| </tr> |
| <tr> |
| <td><tt>(plusp <em>x</em>)</tt></td><td><tt>(> <em>x</em> 0)</tt></td> |
| </tr> |
| <tr> |
| <td><tt>(minusp <em>x</em>)</tt></td><td><tt>(< <em>x</em> 0)</tt></td> |
| </tr> |
| <tr> |
| <td><tt>(evenp <em>x</em>)</tt></td><td><tt>(= (rem <em>x</em> 2) 0)</tt></td> |
| </tr> |
| <tr> |
| <td><tt>(oddp <em>x</em>)</tt></td><td><tt>(/= (rem <em>x</em> 2) 0)</tt></td> |
| </tr> |
| </table> |
|
|
| <p>Second, the <tt>or</tt> form is a logical operator. Like <tt>if</tt>, |
| <tt>or</tt> is not a strict function. |
| It evaluates its arguments from left |
| to right, returning non-<tt>NIL</tt> immediately if it encounters |
| an argument that evaluates to non-<tt>NIL</tt>. It evaluates |
| to <tt>NIL</tt> if all tests fail. |
| For example, in the expression <tt>(or t (= 1 1))</tt>, the second |
| argument <tt>(= 1 1)</tt> will not be evaluated. Similar logical |
| connectives are listed below: |
| <p><table border=1> |
| <tr> |
| <th>Logical Operators</th><th>Meaning</th> |
| </tr> |
| <tr> |
| <td><tt>(or <em>x<sub>1</sub></em> <em>x<sub>2</sub></em> ... <em>x<sub>n</sub></em>)</tt></td> |
| <td>Logical or</td> |
| </tr> |
| <tr> |
| <td><tt>(and <em>x<sub>1</sub></em> <em>x<sub>2</sub></em> ... <em>x<sub>n</sub></em>)</tt></td> |
| <td>Logical and</td> |
| </tr> |
| <tr> |
| <td><tt>(not <em>x</em>)</tt></td> |
| <td>Logical negation</td> |
| </tr> |
| </table> |
|
|
| <p>Third, the function definition contains two self references. |
| It first recursively evaluates <tt>(fibonacci (- N 1))</tt> to |
| compute <em>Fib(N-1)</em>, then evaluates <tt>(fibonacci (- N 2))</tt> |
| to obtain <em>Fib(N-2)</em>, and lastly return their sum. |
| This kind of recursive definitiion is called <em>double recursion</em> |
| (more generally, <em>multiple recursion</em>). |
| Tracing the function yields the following: |
| <pre> |
| USER(20): (fibonacci 3) |
| 0: (FIBONACCI 3) |
| 1: (FIBONACCI 2) |
| 2: (FIBONACCI 1) |
| 2: returned 1 |
| 2: (FIBONACCI 0) |
| 2: returned 1 |
| 1: returned 2 |
| 1: (FIBONACCI 1) |
| 1: returned 1 |
| 0: returned 3 |
| 3 |
| </pre> |
| Note that in each level, there could be up to two recursive invocations. |
|
|
|
|
| <p><hr><b>Exercise</b>: The Binomial Coefficient <em>B(n, r)</em> is |
| the coefficient of the term <em>x<sup>r</sup></em> in the binormial |
| expansion of |
| <em>(1 + x)<sup>n</sup></em>. |
| For example, <em>B(4, 2) = 6</em> because <em>(1+x)<sup>4</sup> |
| = 1 + 4x + 6x<sup>2</sup> + 4x<sup>3</sup> + x<sup>4</sup></em>. |
| The Binomial Coefficient |
| can be computed using the <em>Pascal Triangle</em> formula: |
| <table> |
| <tr> |
| <td> </td> <td><em>B(n, r) = 1</em></td> <td> </td> |
| <td>if <em>r = 0</em> or <em>r = n</em></td> |
| </tr> |
| <tr> |
| <td> </td><td><em>B(n, r) = B(n-1, r-1) + B(n-1, r)</td> <td> </td> |
| <td>otherwise</td> |
| </tr> |
| </table> |
| Implement a doubly recursive function <tt>(binomial <em>N</em> <em>R</em>)</tt> |
| that computes the binomial coefficient <em>B(N, R)</em>. |
| <hr> |
|
|
| <p>Some beginners might find nested function calls like the |
| following very difficult to understand: |
| <pre> |
| (+ (fibonacci (- N 1)) (fibonacci (- N 2))))) |
| </pre> |
| To make such expressions easier to write and comprehend, one |
| could define local name bindings to represent intermediate |
| results: |
| <pre> |
| (let |
| ((F1 (fibonacci (- N 1))) |
| (F2 (fibonacci (- N 2)))) |
| (+ F1 F2)) |
| </pre> |
| The <tt>let</tt> special form above defines two local variables, |
| <tt>F1</tt> and <tt>F2</tt>, which binds to <em>Fib(N-1)</em> |
| and <em>Fib(N-2)</em> respectively. Under these local |
| bindings, <tt>let</tt> evaluates <tt>(+ F1 F2)</tt>. |
| The <tt>fibonacci</tt> function can thus be rewritten as follows: |
| <pre> |
| (defun fibonacci (N) |
| "Compute the N'th Fibonacci number." |
| (if (or (zerop N) (= N 1)) |
| 1 |
| (let |
| ((F1 (fibonacci (- N 1))) |
| (F2 (fibonacci (- N 2)))) |
| (+ F1 F2)))) |
| </pre> |
|
|
| <p>Notice that <tt>let</tt> creates all bindings in parallel. |
| That is, both <tt>(fibonacci (- N 1))</tt> and <tt>(fibonacci (- N 2))</tt> |
| are evaluated first, and then they are bound to <tt>F1</tt> and <tt>F2</tt>. |
| This means that the following LISP code will not work: |
| <pre> |
| (let |
| ((x 1) |
| (y (* x 2))) |
| (+ x y)) |
| </pre> |
| LISP will attempt to evaluate the right hand sides first before |
| the bindings are established. So, the expression |
| <tt>(* x 2)</tt> is evaluated before the binding of <tt>x</tt> |
| is available. To perform sequential binding, use the <tt>let*</tt> |
| form instead: |
| <pre> |
| (let* |
| ((x 1) |
| (y (* x 2))) |
| (+ x y)) |
| </pre> |
| LISP will bind <tt>1</tt> to <tt>x</tt>, then evaluate <tt>(* x 2)</tt> |
| before the value is bound to <tt>y</tt>. |
|
|
| <h2>Lists</h2> |
|
|
| <p>Numeric values are not the only type of data LISP supports. |
| LISP is designed for symbolic computing. The fundamental |
| LISP data structure for supporting symbolic manipulation are |
| lists. In fact, LISP stands for "LISt Processing." |
|
|
| <p>Lists are containers that supports sequential traversal. |
| List is also a <em>recursive data structure</em>: its definition |
| is recursive. |
| As such, most of its traversal algorithms are recursive functions. |
| In order to better |
| understand a recursive abstract data type and prepare oneself |
| to develop recursive operations on the data type, one should present |
| the data type in terms of its <em>constructors</em>, |
| <em>selectors</em> and <em>recognizers</em>. |
|
|
| <p>Constructors are forms that create new |
| instances of a data type (possibly out of some simpler components). |
| A list is obtained by |
| evaluating one of the following constructors: |
| <ol> |
| <li><tt>nil</tt>: Evaluating <tt>nil</tt> creates an <em>empty</em> |
| list; |
| <li><tt>(cons <em>x</em> <em>L</em>)</tt>: Given a LISP object |
| <em>x</em> and a list <em>L</em>, evaluating <tt>(cons <em>x</em> <em>L</em>)</tt> |
| creates a list |
| containing |
| <em>x</em> followed by the elements in <em>L</em>. |
| </ol> |
| <p>Notice that the above definition is inherently recursive. |
| For example, |
| to construct a list containing 1 followed by 2, we could type |
| in the expression: |
| <pre> |
| USER(21): (cons 1 (cons 2 nil)) |
| (1 2) |
| </pre> |
| LISP replies by printing <tt>(1 2)</tt>, which is a more |
| readable representation of a list containing 1 followed by 2. |
| To understand why the above works, notice that <tt>nil</tt> |
| is a list (an empty one), and thus <tt>(cons 2 nil)</tt> is |
| also a list (a list containing 1 followed by nothing). Applying |
| the second constructor again, we see that <tt>(cons 1 (cons 2 nil))</tt> |
| is also a list (a list containing 1 followed by 2 followed by nothing). |
|
|
|
|
| <p>Typing <tt>cons</tt> expressions could be tedious. |
| If we already know all the elements in a list, we could |
| enter our list as <em>list literals</em>. For example, to |
| enter a list containing all prime numbers less than 20, |
| we could type in the following expression: |
| <pre> |
| USER(22): (quote (2 3 5 7 11 13 17 19)) |
| (2 3 5 7 11 13 17 19) |
| </pre> |
| Notice that we have quoted the list using the <tt>quote</tt> special |
| form. This is necessary |
| because, without the quote, LISP would interpret the expression |
| <tt>(2 3 5 7 11 13 17 19)</tt> |
| as a function call to a function with name "2" and arguments 3, 5, ..., 19 |
| The <tt>quote</tt> is just a syntactic device that instructs LISP |
| not to evaluate the a form in applicative order, but rather treat it |
| as a literal. |
| Since quoting is used frequently in LISP programs, |
| there is a shorthand for <tt>quote</tt>: |
| <pre> |
| USER(23): '(2 3 5 7 11 13 17 19) |
| (2 3 5 7 11 13 17 19) |
| </pre> |
| The quote symbol <tt>'</tt> is nothing but a syntactic shorthand for |
| <tt>(quote ...)</tt>. |
|
|
| <p>The second ingredient of an abstract data type are its selectors. |
| Given a composite object constructed out of several components, |
| a selector form returns one of its components. Specifically, |
| suppose a list <em>L<sub>1</sub></em> is constructed by evaluating |
| <tt>(cons <em>x</em> <em>L<sub>2</sub></em>)</tt>, where <em>x</em> is a |
| LISP object and <em>L<sub>2</sub></em> is a list. Then, the selector forms |
| <tt>(first <em>L<sub>1</sub></em>)</tt> and <tt>(rest <em>L<sub>1</sub></em>)</tt> evaluate |
| to <em>x</em> and <em>L<sub>2</sub></em> respectively, as the following |
| examples illustrate: |
| <pre> |
| USER(24): (first '(2 4 8)) |
| 2 |
| USER(25): (rest '(2 4 8)) |
| (4 8) |
| USER(26): (first (rest '(2 4 8))) |
| 4 |
| USER(27): (rest (rest '(2 4 8))) |
| (8) |
| USER(28): (rest (rest (rest '(8)))) |
| NIL |
| </pre> |
|
|
| <p>Finally, we look at recognizers, expressions that test how an |
| object is constructed. Corresponding to each constructor of |
| a data type is a recognizer. In the case of list, they are |
| <tt>null</tt> for <tt>nil</tt> and <tt>consp</tt> for <tt>cons</tt>. |
| Given a list <em>L</em>, <tt>(null <em>L</em>)</tt> returns |
| <tt>t</tt> iff <em>L</em> is <tt>nil</tt>, and <tt>(consp <em>L</em>)</tt> |
| returns <tt>t</tt> iff <em>L</em> is constructed from <tt>cons</tt>. |
| <pre> |
| USER(29): (null nil) |
| T |
| USER(30): (null '(1 2 3)) |
| NIL |
| USER(31): (consp nil) |
| NIL |
| USER(32): (consp '(1 2 3)) |
| T |
| </pre> |
| Notice that, since lists have only two constructors, the recognizers |
| are complementary. Therefore, we usually need only one of them. |
| In our following discussion, we use only <tt>null</tt>. |
|
|
| <h2>Structural Recursion with Lists</h2> |
|
|
| <p>As we have promised, understanding how the constructors, selectors and |
| recognizers of lists work helps us to develop recursive functions that |
| traverse a list. Let us begin with an example. |
| The LISP built-in function <tt>list-length</tt> counts the number of |
| elements in a list. For example, |
| <pre> |
| USER(33): (list-length '(2 3 5 7 11 13 17 19)) |
| 8 |
| </pre> |
| Let us try to see how such a function can be implemented recursively. |
| A given list <em>L</em> is created by either one of the two constructors, |
| namely <tt>nil</tt> or a <tt>cons</tt>: |
| <ul> |
| <li><em>Case 1</em>: <em>L</em> is <tt>nil</tt>.<br> |
| The length of an empty list is zero. |
| <li><em>Case 2</em>: <em>L</em> is constructed by <tt>cons</tt>.<br> |
| Then <em>L</em> is composed of two parts, namely, |
| <tt>(first <em>L</em>)</tt> and <tt>(rest <em>L</em>)</tt>. In such |
| case, the length of <em>L</em> can be obtained inductively by adding |
| 1 to the length of <tt>(rest <em>L</em>)</tt>. |
| </ul> |
| <p>Formally, we could |
| implement our own version of <tt>list-length</tt> as follows: |
| <pre> |
| (defun recursive-list-length (L) |
| "A recursive implementation of list-length." |
| (if (null L) |
| 0 |
| (1+ (recursive-list-length (rest L))))) |
| </pre> |
| Here, we use the recognizer <tt>null</tt> to differentiate how |
| <em>L</em> is constructed. In case <em>L</em> is <tt>nil</tt>, |
| we return 0 as its length. Otherwise, <em>L</em> is a <tt>cons</tt>, |
| and we return 1 plus the length of <tt>(rest <em>L</em>)</tt>. |
| Recall that <tt>(1+ <em>n</em>)</tt> is simply a shorthand for |
| <tt>(+ <em>n</em> 1)</tt>. |
|
|
| <p>Again, it is instructive to use the trace facilities to |
| examine the unfolding of recursive invocations: |
| <pre> |
| USER(40): (trace recursive-list-length) |
| (RECURSIVE-LIST-LENGTH) |
| USER(41): (recursive-list-length '(2 3 5 7 11 13 17 19)) |
| 0: (RECURSIVE-LIST-LENGTH (2 3 5 7 11 13 17 19)) |
| 1: (RECURSIVE-LIST-LENGTH (3 5 7 11 13 17 19)) |
| 2: (RECURSIVE-LIST-LENGTH (5 7 11 13 17 19)) |
| 3: (RECURSIVE-LIST-LENGTH (7 11 13 17 19)) |
| 4: (RECURSIVE-LIST-LENGTH (11 13 17 19)) |
| 5: (RECURSIVE-LIST-LENGTH (13 17 19)) |
| 6: (RECURSIVE-LIST-LENGTH (17 19)) |
| 7: (RECURSIVE-LIST-LENGTH (19)) |
| 8: (RECURSIVE-LIST-LENGTH NIL) |
| 8: returned 0 |
| 7: returned 1 |
| 6: returned 2 |
| 5: returned 3 |
| 4: returned 4 |
| 3: returned 5 |
| 2: returned 6 |
| 1: returned 7 |
| 0: returned 8 |
| 8 |
| </pre> |
|
|
| <p>The kind of recursion we see here is called <em>structural |
| recursion</em>. Its standard pattern is as follows. To |
| process an instance <em>X</em> of a recursive data type: |
| <ol> |
| <li>Use the recognizers to determine how <em>X</em> is created |
| (i.e. which constructor creates it). In our example, we |
| use <tt>null</tt> to decide if a list is created by |
| <tt>nil</tt> or <tt>cons</tt>. |
| <li>For instances that are atomic (i.e. those created by |
| constructors with no components), return a trivial |
| value. For example, in the case when a list is <tt>nil</tt>, |
| we return zero as its length. |
| <li>If the instance is composite, then use the selectors to |
| extract its components. In our example, we use <tt>first</tt> |
| and <tt>rest</tt> to extract the two components of a nonempty |
| list. |
| <li>Following that, we apply recursion on one or more components |
| of <em>X</em>. For instance, we recusively invoked |
| <tt>recursive-list-length</tt> on <tt>(rest L)</tt>. |
| <li>Finally, we use either the constructors or some other |
| functions to combine the result of the recursive calls, |
| yielding the value of the function. In the case of |
| <tt>recursive-list-length</tt>, we return one plus the |
| result of the recursive call. |
| </ol> |
|
|
| <p><hr><b>Exercise</b>: Implement a linearly recursive |
| function <tt>(sum <em>L</em>)</tt> which |
| computes the sum of all numbers in a list <em>L</em>. |
| Compare your solution with the standard pattern of |
| structural recursion. |
| <hr> |
|
|
| <p>Sometimes, long traces like the one for list-length |
| may be difficult to read on a terminal screen. |
| Common LISP allows you to capture screen I/O into a file |
| so that you can, for example, produce a hard copy for |
| more comfortable reading. To capture the trace of |
| executing <tt>(recursive-list-length '(2 3 5 7 11 13 17 19))</tt>, |
| we use the <tt>dribble</tt> command: |
| <pre> |
| USER(42): (dribble "output.txt") |
| dribbling to file "output.txt" |
| |
| NIL |
| USER(43): (recursive-list-length '(2 3 5 7 11 13 17 19)) |
| 0: (RECURSIVE-LIST-LENGTH (2 3 5 7 11 13 17 19)) |
| 1: (RECURSIVE-LIST-LENGTH (3 5 7 11 13 17 19)) |
| 2: (RECURSIVE-LIST-LENGTH (5 7 11 13 17 19)) |
| 3: (RECURSIVE-LIST-LENGTH (7 11 13 17 19)) |
| 4: (RECURSIVE-LIST-LENGTH (11 13 17 19)) |
| 5: (RECURSIVE-LIST-LENGTH (13 17 19)) |
| 6: (RECURSIVE-LIST-LENGTH (17 19)) |
| 7: (RECURSIVE-LIST-LENGTH (19)) |
| 8: (RECURSIVE-LIST-LENGTH NIL) |
| 8: returned 0 |
| 7: returned 1 |
| 6: returned 2 |
| 5: returned 3 |
| 4: returned 4 |
| 3: returned 5 |
| 2: returned 6 |
| 1: returned 7 |
| 0: returned 8 |
| 8 |
| USER(44): (dribble) |
| </pre> |
| The form <tt>(dribble "output.txt")</tt> instructs Common LISP |
| to begin capturing all terminal I/O into a file called <tt>output.txt</tt>. |
| The trailing <tt>(dribble)</tt> form instructs Common LISP to |
| stop I/O capturing, and closes the file <tt>output.txt</tt>. If we |
| examine <tt>output.txt</tt>, we will see the following: |
| <pre> |
| dribbling to file "output.txt" |
| |
| NIL |
| USER(43): (recursive-list-length '(2 3 5 7 11 13 17 19)) |
| 0: (RECURSIVE-LIST-LENGTH (2 3 5 7 11 13 17 19)) |
| 1: (RECURSIVE-LIST-LENGTH (3 5 7 11 13 17 19)) |
| 2: (RECURSIVE-LIST-LENGTH (5 7 11 13 17 19)) |
| 3: (RECURSIVE-LIST-LENGTH (7 11 13 17 19)) |
| 4: (RECURSIVE-LIST-LENGTH (11 13 17 19)) |
| 5: (RECURSIVE-LIST-LENGTH (13 17 19)) |
| 6: (RECURSIVE-LIST-LENGTH (17 19)) |
| 7: (RECURSIVE-LIST-LENGTH (19)) |
| 8: (RECURSIVE-LIST-LENGTH NIL) |
| 8: returned 0 |
| 7: returned 1 |
| 6: returned 2 |
| 5: returned 3 |
| 4: returned 4 |
| 3: returned 5 |
| 2: returned 6 |
| 1: returned 7 |
| 0: returned 8 |
| 8 |
| USER(44): (dribble) |
| </pre> |
|
|
| <h2>Symbols</h2> |
|
|
|
|
| <p>The lists we have seen so far are lists of numbers. Another data |
| type of LISP is <em>symbols</em>. A symbol is simply a sequence |
| of characters: |
| <pre> |
| USER(45): 'a ; LISP is case-insensitive. |
| A |
| USER(46): 'A ; 'a and 'A evaluate to the same symbol. |
| A |
| USER(47): 'apple2 ; Both alphanumeric characters ... |
| APPLE2 |
| USER(48): 'an-apple ; ... and symbolic characters are allowed. |
| AN-APPLE |
| USER(49): t ; Our familiar t is also a symbol. |
| T |
| USER(50): 't ; In addition, quoting is redundant for t. |
| T |
| USER(51): nil ; Our familiar nil is also a symbol. |
| NIL |
| USER(52): 'nil ; Again, it is self-evaluating. |
| NIL |
| </pre> |
|
|
| <p>With symbols, we can build more interesting lists: |
| <pre> |
| USER(53): '(how are you today ?) ; A list of symbols. |
| (HOW ARE YOU TODAY ?) |
| USER(54): '(1 + 2 * x) ; A list of symbols and numbers. |
| (1 + 2 * X) |
| USER(55): '(pair (2 3)) ; A list containing 'pair and '(2 3). |
| (pair (2 3)) |
| </pre> |
| Notice that the list <tt>(pair (2 3))</tt> |
| has length 2: |
| <pre> |
| USER(56): (recursive-list-length '(pair (2 3))) |
| 2 |
| </pre> |
| Notice also the result of applying accessors: |
| <pre> |
| USER(57): (first '(pair (2 3))) |
| PAIR |
| USER(58): (rest '(pair (2 3))) |
| ((2 3)) |
| </pre> |
| Lists containing other lists as members are difficult to understand |
| for beginners. Make sure you understand the above example. |
|
|
| <h2>Example: <tt>nth</tt></h2> |
|
|
| <p>LISP defines a function <tt>(nth <em>N</em> <em>L</em>)</tt> |
| that returns the <em>N</em>'th member of list <em>L</em> (assuming |
| that the elements are numbered from zero onwards): |
| <pre> |
| USER(59): (nth 0 '(a b c d)) |
| A |
| USER(60): (nth 2 '(a b c d)) |
| C |
| </pre> |
| We could implement our own version of <tt>nth</tt> by linear recursion. |
| Given <em>N</em> and <em>L</em>, either <em>L</em> is <tt>nil</tt> |
| or it is constructed by <tt>cons</tt>. |
| <ul> |
| <li><em>Case 1</em>: <em>L</em> is <tt>nil</tt>.<br> |
| Accessing the <em>N</em>'th element is an undefined operation, |
| and our implementation should arbitrarily return <tt>nil</tt> to |
| indicate this. |
| <li><em>Case 2</em>: <em>L</em> is constructed by a <tt>cons</tt>.<br> |
| Then <em>L</em> |
| has two components: <tt>(first <em>L</em>)</tt> and |
| <tt>(rest <em>L</em>)</tt>. There are |
| two subcases: either <em>N = 0</em> or <em>N > 0</em>: |
| <ul> |
| <li> <em>Case 2.1</em>: <em>N = 0</em>.<br> |
| The zeroth element of <em>L</em> is simply |
| <tt>(first <em>L</em>)</tt>. |
| <li> <em>Case 2.2</em>: <em>N > 0</em>.<br> |
| The <em>N</em>'th |
| member of <em>L</em> is exactly the <em>(N-1)</em>'th member of |
| <tt>(rest <em>L</em>)</tt>. |
| </ul> |
| </ul> |
| The following code implements our algorithm: |
| <pre> |
| (defun list-nth (N L) |
| "Return the N'th member of a list L." |
| (if (null L) |
| nil |
| (if (zerop N) |
| (first L) |
| (list-nth (1- N) (rest L))))) |
| </pre> |
| Recall that <tt>(1- N)</tt> is merely a shorthand for <tt>(- N 1)</tt>. |
| Notice that both our implementation and its |
| correctness argument closely follow the standard pattern |
| of structural recursion. |
| Tracing the execution of the function, we get: |
| <pre> |
| USER(61): (list-nth 2 '(a b c d)) |
| 0: (LIST-NTH 2 (A B C D)) |
| 1: (LIST-NTH 1 (B C D)) |
| 2: (LIST-NTH 0 (C D)) |
| 2: returned C |
| 1: returned C |
| 0: returned C |
| C |
| </pre> |
|
|
|
|
| <p><hr><b>Exercise</b>: LISP has a built-in function <tt>(last <em>L</em>)</tt> |
| that returns a the last <tt>cons</tt> structure in a given list <em>L</em>. |
| <pre> |
| USER(62): (last '(a b c d)) |
| (d) |
| USER(63): (last '(1 2 3)) |
| (3) |
| </pre> |
| Implement your own version of <tt>last</tt> |
| using linear recursion. You may assume that <tt>(last nil)</tt> |
| returns <tt>nil</tt>. Compare your implementation with |
| the standard pattern of structural recursion. |
| <hr> |
|
|
| <p>Notice that we have a standard if-then-else-if structure |
| in our implementation of <tt>list-nth</tt>. |
| Such logic can alternatively |
| be implemented using the <tt>cond</tt> special form. |
| <pre> |
| (defun list-nth (n L) |
| "Return the n'th member of a list L." |
| (cond |
| ((null L) nil) |
| ((zerop n) (first L)) |
| (t (list-nth (1- n) (rest L))))) |
| </pre> |
| The <tt>cond</tt> form above is evaluated as follows. The condition |
| <tt>(null L)</tt> is evaluated first. If the result is true, |
| then <tt>nil</tt> is returned. Otherwise, the condition <tt>(zerop n)</tt> |
| is evaluated. If the condition holds, then the value of <tt>(first L)</tt> is |
| returned. In case neither of the conditions holds, |
| the value of <tt>(list-nth |
| (1- n) (rest L))</tt> is returned. |
|
|
| <p><hr><b>Exercise</b>: Survey CLTL2 section 7.6 (pages 156-161) and find |
| out what other conditional special forms are available in Common LISP. |
| Do you know when the special forms <tt>when</tt> and <tt>unless</tt> |
| should be used instead of <tt>if</tt>? |
| <hr> |
|
|
| <h2>Example: <tt>member</tt></h2> |
|
|
| <p>LISP defines a function <tt>(member <em>E</em> <em>L</em>)</tt> |
| that returns non-NIL if <em>E</em> is a member of <em>L</em>. |
| <pre> |
| USER(64): (member 'b '(perhaps today is a good day to die)) ; test fails |
| NIL |
| USER(65): (member 'a '(perhaps today is a good day to die)) ; returns non-NIL |
| '(a good day to die) |
| </pre> |
| We implement our own recursive version as follows: |
| <pre> |
| (defun list-member (E L) |
| "Test if E is a member of L." |
| (cond |
| ((null L) nil) |
| ((eq E (first L)) t) |
| (t (list-member E (rest L))))) |
| </pre> |
| The correctness of the above implementation is easy to justify. The list |
| <em>L</em> is either constructed by <em>nil</em> or by a call to |
| <em>cons</em>: |
| <ul> |
| <li><em>Case 1</em>: <em>L</em> is <tt>nil</tt>.<br> |
| <em>L</em> is |
| empty, and there is no way <em>E</em> is in <em>L</em>. |
| <li><em>Case 2</em>: <em>L</em> |
| is constructed by <tt>cons</tt><br> |
| Then it has two components: |
| <tt>(first <em>L</em>)</tt> and <tt>(rest <em>L</em>)</tt>. |
| There are two cases, either <tt>(first <em>L</em>)</tt> is <em>E</em> |
| itself, or it is not. |
| <ul> |
| <li><em>Case 2.1</em>: <em>E</em> equals <tt>(first <em>L</em>)</tt>.<br> |
| This means that <em>E</em> is a member of <em>L</em>, |
| <li><em>Case 2.2</em>: <em>E</em> does not equal <tt>(first <em>L</em>)</tt>.<br> |
| Then <em>E</em> is a member of <em>L</em> iff <em>E</em> is a |
| member of <tt>(rest <em>L</em>)</tt>. |
| </ul> |
| </ul> |
|
|
| <p>Tracing the execution of <tt>list-member</tt>, we get the following: |
| <pre> |
| USER(70): (list-member 'a '(perhaps today is a good day to die)) |
| 0: (LIST-MEMBER A (PERHAPS TODAY IS A GOOD DAY TO DIE)) |
| 1: (LIST-MEMBER A (TODAY IS A GOOD DAY TO DIE)) |
| 2: (LIST-MEMBER A (IS A GOOD DAY TO DIE)) |
| 3: (LIST-MEMBER A (A GOOD DAY TO DIE)) |
| 3: returned T |
| 2: returned T |
| 1: returned T |
| 0: returned T |
| T |
| </pre> |
|
|
| <p>In the implementation of <tt>list-member</tt>, |
| the function call <tt>(eq <em>x</em> <em>y</em>)</tt> tests if two symbols |
| are the same. In fact, the semantics of this test determines what |
| we mean by a member: |
| <pre> |
| USER(71): (list-member '(a b) '((a a) (a b) (a c))) |
| 0: (LIST-MEMBER (A B) ((A A) (A B) (A C))) |
| 1: (LIST-MEMBER (A B) ((A B) (A C))) |
| 2: (LIST-MEMBER (A B) ((A C))) |
| 3: (LIST-MEMBER (A B) NIL) |
| 3: returned NIL |
| 2: returned NIL |
| 1: returned NIL |
| 0: returned NIL |
| NIL |
| </pre> |
| In the example above, we would have expected a result of <tt>t</tt>. |
| However, since <tt>'(a b)</tt> does not <tt>eq</tt> another copy |
| of <tt>'(a b)</tt> (they are not the same symbol), |
| <tt>list-member</tt> returns <tt>nil</tt>. |
| If we want to account for list equivalence, we could have used |
| the LISP built-in function <tt>equal</tt> instead of <tt>eq</tt>. |
| Common LISP defines the following set of predicates for testing |
| equality: |
| <p><table border=1> |
| <tr> |
| <td><tt>(= <em>x</em> <em>y</em>)</tt></td> |
| <td>True if <em>x</em> and <em>y</em> evaluate to the same number.</td> |
| </tr> |
| <tr> |
| <td><tt>(eq <em>x</em> <em>y</em>)</tt></td> |
| <td>True if <em>x</em> and <em>y</em> evaluate to the same symbol.</td> |
| </tr> |
| <tr> |
| <td><tt>(eql <em>x</em> <em>y</em>)</tt></td> |
| <td>True if <em>x</em> and <em>y</em> are either <tt>=</tt> or <tt>eq</tt>.</td> |
| </tr> |
| <tr> |
| <td><tt>(equal <em>x</em> <em>y</em>)</tt></td> |
| <td>True if <em>x</em> and <em>y</em> are <tt>eql</tt> or if |
| they evaluate to the same list.</td> |
| </tr> |
| <tr> |
| <td><tt>(equalp <em>x</em> <em>y</em>)</tt></td> |
| <td>To be discussed in Tutorial 4.</td> |
| </tr> |
| </table> |
|
|
| <p><hr><b>Exercise</b>: What would be the behavior of <tt>list-member</tt> |
| if we replace <tt>eq</tt> by <tt>=</tt>? By <tt>eql</tt>? By <tt>equal</tt>? |
| <hr> |
|
|
| <h2>Example: <tt>append</tt></h2> |
|
|
| <p>LISP defines a function <tt>append</tt> that appends one list by |
| another: |
| <pre> |
| USER(72): (append '(a b c) '(c d e)) |
| (A B C C D E) |
| </pre> |
| We implement a recursive version of <tt>append</tt>. |
| Suppose we are given two lists <em>L1</em> and <em>L2</em>. |
| <em>L1</em> is either <tt>nil</tt> or constructed by <tt>cons</tt>. |
| <ul> |
| <li><em>Case 1</em>: <em>L1</em> is <tt>nil</tt>.<br> |
| Appending <em>L2</em> to |
| <em>L1</em> simply results in <em>L2</em>. |
| <li><em>Case 2</em>: <em>L1</em> |
| is composed of two parts: <tt>(first <em>L1</em>)</tt> and |
| <tt>(rest <em>L1</em>)</tt>. If we know the result of appending <em>L2</em> |
| to <tt>(rest <em>L1</em>)</tt>, then we can take this result, insert |
| <tt>(first <em>L1</em>)</tt> to the front, and we then have |
| the list we want. |
| </ul> |
| <p>Formally, we define the following function: |
| <pre> |
| (defun list-append (L1 L2) |
| "Append L1 by L2." |
| (if (null L1) |
| L2 |
| (cons (first L1) (list-append (rest L1) L2)))) |
| </pre> |
| An execution trace is the following: |
| <pre> |
| USER(73): (list-append '(a b c) '(c d e)) |
| 0: (LIST-APPEND (A B C) (C D E)) |
| 1: (LIST-APPEND (B C) (C D E)) |
| 2: (LIST-APPEND (C) (C D E)) |
| 3: (LIST-APPEND NIL (C D E)) |
| 3: returned (C D E) |
| 2: returned (C C D E) |
| 1: returned (B C C D E) |
| 0: returned (A B C C D E) |
| (A B C C D E) |
| </pre> |
|
|
| <p><hr><b>Exercise</b>: LISP defines a function <tt>(butlast <em>L</em>)</tt> |
| that returns a list containing the same elements in <em>L</em> except |
| for the last one. Implement your own version of <tt>butlast</tt> |
| using linear recursion. You may assume that <tt>(butlast nil)</tt> |
| returns <tt>nil</tt>. |
| <hr> |
|
|
| <h2>Using Lists as Sets</h2> |
|
|
| Formally, lists are ordered sequences. They differ with sets |
| in two ways: |
| <ol> |
| <li>Sets are unordered, but lists are. <tt>(a b c)</tt> |
| and <tt>(c b a)</tt> are two different lists. |
| <li> |
| An element either belong to a set or it does not. There is |
| no notion of multiple occurrences. |
| Yet, a list may contain multiple occurrences of the same element. |
| <tt>(a b b c)</tt> and <tt>(a b c)</tt> are two different lists. |
| </ol> |
| However, one may use lists to approximate sets, although the |
| performance of such implementation is not the greatest. |
|
|
| <p>We have already seen how we can use the built-in function <tt>member</tt> |
| to test set membership. LISP also defines functions like |
| <tt>(intersection <em>L1</em> <em>L2</em>)</tt>, |
| <tt>(union <em>L1</em> <em>L2</em>)</tt> |
| and <tt>(difference <em>L1</em> <em>L2</em>)</tt> for |
| boolean operations on sets. In fact, these functions |
| are not difficult to implement. Consider the following |
| implementation of set intersection: |
| <pre> |
| (defun list-intersection (L1 L2) |
| "Return a list containing elements belonging to both L1 and L2." |
| (cond |
| ((null L1) nil) |
| ((member (first L1) L2) |
| (cons (first L1) (list-intersection (rest L1) L2))) |
| (t (list-intersection (rest L1) L2)))) |
| </pre> |
| The correctness of the implementation is easy to see. |
| <em>L1</em> is either an empty set (<tt>nil</tt>) or it is not: |
| <ul> |
| <li><em>Case 1</em>: <em>L1</em> is an empty set.<br> |
| Then its interection with <em>L2</em> |
| is obviously empty. |
| <li><em>Case 2</em>: <em>L1</em> is not empty.<br> |
| <em>L1</em> has both a <tt>first</tt> |
| component and a <tt>rest</tt> component. There are two cases: |
| either <tt>(first <em>L1</em>)</tt> is a member of <em>L2</em> or it is not. |
| <ul> |
| <li><em>Case 2.1</em>: <tt>(first <em>L1</em>)</tt> is a member |
| of <em>L2</em>.<br> |
| <tt>(first <em>L1</em>)</tt> belongs to both <em>L1</em> |
| and <em>L2</em>, and thus belong to their intersection. Therefore, |
| the intersection of <em>L1</em> and <em>L2</em> is simply |
| <tt>(first <em>L1</em>)</tt> plus the intersection of <tt>(rest <em>L1</em>)</tt> and |
| <em>L2</em>. |
| <li><em>Case 2.2</em>: <tt>(first <em>L1</em>)</tt> is not a member |
| of <em>L2</em>.<br> |
| Since <tt>(first <em>L1</em>)</tt> |
| does not belong to <em>L2</em>, it does not belong to the intersection |
| of <em>L1</em> and <em>L2</em>. As a result, the intersection of |
| <em>L1</em> and <em>L2</em> is exactly the intersection of |
| <tt>(rest <em>L1</em>)</tt> and <em>L2</em>. |
| </ul> |
| </ul> |
| <p>A trace of executing the function is given below: |
| <pre> |
| USER(80): (trace list-intersection) |
| (LIST-INTERSECTION) |
| USER(81): (list-intersection '(1 3 5 7) '(1 2 3 4)) |
| 0: (LIST-INTERSECTION (1 3 5 7) (1 2 3 4)) |
| 1: (LIST-INTERSECTION (3 5 7) (1 2 3 4)) |
| 2: (LIST-INTERSECTION (5 7) (1 2 3 4)) |
| 3: (LIST-INTERSECTION (7) (1 2 3 4)) |
| 4: (LIST-INTERSECTION NIL (1 2 3 4)) |
| 4: returned NIL |
| 3: returned NIL |
| 2: returned NIL |
| 1: returned (3) |
| 0: returned (1 3) |
| (1 3) |
| </pre> |
| <p><hr><b>Exercise</b>: Give a linearly recursive implementation of |
| <tt>union</tt> and <tt>difference</tt>. |
| <hr> |
| </body> |
| </html> |
|
|