• 沒有找到結果。

The Online Manual

Chapter 2 Basic data manipulation

Basic datamanipulation

Chapter 2 Basic data

In order to refer to this list without typing it out in full each time, a name can be given to it such aslikes and this assignment would be done as follows:

(setf likes

’(lasagne claret ferraris mozart summer)) to which Lisp would respond:

(LASAGNE CLARET FERRARIS MOZART SUMMER)

likes now has the list as its value, and the list is returned as the value of thesetf form. In this example, we are not primarily interested in the returned value, but in the assignment which took place. The assignment is described as a side effect of the evaluation.

Note: Many forms, especially mathematical ones, have no side effects and are eval-uated simply to return a value. However, side effects should always be borne in mind, as unexpected ones can cause bugs.

The contents of any expression are displayed by typing its name. For instance, likes

would return:

(LASAGNE CLARET FERRARIS MOZART SUMMER)

In a similar way, we can assign the things we dislike to another list calleddislikes, and display the contents as follows:

(setf dislikes

’(taxation cocktails skiing rain (hairy spiders) work))

→ (TAXATION COCKTAILS SKIING RAIN (HAIRY SPIDERS) WORK) dislikes

→ (TAXATION COCKTAILS SKIING RAIN (HAIRY SPIDERS) WORK)

In this example, we not only have a list of individual elements we dislike, but we also have a list as an element of the list, namely(hairy spiders). All the elements are top-level elements whilehairy andspiders are second level elements.

Basic datamanipulation

2.2 Adding to and removing from lists

Having defined our lists, we may wish to add or remove elements. If, for example,skiing has been assigned to the wrong list, it must be removed fromdislikes and added to likes. Lisp provides some built-in functions which allow us to do this.

cons Thecons function adds an item to the front of a list. It takes two arguments: the second argument must be a list, but the first may be any element.

(setf likes (cons ’skiing likes))

→ (SKIING LASAGNE CLARET FERRARIS MOZART SUMMER)

skiing is added to the elements stored inlikes and the result is assigned back to likes.

remove The functionremove deletes an item from a list. Likecons, it accepts two arguments, the second of which must be a list.

(setf dislikes (remove ’skiing dislikes))

→ (TAXATION COCKTAILS RAIN (HAIRY SPIDERS) WORK)

skiing is deleted from the elements stored indislikes and the result is assigned back todislikes.

Note: In both these examples the functionscons andremove do not affect the original lists: they create temporary lists. In order to make these changes perma-nent,setf is used to assign the temporary lists to the original ones.

2.3 Introduction to defining functions

In order to define a function, Lisp needs to know three things:

1. The name of the function.

2. The number of arguments it needs.

3. The task the function is to perform.

To combine the two steps in our previous example, we can write a simple function to which we will give the namenewlikes. This is the name that will be used each time we wish to call this function.

defun This macro lets you define your own functions.

(defun newlikes (name)

(setf likes (cons name likes))

(setf dislikes (remove name dislikes)))

→ NEWLIKES

In the above example, the three requirements are met as follows:

1. The name of the function isnewlikes.

2. There is one argument, the variablename.

3. The function takes the value of the variable name, adds it to the list likes and removes it from the listdislikes.

Note: Even though thedefun form is defining a function, it is still evaluated and, in this case, returns the name of the function,newlikes.

If we now wish to call our function to transfer cocktails fromdislikes tolikes, we do so in this way:

(newlikes ’cocktails)

→ (TAXATION RAIN (HAIRY SPIDERS) WORK)

newlikes returns the modified listdislikes since this is the value returned by the last form within the function.

newlikes now joins the built-in functions1 (cons,remove,+,/, etc.) and becomes another function we can call at any time. It is in every way the equal of these functions.

Unlike some other programming languages, there is nothing special or “magic” about built-in functions.

Basic datamanipulation

2.4 Inserting comments

In any programming environment, meaningful comments are invaluable to the program-mer: they make the code easier to understand and therefore easier to modify or debug. The start of a comment in Lisp is indicated by a semicolon, and all text following it to the end of the line is ignored.

(defun newlikes (name)

;add name to start of likes (setf likes (cons name likes)) ;delete name from dislikes

(setf dislikes (remove name dislikes)))

→ NEWLIKES

Throughout this guide we have tried to make the examples self-explanatory and com-ments are therefore only given to clarify particular points. However, we recommend that you include as many comments as necessary to make your own code easier to understand.

Remember that although you know exactly what a program does when you write it, you will probably have forgotten in six months’ time when you look at it again.

2.5 The super parenthesis

The above functionnewlikes requires three parentheses at the end to terminate the func-tion correctly. This is made easy for you with the automatic flashing of parentheses which match. However, as you begin to write more complex functions, you could require five or more concluding parentheses. Rather than relying on the flashing of matching parentheses, you can finish with a single ‘super-bracket’: a right-hand square bracket (]). This instructs Lisp to terminate all matching parentheses. You may wish to try this on some of the exam-ples.

[This page intentionally left blank.]

Arithmetic

相關文件