Ml rcquircs that its fint argument c\ałuate to a symbol. In many cascs. the fint argument ts simply a quotcd symbol. Rccause (his is donc so often. LISP prewides aa ahcraative form. setq. (hat docs not c\aluatc its firsi argument. Instead. sotq nrąuires that the fint argument bc a symbol. For oumplc. ihc fol Iow mg forma arc cquivałcnt:
>(sot*xO)
0
> (setq x 0)
0
Ahhough (his u'C of Mt mak es it possiblc to erratę LISP objeets (hat arc not pure functioos in the mathemaitcal sense. the ability to bind a valuc to a variable in the glofcoS cm ironment is a useful featurc. Many programming tasks arc most nuturally implementcd ustng this ability to definc objeets whose statc persists across funciion calls. The clasac examplc of this is the -“sccd" in a random number generator, cach cali to the funciion changcs and savrs the 'iluc of the sced. Similarły. it would be natural for a databasc program (such as was described in Scction 15.1.3) to storę the databnsc by binding it to a \ariable in the gkobal cm ironment.
So te. we ha\e scen two ways of giving a value to a symbol: csplicitly. by a&sign-ment using Mt or Mtq. or implicitly. w hen a funciion cali binds the calling pa rum et cn to the fornal paratneters in the dcńnition. In the examplc> scen so far. all \ariabłcs in a func-tion body werc citfcer bound ot firt. A bound \anablc is one that appears as a fornal parameter in the definition of the funciion. wliilc a frcc variablc is one that appears in the body of the function but is not a formal parameter. Whcn a funciion is callcd. any binding* that a bound vanabł c may have in the ęlobal cm ironment arc saved and the \araMc ii rcfaound to the calling parameter. Aftcr the function has completcd cxccu(ion. the ongiiul bindings arc restored. Thus. setting the xaluc of a bound variablc inside a function body has no effect on the global bindings of that variablc. as scen in the LISP interaciion:
; ineremont bound variablnx : return its va!ue
X)
floo
> (M*q y 1)
1
>(fooy)
2
: noto that valuo of y is unchanged
>y 1
In the examplc that began this section. X was bound in the function f. whcrcas inc frcc in that function. As we detnomtrated in the examp!e. frcc variables in a function definition arc the primary sourcc of sidc effeets in funclions.
An intcrcstśng alternator to Mt and Mtq is the generalized assigiunent function. setf. Insi cod of assigjung a valuc to a symbol. Mtf cvaluatcs its first argument to obwini mcmory location and places the valuc of the second argument in that location. Whta btnćmg a valuc to a symbol. Mtf bełtnet liltc setq:
> (Mtt) X O)
O
> (Mtf X O)
o
Howcver. bccausc we may cali setf wiih any form thx corrcspood* to a unuaory location. il allows a morc generał seraantics. For exampłe. if w make the fint argument to setf a cali to the car function. setf will rcplacc the fint demem of that Ba. If the fint argument to setf is a cali to the cdr function. setf will rcplacc the taił of that Ba. For curnpk:
> (setf x ’(a b c)) (abc)
>x
(abc)
> (setf (car x) 1)
1
>x
(ibe)
> (setf (cdr x) '(2 3)) (2 3)
>x
(123)
:xls bound to efist ; the va!ue of x is a 6st : the car of x corrasponds to a locatton in mamory : notę that setf changed the vafcie o# the car of x
: notę that x now has a new tai
We may cali setf wirh most LISP forms chat corrcspood to a memory locadoo: these nelude symbols and functions such as car. cdr. and nth. Thus, setf allows the program designer great ftexibility in creating. monipulating. and cvcn repłacmg componcnts of USP data structurcs.
let is another uscful function for cxplicitly controlling the bindmg of \anables. lat allows tbeereation of local variablcs. As an cxampk of the usc of let considcr a function to com-puie the roots of a quadratic cquation. The function quad-roots will take as argumeots the ifaree parameters a. b. and c of the equation ax2*bx*c«0and return a list of the two roots of the cquation. These roots will be calculated from the formula
-b± Jb2-4oc • ^ 2a
> (quad-roots 12 1) (-1.0-1.0)
> (quad-roots 16 8)
CNAPTEftIS/AMłNTROOUCTIONTOUSP 699