LISP incłudes loo many funclions lo lisi in iliis chaptcn for a morę dclailcd discussioo. consult a spccult/cd LISP text orthc manuał for your pariicular implcmcnlation.
In LISP. we prcęnun by dctining ncw ftmctions. conslrucling progmms IVom iłu already rich rcpcnoire of btiilt-in funclions. Thcsc ncw funclions arc defincd using dofun. wtiich is short for definc function. Oncc a funclion is defincd i i may be used in ilie same lasliion as funclions ibai arc buill inio thc language.
Supposc. for cxamp!c. ihc uscr would lifce 10 dcfinc a funclion callcd square (hal takcs a single argument and retums ihc squarc of thal argument. square may be creaicd by łu%ing LISP evaluatc thc following cxprc$$ion:
(defun square (x)
The Ant argument to defun is thc name of the funclion bcing defincd: thc sccond is a lisi of ihc fornal parameters for ihai funclion. which musi all be symbolic aloms; Ihc rcmaining argumenls tire zero or moce $*cxprcssions. which constituic thc body of tlić ncw funclion. thc LISP codę ihai actually defines its bchavior. Unlikc most LISP funclions, defun docs not evaluatc its argumenls; instead. it uses them as spccifications to crcaic a ncw function. As with all LISP functions, hoivcvcr, defun retums a vnluc, although thc yalue retumed is simply thc name of thc ncw funclion.
The important rcsull of cvaluaiing a defun is thc sidc cflcct of ercating a ncw funclion and adding it to thc LISP cnvironmcnt. In thc abovc cxamplc, sejuare is defined as a funclion thal takcs one argument and retums thc rcsull of multiplying tltat argument by itsclf. Once a funclion is defincd, it musi be callcd with Ihc same number of argumenls, or "actual parameters," as thcrc arc forma! parameters spccificd in thc defun. W hen a function is callcd the actual parameters arc bound to thc formal parameters. The body of thc function is ihen evaluatcd with thcsc bindings. For cxamplc. thc cali (square 5) causes 5 to be bound to thc formal parameter x in thc body of thc definition. Whcn thc body (* X x) is c\alu3ied LISP first cvaluato the argumenls to thc function. Bccausc x is bound lo 5 by tbe cali. this leads to the evaJuation of (* 5 5).
Morc conciscty, thc syntax of a defun cxprcssion is:
(defun <funcfión name> (<formal parameters^) <function body>)
In this definition, dcscriptions of thc clcmcnts of a form arc cncloscd in angle brackets < >, We usc this nolaiional comention throughout this tcxt to dcfinc LISP forms. Notc chat thc formal parameters in a defun arc cncloscd in a list.
A ncwly defincd function may be used just like any built-in function. Suppdsc, fol cxaraplc. ihai we need a function lo computc ihc lengih of the hypołenuse of a right trianglc ghen thc lcngths of thc other two sides. This function may bc defined according to thc Pythagorcan theorem, using thc prcviously defined square function along willi the built-in funclion sqrt We haye added a number of commcnts to this sample codę. USP supports “end oflinc commcnts": it ignores all text from thc first to thc end of thc same
lino.
6
PART VI / LAN0UA6E8 FOR Al PROBLEM SOLVING
(defun hypotonuse <x y)
: th« langui of tha hypoteout«
- «qu«re root of tho wm of : the squares of the ottrór sides-
(s<|rt (♦ (squaro x>
(square y))))
This cxamplc is typical in that most LISP programs arc built up of rda«ivciy smali funclions. cach performing n single well-defincd tosk. Oncc defined, thcsc functioos arc used to implcmcm highcr>lcvel functiona umil thc desired ~cop~1eveI~ bchavior ha» bocn defined.
LISP branehing is ais© bascd on function cvaIuation: control function* perform icua and. depending on thc rcsults. sclcct*vely cvaluatc allcrna«ivc form*. Considcr. for cumplc. thc following definition of the nbsolutc v»luc function (notę that LISP has a built-in function. abs. that compulcs absolut e value):
(defun ebsolute-value (x)
(oond ((< x O) (- x}> ; If x Is less (han O. return -x
((>• x O) x)» : othorwlse. return xunchansed
This example uses thc function. cond, to implcmcnt a conditional branch. cond takcs as jnumcnis a number of c'on<iition—<icti<m pairsz
(cond (< conditłonl > < action 1 >)
(< cond itr on 2 > < actłon2 >)
(< conditionn > < aclionn >))
Conditions and actions may be arbitrary s-cxpressions. and cech pair is cncloscd in parentheses. Likc defun. cond does not cvaluatc all of its argumenls. Instead. it cvaluatcs Ihc conditions in order until one of ihcm return* e non-nll >aluc. When this oocur*. it oaluatcs the associatcd action and retums this rcsull as the value of thc cond cxprcssioa. Sonc of thc other actions and nonę of the subscqucnt condition* arc cvaluatcd. If all of thc conditions cvaluatc to nil. cond retums nil.
An altcmative definition of absolute-value is:
(defun absolute-valuo (x)
(cond ((<xO) (- x)) ; If x is lese than O. return -x
(I x)» : othorwlse. return x unchtngtd
This vcrsion notes that the sccond condition. (>= x O), is ahsays truć if thc first i* false. The T atom in the finał condition of the cond statement is a LISP atom that roughly oorrcsponds to “truć.** By comention. t always cvaluates to itself: this caatscs thc last action to bc cvalua«cd if all prcceding conditions return niL This constnsct i* estreme^y wefnl. as it prewides a way of giving a cond statement a default action that is oaluatod if and only ifall prcceding conditions tfail.
687
CHAPTER 15/ANIHTROOUCTIONTOUSP