27 1024x768

27 1024x768



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:

>    (defun foo (x)

; ineremont bound variablnx : return its va!ue


(sotq x (♦ x 1))

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.

15.1.9 Dcfining Local Variables Using let

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

hreamplc:

>    (quad-roots 12 1) (-1.0-1.0)

>    (quad-roots 16 8)

(•2.0 -4.0)

CNAPTEftIS/AMłNTROOUCTIONTOUSP    699


Wyszukiwarka

Podobne podstrony:
Tho knifo was quite probably not all that long. maybo no moie than 6 -9". sińce In each case th
image 19 Slider Category Slider Select post category, that contains posts you wish to show in the ho
Obraz3 (27) I I l Ut Ml II II 11 i Hit (ii "H
Obraz3 (27) I I l Ut Ml II II 11 i Hit (ii "H
12 ii*3* Sł»*- Ł Dziennik Polski i Dziennik Żołnierza 27.VII.Ml. SOW. Brytania nie imienia
Verifying the MD5 Checksum After you have downloaded a MySQL package, you should make surę that its
CCF20111211013 (2) OPULARNA 2010 NR I (27) IAPORT:    Mł.ODZI I MEDIA 17 O
ProgramProdukcjiSTAR07 ■ &**>?*# *322* that time madę it possible to overhaul thoroughly
Watashitachi no Shiawasena Jikan?58 ITS TRUg THAT I PONT UKg THg agRoy... TO L£AVE Mg ALO
oziaragis ♦ (C.ĆOftnjht 2003 Su* farmer 4 Ul Kelly Priek • $1itd Kc«p*du Cerdt T*t *27 21 Ml 1988
265 Kalumbardu at its first foundation. Sułtan Muhammad Bello in his Infaą al-Maisur records that th
31497 Watashitachi no Shiawasena Jikan?58 ITS TRUg THAT I PONT UKg THg agRoy... TO L£AVE
ZESPÓŁ Ml 1.27.00 - BAGAŻNIKT 18 Kc liga znika 1U 11 12 Ml 1.27.03 Ml 1.27.05 Ml 1.27.01
53211 Obraz3 (27) I I l Ut Ml II II 11 i Hit (ii "H
censorship, (27) 150 Blackout clamored that his “past is a secret” and that “a woman is the cause
31497 Watashitachi no Shiawasena Jikan?58 ITS TRUg THAT I PONT UKg THg agRoy... TO L£AVE

więcej podobnych podstron