• Aucun résultat trouvé

Overview of Functions

Dans le document 2A Language (Page 48-51)

Overview of Symbolics Common Lisp

2. Functions, Predicates, Special Forms an~ Macros

2.1 Overview of Functions

Functions are the basic building blocks of Lisp programs. A function is a Lisp object that, when applied to arguments, performs some action and returns a value.

You can manipulate functions in the same ways you manipulate other Lisp objects;

you can pass them as arguments, return them as values, and make other Lisp objects refer to them.

There are four kinds of functions, classified by how they work:

• Interpreted functions, which are defined with defun, represented as list structures, and interpreted by the Lisp evaluator.

• Compiled functions, which are defined by compiling forms from a file or an editor buffer or by loading a binary file, are represented by a special Lisp data type, and are executed directly by the machine.

• Various types of Lisp objects that can be applied to arguments, but when applied, call another function and apply it instead. These include symbols, dynamic and lexical closures, and instances.

• Various types of Lisp objects that, when used as functions, do something special related to the specific data type. These include arrays and stack groups.

Lisp has several functions known as function-defining special forms, which are used in programs to define functions. For example, defun is a common

function-defining special form. Function-function-defining special forms typically take as arguments a function spec (see below) and a description of the function to be made.

Function-defining special forms include defun, defsubst, macro, defselect, deff, and def.

A general programming-style rule of thumb: Anything that is used at top level (not inside a function) and starts with def should be a function-defining special form so that the editor can find it in your source file and show it to you whenever you ask for a deimition.

For more information on function-defming special forms: See the section

"Function-Defming Special Forms", page 258.

The name of a function is usually a symbol, but does not have to be a symbol. A function can be represented by a function spec, which serves to name a function

Symbolics Common Lisp: Language Concepts August 1986

and specifies a place to find and remember a function. Spec is short for specification.

Function specs are not functions. You cannot apply a function spec to arguments.

You use function specs when you want to do something to the function, such as define it, look at its definition, or compile it. Both function specs and functions can be defined. To deime a function spec means to make that function spec remember a given function - a task accomplished by the fdefine function. To define a function means to create a new function and deime a given function spec as that new function - a task accomplished by the defun special-form. Several other special forms, such as defmethod and defselect, also define functions.

A function spec's definition generally consists of a basic definition surrounded by encapsulations. The basic deimition is what defun creates. See the section "How Programs Manipulate Definitions", page 261. The encapsulation is composed of function-altering functions, such as trace and advise. See the section

"Encapsulations", page 263. When the function is called, the function's definition plus the alterations are executed.

For more information on function specs: See the section "Function Specs", page 251.

There are several operations a user would typically want to perform on functions.

These operations are:

• Print out the definition of the function spec with indentation. (This works only with interpreted functions.)

• Find out about a function by looking at its documentation and its arguments.

• Look at the function's debugging information.

• Trace the calling history and customize the definition of a function while debugging.

• Examine the compiled code, if the function is compiled.

For more information on these operations: See the section "Operations the User Can Perform on Functions", page 255.

A Lisp definition is a Lisp expression that appears in a source program file and has a name to which a user can refer. Two definitions with the same name and different types can exist simultaneously, but two definitions with the same name and the same type redefine each other when evaluated. There are four basic types of definitions:

• functions

August 1986 Overview of Symbolics Common Lisp

• variables

• flavors

• structures

Many types of Lisp special forms, such as defun and defvar, can define these four types of definitions. For more information about definitions: See the section

"How Programs Manipulate Dermitions", page 261.

A Lisp declaration is an optional Lisp expression that provides the Lisp system with information about your program, for example, documentation. Many Lisp forms, such as defun, have declarative aspects. See the section "Declarations", page 260.

A dynamic closure is a Lisp functional object for implementing certain advanced access and control structures. Closures give you more explicit control over the environment, by allowing you to save the environment created by the entering of a dynamic contour, and then use that environment elsewhere, even after the contour has been exited. There are several functions that manipulate dynamic closures, for example, closure.

For more information on dynamic closures: See the section "Dynamic Closures", page 265.

2.2 Overview of Predicates

A predicate is a function that tests for some condition involving its arguments and returns some non-nil value if the condition is true, or the symbol nil if it is not true. Many predicates return the symbol t, instead of another non-nil value, if the condition is true.

Predicate names usually end in the letter "p". The way the "p" is added to the end of the predicate depends on whether or not there is an existing hyphen in the name. For example, the predicate that tests for integers is integerp, while the predicate that checks for compiled functions is compiled·function-p.

Predicates fall into several logical categories. These include: type-checking

predicates, which test an object for membership in a particular data type such as numbers, arrays, and so on; property-checking predicates, which determine whether an object has certain properties (such as whether a number is odd or even);

comparison predicates, which compare objects of the same type; and a few others.

For a complete list of predicates: See the section "Predicates", page 271. A full description of each predicate is available in the dictionary of Lisp functions.

Symbolics Common Lisp: Language Concepts August 1986

Dans le document 2A Language (Page 48-51)