22 1024x768

22 1024x768



Although anv pluiibi* .-o.xprcss.ons may bc uscd as thc conditions of a cond. Bili thc sc arc a puticular kind of LISP funciion calkd a prędka w. A pwdicatc u simply a funciion ilu: rciums a valuc of crther tnie or fabe depending on whether or nor iis argument* possess somc pfopcrty. The most olnious e\amples of prcdicatcs aro thc relational operator* typkally uscd in arithmetie such as and > Herc arc somc c\ampleś of arithmetie prcdicatcs in LISP:

>(•9(445)) |

I

>(>■174) t

>    (< 8 (♦ 4 2)) nH

; oddp testt whether its argument is odd or not ; minusp tosts whether its argument is less than : numberp tosts whether its argument is numeric

; zerop is truo if its argument is 0. nil otherwise ; plusp is truo if its argument is strictly groater than 0


>(oddp3)

I

>    (rmnusp 6)

ńń

>    (numberp 17)

(

>    (numberp nil) nil

>    (zerop 0) t

>    (plusp 10)

I

>    (plusp -2)

M

Notę that thc prcdicatcs in thc above caamples do not return “tnie" or “fabe" but ratber t or nil. LISP is defined so that a prcdicate may return nil to indicate “fabe" and anything other than nil (not occessariły t) to indicate “tnie." An example ofa funciion dat uses this fcaturc i> tbe member prcdicate. member takes iwo argument*, thc secood of which must be a list. I f thc fint argument is a member of thc sccond. member rciums tbe suflix of thc secood argument, which contains thc first argument ns its itiifinl element; if il is not. member rciums nil. For examplc:

>    (member 3'(1234 5))

(34 6)

One rationalc for this convcntion is that it allows a prcdicate to return a vnluc that. in the ••truć" casc. may bc of usc in further processing. !t also allows any LISP funciion to be uscd as a condition in a cond form.

As an allcmatnc to cond. the if form takes thrcc argument.. The first is a tot If cvaluaics the test: if it rciums n non-nil saluc. thc if form csaluatcs its sccond atgument and rciums thc rcsull, otherwise it retums the result of cvaluating thc third argument Is cuscs involving a l\vo-vvay branch. tbe if construct generałły provides clcancr. morę rcad-ablc codo than cond. For cxamplc. absolule-va!ue could bc dcftncd using thc if form:

(8


PART VI / LANGUAGES FOR Al PROBLEM SOLVMG

(dafun absolute-value (x)

Of (< x O) (- x) x)>

In addition to if and cond. LISP ofTcrs a «idc KkctioD of ahcmativc contro! eon-Lttucls. including iicrativc constructs such it do and whilo loops. Ahhough tbcsc fune-Itions prwidc LISP programmers wilh a wiele range of contro! structurc* that fu almoit any Lmuiion and programming style, we will not discuss them in this scction; thc rcader is lefcncd to a morc spcciali/cd LISP tcxt for this information.

One of the mon: intcrcuting program control tcchniquc$ in LISP imohes the mc of mc logical connectives and. or. and not. not takes one argument and rclurns t if its argument is nil and nil otherwise. Both and and or may lakę any number of argument* and Muvc as you would cxpcct from the definitiom of thc corrcsponding logical operator*. It L anportant to noto. howevcr. that and and or arc based on conditional cvaluation.

In cvalunting an and form. LISP cvaluatcs its argument* in left-io-righl order, stop-—*ping when any one of thc arguments cvaluatcs to nil or the las! argument has bccn cvalu-Mt1 Upon completion. thc and form rciums thc valuc of thc last argument esaluated. It toefoK retums non-nil only if all its arguments return non-nil. Similarły. thc or form oahotes its arguments only until a non-nil \alue is cncountcrcd. rrtuming this vahae as muh. Both functions may lcavc somc of their arguments uncvaluated, as may bc scen by *c betmior of thc prlnt statemcnis in thc following cxamplc. In addition to printing its agumenL in somc LISP cnvironments print refurw a *aluc ©f nil on compłction.

>    (and (oddp 2) (print "second statemont was evak«als<r)) nil

>    (and (oddp 3) (prlnt "second statemont was evahjałod")) second statemont was evoluated

>    (or (oddp 3) (print "second statemont was evaluatod"))

t

>    (or (oddp 2) (prlnt "second statemont was evaluated~)) second statemont was evaluated

(oddp 2) c\ aluates to nil in the first cxprcssaon*. tbe and simpfy return* nil w «th-m oalualing thc print form. In thc secood cxprcssioe. ho»o«r, (oddp 3) oahutes to t and thc and form then cvaluatcs thc prlnt A similar anaiysis may bc applied to thc or compics. It is important to be aware of this bchavior. particularly if mmc of the argu-neats arc forms whose cvaluations have sidc cfTects. such as thc print fanetkm. The coo* ditional cvaluation of logical conncctivcs makes tlicm uscful in controlling tbe fiow of eiecutKW of LISP program*. For esan^le, an or form may be uscd lo try altcmatńc sołu-uoos to a problem. cvaluating them in order until ooc of them return* a aoo-nfl tesufc

15.1.5 Functions, Lists, and Symbolic Computing

Although thc prcccding sections introduced LISP synux and dcmonstiated a lew uscful USP functions. they did so in thc contcxl of siinplc arithmetie eotmptes, The rrul power •fUSP is in symbolic computing and is based on thc uw of Hsts to coastruct arbitranh

689


CMAPTER15 / AM WTROOUCTtON TO LtSP


Wyszukiwarka

Podobne podstrony:
22 (936) Three simple rules for locking may be derived from these examples. 1.    Loc
I2S8 THE NEW ENCLAND JOUKNAI. OK MKOICINK May 3, IWO nplalioii co an iiivcrsion of llic slccp-wakc s
PROGRAM ROZWOJOWY^1 POLITECHNIKI WARSZAWSKIEJ Eąuation (1.14) may be written as: transformation rota
PROGRAM ROZWOJOWY^1 POLITECHNIKI WARSZAWSKIEJ Eąuation (1.14) may be written as: transformation rota
Healthy2016 MAY 16
cp 13 No matter how simpic a great irutli may bc. to apply it to oncsown lifc successfully. in order
Scan0034 !n thc same *pirir. thc field of lingutstic cheOry may bc said to inelude m lca« four major
22.    C.64485 CENTRAL bank co-operation and intemational liąuidity in the financial
2. 2. 3. www.mtuexam.blogspot.in 10 W/m2.K. Calculate the value of highcst current that may bc passe
CERTIFICATESurveyof 17 May 2017You havc fulfillcd Ihe rcquirements of the External Ouality Assessmen
#• — • At this point it may bc as well to set out briefly the facts about the “ Curzon Linc.” Aciual
Validating Compiling lnvalid Schema Objects As a database administrator (DBA), you may bc asked to r
lmproving Replication Performance As the number of slaves connecting to a master increases, the load
we may have to consider some means of restricting theatten-dance. Whether this will be necessary at
26776 skanuj0084 (20) 87 DYLEMATY WIEJSKIEJ TURYSTYKI KULTUROWEJ authońs opinion it may be assumed t

więcej podobnych podstron