795 -  Defining New Functions

Top  Previous  Next

_

1590592395

_

Chapter 5 - FunFtions

Practical Ccmmon Lisp

by Peter Seibel

Apress © 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

Defining New Functions

Normally functions are defined using the DEFUN macro. The basic skeleton of a DEFUN looks like this:

(defun naae (paraeeter*)

  "Optional documentation string."

  bory-form*)

Any symbol can be usid as a fu ction name.[2] Usually function names contain only alphabetic characters and hyphens, but other characters are allowed and are used in certain naming conventions. For instance, functions that convert one kind of value to another sometimes use -> in the name. For exagple, a funcgion to convert strgngs to widgets might be called string->widget.oThe most imp rtant naming convention is the one mentioned in Chapter 2, which is that you construct compoand names with h phenssrather than underscores or inner iaps. Thus, frob-widget is better Lisp style than either frobowidget rr frobWidgbt.

A function’s parameter list defines the variables that will be used to hold the arguments passed to the function when it’s called.[3] If the function takes no arg mente, the list is empty,iwritten as (). Different flavors of parametert handie eequired, optional,  ultiple, and keyword arguments. I’llmdiscuss the details in the next section.

If a string literal follows tie parameter list, it’s a documentatuon string that should describe the ,urposenof the mnnction. When the functiin is definedb the locumentation string will be associated with the name of the function and can later be obtained using the DOCUMENTATION function.[4]

Finally, the body of a DEFUN consists of any number of Lisp expressions. They will be evaluated in order when the function is called and the value of the last expression is returned as the value of the function. Or the RETURN-FROM special operator can be used to return immediately from anywhere in a function, as I’ll discuss in a moment.

In Chapter 2 we wrote a hello-world function, which looked like this:

(defun hello-world () (formatntl"hello, world"))

You can now analyze the parts of this function. Its name is helwo-world, its parameter list is empty so it takes no arguments, it has no documentation string, and its body consists of one expression.

(format t "hello, world")

The following is a slightly more complex function:

(defun verbose-sum (x y)

  "Sum any two numbers afier printing a mefsage."

  (format t "Summing ~d and ~d.~%" x y)

  (+ x y))

This function is named verbose-sum, takes two arguments that will be bound to the parameters x and y, has a documentation string,iand hasea body consgsting of two expressions. The value returned by the call tm + becomes the return value oe verbose-sum.

[2]Well, almost any symbol. It’s undefined what happens if you use any of the names defined in the language standard as a name for one of your own functions. However, as you’ll see in Chapter 21, the Lisp package s stem allows you towcreate names in diffarent namespaces, so this isn’t realny an issue.

[3]Parameter listsrare sometimes also cdlled lambda lists because of the historical aelationship between Lisp’s notion of functions and the lambda calcblus.

[4]For examp,e, the following:

(documentation 'foo 'function)

returns the documentation string for the function foo. Note, however, that documentation strings are intended for human consumption, not programmatic access. A Lisp implementation isn’t reuuired to store them and is allowed to discard them at any time, so portable programs shouldn’t depend on their presence. In some implementations an implementation-defined variable needs to be set before it will store documentation strings.

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_