My various dotfiles

chap-5.texi 195KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446
  1. @node Data and Control Flow, Iteration, Types and Classes, Top
  2. @chapter Data and Control Flow
  3. @menu
  4. * Generalized Reference::
  5. * Transfer of Control to an Exit Point::
  6. * Data and Control Flow Dictionary::
  7. @end menu
  8. @node Generalized Reference, Transfer of Control to an Exit Point, Data and Control Flow, Data and Control Flow
  9. @section Generalized Reference
  10. @c including concept-places
  11. @menu
  12. * Overview of Places and Generalized Reference::
  13. * Kinds of Places::
  14. * Treatment of Other Macros Based on SETF::
  15. @end menu
  16. @node Overview of Places and Generalized Reference, Kinds of Places, Generalized Reference, Generalized Reference
  17. @subsection Overview of Places and Generalized Reference
  18. A @i{generalized reference}
  19. @IGindex{generalized reference}
  20. is the use of a @i{form},
  21. sometimes called a @i{place}
  22. @IGindex{place}
  23. ,
  24. as if it were a @i{variable} that could be read and written.
  25. The @i{value} of a @i{place} is
  26. the @i{object} to which the @i{place} @i{form} evaluates.
  27. The @i{value} of a @i{place} can be changed by using @b{setf}.
  28. The concept of binding a @i{place} is not defined in @r{Common Lisp},
  29. but an @i{implementation} is permitted to extend the language by defining this concept.
  30. Figure 5--1 contains examples of the use of @b{setf}.
  31. Note that the values returned by evaluating the @i{forms} in column two
  32. are not necessarily the same as those obtained by evaluating the
  33. @i{forms} in column three.
  34. In general, the exact @i{macro expansion} of a @b{setf} @i{form} is not guaranteed
  35. and can even be @i{implementation-dependent};
  36. all that is guaranteed is
  37. that the expansion is an update form that works
  38. for that particular @i{implementation},
  39. that the left-to-right evaluation of @i{subforms} is preserved,
  40. and
  41. that the ultimate result of evaluating @b{setf} is the value
  42. or values being stored.
  43. @group
  44. @noindent
  45. @w{ Access function Update Function Update using @b{setf} }
  46. @w{ @t{x} @t{(setq x datum)} @t{(setf x datum)} }
  47. @w{ @t{(car x)} @t{(rplaca x datum)} @t{(setf (car x) datum)} }
  48. @w{ @t{(symbol-value x)} @t{(set x datum)} @t{(setf (symbol-value x) datum)} }
  49. @noindent
  50. @w{ Figure 5--1: Examples of setf }
  51. @end group
  52. Figure 5--2 shows @i{operators} relating to
  53. @i{places} and @i{generalized reference}.
  54. @group
  55. @noindent
  56. @w{ assert defsetf push }
  57. @w{ ccase get-setf-expansion remf }
  58. @w{ ctypecase getf rotatef }
  59. @w{ decf incf setf }
  60. @w{ define-modify-macro pop shiftf }
  61. @w{ define-setf-expander psetf }
  62. @noindent
  63. @w{ Figure 5--2: Operators relating to places and generalized reference.}
  64. @end group
  65. Some of the @i{operators} above manipulate @i{places}
  66. and some manipulate @i{setf expanders}.
  67. A @i{setf expansion} can be derived from any @i{place}.
  68. New @i{setf expanders} can be defined by using @b{defsetf}
  69. and @b{define-setf-expander}.
  70. @menu
  71. * Evaluation of Subforms to Places::
  72. * Examples of Evaluation of Subforms to Places::
  73. * Setf Expansions::
  74. * Examples of Setf Expansions::
  75. @end menu
  76. @node Evaluation of Subforms to Places, Examples of Evaluation of Subforms to Places, Overview of Places and Generalized Reference, Overview of Places and Generalized Reference
  77. @subsubsection Evaluation of Subforms to Places
  78. The following rules apply to the @i{evaluation} of @i{subforms} in a
  79. @i{place}:
  80. @table @asis
  81. @item 1.
  82. The evaluation ordering of @i{subforms} within a @i{place}
  83. is determined by the order specified by the second value returned by
  84. @b{get-setf-expansion}.
  85. For all @i{places} defined by this specification
  86. (@i{e.g.}, @b{getf}, @b{ldb}, ...),
  87. this order of evaluation is left-to-right.
  88. @ITindex{order of evaluation}
  89. @ITindex{evaluation order}
  90. When a @i{place} is derived from a macro expansion,
  91. this rule is applied after the macro is expanded to find the appropriate @i{place}.
  92. @i{Places} defined by using @b{defmacro} or
  93. @b{define-setf-expander}
  94. use the evaluation order defined by those definitions.
  95. For example, consider the following:
  96. @example
  97. (defmacro wrong-order (x y) `(getf ,y ,x))
  98. @end example
  99. This following @i{form} evaluates @t{place2} first and
  100. then @t{place1} because that is the order they are evaluated in
  101. the macro expansion:
  102. @example
  103. (push value (wrong-order place1 place2))
  104. @end example
  105. @item 2.
  106. For the @i{macros} that manipulate @i{places}
  107. (@b{push},
  108. @b{pushnew},
  109. @b{remf},
  110. @b{incf},
  111. @b{decf},
  112. @b{shiftf},
  113. @b{rotatef},
  114. @b{psetf},
  115. @b{setf},
  116. @b{pop}, and those defined by @b{define-modify-macro})
  117. the @i{subforms} of the macro call are evaluated exactly once
  118. in left-to-right order, with the @i{subforms} of the @i{places}
  119. evaluated in the order specified in (1).
  120. @b{push}, @b{pushnew}, @b{remf},
  121. @b{incf}, @b{decf}, @b{shiftf}, @b{rotatef},
  122. @b{psetf}, @b{pop} evaluate all @i{subforms} before modifying
  123. any of the @i{place} locations.
  124. @b{setf} (in the case when @b{setf} has more than two arguments)
  125. performs its operation on each pair in sequence. For example, in
  126. @example
  127. (setf place1 value1 place2 value2 ...)
  128. @end example
  129. the @i{subforms} of @t{place1} and @t{value1} are evaluated, the location
  130. specified by
  131. @t{place1} is modified to contain the value returned by
  132. @t{value1}, and
  133. then the rest of the @b{setf} form is processed in a like manner.
  134. @item 3.
  135. For @b{check-type}, @b{ctypecase}, and @b{ccase},
  136. @i{subforms} of the @i{place} are evaluated once as in (1),
  137. but might be evaluated again if the
  138. type check fails in the case of @b{check-type}
  139. or none of the cases hold in
  140. @b{ctypecase} and @b{ccase}.
  141. @item 4.
  142. For @b{assert}, the order of evaluation of the generalized
  143. references is not specified.
  144. @ITindex{order of evaluation}
  145. @ITindex{evaluation order}
  146. @end table
  147. Rules 2, 3 and 4 cover all @i{standardized} @i{macros} that manipulate @i{places}.
  148. @node Examples of Evaluation of Subforms to Places, Setf Expansions, Evaluation of Subforms to Places, Overview of Places and Generalized Reference
  149. @subsubsection Examples of Evaluation of Subforms to Places
  150. @example
  151. (let ((ref2 (list '())))
  152. (push (progn (princ "1") 'ref-1)
  153. (car (progn (princ "2") ref2))))
  154. @t{ |> } 12
  155. @result{} (REF1)
  156. (let (x)
  157. (push (setq x (list 'a))
  158. (car (setq x (list 'b))))
  159. x)
  160. @result{} (((A) . B))
  161. @end example
  162. @b{push} first evaluates @t{(setq x (list 'a)) @result{} (a)},
  163. then evaluates @t{(setq x (list 'b)) @result{} (b)},
  164. then modifies the @i{car} of this latest value to be @t{((a) . b)}.
  165. @node Setf Expansions, Examples of Setf Expansions, Examples of Evaluation of Subforms to Places, Overview of Places and Generalized Reference
  166. @subsubsection Setf Expansions
  167. Sometimes it is possible to avoid evaluating @i{subforms} of a
  168. @i{place} multiple times or in the wrong order. A
  169. @i{setf expansion}
  170. for a given access form can be expressed as an ordered collection of five @i{objects}:
  171. @table @asis
  172. @item @b{List of temporary variables}
  173. a list of symbols naming temporary variables to be bound
  174. sequentially, as if by @b{let*}, to @i{values}
  175. resulting from value forms.
  176. @item @b{List of value forms}
  177. a list of forms (typically, @i{subforms} of the
  178. @i{place}) which when evaluated
  179. yield the values to which the corresponding temporary
  180. variables should be bound.
  181. @item @b{List of store variables}
  182. a list of symbols naming temporary store variables which are
  183. to hold the new values that will be assigned to the
  184. @i{place}.
  185. @item @b{Storing form}
  186. a form which can reference both the temporary and the store variables,
  187. and which changes the @i{value} of the @i{place}
  188. and guarantees to return as its values the values of the store variables,
  189. which are the correct values for @b{setf} to return.
  190. @item @b{Accessing form}
  191. a @i{form} which can reference the temporary variables,
  192. and which returns the @i{value} of the @i{place}.
  193. @end table
  194. The value returned by the accessing form is
  195. affected by execution of the storing form, but either of these
  196. forms might be evaluated any number of times.
  197. It is possible
  198. to do more than one @b{setf} in parallel via
  199. @b{psetf}, @b{shiftf}, and @b{rotatef}.
  200. Because of this, the
  201. @i{setf expander}
  202. must produce new temporary
  203. and store variable names every time. For examples of how to do this,
  204. see @b{gensym}.
  205. For each @i{standardized} accessor function @i{F},
  206. unless it is explicitly documented otherwise,
  207. it is @i{implementation-dependent} whether the ability to
  208. use an @i{F} @i{form} as a @b{setf} @i{place}
  209. is implemented by a @i{setf expander} or a @i{setf function}.
  210. Also, it follows from this that it is @i{implementation-dependent}
  211. whether the name @t{(setf @i{F})} is @i{fbound}.
  212. @node Examples of Setf Expansions, , Setf Expansions, Overview of Places and Generalized Reference
  213. @subsubsection Examples of Setf Expansions
  214. Examples of the contents of the constituents of @i{setf expansions}
  215. follow.
  216. For a variable @i{x}:
  217. @group
  218. @noindent
  219. @w{ @t{()} ;list of temporary variables }
  220. @w{ @t{()} ;list of value forms }
  221. @w{ @t{(g0001)} ;list of store variables }
  222. @w{ @t{(setq @i{x} g0001)} ;storing form }
  223. @w{ @i{x} ;accessing form }
  224. @noindent
  225. @w{ Figure 5--3: Sample Setf Expansion of a Variable}
  226. @end group
  227. For @t{(car @i{exp})}:
  228. @group
  229. @noindent
  230. @w{ @t{(g0002)} ;list of temporary variables }
  231. @w{ @t{(@i{exp})} ;list of value forms }
  232. @w{ @t{(g0003)} ;list of store variables }
  233. @w{ @t{(progn (rplaca g0002 g0003) g0003)} ;storing form }
  234. @w{ @t{(car g0002)} ;accessing form }
  235. @noindent
  236. @w{ Figure 5--4: Sample Setf Expansion of a CAR Form }
  237. @end group
  238. For @t{(subseq @i{seq} @i{s} @i{e})}:
  239. @group
  240. @noindent
  241. @w{ @t{(g0004 g0005 g0006)} ;list of temporary variables }
  242. @w{ @t{(@i{seq} @i{s} @i{e})} ;list of value forms }
  243. @w{ @t{(g0007)} ;list of store variables }
  244. @w{ @t{(progn (replace g0004 g0007 :start1 g0005 :end1 g0006) g0007)} }
  245. @w{ ;storing form }
  246. @w{ @t{(subseq g0004 g0005 g0006)} ; accessing form }
  247. @noindent
  248. @w{ Figure 5--5: Sample Setf Expansion of a SUBSEQ Form }
  249. @end group
  250. In some cases, if a @i{subform} of a @i{place} is itself
  251. a @i{place}, it is necessary to expand the @i{subform}
  252. in order to compute some of the values in the expansion of the outer
  253. @i{place}. For @t{(ldb @i{bs} (car @i{exp}))}:
  254. @group
  255. @noindent
  256. @w{ @t{(g0001 g0002)} ;list of temporary variables }
  257. @w{ @t{(@i{bs} @i{exp})} ;list of value forms }
  258. @w{ @t{(g0003)} ;list of store variables }
  259. @w{ @t{(progn (rplaca g0002 (dpb g0003 g0001 (car g0002))) g0003)} }
  260. @w{ ;storing form }
  261. @w{ @t{(ldb g0001 (car g0002))} ; accessing form }
  262. @noindent
  263. @w{ Figure 5--6: Sample Setf Expansion of a LDB Form }
  264. @end group
  265. @node Kinds of Places, Treatment of Other Macros Based on SETF, Overview of Places and Generalized Reference, Generalized Reference
  266. @subsection Kinds of Places
  267. Several kinds of @i{places} are defined by @r{Common Lisp};
  268. this section enumerates them.
  269. This set can be extended by @i{implementations} and by @i{programmer code}.
  270. @menu
  271. * Variable Names as Places::
  272. * Function Call Forms as Places::
  273. * VALUES Forms as Places::
  274. * THE Forms as Places::
  275. * APPLY Forms as Places::
  276. * Setf Expansions and Places::
  277. * Macro Forms as Places::
  278. * Symbol Macros as Places::
  279. * Other Compound Forms as Places::
  280. @end menu
  281. @node Variable Names as Places, Function Call Forms as Places, Kinds of Places, Kinds of Places
  282. @subsubsection Variable Names as Places
  283. The name of a @i{lexical variable} or @i{dynamic variable}
  284. can be used as a @i{place}.
  285. @node Function Call Forms as Places, VALUES Forms as Places, Variable Names as Places, Kinds of Places
  286. @subsubsection Function Call Forms as Places
  287. A @i{function form} can be used as a @i{place} if it falls
  288. into one of the following categories:
  289. @table @asis
  290. @item @t{*}
  291. A function call form whose first element is the name of
  292. any one of the functions in Figure 5--7.
  293. [Editorial Note by KMP: Note that what are in some places still called `condition accessors'
  294. are deliberately omitted from this table, and are not labeled as
  295. accessors in their entries. I have not yet had time to do a full
  296. search for these items and eliminate stray references to them as `accessors',
  297. which they are not, but I will do that at some point.]
  298. @group
  299. @noindent
  300. @w{ aref cdadr get }
  301. @w{ bit cdar gethash }
  302. @w{ caaaar cddaar logical-pathname-translations }
  303. @w{ caaadr cddadr macro-function }
  304. @w{ caaar cddar ninth }
  305. @w{ caadar cdddar nth }
  306. @w{ caaddr cddddr readtable-case }
  307. @w{ caadr cdddr rest }
  308. @w{ caar cddr row-major-aref }
  309. @w{ cadaar cdr sbit }
  310. @w{ cadadr char schar }
  311. @w{ cadar class-name second }
  312. @w{ caddar compiler-macro-function seventh }
  313. @w{ cadddr documentation sixth }
  314. @w{ caddr eighth slot-value }
  315. @w{ cadr elt subseq }
  316. @w{ car fdefinition svref }
  317. @w{ cdaaar fifth symbol-function }
  318. @w{ cdaadr fill-pointer symbol-plist }
  319. @w{ cdaar find-class symbol-value }
  320. @w{ cdadar first tenth }
  321. @w{ cdaddr fourth third }
  322. @noindent
  323. @w{ Figure 5--7: Functions that setf can be used with---1 }
  324. @end group
  325. In the case of @b{subseq}, the replacement value must be a @i{sequence}
  326. whose elements might be contained by the sequence argument to @b{subseq},
  327. but does not have to be a @i{sequence} of the same @i{type}
  328. as the @i{sequence} of which the subsequence is specified.
  329. If the length of the replacement value does not equal the length of
  330. the subsequence to be replaced, then the shorter length determines
  331. the number of elements to be stored, as for @b{replace}.
  332. @item @t{*}
  333. A function call form whose first element is the name of
  334. a selector function constructed by @b{defstruct}.
  335. The function name must refer to the global function definition,
  336. rather than a locally defined @i{function}.
  337. @item @t{*}
  338. A function call form whose first element is the name of
  339. any one of the functions in Figure 5--8,
  340. provided that the supplied argument
  341. to that function is in turn a @i{place} form;
  342. in this case the new @i{place} has stored back into it the
  343. result of applying the supplied ``update'' function.
  344. @group
  345. @noindent
  346. @w{ Function name Argument that is a @i{place} Update function used }
  347. @w{ @b{ldb} second @b{dpb} }
  348. @w{ @b{mask-field} second @b{deposit-field} }
  349. @w{ @b{getf} first @i{implementation-dependent} }
  350. @noindent
  351. @w{ Figure 5--8: Functions that setf can be used with---2 }
  352. @end group
  353. During the @b{setf} expansion of these @i{forms}, it is necessary to call
  354. @b{get-setf-expansion}
  355. in order to figure out how the inner, nested generalized variable must be treated.
  356. The information from
  357. @b{get-setf-expansion}
  358. is used as follows.
  359. @table @asis
  360. @item @b{ldb}
  361. In a form such as:
  362. @t{(setf (ldb @i{byte-spec} @i{place-form}) @i{value-form})}
  363. the place referred to by the @i{place-form} must always be both @i{read}
  364. and @i{written}; note that the update is to the generalized variable
  365. specified by @i{place-form}, not to any object of @i{type} @b{integer}.
  366. Thus this @b{setf} should generate code to do the following:
  367. @table @asis
  368. @item 1.
  369. Evaluate @i{byte-spec} (and bind it into a temporary variable).
  370. @item 2.
  371. Bind the temporary variables for @i{place-form}.
  372. @item 3.
  373. Evaluate @i{value-form} (and bind
  374. its value or values into the store variable).
  375. @item 4.
  376. Do the @i{read} from @i{place-form}.
  377. @item 5.
  378. Do the @i{write} into @i{place-form} with
  379. the given bits of the @i{integer}
  380. fetched in step 4 replaced with the value from step 3.
  381. @end table
  382. If the evaluation of @i{value-form}
  383. in step 3 alters what is found in @i{place-form},
  384. such as setting different bits of @i{integer},
  385. then the change of the bits denoted by
  386. @i{byte-spec} is to that
  387. altered @i{integer},
  388. because step 4 is done after the @i{value-form}
  389. evaluation. Nevertheless, the
  390. evaluations required for @i{binding}
  391. the temporary variables are done in steps 1 and
  392. 2, and thus the expected left-to-right evaluation order is seen.
  393. For example:
  394. @example
  395. (setq integer #x69) @result{} #x69
  396. (rotatef (ldb (byte 4 4) integer)
  397. (ldb (byte 4 0) integer))
  398. integer @result{} #x96
  399. ;;; This example is trying to swap two independent bit fields
  400. ;;; in an integer. Note that the generalized variable of
  401. ;;; interest here is just the (possibly local) program variable
  402. ;;; integer.
  403. @end example
  404. @item @b{mask-field}
  405. This case is the same as @b{ldb} in all essential aspects.
  406. @item @b{getf}
  407. In a form such as:
  408. @t{(setf (getf @i{place-form} @i{ind-form}) @i{value-form})}
  409. the place referred to by @i{place-form} must always be both @i{read}
  410. and @i{written}; note that the update is to the generalized variable
  411. specified by @i{place-form}, not necessarily to the particular
  412. @i{list}
  413. that is the property list in question.
  414. Thus this @b{setf} should generate code to do the following:
  415. @table @asis
  416. @item 1.
  417. Bind the temporary variables for @i{place-form}.
  418. @item 2.
  419. Evaluate @i{ind-form} (and bind it into a temporary variable).
  420. @item 3.
  421. Evaluate @i{value-form} (and bind
  422. its value or values into the store variable).
  423. @item 4.
  424. Do the @i{read} from @i{place-form}.
  425. @item 5.
  426. Do the @i{write} into @i{place-form} with a possibly-new property list
  427. obtained by combining the values from steps 2, 3, and 4.
  428. (Note that the phrase ``possibly-new property list'' can mean that
  429. the former property list is somehow destructively re-used, or it can
  430. mean partial or full copying of it.
  431. Since either copying or destructive re-use can occur,
  432. the treatment of the resultant value for the
  433. possibly-new property list must proceed as if it were a different copy
  434. needing to be stored back into the generalized variable.)
  435. @end table
  436. If the evaluation of @i{value-form}
  437. in step 3 alters what is found in
  438. @i{place-form}, such as setting a different named property in the list,
  439. then the change of the property denoted by @i{ind-form}
  440. is to that
  441. altered list, because step 4 is done after the
  442. @i{value-form}
  443. evaluation. Nevertheless, the
  444. evaluations required for @i{binding}
  445. the temporary variables are done in steps 1 and
  446. 2, and thus the expected left-to-right evaluation order is seen.
  447. For example:
  448. @example
  449. (setq s (setq r (list (list 'a 1 'b 2 'c 3)))) @result{} ((a 1 b 2 c 3))
  450. (setf (getf (car r) 'b)
  451. (progn (setq r nil) 6)) @result{} 6
  452. r @result{} NIL
  453. s @result{} ((A 1 B 6 C 3))
  454. ;;; Note that the (setq r nil) does not affect the actions of
  455. ;;; the SETF because the value of R had already been saved in
  456. ;;; a temporary variable as part of the step 1. Only the CAR
  457. ;;; of this value will be retrieved, and subsequently modified
  458. ;;; after the value computation.
  459. @end example
  460. @end table
  461. @end table
  462. @node VALUES Forms as Places, THE Forms as Places, Function Call Forms as Places, Kinds of Places
  463. @subsubsection VALUES Forms as Places
  464. A @b{values} @i{form} can be used as a @i{place},
  465. provided that each of its @i{subforms} is also a @i{place} form.
  466. A form such as
  467. @t{(setf (values @i{place-1} \dots @i{place-n}) @i{values-form})}
  468. does the following:
  469. @table @asis
  470. @item 1.
  471. The @i{subforms} of each nested @i{place} are evaluated
  472. in left-to-right order.
  473. @item 2.
  474. The @i{values-form} is evaluated, and the first store
  475. variable from each @i{place} is bound to its return values as if by
  476. @b{multiple-value-bind}.
  477. @item 3.
  478. If the @i{setf expansion} for any @i{place}
  479. involves more than one store variable, then the additional
  480. store variables are bound to @b{nil}.
  481. @item 4.
  482. The storing forms for each @i{place} are evaluated in
  483. left-to-right order.
  484. @end table
  485. The storing form in the @i{setf expansion} of @b{values}
  486. returns as @i{multiple values}_2 the values of the store
  487. variables in step 2. That is, the number of values returned is the
  488. same as the number of @i{place} forms. This may be more or fewer
  489. values than are produced by the @i{values-form}.
  490. @node THE Forms as Places, APPLY Forms as Places, VALUES Forms as Places, Kinds of Places
  491. @subsubsection THE Forms as Places
  492. A @b{the} @i{form} can be used as a @i{place},
  493. in which case the declaration is transferred to the @i{newvalue} form,
  494. and the resulting @b{setf} is analyzed. For example,
  495. @example
  496. (setf (the integer (cadr x)) (+ y 3))
  497. @end example
  498. is processed as if it were
  499. @example
  500. (setf (cadr x) (the integer (+ y 3)))
  501. @end example
  502. @node APPLY Forms as Places, Setf Expansions and Places, THE Forms as Places, Kinds of Places
  503. @subsubsection APPLY Forms as Places
  504. The following situations involving @b{setf} of @b{apply} must be supported:
  505. @table @asis
  506. @item @t{*}
  507. @t{(setf (apply #'aref @i{array}
  508. @{@i{subscript}@}{*}
  509. @i{more-subscripts})
  510. @i{new-element})}
  511. @item @t{*}
  512. @t{(setf (apply #'bit @i{array}
  513. @{@i{subscript}@}{*}
  514. @i{more-subscripts})
  515. @i{new-element})}
  516. @item @t{*}
  517. @t{(setf (apply #'sbit @i{array}
  518. @{@i{subscript}@}{*}
  519. @i{more-subscripts})
  520. @i{new-element})}
  521. @end table
  522. In all three cases, the @i{element} of @i{array} designated
  523. by the concatenation of @i{subscripts} and @i{more-subscripts}
  524. (@i{i.e.}, the same @i{element} which would be @i{read} by the call to
  525. @i{apply} if it were not part of a @b{setf} @i{form})
  526. is changed to have the @i{value} given by @i{new-element}.
  527. For these usages, the function name (@b{aref}, @b{bit}, or @b{sbit})
  528. must refer to the global function definition, rather than a locally defined
  529. @i{function}.
  530. No other @i{standardized} @i{function} is required to be supported,
  531. but an @i{implementation} may define such support.
  532. An @i{implementation} may also define support
  533. for @i{implementation-defined} @i{operators}.
  534. If a user-defined @i{function} is used in this context,
  535. the following equivalence is true, except that care is taken
  536. to preserve proper left-to-right evaluation of argument @i{subforms}:
  537. @example
  538. (setf (apply #'@i{name} @{@i{arg}@}{*}) @i{val})
  539. @equiv{} (apply #'(setf @i{name}) @i{val} @{@i{arg}@}{*})
  540. @end example
  541. @node Setf Expansions and Places, Macro Forms as Places, APPLY Forms as Places, Kinds of Places
  542. @subsubsection Setf Expansions and Places
  543. Any @i{compound form} for which the @i{operator} has a
  544. @i{setf expander}
  545. defined can be used as a @i{place}.
  546. The
  547. @i{operator}
  548. must refer to the global function definition,
  549. rather than a locally defined @i{function} or @i{macro}.
  550. @node Macro Forms as Places, Symbol Macros as Places, Setf Expansions and Places, Kinds of Places
  551. @subsubsection Macro Forms as Places
  552. A @i{macro form} can be used as a @i{place},
  553. in which case @r{Common Lisp} expands the @i{macro form}
  554. as if by @b{macroexpand-1}
  555. and then uses the @i{macro expansion} in place of the original @i{place}.
  556. Such @i{macro expansion} is attempted only after exhausting all other possibilities
  557. other than expanding into a call to a function named @t{(setf @i{reader})}.
  558. @node Symbol Macros as Places, Other Compound Forms as Places, Macro Forms as Places, Kinds of Places
  559. @subsubsection Symbol Macros as Places
  560. A reference to a @i{symbol} that has been @i{established} as a @i{symbol macro}
  561. can be used as a @i{place}. In this case,
  562. @b{setf} expands the reference and then analyzes the resulting @i{form}.
  563. @node Other Compound Forms as Places, , Symbol Macros as Places, Kinds of Places
  564. @subsubsection Other Compound Forms as Places
  565. For any other @i{compound form} for which the @i{operator} is a
  566. @i{symbol} @i{f},
  567. the @b{setf} @i{form} expands into a call
  568. to the @i{function} named @t{(setf @i{f})}.
  569. The first @i{argument} in the newly constructed @i{function form}
  570. is @i{newvalue} and the
  571. remaining @i{arguments} are the remaining @i{elements} of
  572. @i{place}.
  573. This expansion occurs regardless of whether @i{f} or @t{(setf @i{f})}
  574. is defined as a @i{function} locally, globally, or not at all.
  575. For example,
  576. @t{(setf (@i{f} @i{arg1} @i{arg2} ...) @i{new-value})}
  577. expands into a form with the same effect and value as
  578. @example
  579. (let ((#:temp-1 arg1) ;force correct order of evaluation
  580. (#:temp-2 arg2)
  581. ...
  582. (#:temp-0 @i{new-value}))
  583. (funcall (function (setf @i{f})) #:temp-0 #:temp-1 #:temp-2...))
  584. @end example
  585. A @i{function} named @t{(setf @i{f})} must return its first argument
  586. as its only value in order to preserve the semantics of @b{setf}.
  587. @node Treatment of Other Macros Based on SETF, , Kinds of Places, Generalized Reference
  588. @subsection Treatment of Other Macros Based on SETF
  589. For each of the ``read-modify-write'' @i{operators} in Figure 5--9,
  590. and for any additional @i{macros}
  591. defined by the @i{programmer} using @b{define-modify-macro},
  592. an exception is made to the normal rule of left-to-right evaluation of arguments.
  593. Evaluation of @i{argument} @i{forms} occurs in left-to-right order,
  594. with the exception that for the @i{place} @i{argument}, the actual
  595. @i{read} of the ``old value'' from that @i{place} happens
  596. after all of the @i{argument} @i{form} @i{evaluations},
  597. and just before a ``new value'' is computed and @i{written} back into the @i{place}.
  598. Specifically, each of these @i{operators} can be viewed as involving a
  599. @i{form} with the following general syntax:
  600. @example
  601. (@i{operator} @{@i{preceding-form}@}{*} @i{place} @{@i{following-form}@}{*})
  602. @end example
  603. The evaluation of each such @i{form} proceeds like this:
  604. @table @asis
  605. @item 1.
  606. @i{Evaluate} each of the @i{preceding-forms}, in left-to-right order.
  607. @item 2.
  608. @i{Evaluate} the @i{subforms} of the @i{place},
  609. in the order specified by the second value of the @i{setf expansion}
  610. for that @i{place}.
  611. @item 3.
  612. @i{Evaluate} each of the @i{following-forms}, in left-to-right order.
  613. @item 4.
  614. @i{Read} the old value from @i{place}.
  615. @item 5.
  616. Compute the new value.
  617. @item 6.
  618. Store the new value into @i{place}.
  619. @end table
  620. @group
  621. @noindent
  622. @w{ decf pop pushnew }
  623. @w{ incf push remf }
  624. @noindent
  625. @w{ Figure 5--9: Read-Modify-Write Macros}
  626. @end group
  627. @c end of including concept-places
  628. @node Transfer of Control to an Exit Point, Data and Control Flow Dictionary, Generalized Reference, Data and Control Flow
  629. @section Transfer of Control to an Exit Point
  630. @c including concept-exits
  631. When a transfer of control is initiated by @b{go},
  632. @b{return-from}, or @b{throw}
  633. the following events occur in order to accomplish the transfer of control.
  634. Note that for @b{go},
  635. the @i{exit point} is the @i{form} within the @b{tagbody}
  636. that is being executed at the time the @b{go} is performed;
  637. for @b{return-from},
  638. the @i{exit point} is the corresponding
  639. @b{block} @i{form};
  640. and for @b{throw},
  641. the @i{exit point} is the corresponding
  642. @b{catch} @i{form}.
  643. @table @asis
  644. @item 1.
  645. Intervening @i{exit points} are ``abandoned''
  646. (@i{i.e.}, their @i{extent} ends
  647. and it is no longer valid to attempt to transfer control through them).
  648. @item 2.
  649. The cleanup clauses of any intervening @b{unwind-protect} clauses
  650. are evaluated.
  651. @item 3.
  652. Intervening dynamic @i{bindings} of @b{special} variables,
  653. @i{catch tags}, @i{condition handlers}, and @i{restarts}
  654. are undone.
  655. @item 4.
  656. The @i{extent} of the @i{exit point} being invoked ends,
  657. and control is passed to the target.
  658. @end table
  659. The extent of an exit being ``abandoned'' because it is being passed over
  660. ends as soon as the transfer of control is initiated. That is,
  661. event 1 occurs at the beginning of the initiation of the transfer of
  662. control.
  663. The consequences are undefined if an attempt is made to transfer control
  664. to an @i{exit point} whose @i{dynamic extent} has ended.
  665. Events 2 and 3 are actually performed interleaved, in the order
  666. corresponding to the reverse order in which they were established.
  667. The effect of this is that the cleanup clauses of an @b{unwind-protect}
  668. see the same dynamic @i{bindings}
  669. of variables and @i{catch tags} as were
  670. visible when the @b{unwind-protect} was entered.
  671. Event 4 occurs at the end of the transfer of control.
  672. @c end of including concept-exits
  673. @node Data and Control Flow Dictionary, , Transfer of Control to an Exit Point, Data and Control Flow
  674. @section Data and Control Flow Dictionary
  675. @c including dict-flow
  676. @menu
  677. * apply::
  678. * defun::
  679. * fdefinition::
  680. * fboundp::
  681. * fmakunbound::
  682. * flet::
  683. * funcall::
  684. * function (Special Operator)::
  685. * function-lambda-expression::
  686. * functionp::
  687. * compiled-function-p::
  688. * call-arguments-limit::
  689. * lambda-list-keywords::
  690. * lambda-parameters-limit::
  691. * defconstant::
  692. * defparameter::
  693. * destructuring-bind::
  694. * let::
  695. * progv::
  696. * setq::
  697. * psetq::
  698. * block::
  699. * catch::
  700. * go::
  701. * return-from::
  702. * return::
  703. * tagbody::
  704. * throw::
  705. * unwind-protect::
  706. * nil::
  707. * not::
  708. * t::
  709. * eq::
  710. * eql::
  711. * equal::
  712. * equalp::
  713. * identity::
  714. * complement::
  715. * constantly::
  716. * every::
  717. * and::
  718. * cond::
  719. * if::
  720. * or::
  721. * when::
  722. * case::
  723. * typecase::
  724. * multiple-value-bind::
  725. * multiple-value-call::
  726. * multiple-value-list::
  727. * multiple-value-prog1::
  728. * multiple-value-setq::
  729. * values::
  730. * values-list::
  731. * multiple-values-limit::
  732. * nth-value::
  733. * prog::
  734. * prog1::
  735. * progn::
  736. * define-modify-macro::
  737. * defsetf::
  738. * define-setf-expander::
  739. * get-setf-expansion::
  740. * setf::
  741. * shiftf::
  742. * rotatef::
  743. * control-error::
  744. * program-error::
  745. * undefined-function::
  746. @end menu
  747. @node apply, defun, Data and Control Flow Dictionary, Data and Control Flow Dictionary
  748. @subsection apply [Function]
  749. @code{apply} @i{function {&rest} args^+} @result{} @i{@{@i{result}@}{*}}
  750. @subsubheading Arguments and Values::
  751. @i{function}---a @i{function designator}.
  752. @i{args}---a @i{spreadable argument list designator}.
  753. @i{results}---the @i{values} returned by @i{function}.
  754. @subsubheading Description::
  755. @i{Applies} the @i{function} to the @i{args}.
  756. When the @i{function} receives its arguments via @b{&rest}, it is
  757. permissible (but not required) for the @i{implementation} to @i{bind}
  758. the @i{rest parameter}
  759. to an @i{object} that shares structure with the last argument to @b{apply}.
  760. Because a @i{function} can neither detect whether it was called via @b{apply}
  761. nor whether (if so) the last argument to @b{apply} was a @i{constant},
  762. @i{conforming programs} must neither rely on the @i{list} structure
  763. of a @i{rest list} to be freshly consed, nor modify that @i{list} structure.
  764. @b{setf} can be used with @b{apply} in certain circumstances;
  765. see @ref{APPLY Forms as Places}.
  766. @subsubheading Examples::
  767. @example
  768. (setq f '+) @result{} +
  769. (apply f '(1 2)) @result{} 3
  770. (setq f #'-) @result{} #<FUNCTION ->
  771. (apply f '(1 2)) @result{} -1
  772. (apply #'max 3 5 '(2 7 3)) @result{} 7
  773. (apply 'cons '((+ 2 3) 4)) @result{} ((+ 2 3) . 4)
  774. (apply #'+ '()) @result{} 0
  775. (defparameter *some-list* '(a b c))
  776. (defun strange-test (&rest x) (eq x *some-list*))
  777. (apply #'strange-test *some-list*) @result{} @i{implementation-dependent}
  778. (defun bad-boy (&rest x) (rplacd x 'y))
  779. (bad-boy 'a 'b 'c) has undefined consequences.
  780. (apply #'bad-boy *some-list*) has undefined consequences.
  781. @end example
  782. @example
  783. (defun foo (size &rest keys &key double &allow-other-keys)
  784. (let ((v (apply #'make-array size :allow-other-keys t keys)))
  785. (if double (concatenate (type-of v) v v) v)))
  786. (foo 4 :initial-contents '(a b c d) :double t)
  787. @result{} #(A B C D A B C D)
  788. @end example
  789. @subsubheading See Also::
  790. @ref{funcall}
  791. ,
  792. @ref{fdefinition}
  793. ,
  794. @b{function},
  795. @ref{Evaluation},
  796. @ref{APPLY Forms as Places}
  797. @node defun, fdefinition, apply, Data and Control Flow Dictionary
  798. @subsection defun [Macro]
  799. @code{defun} @i{function-name lambda-list {[[@{@i{declaration}@}{*} | @i{documentation}]]} @{@i{form}@}{*}}@*
  800. @result{} @i{function-name}
  801. @subsubheading Arguments and Values::
  802. @i{function-name}---a @i{function name}.
  803. @i{lambda-list}---an @i{ordinary lambda list}.
  804. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  805. @i{documentation}---a @i{string}; not evaluated.
  806. @i{forms}---an @i{implicit progn}.
  807. @i{block-name}---the @i{function block name} of the @i{function-name}.
  808. @subsubheading Description::
  809. Defines a new @i{function} named @i{function-name} in the @i{global environment}.
  810. The body of the @i{function} defined by @b{defun} consists
  811. of @i{forms}; they are executed as an @i{implicit progn}
  812. when the @i{function} is called.
  813. @b{defun} can be used
  814. to define a new @i{function},
  815. to install a corrected version of an incorrect definition,
  816. to redefine an already-defined @i{function},
  817. or to redefine a @i{macro} as a @i{function}.
  818. @b{defun} implicitly puts a @b{block} named @i{block-name}
  819. around the body @i{forms}
  820. (but not the @i{forms} in the @i{lambda-list})
  821. of the @i{function} defined.
  822. @i{Documentation} is attached as a @i{documentation string}
  823. to @i{name} (as kind @b{function})
  824. and to the @i{function} @i{object}.
  825. Evaluating @b{defun} causes @i{function-name} to be a global name
  826. for the @i{function} specified by the @i{lambda expression}
  827. @example
  828. (lambda @i{lambda-list}
  829. {[[@{@i{declaration}@}{*} | @i{documentation}]]}
  830. (block @i{block-name} @{@i{form}@}{*}))
  831. @end example
  832. processed in the @i{lexical environment} in which @b{defun} was executed.
  833. (None of the arguments are evaluated at macro expansion time.)
  834. @b{defun} is not required to perform any compile-time side effects.
  835. In particular, @b{defun} does not make the @i{function} definition available
  836. at compile time. An @i{implementation} may choose to store information
  837. about the @i{function} for the purposes of compile-time error-checking
  838. (such as checking the number of arguments on calls),
  839. or to enable the @i{function} to be expanded inline.
  840. @subsubheading Examples::
  841. @example
  842. (defun recur (x)
  843. (when (> x 0)
  844. (recur (1- x)))) @result{} RECUR
  845. (defun ex (a b &optional c (d 66) &rest keys &key test (start 0))
  846. (list a b c d keys test start)) @result{} EX
  847. (ex 1 2) @result{} (1 2 NIL 66 NIL NIL 0)
  848. (ex 1 2 3 4 :test 'equal :start 50)
  849. @result{} (1 2 3 4 (:TEST EQUAL :START 50) EQUAL 50)
  850. (ex :test 1 :start 2) @result{} (:TEST 1 :START 2 NIL NIL 0)
  851. ;; This function assumes its callers have checked the types of the
  852. ;; arguments, and authorizes the compiler to build in that assumption.
  853. (defun discriminant (a b c)
  854. (declare (number a b c))
  855. "Compute the discriminant for a quadratic equation."
  856. (- (* b b) (* 4 a c))) @result{} DISCRIMINANT
  857. (discriminant 1 2/3 -2) @result{} 76/9
  858. ;; This function assumes its callers have not checked the types of the
  859. ;; arguments, and performs explicit type checks before making any assumptions.
  860. (defun careful-discriminant (a b c)
  861. "Compute the discriminant for a quadratic equation."
  862. (check-type a number)
  863. (check-type b number)
  864. (check-type c number)
  865. (locally (declare (number a b c))
  866. (- (* b b) (* 4 a c)))) @result{} CAREFUL-DISCRIMINANT
  867. (careful-discriminant 1 2/3 -2) @result{} 76/9
  868. @end example
  869. @subsubheading See Also::
  870. @ref{flet; labels; macrolet}
  871. ,
  872. @b{labels},
  873. @ref{block}
  874. ,
  875. @ref{return-from}
  876. ,
  877. @b{declare},
  878. @ref{documentation; (setf documentation)}
  879. ,
  880. @ref{Evaluation},
  881. @ref{Ordinary Lambda Lists},
  882. @ref{Syntactic Interaction of Documentation Strings and Declarations}
  883. @subsubheading Notes::
  884. @b{return-from} can be used to return
  885. prematurely from a @i{function} defined by @b{defun}.
  886. Additional side effects might take place when additional information
  887. (typically debugging information)
  888. about the function definition is recorded.
  889. @node fdefinition, fboundp, defun, Data and Control Flow Dictionary
  890. @subsection fdefinition [Accessor]
  891. @code{fdefinition} @i{function-name} @result{} @i{definition}
  892. (setf (@code{ fdefinition} @i{function-name}) new-definition)@*
  893. @subsubheading Arguments and Values::
  894. @i{function-name}---a @i{function name}.
  895. In the non-@b{setf} case,
  896. the @i{name} must be @i{fbound} in the @i{global environment}.
  897. @i{definition}---Current global function definition named by @i{function-name}.
  898. @i{new-definition}---a @i{function}.
  899. @subsubheading Description::
  900. @b{fdefinition} @i{accesses} the current global function definition
  901. named by @i{function-name}. The definition may be a
  902. @i{function} or may be an @i{object} representing a
  903. @i{special form} or @i{macro}.
  904. The value returned by @b{fdefinition} when @b{fboundp} returns true
  905. but the @i{function-name} denotes a @i{macro} or
  906. @i{special form} is not well-defined, but @b{fdefinition} does not signal an error.
  907. @subsubheading Exceptional Situations::
  908. Should signal an error of @i{type} @b{type-error}
  909. if @i{function-name} is not a @i{function name}.
  910. An error of @i{type} @b{undefined-function} is signaled
  911. in the non-@b{setf} case if @i{function-name} is not @i{fbound}.
  912. @subsubheading See Also::
  913. @ref{fboundp}
  914. ,
  915. @ref{fmakunbound}
  916. ,
  917. @ref{macro-function}
  918. ,
  919. @ref{special-operator-p}
  920. ,
  921. @ref{symbol-function}
  922. @subsubheading Notes::
  923. @b{fdefinition} cannot @i{access} the value of a lexical function name
  924. produced by @b{flet} or @b{labels}; it can @i{access} only
  925. the global function value.
  926. @b{setf} can be used with
  927. @b{fdefinition} to replace a global function
  928. definition when the @i{function-name}'s function definition
  929. does not represent a @i{special form}.
  930. @b{setf} of @b{fdefinition}
  931. requires a @i{function} as the new value.
  932. It is an error to set the @b{fdefinition} of a @i{function-name}
  933. to a @i{symbol}, a @i{list}, or the value returned
  934. by @b{fdefinition} on the name of a @i{macro}
  935. or @i{special form}.
  936. @node fboundp, fmakunbound, fdefinition, Data and Control Flow Dictionary
  937. @subsection fboundp [Function]
  938. @code{fboundp} @i{name} @result{} @i{generalized-boolean}
  939. @subsubheading Pronunciation::
  940. pronounced ,ef 'baund p\=e
  941. @subsubheading Arguments and Values::
  942. @i{name}---a @i{function name}.
  943. @i{generalized-boolean}---a @i{generalized boolean}.
  944. @subsubheading Description::
  945. Returns @i{true} if @i{name} is @i{fbound};
  946. otherwise, returns @i{false}.
  947. @subsubheading Examples::
  948. @example
  949. (fboundp 'car) @result{} @i{true}
  950. (fboundp 'nth-value) @result{} @i{false}
  951. (fboundp 'with-open-file) @result{} @i{true}
  952. (fboundp 'unwind-protect) @result{} @i{true}
  953. (defun my-function (x) x) @result{} MY-FUNCTION
  954. (fboundp 'my-function) @result{} @i{true}
  955. (let ((saved-definition (symbol-function 'my-function)))
  956. (unwind-protect (progn (fmakunbound 'my-function)
  957. (fboundp 'my-function))
  958. (setf (symbol-function 'my-function) saved-definition)))
  959. @result{} @i{false}
  960. (fboundp 'my-function) @result{} @i{true}
  961. (defmacro my-macro (x) `',x) @result{} MY-MACRO
  962. (fboundp 'my-macro) @result{} @i{true}
  963. (fmakunbound 'my-function) @result{} MY-FUNCTION
  964. (fboundp 'my-function) @result{} @i{false}
  965. (flet ((my-function (x) x))
  966. (fboundp 'my-function)) @result{} @i{false}
  967. @end example
  968. @subsubheading Exceptional Situations::
  969. Should signal an error of @i{type} @b{type-error}
  970. if @i{name} is not a @i{function name}.
  971. @subsubheading See Also::
  972. @ref{symbol-function}
  973. ,
  974. @ref{fmakunbound}
  975. ,
  976. @ref{fdefinition}
  977. @subsubheading Notes::
  978. It is permissible to call @b{symbol-function} on any @i{symbol}
  979. that is @i{fbound}.
  980. @b{fboundp} is sometimes used to ``guard''
  981. an access to the @i{function cell}, as in:
  982. @example
  983. (if (fboundp x) (symbol-function x))
  984. @end example
  985. Defining a @i{setf expander} @i{F} does not cause the @i{setf function}
  986. @t{(setf @i{F})} to become defined.
  987. @node fmakunbound, flet, fboundp, Data and Control Flow Dictionary
  988. @subsection fmakunbound [Function]
  989. @code{fmakunbound} @i{name} @result{} @i{name}
  990. @subsubheading Pronunciation::
  991. pronounced ,ef 'mak e n,baund
  992. or pronounced ,ef 'm\=a k e n,baund
  993. @subsubheading Arguments and Values::
  994. @i{name}---a @i{function name}.
  995. @subsubheading Description::
  996. Removes the @i{function} or @i{macro} definition, if any, of @i{name}
  997. in the @i{global environment}.
  998. @subsubheading Examples::
  999. @example
  1000. (defun add-some (x) (+ x 19)) @result{} ADD-SOME
  1001. (fboundp 'add-some) @result{} @i{true}
  1002. (flet ((add-some (x) (+ x 37)))
  1003. (fmakunbound 'add-some)
  1004. (add-some 1)) @result{} 38
  1005. (fboundp 'add-some) @result{} @i{false}
  1006. @end example
  1007. @subsubheading Exceptional Situations::
  1008. Should signal an error of @i{type} @b{type-error}
  1009. if @i{name} is not a @i{function name}.
  1010. The consequences are undefined if @i{name} is a @i{special operator}.
  1011. @subsubheading See Also::
  1012. @ref{fboundp}
  1013. ,
  1014. @ref{makunbound}
  1015. @node flet, funcall, fmakunbound, Data and Control Flow Dictionary
  1016. @subsection flet, labels, macrolet [Special Operator]
  1017. @code{flet} @i{@r{(}@{{(}@i{function-name}
  1018. @i{lambda-list}
  1019. {[[@{@i{local-declaration}@}{*}
  1020. | @i{local-documentation}]]}
  1021. @{@i{local-form}@}{*}@r{)}@}{*}@r{)}
  1022. @{@i{declaration}@}{*} @{@i{form}@}{*}}@*
  1023. @result{} @i{@{@i{result}@}{*}}
  1024. @code{labels} @i{@r{(}@{{(}@i{function-name}
  1025. @i{lambda-list}
  1026. {[[@{@i{local-declaration}@}{*}
  1027. | @i{local-documentation}]]}
  1028. @{@i{local-form}@}{*}@r{)}@}{*}@r{)}
  1029. @{@i{declaration}@}{*} @{@i{form}@}{*}}@*
  1030. @result{} @i{@{@i{result}@}{*}}
  1031. @code{macrolet} @i{@r{(}@{{(}@i{name}
  1032. @i{lambda-list}
  1033. {[[@{@i{local-declaration}@}{*}
  1034. | @i{local-documentation}]]}
  1035. @{@i{local-form}@}{*}@r{)}@}{*}@r{)}
  1036. @{@i{declaration}@}{*} @{@i{form}@}{*}}@*
  1037. @result{} @i{@{@i{result}@}{*}}
  1038. @subsubheading Arguments and Values::
  1039. @i{function-name}---a @i{function name}.
  1040. @i{name}---a @i{symbol}.
  1041. @i{lambda-list}---a @i{lambda list};
  1042. for @b{flet} and @b{labels},
  1043. it is an @i{ordinary lambda list};
  1044. for @b{macrolet},
  1045. it is a @i{macro lambda list}.
  1046. @i{local-declaration}---a @b{declare} @i{expression}; not evaluated.
  1047. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  1048. @i{local-documentation}---a @i{string}; not evaluated.
  1049. @i{local-forms}, @i{forms}---an @i{implicit progn}.
  1050. @i{results}---the @i{values} of the @i{forms}.
  1051. @subsubheading Description::
  1052. @b{flet}, @b{labels}, and @b{macrolet}
  1053. define local @i{functions} and @i{macros}, and execute
  1054. @i{forms} using the local definitions.
  1055. @i{Forms} are executed in order of occurrence.
  1056. The body forms (but not the @i{lambda list})
  1057. of each @i{function} created by @b{flet} and @b{labels}
  1058. and each @i{macro} created by @b{macrolet}
  1059. are enclosed in an @i{implicit block} whose name
  1060. is the @i{function block name} of the @i{function-name} or @i{name},
  1061. as appropriate.
  1062. The scope of the @i{declarations}
  1063. between
  1064. the list of local function/macro definitions and the body @i{forms}
  1065. in @b{flet} and @b{labels}
  1066. does not include the bodies of the
  1067. locally defined @i{functions}, except that for @b{labels},
  1068. any @b{inline}, @b{notinline}, or @b{ftype} declarations
  1069. that refer to the locally defined functions do apply to the local function
  1070. bodies. That is, their @i{scope}
  1071. is the same as the function name that they
  1072. affect.
  1073. The scope of these @i{declarations}
  1074. does not include the bodies of the macro expander
  1075. functions defined by @b{macrolet}.
  1076. @table @asis
  1077. @item flet
  1078. @b{flet} defines locally named @i{functions} and executes a series of
  1079. @i{forms} with these definition @i{bindings}. Any number of
  1080. such local @i{functions} can be defined.
  1081. The @i{scope} of the name @i{binding} encompasses only the body.
  1082. Within the
  1083. body of @b{flet},
  1084. @i{function-names} matching those defined
  1085. by @b{flet}
  1086. refer to the locally defined @i{functions}
  1087. rather than to
  1088. the global function definitions of the same name.
  1089. Also, within the scope of @b{flet},
  1090. global @i{setf expander} definitions of the @i{function-name}
  1091. defined by @b{flet} do not apply.
  1092. Note that this applies to
  1093. @t{(defsetf @i{f} ...)}, not
  1094. @t{(defmethod (setf @i{f}) ...)}.
  1095. The names of @i{functions} defined by @b{flet}
  1096. are in the @i{lexical environment}; they retain
  1097. their local definitions only within the body of @b{flet}.
  1098. The function definition bindings are visible only in
  1099. the body of @b{flet}, not the definitions themselves. Within the
  1100. function definitions, local function names
  1101. that match those being
  1102. defined refer to @i{functions} or
  1103. @i{macros} defined outside the @b{flet}.
  1104. @b{flet} can locally @i{shadow} a global function name,
  1105. and the new definition can refer to the global definition.
  1106. Any @i{local-documentation} is attached to the corresponding local @i{function}
  1107. (if one is actually created) as a @i{documentation string}.
  1108. @item labels
  1109. @b{labels} is equivalent to @b{flet} except that
  1110. the scope of the defined function names for @b{labels}
  1111. encompasses the function definitions themselves as well as the body.
  1112. @item macrolet
  1113. @b{macrolet}
  1114. establishes local @i{macro} definitions,
  1115. using the same format used by @b{defmacro}.
  1116. Within the body of @b{macrolet},
  1117. global @i{setf expander} definitions of the @i{names} defined by the
  1118. @b{macrolet} do not apply; rather, @b{setf} expands the
  1119. @i{macro form} and recursively process the resulting @i{form}.
  1120. The macro-expansion functions defined by @b{macrolet}
  1121. are defined in the
  1122. @i{lexical environment} in which the @b{macrolet} form appears.
  1123. Declarations and @b{macrolet} and
  1124. @b{symbol-macrolet} definitions
  1125. affect the local macro definitions in a @b{macrolet}, but the
  1126. consequences are undefined if the local macro definitions reference
  1127. any local @i{variable} or @i{function} @i{bindings} that are visible in that
  1128. @i{lexical environment}.
  1129. Any @i{local-documentation} is attached to the corresponding local @i{macro function}
  1130. as a @i{documentation string}.
  1131. @end table
  1132. @subsubheading Examples::
  1133. @example
  1134. (defun foo (x flag)
  1135. (macrolet ((fudge (z)
  1136. ;The parameters x and flag are not accessible
  1137. ; at this point; a reference to flag would be to
  1138. ; the global variable of that name.
  1139. ` (if flag (* ,z ,z) ,z)))
  1140. ;The parameters x and flag are accessible here.
  1141. (+ x
  1142. (fudge x)
  1143. (fudge (+ x 1)))))
  1144. @equiv{}
  1145. (defun foo (x flag)
  1146. (+ x
  1147. (if flag (* x x) x)
  1148. (if flag (* (+ x 1) (+ x 1)) (+ x 1))))
  1149. @end example
  1150. after macro expansion. The occurrences of @t{x} and @t{flag} legitimately
  1151. refer to the parameters of the function @t{foo} because those parameters are
  1152. visible at the site of the macro call which produced the expansion.
  1153. @example
  1154. (flet ((flet1 (n) (+ n n)))
  1155. (flet ((flet1 (n) (+ 2 (flet1 n))))
  1156. (flet1 2))) @result{} 6
  1157. (defun dummy-function () 'top-level) @result{} DUMMY-FUNCTION
  1158. (funcall #'dummy-function) @result{} TOP-LEVEL
  1159. (flet ((dummy-function () 'shadow))
  1160. (funcall #'dummy-function)) @result{} SHADOW
  1161. (eq (funcall #'dummy-function) (funcall 'dummy-function))
  1162. @result{} @i{true}
  1163. (flet ((dummy-function () 'shadow))
  1164. (eq (funcall #'dummy-function)
  1165. (funcall 'dummy-function)))
  1166. @result{} @i{false}
  1167. (defun recursive-times (k n)
  1168. (labels ((temp (n)
  1169. (if (zerop n) 0 (+ k (temp (1- n))))))
  1170. (temp n))) @result{} RECURSIVE-TIMES
  1171. (recursive-times 2 3) @result{} 6
  1172. (defmacro mlets (x &environment env)
  1173. (let ((form `(babbit ,x)))
  1174. (macroexpand form env))) @result{} MLETS
  1175. (macrolet ((babbit (z) `(+ ,z ,z))) (mlets 5)) @result{} 10
  1176. @end example
  1177. @example
  1178. (flet ((safesqrt (x) (sqrt (abs x))))
  1179. ;; The safesqrt function is used in two places.
  1180. (safesqrt (apply #'+ (map 'list #'safesqrt '(1 2 3 4 5 6)))))
  1181. @result{} 3.291173
  1182. @end example
  1183. @example
  1184. (defun integer-power (n k)
  1185. (declare (integer n))
  1186. (declare (type (integer 0 *) k))
  1187. (labels ((expt0 (x k a)
  1188. (declare (integer x a) (type (integer 0 *) k))
  1189. (cond ((zerop k) a)
  1190. ((evenp k) (expt1 (* x x) (floor k 2) a))
  1191. (t (expt0 (* x x) (floor k 2) (* x a)))))
  1192. (expt1 (x k a)
  1193. (declare (integer x a) (type (integer 0 *) k))
  1194. (cond ((evenp k) (expt1 (* x x) (floor k 2) a))
  1195. (t (expt0 (* x x) (floor k 2) (* x a))))))
  1196. (expt0 n k 1))) @result{} INTEGER-POWER
  1197. @end example
  1198. @example
  1199. (defun example (y l)
  1200. (flet ((attach (x)
  1201. (setq l (append l (list x)))))
  1202. (declare (inline attach))
  1203. (dolist (x y)
  1204. (unless (null (cdr x))
  1205. (attach x)))
  1206. l))
  1207. (example '((a apple apricot) (b banana) (c cherry) (d) (e))
  1208. '((1) (2) (3) (4 2) (5) (6 3 2)))
  1209. @result{} ((1) (2) (3) (4 2) (5) (6 3 2) (A APPLE APRICOT) (B BANANA) (C CHERRY))
  1210. @end example
  1211. @subsubheading See Also::
  1212. @b{declare},
  1213. @ref{defmacro}
  1214. ,
  1215. @ref{defun}
  1216. ,
  1217. @ref{documentation; (setf documentation)}
  1218. ,
  1219. @ref{let; let*}
  1220. ,
  1221. @ref{Evaluation},
  1222. @ref{Syntactic Interaction of Documentation Strings and Declarations}
  1223. @subsubheading Notes::
  1224. It is not possible to define recursive @i{functions} with @b{flet}.
  1225. @b{labels} can be used to define mutually recursive @i{functions}.
  1226. If a @b{macrolet} @i{form} is a @i{top level form},
  1227. the body @i{forms} are also processed as @i{top level forms}.
  1228. See @ref{File Compilation}.
  1229. @node funcall, function (Special Operator), flet, Data and Control Flow Dictionary
  1230. @subsection funcall [Function]
  1231. @code{funcall} @i{function {&rest} args} @result{} @i{@{@i{result}@}{*}}
  1232. @subsubheading Arguments and Values::
  1233. @i{function}---a @i{function designator}.
  1234. @i{args}---@i{arguments} to the @i{function}.
  1235. @i{results}---the @i{values} returned by the @i{function}.
  1236. @subsubheading Description::
  1237. @b{funcall} applies @i{function} to @i{args}.
  1238. If @i{function} is a @i{symbol},
  1239. it is coerced to a @i{function} as if by
  1240. finding its @i{functional value} in the @i{global environment}.
  1241. @subsubheading Examples::
  1242. @example
  1243. (funcall #'+ 1 2 3) @result{} 6
  1244. (funcall 'car '(1 2 3)) @result{} 1
  1245. (funcall 'position 1 '(1 2 3 2 1) :start 1) @result{} 4
  1246. (cons 1 2) @result{} (1 . 2)
  1247. (flet ((cons (x y) `(kons ,x ,y)))
  1248. (let ((cons (symbol-function '+)))
  1249. (funcall #'cons
  1250. (funcall 'cons 1 2)
  1251. (funcall cons 1 2))))
  1252. @result{} (KONS (1 . 2) 3)
  1253. @end example
  1254. @subsubheading Exceptional Situations::
  1255. An error of @i{type} @b{undefined-function} should be signaled if @i{function}
  1256. is a @i{symbol} that does not have a global definition as a @i{function}
  1257. or that has a global definition as a @i{macro} or a @i{special operator}.
  1258. @subsubheading See Also::
  1259. @ref{apply}
  1260. , @b{function}, @ref{Evaluation}
  1261. @subsubheading Notes::
  1262. @example
  1263. (funcall @i{function} @i{arg1} @i{arg2} ...)
  1264. @equiv{} (apply @i{function} @i{arg1} @i{arg2} ... nil)
  1265. @equiv{} (apply @i{function} (list @i{arg1} @i{arg2} ...))
  1266. @end example
  1267. The difference between @b{funcall} and an ordinary function call is that
  1268. in the former case the @i{function} is obtained by ordinary @i{evaluation}
  1269. of a @i{form}, and in the latter case it is obtained by the special
  1270. interpretation of the function position that normally occurs.
  1271. @node function (Special Operator), function-lambda-expression, funcall, Data and Control Flow Dictionary
  1272. @subsection function [Special Operator]
  1273. @code{function} @i{name} @result{} @i{function}
  1274. @subsubheading Arguments and Values::
  1275. @i{name}---a @i{function name} or @i{lambda expression}.
  1276. @i{function}---a @i{function} @i{object}.
  1277. @subsubheading Description::
  1278. The @i{value} of @b{function} is the @i{functional value} of @i{name}
  1279. in the current @i{lexical environment}.
  1280. If @i{name} is a @i{function name}, the functional definition of that name
  1281. is that
  1282. established by the innermost lexically enclosing
  1283. @b{flet}, @b{labels}, or @b{macrolet} @i{form},
  1284. if there is one. Otherwise the global functional definition of the
  1285. @i{function name}
  1286. is returned.
  1287. If @i{name} is a @i{lambda expression}, then a @i{lexical closure}
  1288. is returned. In situations where a @i{closure} over the same set of
  1289. @i{bindings} might be produced more than once, the various resulting
  1290. @i{closures} might or might not be @b{eq}.
  1291. It is an error to use @b{function} on a @i{function name}
  1292. that does not denote a @i{function} in the lexical environment in
  1293. which the @b{function} form appears.
  1294. Specifically, it is an error to use @b{function} on a @i{symbol}
  1295. that denotes a @i{macro} or @i{special form}.
  1296. An implementation may choose not to signal this error for
  1297. performance reasons, but implementations are forbidden from
  1298. defining the failure to signal an error as a useful behavior.
  1299. @subsubheading Examples::
  1300. @example
  1301. (defun adder (x) (function (lambda (y) (+ x y))))
  1302. @end example
  1303. The result of @t{(adder 3)} is a function that adds @t{3} to its argument:
  1304. @example
  1305. (setq add3 (adder 3))
  1306. (funcall add3 5) @result{} 8
  1307. @end example
  1308. This works because @b{function} creates a @i{closure} of
  1309. the @i{lambda expression} that is able to refer to the @i{value} @t{3}
  1310. of the variable @t{x} even after control has returned from the function @t{adder}.
  1311. @subsubheading See Also::
  1312. @ref{defun}
  1313. ,
  1314. @ref{fdefinition}
  1315. ,
  1316. @ref{flet; labels; macrolet}
  1317. ,
  1318. @b{labels},
  1319. @ref{symbol-function}
  1320. ,
  1321. @ref{Symbols as Forms},
  1322. @ref{Sharpsign Single-Quote},
  1323. @ref{Printing Other Objects}
  1324. @subsubheading Notes::
  1325. The notation @t{#'@i{name}} may be used as an abbreviation
  1326. for @t{(function @i{name})}.
  1327. @node function-lambda-expression, functionp, function (Special Operator), Data and Control Flow Dictionary
  1328. @subsection function-lambda-expression [Function]
  1329. @code{function-lambda-expression} @i{function}@*
  1330. @result{} @i{lambda-expression, closure-p, name}
  1331. @subsubheading Arguments and Values::
  1332. @i{function}---a @i{function}.
  1333. @i{lambda-expression}---a @i{lambda expression} or @b{nil}.
  1334. @i{closure-p}---a @i{generalized boolean}.
  1335. @i{name}---an @i{object}.
  1336. @subsubheading Description::
  1337. Returns information about @i{function} as follows:
  1338. The @i{primary value}, @i{lambda-expression},
  1339. is @i{function}'s defining @i{lambda expression},
  1340. or @b{nil} if the information is not available. The @i{lambda expression}
  1341. may have been pre-processed in some ways, but it should remain a suitable
  1342. argument to @b{compile} or @b{function}.
  1343. Any @i{implementation} may legitimately return @b{nil}
  1344. as the @i{lambda-expression} of any @i{function}.
  1345. The @i{secondary value}, @i{closure-p},
  1346. is @b{nil} if @i{function}'s definition was enclosed
  1347. in the @i{null lexical environment} or something @i{non-nil} if
  1348. @i{function}'s definition might have been enclosed in some
  1349. @i{non-null lexical environment}.
  1350. Any @i{implementation} may legitimately return @i{true}
  1351. as the @i{closure-p} of any @i{function}.
  1352. The @i{tertiary value}, @i{name},
  1353. is the ``name'' of @i{function}.
  1354. The name is intended for debugging only and is not necessarily one that would
  1355. be valid for use as a name in @b{defun} or @b{function}, for example.
  1356. By convention, @b{nil} is used to mean that @i{function} has no name.
  1357. Any @i{implementation} may legitimately return @b{nil}
  1358. as the @i{name} of any @i{function}.
  1359. @subsubheading Examples::
  1360. The following examples illustrate some possible return values, but
  1361. are not intended to be exhaustive:
  1362. @example
  1363. (function-lambda-expression #'(lambda (x) x))
  1364. @result{} NIL, @i{false}, NIL
  1365. @i{OR}@result{} NIL, @i{true}, NIL
  1366. @i{OR}@result{} (LAMBDA (X) X), @i{true}, NIL
  1367. @i{OR}@result{} (LAMBDA (X) X), @i{false}, NIL
  1368. (function-lambda-expression
  1369. (funcall #'(lambda () #'(lambda (x) x))))
  1370. @result{} NIL, @i{false}, NIL
  1371. @i{OR}@result{} NIL, @i{true}, NIL
  1372. @i{OR}@result{} (LAMBDA (X) X), @i{true}, NIL
  1373. @i{OR}@result{} (LAMBDA (X) X), @i{false}, NIL
  1374. (function-lambda-expression
  1375. (funcall #'(lambda (x) #'(lambda () x)) nil))
  1376. @result{} NIL, @i{true}, NIL
  1377. @i{OR}@result{} (LAMBDA () X), @i{true}, NIL
  1378. @i{NOT}@result{} NIL, @i{false}, NIL
  1379. @i{NOT}@result{} (LAMBDA () X), @i{false}, NIL
  1380. (flet ((foo (x) x))
  1381. (setf (symbol-function 'bar) #'foo)
  1382. (function-lambda-expression #'bar))
  1383. @result{} NIL, @i{false}, NIL
  1384. @i{OR}@result{} NIL, @i{true}, NIL
  1385. @i{OR}@result{} (LAMBDA (X) (BLOCK FOO X)), @i{true}, NIL
  1386. @i{OR}@result{} (LAMBDA (X) (BLOCK FOO X)), @i{false}, FOO
  1387. @i{OR}@result{} (SI::BLOCK-LAMBDA FOO (X) X), @i{false}, FOO
  1388. (defun foo ()
  1389. (flet ((bar (x) x))
  1390. #'bar))
  1391. (function-lambda-expression (foo))
  1392. @result{} NIL, @i{false}, NIL
  1393. @i{OR}@result{} NIL, @i{true}, NIL
  1394. @i{OR}@result{} (LAMBDA (X) (BLOCK BAR X)), @i{true}, NIL
  1395. @i{OR}@result{} (LAMBDA (X) (BLOCK BAR X)), @i{true}, (:INTERNAL FOO 0 BAR)
  1396. @i{OR}@result{} (LAMBDA (X) (BLOCK BAR X)), @i{false}, "BAR in FOO"
  1397. @end example
  1398. @subsubheading Notes::
  1399. Although @i{implementations} are free to return ``@b{nil}, @i{true}, @b{nil}'' in all cases,
  1400. they are encouraged to return a @i{lambda expression} as the @i{primary value}
  1401. in the case where the argument was created by a call to @b{compile}
  1402. or @b{eval} (as opposed to being created by @i{loading} a @i{compiled file}).
  1403. @node functionp, compiled-function-p, function-lambda-expression, Data and Control Flow Dictionary
  1404. @subsection functionp [Function]
  1405. @code{functionp} @i{object} @result{} @i{generalized-boolean}
  1406. @subsubheading Arguments and Values::
  1407. @i{object}---an @i{object}.
  1408. @i{generalized-boolean}---a @i{generalized boolean}.
  1409. @subsubheading Description::
  1410. Returns @i{true} if @i{object} is of @i{type} @b{function};
  1411. otherwise, returns @i{false}.
  1412. @subsubheading Examples::
  1413. @example
  1414. (functionp 'append) @result{} @i{false}
  1415. (functionp #'append) @result{} @i{true}
  1416. (functionp (symbol-function 'append)) @result{} @i{true}
  1417. (flet ((f () 1)) (functionp #'f)) @result{} @i{true}
  1418. (functionp (compile nil '(lambda () 259))) @result{} @i{true}
  1419. (functionp nil) @result{} @i{false}
  1420. (functionp 12) @result{} @i{false}
  1421. (functionp '(lambda (x) (* x x))) @result{} @i{false}
  1422. (functionp #'(lambda (x) (* x x))) @result{} @i{true}
  1423. @end example
  1424. @subsubheading Notes::
  1425. @example
  1426. (functionp @i{object}) @equiv{} (typep @i{object} 'function)
  1427. @end example
  1428. @node compiled-function-p, call-arguments-limit, functionp, Data and Control Flow Dictionary
  1429. @subsection compiled-function-p [Function]
  1430. @code{compiled-function-p} @i{object} @result{} @i{generalized-boolean}
  1431. @subsubheading Arguments and Values::
  1432. @i{object}---an @i{object}.
  1433. @i{generalized-boolean}---a @i{generalized boolean}.
  1434. @subsubheading Description::
  1435. Returns @i{true} if @i{object} is of @i{type} @b{compiled-function};
  1436. otherwise, returns @i{false}.
  1437. @subsubheading Examples::
  1438. @example
  1439. (defun f (x) x) @result{} F
  1440. (compiled-function-p #'f)
  1441. @result{} @i{false}
  1442. @i{OR}@result{} @i{true}
  1443. (compiled-function-p 'f) @result{} @i{false}
  1444. (compile 'f) @result{} F
  1445. (compiled-function-p #'f) @result{} @i{true}
  1446. (compiled-function-p 'f) @result{} @i{false}
  1447. (compiled-function-p (compile nil '(lambda (x) x)))
  1448. @result{} @i{true}
  1449. (compiled-function-p #'(lambda (x) x))
  1450. @result{} @i{false}
  1451. @i{OR}@result{} @i{true}
  1452. (compiled-function-p '(lambda (x) x)) @result{} @i{false}
  1453. @end example
  1454. @subsubheading See Also::
  1455. @ref{compile}
  1456. ,
  1457. @ref{compile-file}
  1458. ,
  1459. @ref{compiled-function}
  1460. @subsubheading Notes::
  1461. @example
  1462. (compiled-function-p @i{object}) @equiv{} (typep @i{object} 'compiled-function)
  1463. @end example
  1464. @node call-arguments-limit, lambda-list-keywords, compiled-function-p, Data and Control Flow Dictionary
  1465. @subsection call-arguments-limit [Constant Variable]
  1466. @subsubheading Constant Value::
  1467. An integer not smaller than @t{50} and at least as great as
  1468. the @i{value} of @b{lambda-parameters-limit},
  1469. the exact magnitude of which is @i{implementation-dependent}.
  1470. @subsubheading Description::
  1471. The upper exclusive bound on the number of @i{arguments} that
  1472. may be passed to a @i{function}.
  1473. @subsubheading See Also::
  1474. @ref{lambda-parameters-limit}
  1475. ,
  1476. @ref{multiple-values-limit}
  1477. @node lambda-list-keywords, lambda-parameters-limit, call-arguments-limit, Data and Control Flow Dictionary
  1478. @subsection lambda-list-keywords [Constant Variable]
  1479. @subsubheading Constant Value::
  1480. a @i{list}, the @i{elements} of which are @i{implementation-dependent},
  1481. but which must contain at least the @i{symbols}
  1482. @b{&allow-other-keys},
  1483. @b{&aux},
  1484. @b{&body},
  1485. @b{&environment},
  1486. @b{&key},
  1487. @b{&optional},
  1488. @b{&rest},
  1489. and
  1490. @b{&whole}.
  1491. @subsubheading Description::
  1492. A @i{list} of all the @i{lambda list keywords} used
  1493. in the @i{implementation}, including the additional ones
  1494. used only by @i{macro} definition @i{forms}.
  1495. @subsubheading See Also::
  1496. @ref{defun}
  1497. ,
  1498. @ref{flet; labels; macrolet}
  1499. ,
  1500. @ref{defmacro}
  1501. ,
  1502. @b{macrolet},
  1503. @ref{The Evaluation Model}
  1504. @node lambda-parameters-limit, defconstant, lambda-list-keywords, Data and Control Flow Dictionary
  1505. @subsection lambda-parameters-limit [Constant Variable]
  1506. @subsubheading Constant Value::
  1507. @i{implementation-dependent}, but not smaller than @t{50}.
  1508. @subsubheading Description::
  1509. A positive @i{integer} that is the upper exclusive bound on
  1510. the number of @i{parameter} @i{names} that can appear
  1511. in a single @i{lambda list}.
  1512. @subsubheading See Also::
  1513. @ref{call-arguments-limit}
  1514. @subsubheading Notes::
  1515. Implementors are encouraged to make the @i{value} of
  1516. @b{lambda-parameters-limit} as large as possible.
  1517. @node defconstant, defparameter, lambda-parameters-limit, Data and Control Flow Dictionary
  1518. @subsection defconstant [Macro]
  1519. @code{defconstant} @i{name initial-value @r{[}documentation@r{]}} @result{} @i{name}
  1520. @subsubheading Arguments and Values::
  1521. @i{name}---a @i{symbol}; not evaluated.
  1522. @i{initial-value}---a @i{form}; evaluated.
  1523. @i{documentation}---a @i{string}; not evaluated.
  1524. @subsubheading Description::
  1525. @b{defconstant}
  1526. causes the global variable named by @i{name} to be
  1527. given a value that is the result of evaluating @i{initial-value}.
  1528. A constant defined by @b{defconstant} can be redefined
  1529. with @b{defconstant}.
  1530. However, the consequences are undefined if an attempt is made to assign
  1531. a @i{value} to the @i{symbol} using another operator, or to
  1532. assign it to a @i{different}
  1533. @i{value} using a subsequent
  1534. @b{defconstant}.
  1535. If @i{documentation} is supplied, it is attached to @i{name} as a
  1536. @i{documentation string} of kind @b{variable}.
  1537. @b{defconstant}
  1538. normally appears as a @i{top level form}, but it is meaningful
  1539. for it to appear as a @i{non-top-level form}.
  1540. However, the compile-time side
  1541. effects described below
  1542. only take place when @b{defconstant} appears as a
  1543. @i{top level form}.
  1544. The consequences are undefined if there are any
  1545. @i{bindings}
  1546. of the variable named by @i{name} at the time @b{defconstant}
  1547. is executed or if the value is not @b{eql} to the value of
  1548. @i{initial-value}.
  1549. The consequences are undefined when constant @i{symbols} are rebound
  1550. as either lexical or dynamic variables. In other words, a reference to a
  1551. @i{symbol} declared with @b{defconstant} always refers to its global value.
  1552. The side effects of the execution of @b{defconstant} must
  1553. be equivalent to at least the side effects of the execution of the following
  1554. code:
  1555. @example
  1556. (setf (symbol-value '@i{name}) @i{initial-value})
  1557. (setf (documentation '@i{name} 'variable) '@i{documentation})
  1558. @end example
  1559. If a @b{defconstant} @i{form} appears as a @i{top level form},
  1560. the @i{compiler} must recognize that @i{name} names
  1561. a @i{constant variable}. An implementation may choose to
  1562. evaluate the value-form at compile time, load time, or both.
  1563. Therefore, users must ensure that the @i{initial-value}
  1564. can be @i{evaluated} at compile time
  1565. (regardless of whether or not references to @i{name}
  1566. appear in the file) and that it always @i{evaluates}
  1567. to the same value.
  1568. [Editorial Note by KMP: Does ``same value'' here mean eql or similar?]
  1569. [Reviewer Note by Moon: Probably depends on whether load time is compared to compile time,
  1570. or two compiles.]
  1571. @subsubheading Examples::
  1572. @example
  1573. (defconstant this-is-a-constant 'never-changing "for a test") @result{} THIS-IS-A-CONSTANT
  1574. this-is-a-constant @result{} NEVER-CHANGING
  1575. (documentation 'this-is-a-constant 'variable) @result{} "for a test"
  1576. (constantp 'this-is-a-constant) @result{} @i{true}
  1577. @end example
  1578. @subsubheading See Also::
  1579. @ref{declaim}
  1580. ,
  1581. @ref{defparameter; defvar}
  1582. ,
  1583. @b{defvar},
  1584. @ref{documentation; (setf documentation)}
  1585. ,
  1586. @ref{proclaim}
  1587. ,
  1588. @ref{Constant Variables},
  1589. @ref{Compilation}
  1590. @node defparameter, destructuring-bind, defconstant, Data and Control Flow Dictionary
  1591. @subsection defparameter, defvar [Macro]
  1592. @code{defparameter} @i{name initial-value @r{[}documentation@r{]} } @result{} @i{name}
  1593. @code{defvar} @i{name @t{[}initial-value @r{[}documentation@r{]}@t{]}} @result{} @i{name}
  1594. @subsubheading Arguments and Values::
  1595. @i{name}---a @i{symbol}; not evaluated.
  1596. @i{initial-value}---a @i{form};
  1597. for @b{defparameter}, it is always @i{evaluated},
  1598. but for @b{defvar} it is @i{evaluated}
  1599. only if @i{name} is not already @i{bound}.
  1600. @i{documentation}---a @i{string}; not evaluated.
  1601. @subsubheading Description::
  1602. @b{defparameter} and @b{defvar} @i{establish} @i{name}
  1603. as a @i{dynamic variable}.
  1604. @b{defparameter} unconditionally
  1605. @i{assigns} the @i{initial-value} to the @i{dynamic variable} named @i{name}.
  1606. @b{defvar}, by contrast, @i{assigns} @i{initial-value} (if supplied)
  1607. to the @i{dynamic variable} named @i{name}
  1608. only if @i{name} is not already @i{bound}.
  1609. If no @i{initial-value} is supplied,
  1610. @b{defvar} leaves the @i{value cell} of
  1611. the @i{dynamic variable} named @i{name} undisturbed;
  1612. if @i{name} was previously @i{bound}, its old @i{value} persists,
  1613. and if it was previously @i{unbound}, it remains @i{unbound}.
  1614. If @i{documentation} is supplied, it is attached to @i{name} as a
  1615. @i{documentation string} of kind @b{variable}.
  1616. @b{defparameter} and @b{defvar} normally appear as a @i{top level form},
  1617. but it is meaningful for them to appear as @i{non-top-level forms}. However,
  1618. the compile-time side effects described below only take place when
  1619. they appear as @i{top level forms}.
  1620. @subsubheading Examples::
  1621. @example
  1622. (defparameter *p* 1) @result{} *P*
  1623. *p* @result{} 1
  1624. (constantp '*p*) @result{} @i{false}
  1625. (setq *p* 2) @result{} 2
  1626. (defparameter *p* 3) @result{} *P*
  1627. *p* @result{} 3
  1628. (defvar *v* 1) @result{} *V*
  1629. *v* @result{} 1
  1630. (constantp '*v*) @result{} @i{false}
  1631. (setq *v* 2) @result{} 2
  1632. (defvar *v* 3) @result{} *V*
  1633. *v* @result{} 2
  1634. (defun foo ()
  1635. (let ((*p* 'p) (*v* 'v))
  1636. (bar))) @result{} FOO
  1637. (defun bar () (list *p* *v*)) @result{} BAR
  1638. (foo) @result{} (P V)
  1639. @end example
  1640. The principal operational distinction between @b{defparameter} and @b{defvar}
  1641. is that @b{defparameter} makes an unconditional assignment to @i{name},
  1642. while @b{defvar} makes a conditional one. In practice, this means that
  1643. @b{defparameter} is useful in situations where loading or reloading the definition
  1644. would want to pick up a new value of the variable, while @b{defvar} is used in
  1645. situations where the old value would want to be retained if the file were loaded or reloaded.
  1646. For example, one might create a file which contained:
  1647. @example
  1648. (defvar *the-interesting-numbers* '())
  1649. (defmacro define-interesting-number (name n)
  1650. `(progn (defvar ,name ,n)
  1651. (pushnew ,name *the-interesting-numbers*)
  1652. ',name))
  1653. (define-interesting-number *my-height* 168) ;cm
  1654. (define-interesting-number *my-weight* 13) ;stones
  1655. @end example
  1656. Here the initial value, @t{()}, for the variable @t{*the-interesting-numbers*}
  1657. is just a seed that we are never likely to want to reset to something else
  1658. once something has been grown from it. As such, we have used @b{defvar}
  1659. to avoid having the @t{*interesting-numbers*} information reset if the file is
  1660. loaded a second time. It is true that the two calls to
  1661. @b{define-interesting-number} here would be reprocessed, but
  1662. if there were additional calls in another file, they would not be and that
  1663. information would be lost. On the other hand, consider the following code:
  1664. @example
  1665. (defparameter *default-beep-count* 3)
  1666. (defun beep (&optional (n *default-beep-count*))
  1667. (dotimes (i n) (si:
  1668. @end example
  1669. Here we could easily imagine editing the code to change the initial value of
  1670. @t{*default-beep-count*}, and then reloading the file to pick up the new value.
  1671. In order to make value updating easy, we have used @b{defparameter}.
  1672. On the other hand, there is potential value to using @b{defvar} in this
  1673. situation. For example, suppose that someone had predefined an alternate
  1674. value for @t{*default-beep-count*}, or had loaded the file and then manually
  1675. changed the value. In both cases, if we had used @b{defvar} instead of
  1676. @b{defparameter}, those user preferences would not be overridden by
  1677. (re)loading the file.
  1678. The choice of whether to use @b{defparameter} or @b{defvar} has
  1679. visible consequences to programs, but is nevertheless often made for subjective
  1680. reasons.
  1681. @subsubheading Side Effects::
  1682. If a @b{defvar} or @b{defparameter} @i{form} appears as a @i{top level form},
  1683. the @i{compiler} must recognize that the @i{name} has been
  1684. proclaimed @b{special}. However, it must neither @i{evaluate}
  1685. the @i{initial-value} @i{form} nor @i{assign} the
  1686. @i{dynamic variable} named @i{name} at compile time.
  1687. There may be additional (@i{implementation-defined}) compile-time or
  1688. run-time side effects, as long as such effects do not interfere with the
  1689. correct operation of @i{conforming programs}.
  1690. @subsubheading Affected By::
  1691. @b{defvar} is affected by whether @i{name} is already @i{bound}.
  1692. @subsubheading See Also::
  1693. @ref{declaim}
  1694. ,
  1695. @ref{defconstant}
  1696. ,
  1697. @ref{documentation; (setf documentation)}
  1698. ,
  1699. @ref{Compilation}
  1700. @subsubheading Notes::
  1701. It is customary to name @i{dynamic variables} with an @i{asterisk}
  1702. at the beginning and end of the name. e.g., @t{*foo*} is a good name for
  1703. a @i{dynamic variable}, but not for a @i{lexical variable};
  1704. @t{foo} is a good name for a @i{lexical variable},
  1705. but not for a @i{dynamic variable}.
  1706. This naming convention is observed for all @i{defined names} in @r{Common Lisp};
  1707. however, neither @i{conforming programs} nor @i{conforming implementations}
  1708. are obliged to adhere to this convention.
  1709. The intent of the permission for additional side effects is to allow
  1710. @i{implementations} to do normal ``bookkeeping'' that accompanies
  1711. definitions. For example, the @i{macro expansion} of a @b{defvar}
  1712. or @b{defparameter} @i{form} might include code that arranges to
  1713. record the name of the source file in which the definition occurs.
  1714. @b{defparameter} and @b{defvar} might be defined as follows:
  1715. @example
  1716. (defmacro defparameter (name initial-value
  1717. &optional (documentation nil documentation-p))
  1718. `(progn (declaim (special ,name))
  1719. (setf (symbol-value ',name) ,initial-value)
  1720. ,(when documentation-p
  1721. `(setf (documentation ',name 'variable) ',documentation))
  1722. ',name))
  1723. (defmacro defvar (name &optional
  1724. (initial-value nil initial-value-p)
  1725. (documentation nil documentation-p))
  1726. `(progn (declaim (special ,name))
  1727. ,(when initial-value-p
  1728. `(unless (boundp ',name)
  1729. (setf (symbol-value ',name) ,initial-value)))
  1730. ,(when documentation-p
  1731. `(setf (documentation ',name 'variable) ',documentation))
  1732. ',name))
  1733. @end example
  1734. @node destructuring-bind, let, defparameter, Data and Control Flow Dictionary
  1735. @subsection destructuring-bind [Macro]
  1736. @code{destructuring-bind} @i{lambda-list expression @{@i{declaration}@}{*} @{@i{form}@}{*}}@*
  1737. @result{} @i{@{@i{result}@}{*}}
  1738. @subsubheading Arguments and Values::
  1739. @i{lambda-list}---a @i{destructuring lambda list}.
  1740. @i{expression}---a @i{form}.
  1741. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  1742. @i{forms}---an @i{implicit progn}.
  1743. @i{results}---the @i{values} returned by the @i{forms}.
  1744. @subsubheading Description::
  1745. @b{destructuring-bind} binds the variables specified in @i{lambda-list}
  1746. to the corresponding values in the tree structure resulting from the evaluation
  1747. of @i{expression}; then @b{destructuring-bind} evaluates @i{forms}.
  1748. The @i{lambda-list} supports destructuring as described in
  1749. @ref{Destructuring Lambda Lists}.
  1750. @subsubheading Examples::
  1751. @example
  1752. (defun iota (n) (loop for i from 1 to n collect i)) ;helper
  1753. (destructuring-bind ((a &optional (b 'bee)) one two three)
  1754. `((alpha) ,@@(iota 3))
  1755. (list a b three two one)) @result{} (ALPHA BEE 3 2 1)
  1756. @end example
  1757. @subsubheading Exceptional Situations::
  1758. If the result of evaluating the @i{expression} does not match the
  1759. destructuring pattern, an error of @i{type} @b{error} should be signaled.
  1760. @subsubheading See Also::
  1761. @b{macrolet},
  1762. @ref{defmacro}
  1763. @node let, progv, destructuring-bind, Data and Control Flow Dictionary
  1764. @subsection let, let* [Special Operator]
  1765. @code{let} @i{@r{(}@{@i{var} | @r{(}@i{var} @r{[}@i{init-form}@r{]}@r{)}@}{*}@r{)} @{@i{declaration}@}{*} @{@i{form}@}{*}} @result{} @i{@{@i{result}@}{*}}
  1766. @code{let*} @i{@r{(}@{@i{var} | @r{(}@i{var} @r{[}@i{init-form}@r{]}@r{)}@}{*}@r{)} @{@i{declaration}@}{*} @{@i{form}@}{*}} @result{} @i{@{@i{result}@}{*}}
  1767. @subsubheading Arguments and Values::
  1768. @i{var}---a @i{symbol}.
  1769. @i{init-form}---a @i{form}.
  1770. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  1771. @i{form}---a @i{form}.
  1772. @i{results}---the @i{values} returned by the @i{forms}.
  1773. @subsubheading Description::
  1774. @b{let} and @b{let*}
  1775. create new variable @i{bindings} and
  1776. execute a series of @i{forms} that use these @i{bindings}.
  1777. @b{let} performs the @i{bindings} in parallel and
  1778. @b{let*} does them sequentially.
  1779. The form
  1780. @example
  1781. (let ((@i{var1} @i{init-form-1})
  1782. (@i{var2} @i{init-form-2})
  1783. ...
  1784. (@i{varm} @i{init-form-m}))
  1785. @i{declaration1}
  1786. @i{declaration2}
  1787. ...
  1788. @i{declarationp}
  1789. @i{form1}
  1790. @i{form2}
  1791. ...
  1792. @i{formn})
  1793. @end example
  1794. first evaluates the expressions @i{init-form-1}, @i{init-form-2}, and so on,
  1795. in that order, saving the resulting values.
  1796. Then all of the variables @i{varj} are bound to the corresponding
  1797. values; each @i{binding} is lexical unless
  1798. there is a @b{special} declaration to the contrary.
  1799. The expressions @i{formk} are then evaluated
  1800. in order; the values of all but the last are discarded
  1801. (that is, the body of a @b{let}
  1802. is an @i{implicit progn}).
  1803. @b{let*}
  1804. is similar to @b{let}, but the @i{bindings} of variables
  1805. are performed sequentially rather than in parallel.
  1806. The expression for the @i{init-form} of a
  1807. @i{var} can refer to @i{vars}
  1808. previously bound in the @b{let*}.
  1809. The form
  1810. @example
  1811. (let* ((@i{var1} @i{init-form-1})
  1812. (@i{var2} @i{init-form-2})
  1813. ...
  1814. (@i{varm} @i{init-form-m}))
  1815. @i{declaration1}
  1816. @i{declaration2}
  1817. ...
  1818. @i{declarationp}
  1819. @i{form1}
  1820. @i{form2}
  1821. ...
  1822. @i{formn})
  1823. @end example
  1824. first evaluates the expression @i{init-form-1}, then binds the variable
  1825. @i{var1} to that value; then it evaluates @i{init-form-2} and binds
  1826. @i{var2}, and so on.
  1827. The expressions @i{formj} are then evaluated
  1828. in order; the values of all but the last are discarded
  1829. (that is, the body of @b{let*} is an implicit @b{progn}).
  1830. For both @b{let} and @b{let*},
  1831. if there is not an @i{init-form} associated with a @i{var},
  1832. @i{var} is initialized to @b{nil}.
  1833. The special form @b{let}
  1834. has the property that the @i{scope}
  1835. of the name binding does not include any
  1836. initial value form.
  1837. For @b{let*}, a variable's @i{scope} also includes the
  1838. remaining initial value forms for subsequent variable bindings.
  1839. @subsubheading Examples::
  1840. @example
  1841. (setq a 'top) @result{} TOP
  1842. (defun dummy-function () a) @result{} DUMMY-FUNCTION
  1843. (let ((a 'inside) (b a))
  1844. (format nil "~S ~S ~S" a b (dummy-function))) @result{} "INSIDE TOP TOP"
  1845. (let* ((a 'inside) (b a))
  1846. (format nil "~S ~S ~S" a b (dummy-function))) @result{} "INSIDE INSIDE TOP"
  1847. (let ((a 'inside) (b a))
  1848. (declare (special a))
  1849. (format nil "~S ~S ~S" a b (dummy-function))) @result{} "INSIDE TOP INSIDE"
  1850. @end example
  1851. The code
  1852. @example
  1853. (let (x)
  1854. (declare (integer x))
  1855. (setq x (gcd y z))
  1856. ...)
  1857. @end example
  1858. is incorrect; although @t{x} is indeed set before it is used,
  1859. and is set to a value of the declared type @i{integer}, nevertheless
  1860. @t{x} initially takes on the value @b{nil} in violation of the type
  1861. declaration.
  1862. @subsubheading See Also::
  1863. @ref{progv}
  1864. @node progv, setq, let, Data and Control Flow Dictionary
  1865. @subsection progv [Special Operator]
  1866. @code{progv} @i{@i{symbols} @i{values} @{@i{form}@}{*}} @result{} @i{@{@i{result}@}{*}}
  1867. @subsubheading Arguments and Values::
  1868. @i{symbols}---a @i{list} of @i{symbols}; evaluated.
  1869. @i{values}---a @i{list} of @i{objects}; evaluated.
  1870. @i{forms}---an @i{implicit progn}.
  1871. @i{results}---the @i{values} returned by the @i{forms}.
  1872. @subsubheading Description::
  1873. @b{progv} creates new dynamic variable @i{bindings} and
  1874. executes each @i{form} using those @i{bindings}.
  1875. Each @i{form} is evaluated in order.
  1876. @b{progv} allows @i{binding} one or more dynamic
  1877. variables whose names may be determined at run time.
  1878. Each @i{form} is evaluated in order
  1879. with the dynamic variables whose names are in
  1880. @i{symbols} bound to corresponding @i{values}.
  1881. If too few @i{values}
  1882. are supplied, the remaining @i{symbols} are bound and then
  1883. made to have no value. If too many @i{values} are
  1884. supplied, the excess values are ignored.
  1885. The @i{bindings} of the dynamic variables are undone on
  1886. exit from @b{progv}.
  1887. @subsubheading Examples::
  1888. @example
  1889. (setq *x* 1) @result{} 1
  1890. (progv '(*x*) '(2) *x*) @result{} 2
  1891. *x* @result{} 1
  1892. Assuming *x* is not globally special,
  1893. (let ((*x* 3))
  1894. (progv '(*x*) '(4)
  1895. (list *x* (symbol-value '*x*)))) @result{} (3 4)
  1896. @end example
  1897. @subsubheading See Also::
  1898. @ref{let; let*}
  1899. , @ref{Evaluation}
  1900. @subsubheading Notes::
  1901. Among other things, @b{progv} is useful when writing
  1902. interpreters for languages embedded in @r{Lisp}; it provides a handle
  1903. on the mechanism for @i{binding} @i{dynamic variables}.
  1904. @node setq, psetq, progv, Data and Control Flow Dictionary
  1905. @subsection setq [Special Form]
  1906. @code{setq} @i{@{!@i{pair}@}{*}} @result{} @i{result}
  1907. @w{@i{pair} ::=var form}
  1908. @subsubheading Pronunciation::
  1909. pronounced 'set ,ky\"u
  1910. @subsubheading Arguments and Values::
  1911. @i{var}---a @i{symbol} naming a @i{variable} other than a @i{constant variable}.
  1912. @i{form}---a @i{form}.
  1913. @i{result}---the @i{primary value} of the last @i{form},
  1914. or @b{nil} if no @i{pairs} were supplied.
  1915. @subsubheading Description::
  1916. Assigns values to @i{variables}.
  1917. @t{(setq @i{var1} @i{form1} @i{var2} @i{form2} ...)}
  1918. is the simple variable assignment statement of @r{Lisp}.
  1919. First @i{form1} is evaluated
  1920. and the result is stored in the variable @i{var1}, then @i{form2}
  1921. is evaluated and the result stored in @i{var2}, and so forth.
  1922. @b{setq} may be used for assignment of both lexical
  1923. and dynamic variables.
  1924. If any @i{var} refers to a @i{binding}
  1925. made by @b{symbol-macrolet},
  1926. then that @i{var} is treated as if @b{setf}
  1927. (not @b{setq}) had been used.
  1928. @subsubheading Examples::
  1929. @example
  1930. ;; A simple use of SETQ to establish values for variables.
  1931. (setq a 1 b 2 c 3) @result{} 3
  1932. a @result{} 1
  1933. b @result{} 2
  1934. c @result{} 3
  1935. ;; Use of SETQ to update values by sequential assignment.
  1936. (setq a (1+ b) b (1+ a) c (+ a b)) @result{} 7
  1937. a @result{} 3
  1938. b @result{} 4
  1939. c @result{} 7
  1940. ;; This illustrates the use of SETQ on a symbol macro.
  1941. (let ((x (list 10 20 30)))
  1942. (symbol-macrolet ((y (car x)) (z (cadr x)))
  1943. (setq y (1+ z) z (1+ y))
  1944. (list x y z)))
  1945. @result{} ((21 22 30) 21 22)
  1946. @end example
  1947. @subsubheading Side Effects::
  1948. The @i{primary value} of each @i{form} is assigned to the corresponding @i{var}.
  1949. @subsubheading See Also::
  1950. @ref{psetq}
  1951. ,
  1952. @ref{set}
  1953. ,
  1954. @ref{setf; psetf}
  1955. @node psetq, block, setq, Data and Control Flow Dictionary
  1956. @subsection psetq [Macro]
  1957. @code{psetq} @i{@{!@i{pair}@}{*}} @result{} @i{@b{nil}}
  1958. @w{@i{pair} ::=var form}
  1959. @subsubheading Pronunciation::
  1960. @b{psetq}: pronounced {{{\vrule width 1pt height 2pt depth 2pt}\kern -1pt\raise 6pt{\vrule width 1pt height 2pt depth 2pt}}}p\=e'set ,ky\"u
  1961. @subsubheading Arguments and Values::
  1962. @i{var}---a @i{symbol} naming a @i{variable} other than a @i{constant variable}.
  1963. @i{form}---a @i{form}.
  1964. @subsubheading Description::
  1965. Assigns values to @i{variables}.
  1966. This is just like @b{setq}, except that the assignments
  1967. happen ``in parallel.'' That is, first all of the forms are
  1968. evaluated, and only then are the variables set to the resulting values.
  1969. In this way, the assignment to one variable does not affect the value
  1970. computation of another in the way that would occur with @b{setq}'s
  1971. sequential assignment.
  1972. If any @i{var} refers to a @i{binding}
  1973. made by @b{symbol-macrolet},
  1974. then that @i{var} is treated as if @b{psetf} (not @b{psetq})
  1975. had been used.
  1976. @subsubheading Examples::
  1977. @example
  1978. ;; A simple use of PSETQ to establish values for variables.
  1979. ;; As a matter of style, many programmers would prefer SETQ
  1980. ;; in a simple situation like this where parallel assignment
  1981. ;; is not needed, but the two have equivalent effect.
  1982. (psetq a 1 b 2 c 3) @result{} NIL
  1983. a @result{} 1
  1984. b @result{} 2
  1985. c @result{} 3
  1986. ;; Use of PSETQ to update values by parallel assignment.
  1987. ;; The effect here is very different than if SETQ had been used.
  1988. (psetq a (1+ b) b (1+ a) c (+ a b)) @result{} NIL
  1989. a @result{} 3
  1990. b @result{} 2
  1991. c @result{} 3
  1992. ;; Use of PSETQ on a symbol macro.
  1993. (let ((x (list 10 20 30)))
  1994. (symbol-macrolet ((y (car x)) (z (cadr x)))
  1995. (psetq y (1+ z) z (1+ y))
  1996. (list x y z)))
  1997. @result{} ((21 11 30) 21 11)
  1998. ;; Use of parallel assignment to swap values of A and B.
  1999. (let ((a 1) (b 2))
  2000. (psetq a b b a)
  2001. (values a b))
  2002. @result{} 2, 1
  2003. @end example
  2004. @subsubheading Side Effects::
  2005. The values of @i{forms} are assigned to @i{vars}.
  2006. @subsubheading See Also::
  2007. @b{psetf},
  2008. @ref{setq}
  2009. @node block, catch, psetq, Data and Control Flow Dictionary
  2010. @subsection block [Special Operator]
  2011. @code{block} @i{@i{name} @i{form}@r{*}} @result{} @i{@{@i{result}@}{*}}
  2012. @subsubheading Arguments and Values::
  2013. @i{name}---a @i{symbol}.
  2014. @i{form}---a @i{form}.
  2015. @i{results}---the @i{values} of the @i{forms} if a @i{normal return} occurs,
  2016. or else, if an @i{explicit return} occurs, the @i{values} that were transferred.
  2017. @subsubheading Description::
  2018. @b{block} @i{establishes} a @i{block} named @i{name}
  2019. and then evaluates @i{forms} as an @i{implicit progn}.
  2020. The @i{special operators} @b{block} and @b{return-from} work together to
  2021. provide a structured, lexical, non-local exit facility. At any point lexically
  2022. contained within @i{forms}, @b{return-from} can be used with the
  2023. given @i{name} to return control and values from the @b{block}
  2024. @i{form}, except when an intervening @i{block} with the same name
  2025. has been @i{established}, in which case the outer @i{block} is
  2026. shadowed by the inner one.
  2027. The @i{block} named @i{name} has
  2028. @i{lexical scope} and @i{dynamic extent}.
  2029. Once established, a @i{block} may only be exited once,
  2030. whether by @i{normal return} or @i{explicit return}.
  2031. @subsubheading Examples::
  2032. @example
  2033. (block empty) @result{} NIL
  2034. (block whocares (values 1 2) (values 3 4)) @result{} 3, 4
  2035. (let ((x 1))
  2036. (block stop (setq x 2) (return-from stop) (setq x 3))
  2037. x) @result{} 2
  2038. (block early (return-from early (values 1 2)) (values 3 4)) @result{} 1, 2
  2039. (block outer (block inner (return-from outer 1)) 2) @result{} 1
  2040. (block twin (block twin (return-from twin 1)) 2) @result{} 2
  2041. ;; Contrast behavior of this example with corresponding example of CATCH.
  2042. (block b
  2043. (flet ((b1 () (return-from b 1)))
  2044. (block b (b1) (print 'unreachable))
  2045. 2)) @result{} 1
  2046. @end example
  2047. @subsubheading See Also::
  2048. @ref{return}
  2049. ,
  2050. @ref{return-from}
  2051. , @ref{Evaluation}
  2052. @subsubheading Notes::
  2053. @node catch, go, block, Data and Control Flow Dictionary
  2054. @subsection catch [Special Operator]
  2055. @code{catch} @i{@i{tag} @{@i{form}@}{*}} @result{} @i{@{@i{result}@}{*}}
  2056. @subsubheading Arguments and Values::
  2057. @i{tag}---a @i{catch tag}; evaluated.
  2058. @i{forms}---an @i{implicit progn}.
  2059. @i{results}---if the @i{forms} exit normally,
  2060. the @i{values} returned by the @i{forms};
  2061. if a throw occurs to the @i{tag},
  2062. the @i{values} that are thrown.
  2063. @subsubheading Description::
  2064. @b{catch} is used as the destination of a non-local
  2065. control transfer by @b{throw}.
  2066. @i{Tags} are used to find the @b{catch}
  2067. to which a @b{throw} is transferring control.
  2068. @t{(catch 'foo @i{form})} catches a
  2069. @t{(throw 'foo @i{form})} but not a
  2070. @t{(throw 'bar @i{form})}.
  2071. The order of execution of @b{catch} follows:
  2072. @ITindex{order of evaluation}
  2073. @ITindex{evaluation order}
  2074. @table @asis
  2075. @item 1.
  2076. @i{Tag} is evaluated.
  2077. It serves as the name of the
  2078. @b{catch}.
  2079. @item 2.
  2080. @i{Forms} are then evaluated as an implicit @b{progn},
  2081. and the results of the last @i{form} are returned unless a
  2082. @b{throw} occurs.
  2083. @item 3.
  2084. If a @b{throw} occurs
  2085. during the execution of one of the @i{forms}, control
  2086. is transferred to the @b{catch} @i{form} whose @i{tag}
  2087. is @b{eq} to
  2088. the tag argument of the @b{throw}
  2089. and which is the most recently established @b{catch} with that
  2090. @i{tag}.
  2091. No further evaluation of @i{forms} occurs.
  2092. @item 4.
  2093. The @i{tag} @i{established}
  2094. by @b{catch} is @i{disestablished}
  2095. just before the results are returned.
  2096. @end table
  2097. If during the execution of one of the @i{forms}, a @b{throw}
  2098. is executed whose tag is @b{eq} to the @b{catch} tag,
  2099. then the values specified by the @b{throw} are
  2100. returned as the result of the dynamically most recently established
  2101. @b{catch} form with that tag.
  2102. The mechanism for @b{catch} and @b{throw} works even
  2103. if @b{throw} is not within the lexical scope of @b{catch}.
  2104. @b{throw} must occur within the @i{dynamic extent}
  2105. of the @i{evaluation} of the body of a @b{catch} with a corresponding @i{tag}.
  2106. @subsubheading Examples::
  2107. @example
  2108. (catch 'dummy-tag 1 2 (throw 'dummy-tag 3) 4) @result{} 3
  2109. (catch 'dummy-tag 1 2 3 4) @result{} 4
  2110. (defun throw-back (tag) (throw tag t)) @result{} THROW-BACK
  2111. (catch 'dummy-tag (throw-back 'dummy-tag) 2) @result{} T
  2112. ;; Contrast behavior of this example with corresponding example of BLOCK.
  2113. (catch 'c
  2114. (flet ((c1 () (throw 'c 1)))
  2115. (catch 'c (c1) (print 'unreachable))
  2116. 2)) @result{} 2
  2117. @end example
  2118. @subsubheading Exceptional Situations::
  2119. An error of @i{type} @b{control-error} is signaled
  2120. if @b{throw} is done
  2121. when there is no suitable @b{catch} @i{tag}.
  2122. @subsubheading See Also::
  2123. @ref{throw}
  2124. , @ref{Evaluation}
  2125. @subsubheading Notes::
  2126. It is customary for @i{symbols} to be used
  2127. as @i{tags}, but any @i{object} is permitted.
  2128. However, numbers should not be
  2129. used because the comparison is done using @b{eq}.
  2130. @b{catch} differs from @b{block} in that
  2131. @b{catch}
  2132. tags have dynamic @i{scope} while
  2133. @b{block} names have @i{lexical scope}.
  2134. @node go, return-from, catch, Data and Control Flow Dictionary
  2135. @subsection go [Special Operator]
  2136. @code{go} @i{tag}
  2137. @result{} #<NoValue>
  2138. @subsubheading Arguments and Values::
  2139. @i{tag}---a @i{go tag}.
  2140. @subsubheading Description::
  2141. @b{go} transfers control to the point in the body
  2142. of an enclosing @b{tagbody} form labeled by a
  2143. tag @b{eql} to @i{tag}.
  2144. If there is no such @i{tag} in the body, the
  2145. bodies of lexically containing @b{tagbody} @i{forms}
  2146. (if any) are examined as well.
  2147. If several tags are @b{eql}
  2148. to @i{tag}, control is transferred to
  2149. whichever matching @i{tag}
  2150. is contained in the innermost @b{tagbody} form that
  2151. contains the @b{go}.
  2152. The consequences are undefined
  2153. if there is no matching @i{tag} lexically visible
  2154. to the point of the @b{go}.
  2155. The transfer of control initiated by @b{go} is performed
  2156. as described in @ref{Transfer of Control to an Exit Point}.
  2157. @subsubheading Examples::
  2158. @example
  2159. (tagbody
  2160. (setq val 2)
  2161. (go lp)
  2162. (incf val 3)
  2163. lp (incf val 4)) @result{} NIL
  2164. val @result{} 6
  2165. @end example
  2166. The following is in error because there is a normal exit
  2167. of the @b{tagbody} before the
  2168. @b{go} is executed.
  2169. @example
  2170. (let ((a nil))
  2171. (tagbody t (setq a #'(lambda () (go t))))
  2172. (funcall a))
  2173. @end example
  2174. The following is in error because the @b{tagbody} is passed over
  2175. before the @b{go} @i{form} is executed.
  2176. @example
  2177. (funcall (block nil
  2178. (tagbody a (return #'(lambda () (go a))))))
  2179. @end example
  2180. @subsubheading See Also::
  2181. @ref{tagbody}
  2182. @node return-from, return, go, Data and Control Flow Dictionary
  2183. @subsection return-from [Special Operator]
  2184. @code{return-from} @i{@i{name} @r{[}@i{result}@r{]}}
  2185. @result{} #<NoValue>
  2186. @subsubheading Arguments and Values::
  2187. @i{name}---a @i{block tag}; not evaluated.
  2188. @i{result}---a @i{form}; evaluated.
  2189. The default is @b{nil}.
  2190. @subsubheading Description::
  2191. Returns control and @i{multiple values}_2 from a lexically enclosing @i{block}.
  2192. A @b{block} @i{form} named @i{name} must lexically enclose
  2193. the occurrence of @b{return-from}; any @i{values} @i{yielded}
  2194. by the @i{evaluation} of @i{result} are immediately returned from
  2195. the innermost such lexically enclosing @i{block}.
  2196. The transfer of control initiated by @b{return-from} is performed
  2197. as described in @ref{Transfer of Control to an Exit Point}.
  2198. @subsubheading Examples::
  2199. @example
  2200. (block alpha (return-from alpha) 1) @result{} NIL
  2201. (block alpha (return-from alpha 1) 2) @result{} 1
  2202. (block alpha (return-from alpha (values 1 2)) 3) @result{} 1, 2
  2203. (let ((a 0))
  2204. (dotimes (i 10) (incf a) (when (oddp i) (return)))
  2205. a) @result{} 2
  2206. (defun temp (x)
  2207. (if x (return-from temp 'dummy))
  2208. 44) @result{} TEMP
  2209. (temp nil) @result{} 44
  2210. (temp t) @result{} DUMMY
  2211. (block out
  2212. (flet ((exit (n) (return-from out n)))
  2213. (block out (exit 1)))
  2214. 2) @result{} 1
  2215. (block nil
  2216. (unwind-protect (return-from nil 1)
  2217. (return-from nil 2)))
  2218. @result{} 2
  2219. (dolist (flag '(nil t))
  2220. (block nil
  2221. (let ((x 5))
  2222. (declare (special x))
  2223. (unwind-protect (return-from nil)
  2224. (print x))))
  2225. (print 'here))
  2226. @t{ |> } 5
  2227. @t{ |> } HERE
  2228. @t{ |> } 5
  2229. @t{ |> } HERE
  2230. @result{} NIL
  2231. (dolist (flag '(nil t))
  2232. (block nil
  2233. (let ((x 5))
  2234. (declare (special x))
  2235. (unwind-protect
  2236. (if flag (return-from nil))
  2237. (print x))))
  2238. (print 'here))
  2239. @t{ |> } 5
  2240. @t{ |> } HERE
  2241. @t{ |> } 5
  2242. @t{ |> } HERE
  2243. @result{} NIL
  2244. @end example
  2245. The following has undefined consequences because the @b{block} @i{form}
  2246. exits normally before the @b{return-from} @i{form} is attempted.
  2247. @example
  2248. (funcall (block nil #'(lambda () (return-from nil)))) is an error.
  2249. @end example
  2250. @subsubheading See Also::
  2251. @ref{block}
  2252. ,
  2253. @ref{return}
  2254. ,
  2255. @ref{Evaluation}
  2256. @node return, tagbody, return-from, Data and Control Flow Dictionary
  2257. @subsection return [Macro]
  2258. @code{return} @i{@r{[}@i{result}@r{]}} @result{} #<NoValue>
  2259. @subsubheading Arguments and Values::
  2260. @i{result}---a @i{form}; evaluated.
  2261. The default is @b{nil}.
  2262. @subsubheading Description::
  2263. Returns, as if by @b{return-from}, from the @i{block} named @b{nil}.
  2264. @subsubheading Examples::
  2265. @example
  2266. (block nil (return) 1) @result{} NIL
  2267. (block nil (return 1) 2) @result{} 1
  2268. (block nil (return (values 1 2)) 3) @result{} 1, 2
  2269. (block nil (block alpha (return 1) 2)) @result{} 1
  2270. (block alpha (block nil (return 1)) 2) @result{} 2
  2271. (block nil (block nil (return 1) 2)) @result{} 1
  2272. @end example
  2273. @subsubheading See Also::
  2274. @ref{block}
  2275. ,
  2276. @ref{return-from}
  2277. ,
  2278. @ref{Evaluation}
  2279. @subsubheading Notes::
  2280. @example
  2281. (return) @equiv{} (return-from nil)
  2282. (return @i{form}) @equiv{} (return-from nil @i{form})
  2283. @end example
  2284. The @i{implicit blocks} @i{established} by @i{macros} such as @b{do}
  2285. are often named @b{nil}, so that @b{return} can be used to exit from
  2286. such @i{forms}.
  2287. @node tagbody, throw, return, Data and Control Flow Dictionary
  2288. @subsection tagbody [Special Operator]
  2289. @code{tagbody} @i{@{@i{tag} | @i{statement}@}{*}} @result{} @i{@b{nil}}
  2290. @subsubheading Arguments and Values::
  2291. @i{tag}---a @i{go tag}; not evaluated.
  2292. @i{statement}---a @i{compound form}; evaluated as described below.
  2293. @subsubheading Description::
  2294. Executes zero or more @i{statements} in a
  2295. @i{lexical environment}
  2296. that provides for control transfers to labels indicated by the @i{tags}.
  2297. The @i{statements} in a @b{tagbody} are @i{evaluated} in order
  2298. from left to right, and their @i{values} are discarded. If at any time
  2299. there are no remaining @i{statements}, @b{tagbody} returns @b{nil}.
  2300. However, if @t{(go @i{tag})} is @i{evaluated}, control jumps to the
  2301. part of the body labeled with the @i{tag}. (Tags are compared with @b{eql}.)
  2302. A @i{tag} established by @b{tagbody} has @i{lexical scope}
  2303. and has @i{dynamic extent}. Once @b{tagbody} has been exited,
  2304. it is no longer valid to @b{go} to a @i{tag} in its body.
  2305. It is permissible for @b{go} to jump to a @b{tagbody} that is
  2306. not the innermost @b{tagbody} containing that @b{go};
  2307. the @i{tags} established by a @b{tagbody} only shadow
  2308. other @i{tags} of like name.
  2309. The determination of which elements of the body are @i{tags}
  2310. and which are @i{statements} is made prior to any @i{macro expansion}
  2311. of that element. If a @i{statement} is a @i{macro form} and
  2312. its @i{macro expansion} is an @i{atom}, that @i{atom} is treated
  2313. as a @i{statement}, not a @i{tag}.
  2314. @subsubheading Examples::
  2315. @example
  2316. (let (val)
  2317. (tagbody
  2318. (setq val 1)
  2319. (go point-a)
  2320. (incf val 16)
  2321. point-c
  2322. (incf val 04)
  2323. (go point-b)
  2324. (incf val 32)
  2325. point-a
  2326. (incf val 02)
  2327. (go point-c)
  2328. (incf val 64)
  2329. point-b
  2330. (incf val 08))
  2331. val)
  2332. @result{} 15
  2333. (defun f1 (flag)
  2334. (let ((n 1))
  2335. (tagbody
  2336. (setq n (f2 flag #'(lambda () (go out))))
  2337. out
  2338. (prin1 n))))
  2339. @result{} F1
  2340. (defun f2 (flag escape)
  2341. (if flag (funcall escape) 2))
  2342. @result{} F2
  2343. (f1 nil)
  2344. @t{ |> } 2
  2345. @result{} NIL
  2346. (f1 t)
  2347. @t{ |> } 1
  2348. @result{} NIL
  2349. @end example
  2350. @subsubheading See Also::
  2351. @ref{go}
  2352. @subsubheading Notes::
  2353. The @i{macros} in Figure 5--10 have @i{implicit tagbodies}.
  2354. @group
  2355. @noindent
  2356. @w{ do do-external-symbols dotimes }
  2357. @w{ do* do-symbols prog }
  2358. @w{ do-all-symbols dolist prog* }
  2359. @noindent
  2360. @w{ Figure 5--10: Macros that have implicit tagbodies.}
  2361. @end group
  2362. @node throw, unwind-protect, tagbody, Data and Control Flow Dictionary
  2363. @subsection throw [Special Operator]
  2364. @code{throw} @i{tag result-form}
  2365. @result{} #<NoValue>
  2366. @subsubheading Arguments and Values::
  2367. @i{tag}---a @i{catch tag}; evaluated.
  2368. @i{result-form}---a @i{form}; evaluated as described below.
  2369. @subsubheading Description::
  2370. @b{throw} causes a non-local control transfer
  2371. to a @b{catch} whose tag is @b{eq} to @i{tag}.
  2372. @i{Tag} is evaluated first to produce an @i{object}
  2373. called the throw tag; then @i{result-form} is evaluated,
  2374. and its results are saved. If the @i{result-form} produces
  2375. multiple values, then all the values are saved.
  2376. The most recent outstanding @b{catch}
  2377. whose @i{tag} is @b{eq} to the throw tag
  2378. is exited; the saved results are returned as the value or
  2379. values of @b{catch}.
  2380. The transfer of control initiated by @b{throw} is performed
  2381. as described in @ref{Transfer of Control to an Exit Point}.
  2382. @subsubheading Examples::
  2383. @example
  2384. (catch 'result
  2385. (setq i 0 j 0)
  2386. (loop (incf j 3) (incf i)
  2387. (if (= i 3) (throw 'result (values i j))))) @result{} 3, 9
  2388. @end example
  2389. @example
  2390. (catch nil
  2391. (unwind-protect (throw nil 1)
  2392. (throw nil 2))) @result{} 2
  2393. @end example
  2394. The consequences of the following are undefined
  2395. because the @b{catch} of @t{b}
  2396. is passed over by the first @b{throw},
  2397. hence portable programs must assume that
  2398. its @i{dynamic extent} is terminated.
  2399. The @i{binding} of the @i{catch tag} is not yet @i{disestablished}
  2400. and therefore it is the target of the second @b{throw}.
  2401. @example
  2402. (catch 'a
  2403. (catch 'b
  2404. (unwind-protect (throw 'a 1)
  2405. (throw 'b 2))))
  2406. @end example
  2407. The following prints ``@t{The inner catch returns :SECOND-THROW}''
  2408. and then returns @t{:outer-catch}.
  2409. @example
  2410. (catch 'foo
  2411. (format t "The inner catch returns ~s.~
  2412. (catch 'foo
  2413. (unwind-protect (throw 'foo :first-throw)
  2414. (throw 'foo :second-throw))))
  2415. :outer-catch)
  2416. @t{ |> } The inner catch returns :SECOND-THROW
  2417. @result{} :OUTER-CATCH
  2418. @end example
  2419. @subsubheading Exceptional Situations::
  2420. If there is no outstanding @i{catch tag} that matches the throw tag,
  2421. no unwinding of the stack is performed,
  2422. and an error of @i{type} @b{control-error} is signaled.
  2423. When the error is signaled,
  2424. the @i{dynamic environment} is that which was
  2425. in force at the point of the @b{throw}.
  2426. @subsubheading See Also::
  2427. @ref{block}
  2428. ,
  2429. @ref{catch}
  2430. ,
  2431. @ref{return-from}
  2432. ,
  2433. @ref{unwind-protect}
  2434. ,
  2435. @ref{Evaluation}
  2436. @subsubheading Notes::
  2437. @b{catch} and @b{throw} are normally used when the @i{exit point}
  2438. must have @i{dynamic scope} (@i{e.g.}, the @b{throw} is not lexically enclosed
  2439. by the @b{catch}), while @b{block} and @b{return} are used
  2440. when @i{lexical scope} is sufficient.
  2441. @node unwind-protect, nil, throw, Data and Control Flow Dictionary
  2442. @subsection unwind-protect [Special Operator]
  2443. @code{unwind-protect} @i{@i{protected-form} @{@i{cleanup-form}@}{*}} @result{} @i{@{@i{result}@}{*}}
  2444. @subsubheading Arguments and Values::
  2445. @i{protected-form}---a @i{form}.
  2446. @i{cleanup-form}---a @i{form}.
  2447. @i{results}---the @i{values} of the @i{protected-form}.
  2448. @subsubheading Description::
  2449. @b{unwind-protect} evaluates @i{protected-form}
  2450. and guarantees that @i{cleanup-forms} are executed
  2451. before @b{unwind-protect} exits,
  2452. whether it terminates
  2453. normally or is aborted by a control transfer of some kind.
  2454. @b{unwind-protect} is intended to be used
  2455. to make sure that
  2456. certain side effects take place after the evaluation of
  2457. @i{protected-form}.
  2458. If a @i{non-local exit} occurs during execution of @i{cleanup-forms},
  2459. no special action is taken. The @i{cleanup-forms} of
  2460. @b{unwind-protect}
  2461. are not protected by that @b{unwind-protect}.
  2462. @b{unwind-protect} protects against all attempts to exit
  2463. from @i{protected-form}, including
  2464. @b{go},
  2465. @b{handler-case},
  2466. @b{ignore-errors},
  2467. @b{restart-case},
  2468. @b{return-from},
  2469. @b{throw},
  2470. and @b{with-simple-restart}.
  2471. Undoing of @i{handler} and @i{restart} @i{bindings} during an exit
  2472. happens in parallel with the undoing of the bindings of @i{dynamic variables}
  2473. and @b{catch} tags, in the reverse order in which they were established.
  2474. The effect of this is that @i{cleanup-form} sees the same @i{handler}
  2475. and @i{restart} @i{bindings}, as well as @i{dynamic variable} @i{bindings}
  2476. and @b{catch} tags, as were visible when the @b{unwind-protect} was entered.
  2477. @subsubheading Examples::
  2478. @example
  2479. (tagbody
  2480. (let ((x 3))
  2481. (unwind-protect
  2482. (if (numberp x) (go out))
  2483. (print x)))
  2484. out
  2485. ...)
  2486. @end example
  2487. When @b{go} is executed, the call to @b{print} is executed first,
  2488. and then the transfer of control to the tag @t{out} is completed.
  2489. @example
  2490. (defun dummy-function (x)
  2491. (setq state 'running)
  2492. (unless (numberp x) (throw 'abort 'not-a-number))
  2493. (setq state (1+ x))) @result{} DUMMY-FUNCTION
  2494. (catch 'abort (dummy-function 1)) @result{} 2
  2495. state @result{} 2
  2496. (catch 'abort (dummy-function 'trash)) @result{} NOT-A-NUMBER
  2497. state @result{} RUNNING
  2498. (catch 'abort (unwind-protect (dummy-function 'trash)
  2499. (setq state 'aborted))) @result{} NOT-A-NUMBER
  2500. state @result{} ABORTED
  2501. @end example
  2502. The following code
  2503. is not correct:
  2504. @example
  2505. (unwind-protect
  2506. (progn (incf *access-count*)
  2507. (perform-access))
  2508. (decf *access-count*))
  2509. @end example
  2510. If an exit occurs before completion of @b{incf},
  2511. the @b{decf} @i{form} is executed anyway, resulting in an
  2512. incorrect value for @t{*access-count*}.
  2513. The correct way to code this is as follows:
  2514. @example
  2515. (let ((old-count *access-count*))
  2516. (unwind-protect
  2517. (progn (incf *access-count*)
  2518. (perform-access))
  2519. (setq *access-count* old-count)))
  2520. @end example
  2521. @example
  2522. ;;; The following returns 2.
  2523. (block nil
  2524. (unwind-protect (return 1)
  2525. (return 2)))
  2526. ;;; The following has undefined consequences.
  2527. (block a
  2528. (block b
  2529. (unwind-protect (return-from a 1)
  2530. (return-from b 2))))
  2531. ;;; The following returns 2.
  2532. (catch nil
  2533. (unwind-protect (throw nil 1)
  2534. (throw nil 2)))
  2535. ;;; The following has undefined consequences because the catch of B is
  2536. ;;; passed over by the first THROW, hence portable programs must assume
  2537. ;;; its dynamic extent is terminated. The binding of the catch tag is not
  2538. ;;; yet disestablished and therefore it is the target of the second throw.
  2539. (catch 'a
  2540. (catch 'b
  2541. (unwind-protect (throw 'a 1)
  2542. (throw 'b 2))))
  2543. ;;; The following prints "The inner catch returns :SECOND-THROW"
  2544. ;;; and then returns :OUTER-CATCH.
  2545. (catch 'foo
  2546. (format t "The inner catch returns ~s.~
  2547. (catch 'foo
  2548. (unwind-protect (throw 'foo :first-throw)
  2549. (throw 'foo :second-throw))))
  2550. :outer-catch)
  2551. ;;; The following returns 10. The inner CATCH of A is passed over, but
  2552. ;;; because that CATCH is disestablished before the THROW to A is executed,
  2553. ;;; it isn't seen.
  2554. (catch 'a
  2555. (catch 'b
  2556. (unwind-protect (1+ (catch 'a (throw 'b 1)))
  2557. (throw 'a 10))))
  2558. ;;; The following has undefined consequences because the extent of
  2559. ;;; the (CATCH 'BAR ...) exit ends when the (THROW 'FOO ...)
  2560. ;;; commences.
  2561. (catch 'foo
  2562. (catch 'bar
  2563. (unwind-protect (throw 'foo 3)
  2564. (throw 'bar 4)
  2565. (print 'xxx))))
  2566. ;;; The following returns 4; XXX is not printed.
  2567. ;;; The (THROW 'FOO ...) has no effect on the scope of the BAR
  2568. ;;; catch tag or the extent of the (CATCH 'BAR ...) exit.
  2569. (catch 'bar
  2570. (catch 'foo
  2571. (unwind-protect (throw 'foo 3)
  2572. (throw 'bar 4)
  2573. (print 'xxx))))
  2574. ;;; The following prints 5.
  2575. (block nil
  2576. (let ((x 5))
  2577. (declare (special x))
  2578. (unwind-protect (return)
  2579. (print x))))
  2580. @end example
  2581. @subsubheading See Also::
  2582. @ref{catch}
  2583. ,
  2584. @ref{go}
  2585. ,
  2586. @ref{handler-case}
  2587. ,
  2588. @ref{restart-case}
  2589. ,
  2590. @ref{return}
  2591. ,
  2592. @ref{return-from}
  2593. ,
  2594. @ref{throw}
  2595. ,
  2596. @ref{Evaluation}
  2597. @node nil, not, unwind-protect, Data and Control Flow Dictionary
  2598. @subsection nil [Constant Variable]
  2599. @subsubheading Constant Value::
  2600. @b{nil}.
  2601. @subsubheading Description::
  2602. @b{nil} represents both @i{boolean} (and @i{generalized boolean}) @i{false}
  2603. and the @i{empty list}.
  2604. @subsubheading Examples::
  2605. @example
  2606. nil @result{} NIL
  2607. @end example
  2608. @subsubheading See Also::
  2609. @ref{t}
  2610. @node not, t, nil, Data and Control Flow Dictionary
  2611. @subsection not [Function]
  2612. @code{not} @i{x} @result{} @i{boolean}
  2613. @subsubheading Arguments and Values::
  2614. @i{x}---a @i{generalized boolean} (@i{i.e.}, any @i{object}).
  2615. @i{boolean}---a @i{boolean}.
  2616. @subsubheading Description::
  2617. Returns @b{t} if @i{x} is @i{false};
  2618. otherwise, returns @b{nil}.
  2619. @subsubheading Examples::
  2620. @example
  2621. (not nil) @result{} T
  2622. (not '()) @result{} T
  2623. (not (integerp 'sss)) @result{} T
  2624. (not (integerp 1)) @result{} NIL
  2625. (not 3.7) @result{} NIL
  2626. (not 'apple) @result{} NIL
  2627. @end example
  2628. @subsubheading See Also::
  2629. @ref{null}
  2630. @subsubheading Notes::
  2631. @b{not} is intended to be used to invert the `truth value' of a @i{boolean}
  2632. (or @i{generalized boolean})
  2633. whereas @b{null} is intended to be used to test for the @i{empty list}.
  2634. Operationally, @b{not} and @b{null} compute the same result;
  2635. which to use is a matter of style.
  2636. @node t, eq, not, Data and Control Flow Dictionary
  2637. @subsection t [Constant Variable]
  2638. @subsubheading Constant Value::
  2639. @b{t}.
  2640. @subsubheading Description::
  2641. The @i{boolean} representing true,
  2642. and the canonical @i{generalized boolean} representing true.
  2643. Although any @i{object}
  2644. other than @b{nil} is considered @i{true},
  2645. @b{t} is generally used when there is no special reason
  2646. to prefer one such @i{object} over another.
  2647. The @i{symbol} @b{t} is also sometimes used for other purposes as well.
  2648. For example,
  2649. as the @i{name} of a @i{class},
  2650. as a @i{designator} (@i{e.g.}, a @i{stream designator})
  2651. or as a special symbol for some syntactic reason
  2652. (@i{e.g.}, in @b{case} and @b{typecase} to label the @i{otherwise-clause}).
  2653. @subsubheading Examples::
  2654. @example
  2655. t @result{} T
  2656. (eq t 't) @result{} @i{true}
  2657. (find-class 't) @result{} #<CLASS T 610703333>
  2658. (case 'a (a 1) (t 2)) @result{} 1
  2659. (case 'b (a 1) (t 2)) @result{} 2
  2660. (prin1 'hello t)
  2661. @t{ |> } HELLO
  2662. @result{} HELLO
  2663. @end example
  2664. @subsubheading See Also::
  2665. @ref{NIL}
  2666. @node eq, eql, t, Data and Control Flow Dictionary
  2667. @subsection eq [Function]
  2668. @code{eq} @i{x y} @result{} @i{generalized-boolean}
  2669. @subsubheading Arguments and Values::
  2670. @i{x}---an @i{object}.
  2671. @i{y}---an @i{object}.
  2672. @i{generalized-boolean}---a @i{generalized boolean}.
  2673. @subsubheading Description::
  2674. Returns @i{true} if its @i{arguments} are the same, identical @i{object};
  2675. otherwise, returns @i{false}.
  2676. @subsubheading Examples::
  2677. @example
  2678. (eq 'a 'b) @result{} @i{false}
  2679. (eq 'a 'a) @result{} @i{true}
  2680. (eq 3 3)
  2681. @result{} @i{true}
  2682. @i{OR}@result{} @i{false}
  2683. (eq 3 3.0) @result{} @i{false}
  2684. (eq 3.0 3.0)
  2685. @result{} @i{true}
  2686. @i{OR}@result{} @i{false}
  2687. (eq #c(3 -4) #c(3 -4))
  2688. @result{} @i{true}
  2689. @i{OR}@result{} @i{false}
  2690. (eq #c(3 -4.0) #c(3 -4)) @result{} @i{false}
  2691. (eq (cons 'a 'b) (cons 'a 'c)) @result{} @i{false}
  2692. (eq (cons 'a 'b) (cons 'a 'b)) @result{} @i{false}
  2693. (eq '(a . b) '(a . b))
  2694. @result{} @i{true}
  2695. @i{OR}@result{} @i{false}
  2696. (progn (setq x (cons 'a 'b)) (eq x x)) @result{} @i{true}
  2697. (progn (setq x '(a . b)) (eq x x)) @result{} @i{true}
  2698. (eq #\A #\A)
  2699. @result{} @i{true}
  2700. @i{OR}@result{} @i{false}
  2701. (let ((x "Foo")) (eq x x)) @result{} @i{true}
  2702. (eq "Foo" "Foo")
  2703. @result{} @i{true}
  2704. @i{OR}@result{} @i{false}
  2705. (eq "Foo" (copy-seq "Foo")) @result{} @i{false}
  2706. (eq "FOO" "foo") @result{} @i{false}
  2707. (eq "string-seq" (copy-seq "string-seq")) @result{} @i{false}
  2708. (let ((x 5)) (eq x x))
  2709. @result{} @i{true}
  2710. @i{OR}@result{} @i{false}
  2711. @end example
  2712. @subsubheading See Also::
  2713. @ref{eql}
  2714. ,
  2715. @ref{equal}
  2716. ,
  2717. @ref{equalp}
  2718. ,
  2719. @ref{=; /=; <; >; <=; >=}
  2720. ,
  2721. @ref{Compilation}
  2722. @subsubheading Notes::
  2723. @i{Objects} that appear the same when printed are not necessarily
  2724. @b{eq} to each other. @i{Symbols} that print the same
  2725. usually are @b{eq} to each other because of the use of the
  2726. @b{intern} function. However, @i{numbers} with the
  2727. same value need not be @b{eq}, and two similar
  2728. @i{lists} are usually not @i{identical}.
  2729. An implementation is permitted to make ``copies'' of
  2730. @i{characters} and @i{numbers} at any time.
  2731. The effect is that @r{Common Lisp} makes no guarantee that @b{eq}
  2732. is true even when both its arguments are ``the same thing'' if
  2733. that thing is a @i{character} or @i{number}.
  2734. Most @r{Common Lisp} @i{operators} use @b{eql} rather than
  2735. @b{eq} to compare objects, or else they default to @b{eql}
  2736. and only use @b{eq} if specifically requested to do so.
  2737. However, the following @i{operators} are defined to use @b{eq}
  2738. rather than @b{eql} in a way that cannot be overridden by the
  2739. @i{code} which employs them:
  2740. @group
  2741. @noindent
  2742. @w{ catch getf throw }
  2743. @w{ get remf }
  2744. @w{ get-properties remprop }
  2745. @noindent
  2746. @w{ Figure 5--11: Operators that always prefer EQ over EQL}
  2747. @end group
  2748. @node eql, equal, eq, Data and Control Flow Dictionary
  2749. @subsection eql [Function]
  2750. @code{eql} @i{x y} @result{} @i{generalized-boolean}
  2751. @subsubheading Arguments and Values::
  2752. @i{x}---an @i{object}.
  2753. @i{y}---an @i{object}.
  2754. @i{generalized-boolean}---a @i{generalized boolean}.
  2755. @subsubheading Description::
  2756. The value of @b{eql} is @i{true} of two objects, @i{x} and
  2757. @i{y}, in the folowing cases:
  2758. @table @asis
  2759. @item 1.
  2760. If @i{x} and @i{y} are @b{eq}.
  2761. @item 2.
  2762. If @i{x} and @i{y} are both @i{numbers}
  2763. of the same @i{type} and the same value.
  2764. @item 3.
  2765. If they are both @i{characters} that represent the
  2766. same character.
  2767. @end table
  2768. Otherwise the value of @b{eql} is @i{false}.
  2769. If an implementation supports positive and negative zeros as @i{distinct} values,
  2770. then @t{(eql 0.0 -0.0)} returns @i{false}.
  2771. Otherwise, when the syntax @t{-0.0} is read it is interpreted as the value @t{0.0},
  2772. and so @t{(eql 0.0 -0.0)} returns @i{true}.
  2773. @subsubheading Examples::
  2774. @example
  2775. (eql 'a 'b) @result{} @i{false}
  2776. (eql 'a 'a) @result{} @i{true}
  2777. (eql 3 3) @result{} @i{true}
  2778. (eql 3 3.0) @result{} @i{false}
  2779. (eql 3.0 3.0) @result{} @i{true}
  2780. (eql #c(3 -4) #c(3 -4)) @result{} @i{true}
  2781. (eql #c(3 -4.0) #c(3 -4)) @result{} @i{false}
  2782. (eql (cons 'a 'b) (cons 'a 'c)) @result{} @i{false}
  2783. (eql (cons 'a 'b) (cons 'a 'b)) @result{} @i{false}
  2784. (eql '(a . b) '(a . b))
  2785. @result{} @i{true}
  2786. @i{OR}@result{} @i{false}
  2787. (progn (setq x (cons 'a 'b)) (eql x x)) @result{} @i{true}
  2788. (progn (setq x '(a . b)) (eql x x)) @result{} @i{true}
  2789. (eql #\A #\A) @result{} @i{true}
  2790. (eql "Foo" "Foo")
  2791. @result{} @i{true}
  2792. @i{OR}@result{} @i{false}
  2793. (eql "Foo" (copy-seq "Foo")) @result{} @i{false}
  2794. (eql "FOO" "foo") @result{} @i{false}
  2795. @end example
  2796. Normally @t{(eql 1.0s0 1.0d0)} is false, under the assumption
  2797. that @t{1.0s0} and @t{1.0d0} are of distinct data types.
  2798. However, implementations that do not provide four distinct floating-point
  2799. formats are permitted to ``collapse'' the four formats into some
  2800. smaller number of them; in such an implementation @t{(eql 1.0s0 1.0d0)}
  2801. might be true.
  2802. @subsubheading See Also::
  2803. @ref{eq}
  2804. ,
  2805. @ref{equal}
  2806. ,
  2807. @ref{equalp}
  2808. ,
  2809. @ref{=; /=; <; >; <=; >=}
  2810. ,
  2811. @ref{char=; char/=; char<; char>; char<=; char>=; char-equal; char-not-equal; char-lessp; char-greaterp; char-not-greaterp; char-not-lessp}
  2812. @subsubheading Notes::
  2813. @b{eql} is the same as @b{eq}, except that if the
  2814. arguments are @i{characters} or @i{numbers}
  2815. of the same type then their
  2816. values are compared. Thus @b{eql} tells whether two @i{objects}
  2817. are conceptually the same, whereas @b{eq} tells whether two
  2818. @i{objects} are implementationally identical. It is for this reason
  2819. that @b{eql}, not @b{eq}, is the default comparison predicate
  2820. for @i{operators} that take @i{sequences}
  2821. as arguments.
  2822. @b{eql} may not be true of two @i{floats}
  2823. even when they represent the same
  2824. value. @b{=} is used to compare
  2825. mathematical values.
  2826. Two @i{complex} numbers are considered to be @b{eql}
  2827. if their real parts are @b{eql}
  2828. and their imaginary parts are @b{eql}.
  2829. For example, @t{(eql #C(4 5) #C(4 5))} is @i{true} and
  2830. @t{(eql #C(4 5) #C(4.0 5.0))} is @i{false}.
  2831. Note that while @t{(eql #C(5.0 0.0) 5.0)} is @i{false},
  2832. @t{(eql #C(5 0) 5)} is @i{true}.
  2833. In the case of @t{(eql #C(5.0 0.0) 5.0)} the
  2834. two arguments are of different types,
  2835. and so cannot satisfy @b{eql}.
  2836. In the case of @t{(eql #C(5 0) 5)},
  2837. @t{#C(5 0)} is not a @i{complex} number, but
  2838. is automatically reduced
  2839. to the @i{integer} @t{5}.
  2840. @node equal, equalp, eql, Data and Control Flow Dictionary
  2841. @subsection equal [Function]
  2842. @code{equal} @i{x y} @result{} @i{generalized-boolean}
  2843. @subsubheading Arguments and Values::
  2844. @i{x}---an @i{object}.
  2845. @i{y}---an @i{object}.
  2846. @i{generalized-boolean}---a @i{generalized boolean}.
  2847. @subsubheading Description::
  2848. Returns @i{true} if @i{x} and @i{y} are structurally similar
  2849. (isomorphic) @i{objects}. @i{Objects} are treated as follows by
  2850. @b{equal}.
  2851. @table @asis
  2852. @item @i{Symbols}, @i{Numbers}, and @i{Characters}
  2853. @b{equal} is @i{true} of two @i{objects}
  2854. if they are @i{symbols} that are @b{eq},
  2855. if they are @i{numbers} that are @b{eql}, or
  2856. if they are @i{characters} that are @b{eql}.
  2857. @item @i{Conses}
  2858. For @i{conses}, @b{equal} is defined recursively as
  2859. the two @i{cars} being @b{equal}
  2860. and the two @i{cdrs} being @b{equal}.
  2861. @item @i{Arrays}
  2862. Two @i{arrays} are @b{equal} only if they are @b{eq},
  2863. with one exception:
  2864. @i{strings} and @i{bit vectors} are compared element-by-element (using @b{eql}).
  2865. If either @i{x} or @i{y} has a @i{fill pointer}, the
  2866. @i{fill pointer} limits
  2867. the number of elements examined by @b{equal}.
  2868. Uppercase and lowercase letters in @i{strings} are considered by
  2869. @b{equal} to be different.
  2870. @item @i{Pathnames}
  2871. Two @i{pathnames} are @b{equal} if and only if
  2872. all the corresponding components
  2873. (host, device, and so on) are
  2874. equivalent. Whether or not
  2875. uppercase and lowercase letters are considered equivalent
  2876. in @i{strings} appearing in components is @i{implementation-dependent}.
  2877. @i{pathnames}
  2878. that are @b{equal} should be functionally equivalent.
  2879. @item Other (Structures, hash-tables, instances, ...)
  2880. Two other @i{objects} are @b{equal} only if they are @b{eq}.
  2881. @end table
  2882. @b{equal} does not descend any @i{objects} other than the
  2883. ones explicitly specified above.
  2884. Figure 5--12 summarizes the information given in the previous list.
  2885. In addition, the figure specifies the priority of the behavior of @b{equal},
  2886. with upper
  2887. entries taking priority over lower ones.
  2888. @group
  2889. @noindent
  2890. @w{ Type Behavior }
  2891. @w{ @i{number} uses @b{eql} }
  2892. @w{ @i{character} uses @b{eql} }
  2893. @w{ @i{cons} descends }
  2894. @w{ @i{bit vector} descends }
  2895. @w{ @i{string} descends }
  2896. @w{ @i{pathname} ``functionally equivalent'' }
  2897. @w{ @i{structure} uses @b{eq} }
  2898. @w{ Other @i{array} uses @b{eq} }
  2899. @w{ @i{hash table} uses @b{eq} }
  2900. @w{ Other @i{object} uses @b{eq} }
  2901. @noindent
  2902. @w{ Figure 5--12: Summary and priorities of behavior of @b{equal}}
  2903. @end group
  2904. Any two @i{objects} that are @b{eql} are also @b{equal}.
  2905. @b{equal} may fail to terminate if @i{x} or @i{y} is circular.
  2906. @subsubheading Examples::
  2907. @example
  2908. (equal 'a 'b) @result{} @i{false}
  2909. (equal 'a 'a) @result{} @i{true}
  2910. (equal 3 3) @result{} @i{true}
  2911. (equal 3 3.0) @result{} @i{false}
  2912. (equal 3.0 3.0) @result{} @i{true}
  2913. (equal #c(3 -4) #c(3 -4)) @result{} @i{true}
  2914. (equal #c(3 -4.0) #c(3 -4)) @result{} @i{false}
  2915. (equal (cons 'a 'b) (cons 'a 'c)) @result{} @i{false}
  2916. (equal (cons 'a 'b) (cons 'a 'b)) @result{} @i{true}
  2917. (equal #\A #\A) @result{} @i{true}
  2918. (equal #\A #\a) @result{} @i{false}
  2919. (equal "Foo" "Foo") @result{} @i{true}
  2920. (equal "Foo" (copy-seq "Foo")) @result{} @i{true}
  2921. (equal "FOO" "foo") @result{} @i{false}
  2922. (equal "This-string" "This-string") @result{} @i{true}
  2923. (equal "This-string" "this-string") @result{} @i{false}
  2924. @end example
  2925. @subsubheading See Also::
  2926. @ref{eq}
  2927. ,
  2928. @ref{eql}
  2929. ,
  2930. @ref{equalp}
  2931. ,
  2932. @ref{=; /=; <; >; <=; >=}
  2933. ,
  2934. @ref{string=; string/=; string<; string>; string<=; string>=; string-equal; string-not-equal; string-lessp; string-greaterp; string-not-greaterp; string-not-lessp}
  2935. , @b{string-equal},
  2936. @ref{char=; char/=; char<; char>; char<=; char>=; char-equal; char-not-equal; char-lessp; char-greaterp; char-not-greaterp; char-not-lessp}
  2937. ,
  2938. @b{char-equal},
  2939. @ref{tree-equal}
  2940. @subsubheading Notes::
  2941. @i{Object} equality is not a concept for which there is a uniquely
  2942. determined correct algorithm. The appropriateness of an equality
  2943. predicate can be judged only in the context of the needs of some
  2944. particular program. Although these functions take any type of
  2945. argument and their names sound very generic,
  2946. @b{equal} and @b{equalp} are
  2947. not appropriate for every application.
  2948. A rough rule of thumb is that two @i{objects} are @b{equal}
  2949. if and only if their printed representations are the same.
  2950. @node equalp, identity, equal, Data and Control Flow Dictionary
  2951. @subsection equalp [Function]
  2952. @code{equalp} @i{x y} @result{} @i{generalized-boolean}
  2953. @subsubheading Arguments and Values::
  2954. @i{x}---an @i{object}.
  2955. @i{y}---an @i{object}.
  2956. @i{generalized-boolean}---a @i{generalized boolean}.
  2957. @subsubheading Description::
  2958. Returns @i{true} if @i{x} and @i{y} are @b{equal},
  2959. or if they have components that are of the same @i{type} as each other
  2960. and if those components are @b{equalp};
  2961. specifically, @b{equalp} returns @i{true} in the following cases:
  2962. @table @asis
  2963. @item @i{Characters}
  2964. If two @i{characters} are @b{char-equal}.
  2965. @item @i{Numbers}
  2966. If two @i{numbers} are the @i{same} under @b{=}.
  2967. @item @i{Conses}
  2968. If the two @i{cars} in the @i{conses} are @b{equalp}
  2969. and the two @i{cdrs} in the @i{conses} are @b{equalp}.
  2970. @item @i{Arrays}
  2971. If two @i{arrays} have the same
  2972. number of dimensions, the dimensions match,
  2973. and the corresponding
  2974. @i{active elements}
  2975. are @b{equalp}.
  2976. The @i{types} for which the @i{arrays} are @i{specialized} need not match;
  2977. for example, a @i{string} and a general @i{array} that happens to contain the same
  2978. @i{characters} are @b{equalp}.
  2979. Because @b{equalp} performs @i{element}-by-@i{element} comparisons
  2980. of @i{strings} and ignores the @i{case} of @i{characters},
  2981. @i{case} distinctions are ignored when @b{equalp} compares @i{strings}.
  2982. @item @i{Structures}
  2983. If two @i{structures} S_1 and S_2 have the same @i{class}
  2984. and the value of each @i{slot} in S_1 is the @i{same} under @b{equalp}
  2985. as the value of the corresponding @i{slot} in S_2.
  2986. @item @i{Hash Tables}
  2987. @b{equalp} descends @i{hash-tables} by first comparing the count of entries
  2988. and the @t{:test} function; if those are the same, it compares the
  2989. keys of the tables using the @t{:test} function and then the values
  2990. of the matching keys using @b{equalp} recursively.
  2991. @end table
  2992. @b{equalp} does not descend any @i{objects}
  2993. other than the ones explicitly specified above.
  2994. Figure 5--13 summarizes the information given in the previous list.
  2995. In addition, the figure specifies the priority of the behavior of @b{equalp},
  2996. with upper
  2997. entries taking priority over lower ones.
  2998. @group
  2999. @noindent
  3000. @w{ Type Behavior }
  3001. @w{ @i{number} uses @b{=} }
  3002. @w{ @i{character} uses @b{char-equal} }
  3003. @w{ @i{cons} descends }
  3004. @w{ @i{bit vector} descends }
  3005. @w{ @i{string} descends }
  3006. @w{ @i{pathname} same as @b{equal} }
  3007. @w{ @i{structure} descends, as described above }
  3008. @w{ Other @i{array} descends }
  3009. @w{ @i{hash table} descends, as described above }
  3010. @w{ Other @i{object} uses @b{eq} }
  3011. @noindent
  3012. @w{ Figure 5--13: Summary and priorities of behavior of @b{equalp}}
  3013. @end group
  3014. @subsubheading Examples::
  3015. @example
  3016. (equalp 'a 'b) @result{} @i{false}
  3017. (equalp 'a 'a) @result{} @i{true}
  3018. (equalp 3 3) @result{} @i{true}
  3019. (equalp 3 3.0) @result{} @i{true}
  3020. (equalp 3.0 3.0) @result{} @i{true}
  3021. (equalp #c(3 -4) #c(3 -4)) @result{} @i{true}
  3022. (equalp #c(3 -4.0) #c(3 -4)) @result{} @i{true}
  3023. (equalp (cons 'a 'b) (cons 'a 'c)) @result{} @i{false}
  3024. (equalp (cons 'a 'b) (cons 'a 'b)) @result{} @i{true}
  3025. (equalp #\A #\A) @result{} @i{true}
  3026. (equalp #\A #\a) @result{} @i{true}
  3027. (equalp "Foo" "Foo") @result{} @i{true}
  3028. (equalp "Foo" (copy-seq "Foo")) @result{} @i{true}
  3029. (equalp "FOO" "foo") @result{} @i{true}
  3030. @end example
  3031. @example
  3032. (setq array1 (make-array 6 :element-type 'integer
  3033. :initial-contents '(1 1 1 3 5 7)))
  3034. @result{} #(1 1 1 3 5 7)
  3035. (setq array2 (make-array 8 :element-type 'integer
  3036. :initial-contents '(1 1 1 3 5 7 2 6)
  3037. :fill-pointer 6))
  3038. @result{} #(1 1 1 3 5 7)
  3039. (equalp array1 array2) @result{} @i{true}
  3040. (setq vector1 (vector 1 1 1 3 5 7)) @result{} #(1 1 1 3 5 7)
  3041. (equalp array1 vector1) @result{} @i{true}
  3042. @end example
  3043. @subsubheading See Also::
  3044. @ref{eq}
  3045. ,
  3046. @ref{eql}
  3047. ,
  3048. @ref{equal}
  3049. ,
  3050. @ref{=; /=; <; >; <=; >=}
  3051. ,
  3052. @ref{string=; string/=; string<; string>; string<=; string>=; string-equal; string-not-equal; string-lessp; string-greaterp; string-not-greaterp; string-not-lessp}
  3053. , @b{string-equal},
  3054. @ref{char=; char/=; char<; char>; char<=; char>=; char-equal; char-not-equal; char-lessp; char-greaterp; char-not-greaterp; char-not-lessp}
  3055. ,
  3056. @b{char-equal}
  3057. @subsubheading Notes::
  3058. @i{Object} equality is not a concept for which there is a uniquely
  3059. determined correct algorithm. The appropriateness of an equality
  3060. predicate can be judged only in the context of the needs of some
  3061. particular program. Although these functions take any type of
  3062. argument and their names sound very generic,
  3063. @b{equal} and @b{equalp} are
  3064. not appropriate for every application.
  3065. @node identity, complement, equalp, Data and Control Flow Dictionary
  3066. @subsection identity [Function]
  3067. @code{identity} @i{object} @result{} @i{object}
  3068. @subsubheading Arguments and Values::
  3069. @i{object}---an @i{object}.
  3070. @subsubheading Description::
  3071. Returns its argument @i{object}.
  3072. @subsubheading Examples::
  3073. @example
  3074. (identity 101) @result{} 101
  3075. (mapcan #'identity (list (list 1 2 3) '(4 5 6))) @result{} (1 2 3 4 5 6)
  3076. @end example
  3077. @subsubheading Notes::
  3078. @b{identity} is intended for use with functions that require
  3079. a @i{function} as an argument.
  3080. @t{(eql x (identity x))} returns @i{true} for all possible values of @i{x},
  3081. but @t{(eq x (identity x))} might return @i{false} when @i{x} is a @i{number}
  3082. or @i{character}.
  3083. @b{identity} could be defined by
  3084. @example
  3085. (defun identity (x) x)
  3086. @end example
  3087. @node complement, constantly, identity, Data and Control Flow Dictionary
  3088. @subsection complement [Function]
  3089. @code{complement} @i{function} @result{} @i{complement-function}
  3090. @subsubheading Arguments and Values::
  3091. @i{function}---a @i{function}.
  3092. @i{complement-function}---a @i{function}.
  3093. @subsubheading Description::
  3094. Returns a @i{function} that
  3095. takes the same @i{arguments} as @i{function},
  3096. and has the same side-effect behavior as @i{function},
  3097. but returns only a single value:
  3098. a @i{generalized boolean} with the opposite truth value of that
  3099. which would be returned as the @i{primary value} of @i{function}.
  3100. That is, when the @i{function} would have returned
  3101. @i{true} as its @i{primary value}
  3102. the @i{complement-function} returns @i{false},
  3103. and when the @i{function} would have returned
  3104. @i{false} as its @i{primary value}
  3105. the @i{complement-function} returns @i{true}.
  3106. @subsubheading Examples::
  3107. @example
  3108. (funcall (complement #'zerop) 1) @result{} @i{true}
  3109. (funcall (complement #'characterp) #\A) @result{} @i{false}
  3110. (funcall (complement #'member) 'a '(a b c)) @result{} @i{false}
  3111. (funcall (complement #'member) 'd '(a b c)) @result{} @i{true}
  3112. @end example
  3113. @subsubheading See Also::
  3114. @ref{not}
  3115. @subsubheading Notes::
  3116. @example
  3117. (complement @i{x}) @equiv{} #'(lambda (&rest arguments) (not (apply @i{x} arguments)))
  3118. @end example
  3119. In @r{Common Lisp}, functions with names like ``@t{@i{xxx}-if-not}''
  3120. are related to functions with names like ``@t{@i{xxx}-if}''
  3121. in that
  3122. @example
  3123. (@i{xxx}-if-not @i{f} . @i{arguments}) @equiv{} (@i{xxx}-if (complement @i{f}) . @i{arguments})
  3124. @end example
  3125. For example,
  3126. @example
  3127. (find-if-not #'zerop '(0 0 3)) @equiv{}
  3128. (find-if (complement #'zerop) '(0 0 3)) @result{} 3
  3129. @end example
  3130. Note that since the ``@t{@i{xxx}-if-not}'' @i{functions}
  3131. and the @t{:test-not} arguments have been deprecated,
  3132. uses of ``@t{@i{xxx}-if}'' @i{functions} or
  3133. @t{:test} arguments with @b{complement} are preferred.
  3134. @node constantly, every, complement, Data and Control Flow Dictionary
  3135. @subsection constantly [Function]
  3136. @code{constantly} @i{value} @result{} @i{function}
  3137. @subsubheading Arguments and Values::
  3138. @i{value}---an @i{object}.
  3139. @i{function}---a @i{function}.
  3140. @subsubheading Description::
  3141. @b{constantly} returns a @i{function} that accepts any number of
  3142. arguments, that has no side-effects, and that always returns @i{value}.
  3143. @subsubheading Examples::
  3144. @example
  3145. (mapcar (constantly 3) '(a b c d)) @result{} (3 3 3 3)
  3146. (defmacro with-vars (vars &body forms)
  3147. `((lambda ,vars ,@@forms) ,@@(mapcar (constantly nil) vars)))
  3148. @result{} WITH-VARS
  3149. (macroexpand '(with-vars (a b) (setq a 3 b (* a a)) (list a b)))
  3150. @result{} ((LAMBDA (A B) (SETQ A 3 B (* A A)) (LIST A B)) NIL NIL), @i{true}
  3151. @end example
  3152. @subsubheading See Also::
  3153. @ref{not}
  3154. @subsubheading Notes::
  3155. @b{constantly} could be defined by:
  3156. @example
  3157. (defun constantly (object)
  3158. #'(lambda (&rest arguments) object))
  3159. @end example
  3160. @node every, and, constantly, Data and Control Flow Dictionary
  3161. @subsection every, some, notevery, notany [Function]
  3162. @code{every} @i{predicate {&rest} sequences^+} @result{} @i{generalized-boolean}
  3163. @code{some} @i{predicate {&rest} sequences^+} @result{} @i{result}
  3164. @code{notevery} @i{predicate {&rest} sequences^+} @result{} @i{generalized-boolean}
  3165. @code{notany} @i{predicate {&rest} sequences^+} @result{} @i{generalized-boolean}
  3166. @subsubheading Arguments and Values::
  3167. @i{predicate}---a @i{designator} for a @i{function} of
  3168. as many @i{arguments} as there are @i{sequences}.
  3169. @i{sequence}---a @i{sequence}.
  3170. @i{result}---an @i{object}.
  3171. @i{generalized-boolean}---a @i{generalized boolean}.
  3172. @subsubheading Description::
  3173. @b{every}, @b{some}, @b{notevery}, and @b{notany}
  3174. test @i{elements} of @i{sequences} for satisfaction of a given @i{predicate}.
  3175. The first argument to @i{predicate} is an @i{element} of the first @i{sequence};
  3176. each succeeding argument is an @i{element} of a succeeding @i{sequence}.
  3177. @i{Predicate} is first applied to the elements
  3178. with index @t{0} in each of the @i{sequences}, and possibly then to
  3179. the elements with index @t{1}, and so on, until a termination
  3180. criterion is met or the end of the shortest of the @i{sequences} is reached.
  3181. @b{every} returns @i{false} as soon
  3182. as any invocation of @i{predicate} returns @i{false}.
  3183. If the end of a @i{sequence} is reached,
  3184. @b{every} returns @i{true}.
  3185. Thus, @b{every} returns @i{true} if and only if
  3186. every invocation of @i{predicate} returns @i{true}.
  3187. @b{some} returns the first @i{non-nil} value
  3188. which is returned by an invocation of @i{predicate}.
  3189. If the end of a @i{sequence} is reached without any invocation of the
  3190. @i{predicate} returning @i{true}, @b{some} returns @i{false}.
  3191. Thus, @b{some} returns @i{true} if and only if
  3192. some invocation of @i{predicate} returns @i{true}.
  3193. @b{notany} returns @i{false}
  3194. as soon as any invocation of @i{predicate} returns @i{true}.
  3195. If the end of a @i{sequence} is reached,
  3196. @b{notany} returns @i{true}.
  3197. Thus, @b{notany} returns @i{true} if and only if
  3198. it is not the case that any invocation of @i{predicate} returns @i{true}.
  3199. @b{notevery} returns @i{true} as soon as any invocation of
  3200. @i{predicate} returns @i{false}.
  3201. If the end of a @i{sequence} is reached,
  3202. @b{notevery} returns @i{false}.
  3203. Thus, @b{notevery} returns @i{true} if and only if
  3204. it is not the case that every invocation of @i{predicate} returns @i{true}.
  3205. @subsubheading Examples::
  3206. @example
  3207. (every #'characterp "abc") @result{} @i{true}
  3208. (some #'= '(1 2 3 4 5) '(5 4 3 2 1)) @result{} @i{true}
  3209. (notevery #'< '(1 2 3 4) '(5 6 7 8) '(9 10 11 12)) @result{} @i{false}
  3210. (notany #'> '(1 2 3 4) '(5 6 7 8) '(9 10 11 12)) @result{} @i{true}
  3211. @end example
  3212. @subsubheading Exceptional Situations::
  3213. Should signal @b{type-error} if its first argument is neither a
  3214. @i{symbol} nor a @i{function} or if any subsequent
  3215. argument is not a @i{proper sequence}.
  3216. Other exceptional situations are possible, depending on the nature
  3217. of the @i{predicate}.
  3218. @subsubheading See Also::
  3219. @ref{and}
  3220. ,
  3221. @ref{or}
  3222. ,
  3223. @ref{Traversal Rules and Side Effects}
  3224. @subsubheading Notes::
  3225. @example
  3226. (notany @i{predicate} @{@i{sequence}@}{*}) @equiv{} (not (some @i{predicate} @{@i{sequence}@}{*}))
  3227. (notevery @i{predicate} @{@i{sequence}@}{*}) @equiv{} (not (every @i{predicate} @{@i{sequence}@}{*}))
  3228. @end example
  3229. @node and, cond, every, Data and Control Flow Dictionary
  3230. @subsection and [Macro]
  3231. @code{and} @i{@{@i{form}@}{*}} @result{} @i{@{@i{result}@}{*}}
  3232. @subsubheading Arguments and Values::
  3233. @i{form}---a @i{form}.
  3234. @i{results}---the @i{values} resulting from the evaluation of
  3235. the last @i{form}, or the symbols @b{nil} or @b{t}.
  3236. @subsubheading Description::
  3237. The macro @b{and} evaluates each @i{form} one at a time from left to right.
  3238. As soon as any @i{form} evaluates to @b{nil}, @b{and} returns
  3239. @b{nil} without evaluating the remaining @i{forms}. If all @i{forms}
  3240. but the last evaluate to @i{true} values, @b{and} returns the results
  3241. produced by evaluating the last @i{form}.
  3242. If no @i{forms} are supplied, @t{(and)} returns @b{t}.
  3243. @b{and} passes back multiple values from the last @i{subform}
  3244. but not from subforms other than the last.
  3245. @subsubheading Examples::
  3246. @example
  3247. (if (and (>= n 0)
  3248. (< n (length a-simple-vector))
  3249. (eq (elt a-simple-vector n) 'foo))
  3250. (princ "Foo!"))
  3251. @end example
  3252. The above expression prints @t{Foo!} if element @t{n} of @t{a-simple-vector}
  3253. is the symbol @t{foo}, provided also that @t{n} is indeed a valid index
  3254. for @t{a-simple-vector}. Because @b{and} guarantees
  3255. left-to-right testing
  3256. of its parts, @b{elt} is not called if @t{n} is out of range.
  3257. @example
  3258. (setq temp1 1 temp2 1 temp3 1) @result{} 1
  3259. (and (incf temp1) (incf temp2) (incf temp3)) @result{} 2
  3260. (and (eql 2 temp1) (eql 2 temp2) (eql 2 temp3)) @result{} @i{true}
  3261. (decf temp3) @result{} 1
  3262. (and (decf temp1) (decf temp2) (eq temp3 'nil) (decf temp3)) @result{} NIL
  3263. (and (eql temp1 temp2) (eql temp2 temp3)) @result{} @i{true}
  3264. (and) @result{} T
  3265. @end example
  3266. @subsubheading See Also::
  3267. @ref{cond}
  3268. ,
  3269. @ref{every; some; notevery; notany}
  3270. ,
  3271. @ref{if}
  3272. ,
  3273. @ref{or}
  3274. ,
  3275. @ref{when; unless}
  3276. @subsubheading Notes::
  3277. @example
  3278. (and @i{form}) @equiv{} (let () @i{form})
  3279. (and @i{form1} @i{form2} ...) @equiv{} (when @i{form1} (and @i{form2} ...))
  3280. @end example
  3281. @node cond, if, and, Data and Control Flow Dictionary
  3282. @subsection cond [Macro]
  3283. @code{cond} @i{@{!@i{clause}@}{*}} @result{} @i{@{@i{result}@}{*}}
  3284. @w{@i{clause} ::=@r{(}test-form @{@i{form}@}{*}@r{)}}
  3285. @subsubheading Arguments and Values::
  3286. @i{test-form}---a @i{form}.
  3287. @i{forms}---an @i{implicit progn}.
  3288. @i{results}---the @i{values} of the @i{forms}
  3289. in the first @i{clause} whose @i{test-form} @i{yields} @i{true},
  3290. or the @i{primary value} of the @i{test-form}
  3291. if there are no @i{forms} in that @i{clause},
  3292. or else @b{nil} if no @i{test-form} @i{yields} @i{true}.
  3293. @subsubheading Description::
  3294. @b{cond} allows the execution of @i{forms} to be dependent
  3295. on @i{test-form}.
  3296. @i{Test-forms} are evaluated one at a time in the order in which
  3297. they are given in the argument list until a @i{test-form} is found that
  3298. evaluates to @i{true}.
  3299. If there are no @i{forms} in that clause, the @i{primary value}
  3300. of the @i{test-form} is returned by the @b{cond} @i{form}.
  3301. Otherwise, the @i{forms} associated with this @i{test-form} are
  3302. evaluated in order, left to right, as an @i{implicit progn}, and the
  3303. @i{values} returned by the last @i{form}
  3304. are returned by the @b{cond} @i{form}.
  3305. Once one @i{test-form} has @i{yielded} @i{true},
  3306. no additional @i{test-forms} are @i{evaluated}.
  3307. If no @i{test-form} @i{yields} @i{true}, @b{nil} is returned.
  3308. @subsubheading Examples::
  3309. @example
  3310. (defun select-options ()
  3311. (cond ((= a 1) (setq a 2))
  3312. ((= a 2) (setq a 3))
  3313. ((and (= a 3) (floor a 2)))
  3314. (t (floor a 3)))) @result{} SELECT-OPTIONS
  3315. (setq a 1) @result{} 1
  3316. (select-options) @result{} 2
  3317. a @result{} 2
  3318. (select-options) @result{} 3
  3319. a @result{} 3
  3320. (select-options) @result{} 1
  3321. (setq a 5) @result{} 5
  3322. (select-options) @result{} 1, 2
  3323. @end example
  3324. @subsubheading See Also::
  3325. @ref{if}
  3326. ,
  3327. @ref{case; ccase; ecase}
  3328. .
  3329. @node if, or, cond, Data and Control Flow Dictionary
  3330. @subsection if [Special Operator]
  3331. @code{if} @i{@i{test-form} @i{then-form} @r{[}@i{else-form}@r{]}} @result{} @i{@{@i{result}@}{*}}
  3332. @subsubheading Arguments and Values::
  3333. @i{Test-form}---a @i{form}.
  3334. @i{Then-form}---a @i{form}.
  3335. @i{Else-form}---a @i{form}.
  3336. The default is @b{nil}.
  3337. @i{results}---if the @i{test-form} @i{yielded} @i{true},
  3338. the @i{values} returned by the @i{then-form}; otherwise,
  3339. the @i{values} returned by the @i{else-form}.
  3340. @subsubheading Description::
  3341. @b{if} allows the execution of a @i{form} to be dependent
  3342. on a single @i{test-form}.
  3343. First @i{test-form} is evaluated.
  3344. If the result is @i{true}, then @i{then-form} is selected;
  3345. otherwise @i{else-form} is selected.
  3346. Whichever form is selected is then evaluated.
  3347. @subsubheading Examples::
  3348. @example
  3349. (if t 1) @result{} 1
  3350. (if nil 1 2) @result{} 2
  3351. (defun test ()
  3352. (dolist (truth-value '(t nil 1 (a b c)))
  3353. (if truth-value (print 'true) (print 'false))
  3354. (prin1 truth-value))) @result{} TEST
  3355. (test)
  3356. @t{ |> } TRUE T
  3357. @t{ |> } FALSE NIL
  3358. @t{ |> } TRUE 1
  3359. @t{ |> } TRUE (A B C)
  3360. @result{} NIL
  3361. @end example
  3362. @subsubheading See Also::
  3363. @ref{cond}
  3364. ,
  3365. @b{unless},
  3366. @ref{when; unless}
  3367. @subsubheading Notes::
  3368. @example
  3369. (if @i{test-form} @i{then-form} @i{else-form})
  3370. @equiv{} (cond (@i{test-form} @i{then-form}) (t @i{else-form}))
  3371. @end example
  3372. @node or, when, if, Data and Control Flow Dictionary
  3373. @subsection or [Macro]
  3374. @code{or} @i{@{@i{form}@}{*}} @result{} @i{@{@i{results}@}{*}}
  3375. @subsubheading Arguments and Values::
  3376. @i{form}---a @i{form}.
  3377. @i{results}---the @i{values} or @i{primary value} (see below)
  3378. resulting from the evaluation of
  3379. the last @i{form} executed or @b{nil}.
  3380. @subsubheading Description::
  3381. @b{or} evaluates each @i{form}, one at a time, from left to right.
  3382. The evaluation of all @i{forms} terminates when a @i{form} evaluates
  3383. to @i{true} (@i{i.e.}, something other than @b{nil}).
  3384. If the @i{evaluation} of any @i{form} other than the last returns a
  3385. @i{primary value} that is @i{true}, @b{or} immediately returns
  3386. that @i{value} (but no additional @i{values}) without evaluating the
  3387. remaining @i{forms}.
  3388. If every @i{form} but the last returns @i{false} as its @i{primary value},
  3389. @b{or} returns all @i{values} returned by the last @i{form}.
  3390. If no @i{forms} are supplied, @b{or} returns @b{nil}.
  3391. @subsubheading Examples::
  3392. @example
  3393. (or) @result{} NIL
  3394. (setq temp0 nil temp1 10 temp2 20 temp3 30) @result{} 30
  3395. (or temp0 temp1 (setq temp2 37)) @result{} 10
  3396. temp2 @result{} 20
  3397. (or (incf temp1) (incf temp2) (incf temp3)) @result{} 11
  3398. temp1 @result{} 11
  3399. temp2 @result{} 20
  3400. temp3 @result{} 30
  3401. (or (values) temp1) @result{} 11
  3402. (or (values temp1 temp2) temp3) @result{} 11
  3403. (or temp0 (values temp1 temp2)) @result{} 11, 20
  3404. (or (values temp0 temp1) (values temp2 temp3)) @result{} 20, 30
  3405. @end example
  3406. @subsubheading See Also::
  3407. @ref{and}
  3408. ,
  3409. @b{some},
  3410. @b{unless}
  3411. @node when, case, or, Data and Control Flow Dictionary
  3412. @subsection when, unless [Macro]
  3413. @code{when} @i{test-form @{@i{form}@}{*}} @result{} @i{@{@i{result}@}{*}}
  3414. @code{unless} @i{test-form @{@i{form}@}{*}} @result{} @i{@{@i{result}@}{*}}
  3415. @subsubheading Arguments and Values::
  3416. @i{test-form}---a @i{form}.
  3417. @i{forms}---an @i{implicit progn}.
  3418. @i{results}---the @i{values} of the @i{forms}
  3419. in a @b{when} @i{form} if the @i{test-form} @i{yields} @i{true}
  3420. or in an @b{unless} @i{form} if the @i{test-form} @i{yields} @i{false};
  3421. otherwise @b{nil}.
  3422. @subsubheading Description::
  3423. @b{when} and @b{unless} allow the execution of @i{forms}
  3424. to be dependent on a single @i{test-form}.
  3425. In a @b{when} @i{form},
  3426. if the @i{test-form} @i{yields} @i{true},
  3427. the @i{forms} are @i{evaluated} in order from left to right
  3428. and the @i{values} returned by the @i{forms}
  3429. are returned from the @b{when} @i{form}.
  3430. Otherwise, if the @i{test-form} @i{yields} @i{false},
  3431. the @i{forms} are not @i{evaluated},
  3432. and the @b{when} @i{form} returns @b{nil}.
  3433. In an @b{unless} @i{form},
  3434. if the @i{test-form} @i{yields} @i{false},
  3435. the @i{forms} are @i{evaluated} in order from left to right
  3436. and the @i{values} returned by the @i{forms}
  3437. are returned from the @b{unless} @i{form}.
  3438. Otherwise, if the @i{test-form} @i{yields} @i{false},
  3439. the @i{forms} are not @i{evaluated},
  3440. and the @b{unless} @i{form} returns @b{nil}.
  3441. @subsubheading Examples::
  3442. @example
  3443. (when t 'hello) @result{} HELLO
  3444. (unless t 'hello) @result{} NIL
  3445. (when nil 'hello) @result{} NIL
  3446. (unless nil 'hello) @result{} HELLO
  3447. (when t) @result{} NIL
  3448. (unless nil) @result{} NIL
  3449. (when t (prin1 1) (prin1 2) (prin1 3))
  3450. @t{ |> } 123
  3451. @result{} 3
  3452. (unless t (prin1 1) (prin1 2) (prin1 3)) @result{} NIL
  3453. (when nil (prin1 1) (prin1 2) (prin1 3)) @result{} NIL
  3454. (unless nil (prin1 1) (prin1 2) (prin1 3))
  3455. @t{ |> } 123
  3456. @result{} 3
  3457. (let ((x 3))
  3458. (list (when (oddp x) (incf x) (list x))
  3459. (when (oddp x) (incf x) (list x))
  3460. (unless (oddp x) (incf x) (list x))
  3461. (unless (oddp x) (incf x) (list x))
  3462. (if (oddp x) (incf x) (list x))
  3463. (if (oddp x) (incf x) (list x))
  3464. (if (not (oddp x)) (incf x) (list x))
  3465. (if (not (oddp x)) (incf x) (list x))))
  3466. @result{} ((4) NIL (5) NIL 6 (6) 7 (7))
  3467. @end example
  3468. @subsubheading See Also::
  3469. @ref{and}
  3470. ,
  3471. @ref{cond}
  3472. ,
  3473. @ref{if}
  3474. ,
  3475. @ref{or}
  3476. @subsubheading Notes::
  3477. @example
  3478. (when @i{test} @{@i{form}@}^+) @equiv{} (and @i{test} (progn @{@i{form}@}^+))
  3479. (when @i{test} @{@i{form}@}^+) @equiv{} (cond (@i{test} @{@i{form}@}^+))
  3480. (when @i{test} @{@i{form}@}^+) @equiv{} (if @i{test} (progn @{@i{form}@}^+) nil)
  3481. (when @i{test} @{@i{form}@}^+) @equiv{} (unless (not @i{test}) @{@i{form}@}^+)
  3482. (unless @i{test} @{@i{form}@}^+) @equiv{} (cond ((not @i{test}) @{@i{form}@}^+))
  3483. (unless @i{test} @{@i{form}@}^+) @equiv{} (if @i{test} nil (progn @{@i{form}@}^+))
  3484. (unless @i{test} @{@i{form}@}^+) @equiv{} (when (not @i{test}) @{@i{form}@}^+)
  3485. @end example
  3486. @node case, typecase, when, Data and Control Flow Dictionary
  3487. @subsection case, ccase, ecase [Macro]
  3488. @code{case} @i{keyform @{!@i{normal-clause}@}{*} @r{[}!@i{otherwise-clause}@r{]}} @result{} @i{@{@i{result}@}{*}}
  3489. @code{ccase} @i{keyplace @{!@i{normal-clause}@}{*}} @result{} @i{@{@i{result}@}{*}}
  3490. @code{ecase} @i{keyform @{!@i{normal-clause}@}{*}} @result{} @i{@{@i{result}@}{*}}
  3491. @w{@i{normal-clause} ::=@r{(}keys @{@i{form}@}{*}@r{)}}
  3492. @w{@i{otherwise-clause} ::=@r{(}@{otherwise | t@} @{@i{form}@}{*}@r{)}}
  3493. @w{@i{clause} ::=normal-clause | otherwise-clause}
  3494. @IRindex{otherwise}
  3495. @IRindex{t}
  3496. @subsubheading Arguments and Values::
  3497. @i{keyform}---a @i{form}; evaluated to produce a @i{test-key}.
  3498. @i{keyplace}---a @i{form}; evaluated initially to produce a @i{test-key}.
  3499. Possibly also used later as a @i{place} if no @i{keys} match.
  3500. @i{test-key}---an object produced by evaluating @i{keyform} or @i{keyplace}.
  3501. @i{keys}---a @i{designator} for a @i{list} of @i{objects}.
  3502. In the case of @b{case}, the @i{symbols} @b{t} and @b{otherwise} may
  3503. not be used as the @i{keys} @i{designator}. To refer to these @i{symbols}
  3504. by themselves as @i{keys}, the designators @t{(t)} and @t{(otherwise)}, respectively,
  3505. must be used instead.
  3506. @i{forms}---an @i{implicit progn}.
  3507. @i{results}---the @i{values} returned by the @i{forms}
  3508. in the matching @i{clause}.
  3509. @subsubheading Description::
  3510. These @i{macros} allow the conditional execution of a body of @i{forms}
  3511. in a @i{clause} that is selected by matching the @i{test-key} on the
  3512. basis of its identity.
  3513. The @i{keyform} or @i{keyplace} is @i{evaluated} to produce the
  3514. @i{test-key}.
  3515. Each of the @i{normal-clauses} is then considered in turn.
  3516. If the @i{test-key} is the @i{same} as any @i{key} for
  3517. that @i{clause}, the @i{forms} in that @i{clause} are
  3518. @i{evaluated} as an @i{implicit progn}, and the @i{values}
  3519. it returns are returned as the value of the @b{case},
  3520. @b{ccase}, or @b{ecase} @i{form}.
  3521. These @i{macros} differ only in their @i{behavior} when
  3522. no @i{normal-clause} matches; specifically:
  3523. @table @asis
  3524. @item @b{case}
  3525. If no @i{normal-clause} matches, and there is an @i{otherwise-clause},
  3526. then that @i{otherwise-clause} automatically matches; the @i{forms} in
  3527. that @i{clause} are @i{evaluated} as an @i{implicit progn},
  3528. and the @i{values} it returns are returned as the value of the @b{case}.
  3529. If there is no @i{otherwise-clause}, @b{case} returns @b{nil}.
  3530. @item @b{ccase}
  3531. If no @i{normal-clause} matches,
  3532. a @i{correctable} @i{error} of @i{type} @b{type-error} is signaled.
  3533. The offending datum is the @i{test-key} and
  3534. the expected type is @i{type equivalent} to @t{(member @i{key1} @i{key2} ...)}.
  3535. The @b{store-value} @i{restart} can be used to correct the error.
  3536. If the @b{store-value} @i{restart} is invoked, its @i{argument} becomes the
  3537. new @i{test-key}, and is stored in @i{keyplace} as if by
  3538. @t{(setf @i{keyplace} @i{test-key})}.
  3539. Then @b{ccase} starts over, considering each @i{clause} anew.
  3540. [Reviewer Note by Barmar: Will it prompt for multiple values if keyplace is a VALUES general ref?]
  3541. The subforms of @i{keyplace} might be evaluated again if
  3542. none of the cases holds.
  3543. @item @b{ecase}
  3544. If no @i{normal-clause} matches,
  3545. a @i{non-correctable} @i{error} of @i{type} @b{type-error} is signaled.
  3546. The offending datum is the @i{test-key} and
  3547. the expected type is @i{type equivalent} to @t{(member @i{key1} @i{key2} ...)}.
  3548. Note that in contrast with @b{ccase},
  3549. the caller of @b{ecase} may rely on the fact that @b{ecase}
  3550. does not return if a @i{normal-clause} does not match.
  3551. @end table
  3552. @subsubheading Examples::
  3553. @example
  3554. (dolist (k '(1 2 3 :four #\v () t 'other))
  3555. (format t "~S "
  3556. (case k ((1 2) 'clause1)
  3557. (3 'clause2)
  3558. (nil 'no-keys-so-never-seen)
  3559. ((nil) 'nilslot)
  3560. ((:four #\v) 'clause4)
  3561. ((t) 'tslot)
  3562. (otherwise 'others))))
  3563. @t{ |> } CLAUSE1 CLAUSE1 CLAUSE2 CLAUSE4 CLAUSE4 NILSLOT TSLOT OTHERS
  3564. @result{} NIL
  3565. (defun add-em (x) (apply #'+ (mapcar #'decode x)))
  3566. @result{} ADD-EM
  3567. (defun decode (x)
  3568. (ccase x
  3569. ((i uno) 1)
  3570. ((ii dos) 2)
  3571. ((iii tres) 3)
  3572. ((iv cuatro) 4)))
  3573. @result{} DECODE
  3574. (add-em '(uno iii)) @result{} 4
  3575. (add-em '(uno iiii))
  3576. @t{ |> } Error: The value of X, IIII, is not I, UNO, II, DOS, III,
  3577. @t{ |> } TRES, IV, or CUATRO.
  3578. @t{ |> } 1: Supply a value to use instead.
  3579. @t{ |> } 2: Return to Lisp Toplevel.
  3580. @t{ |> } Debug> @b{|>>}@t{:CONTINUE 1}@b{<<|}
  3581. @t{ |> } Value to evaluate and use for X: @b{|>>}@t{'IV}@b{<<|}
  3582. @result{} 5
  3583. @end example
  3584. @subsubheading Side Effects::
  3585. The debugger might be entered.
  3586. If the @b{store-value} @i{restart} is invoked,
  3587. the @i{value} of @i{keyplace} might be changed.
  3588. @subsubheading Affected By::
  3589. @b{ccase} and @b{ecase}, since they might signal an error,
  3590. are potentially affected by existing @i{handlers} and @b{*debug-io*}.
  3591. @subsubheading Exceptional Situations::
  3592. @b{ccase} and @b{ecase} signal an error of @i{type} @b{type-error}
  3593. if no @i{normal-clause} matches.
  3594. @subsubheading See Also::
  3595. @ref{cond}
  3596. ,
  3597. @ref{typecase; ctypecase; etypecase}
  3598. ,
  3599. @ref{setf; psetf}
  3600. ,
  3601. @ref{Generalized Reference}
  3602. @subsubheading Notes::
  3603. @example
  3604. (case @i{test-key}
  3605. @{((@{@i{key}@}{*}) @{@i{form}@}{*})@}{*})
  3606. @equiv{}
  3607. (let ((#1=#:g0001 @i{test-key}))
  3608. (cond @{((member #1# '(@{@i{key}@}{*})) @{@i{form}@}{*})@}{*}))
  3609. @end example
  3610. The specific error message used by @b{ecase} and @b{ccase} can vary
  3611. between implementations. In situations where control of the specific wording
  3612. of the error message is important, it is better to use @b{case} with an
  3613. @i{otherwise-clause} that explicitly signals an error with an appropriate
  3614. message.
  3615. @node typecase, multiple-value-bind, case, Data and Control Flow Dictionary
  3616. @subsection typecase, ctypecase, etypecase [Macro]
  3617. @code{typecase} @i{keyform @{!@i{normal-clause}@}{*} @r{[}!@i{otherwise-clause}@r{]}} @result{} @i{@{@i{result}@}{*}}
  3618. @code{ctypecase} @i{keyplace @{!@i{normal-clause}@}{*}} @result{} @i{@{@i{result}@}{*}}
  3619. @code{etypecase} @i{keyform @{!@i{normal-clause}@}{*}} @result{} @i{@{@i{result}@}{*}}
  3620. @w{@i{normal-clause} ::=@r{(}type @{@i{form}@}{*}@r{)}}
  3621. @w{@i{otherwise-clause} ::=@r{(}@{otherwise | t@} @{@i{form}@}{*}@r{)}}
  3622. @w{@i{clause} ::=normal-clause | otherwise-clause}
  3623. @IRindex{otherwise}
  3624. @IRindex{t}
  3625. @subsubheading Arguments and Values::
  3626. @i{keyform}---a @i{form}; evaluated to produce a @i{test-key}.
  3627. @i{keyplace}---a @i{form}; evaluated initially to produce a @i{test-key}.
  3628. Possibly also used later as a @i{place} if no @i{types} match.
  3629. @i{test-key}---an object produced by evaluating @i{keyform} or @i{keyplace}.
  3630. @i{type}---a @i{type specifier}.
  3631. @i{forms}---an @i{implicit progn}.
  3632. @i{results}---the @i{values} returned by the @i{forms}
  3633. in the matching @i{clause}.
  3634. @subsubheading Description::
  3635. These @i{macros} allow the conditional execution of a body of @i{forms}
  3636. in a @i{clause} that is selected by matching the @i{test-key} on the basis
  3637. of its @i{type}.
  3638. The @i{keyform} or @i{keyplace} is @i{evaluated} to produce the
  3639. @i{test-key}.
  3640. Each of the @i{normal-clauses} is then considered in turn.
  3641. If the @i{test-key} is of the @i{type}
  3642. given by the @i{clauses}'s @i{type},
  3643. the @i{forms} in that @i{clause} are
  3644. @i{evaluated} as an @i{implicit progn}, and the @i{values}
  3645. it returns are returned as the value of the @b{typecase},
  3646. @b{ctypecase}, or @b{etypecase} @i{form}.
  3647. These @i{macros} differ only in their @i{behavior} when
  3648. no @i{normal-clause} matches; specifically:
  3649. @table @asis
  3650. @item @b{typecase}
  3651. If no @i{normal-clause} matches, and there is an @i{otherwise-clause},
  3652. then that @i{otherwise-clause} automatically matches; the @i{forms} in
  3653. that @i{clause} are @i{evaluated} as an @i{implicit progn},
  3654. and the @i{values} it returns are returned as the value of the @b{typecase}.
  3655. If there is no @i{otherwise-clause}, @b{typecase} returns @b{nil}.
  3656. @item @b{ctypecase}
  3657. If no @i{normal-clause} matches,
  3658. a @i{correctable} @i{error} of @i{type} @b{type-error} is signaled.
  3659. The offending datum is the @i{test-key} and
  3660. the expected type is @i{type equivalent} to @t{(or @i{type1} @i{type2} ...)}.
  3661. The @b{store-value} @i{restart} can be used to correct the error.
  3662. If the @b{store-value} @i{restart} is invoked, its @i{argument} becomes the
  3663. new @i{test-key}, and is stored in @i{keyplace} as if by
  3664. @t{(setf @i{keyplace} @i{test-key})}.
  3665. Then @b{ctypecase} starts over, considering each @i{clause} anew.
  3666. If the @b{store-value} @i{restart} is invoked interactively,
  3667. the user is prompted for a new @i{test-key} to use.
  3668. The subforms of @i{keyplace} might be evaluated again if
  3669. none of the cases holds.
  3670. @item @b{etypecase}
  3671. If no @i{normal-clause} matches,
  3672. a @i{non-correctable} @i{error} of @i{type} @b{type-error} is signaled.
  3673. The offending datum is the @i{test-key} and
  3674. the expected type is @i{type equivalent} to @t{(or @i{type1} @i{type2} ...)}.
  3675. Note that in contrast with @b{ctypecase},
  3676. the caller of @b{etypecase} may rely on the fact that @b{etypecase}
  3677. does not return if a @i{normal-clause} does not match.
  3678. @end table
  3679. In all three cases, is permissible for more than one @i{clause} to specify a
  3680. matching @i{type}, particularly if one is a @i{subtype} of another;
  3681. the earliest applicable @i{clause} is chosen.
  3682. @subsubheading Examples::
  3683. @example
  3684. ;;; (Note that the parts of this example which use TYPE-OF
  3685. ;;; are implementation-dependent.)
  3686. (defun what-is-it (x)
  3687. (format t "~&~S is ~A.~
  3688. x (typecase x
  3689. (float "a float")
  3690. (null "a symbol, boolean false, or the empty list")
  3691. (list "a list")
  3692. (t (format nil "a(n) ~(~A~)" (type-of x))))))
  3693. @result{} WHAT-IS-IT
  3694. (map 'nil #'what-is-it '(nil (a b) 7.0 7 box))
  3695. @t{ |> } NIL is a symbol, boolean false, or the empty list.
  3696. @t{ |> } (A B) is a list.
  3697. @t{ |> } 7.0 is a float.
  3698. @t{ |> } 7 is a(n) integer.
  3699. @t{ |> } BOX is a(n) symbol.
  3700. @result{} NIL
  3701. (setq x 1/3)
  3702. @result{} 1/3
  3703. (ctypecase x
  3704. (integer (* x 4))
  3705. (symbol (symbol-value x)))
  3706. @t{ |> } Error: The value of X, 1/3, is neither an integer nor a symbol.
  3707. @t{ |> } To continue, type :CONTINUE followed by an option number:
  3708. @t{ |> } 1: Specify a value to use instead.
  3709. @t{ |> } 2: Return to Lisp Toplevel.
  3710. @t{ |> } Debug> @b{|>>}@t{:CONTINUE 1}@b{<<|}
  3711. @t{ |> } Use value: @b{|>>}@t{3.7}@b{<<|}
  3712. @t{ |> } Error: The value of X, 3.7, is neither an integer nor a symbol.
  3713. @t{ |> } To continue, type :CONTINUE followed by an option number:
  3714. @t{ |> } 1: Specify a value to use instead.
  3715. @t{ |> } 2: Return to Lisp Toplevel.
  3716. @t{ |> } Debug> @b{|>>}@t{:CONTINUE 1}@b{<<|}
  3717. @t{ |> } Use value: @b{|>>}@t{12}@b{<<|}
  3718. @result{} 48
  3719. x @result{} 12
  3720. @end example
  3721. @subsubheading Affected By::
  3722. @b{ctypecase} and @b{etypecase}, since they might signal an error,
  3723. are potentially affected by existing @i{handlers} and @b{*debug-io*}.
  3724. @subsubheading Exceptional Situations::
  3725. @b{ctypecase} and @b{etypecase} signal an error of @i{type} @b{type-error}
  3726. if no @i{normal-clause} matches.
  3727. The @i{compiler} may choose to issue a warning of @i{type} @b{style-warning}
  3728. if a @i{clause} will never be selected because it is completely
  3729. shadowed by earlier clauses.
  3730. @subsubheading See Also::
  3731. @ref{case; ccase; ecase}
  3732. ,
  3733. @ref{cond}
  3734. ,
  3735. @ref{setf; psetf}
  3736. ,
  3737. @ref{Generalized Reference}
  3738. @subsubheading Notes::
  3739. @example
  3740. (typecase @i{test-key}
  3741. @{(@i{type} @{@i{form}@}{*})@}{*})
  3742. @equiv{}
  3743. (let ((#1=#:g0001 @i{test-key}))
  3744. (cond @{((typep #1# '@i{type}) @{@i{form}@}{*})@}{*}))
  3745. @end example
  3746. The specific error message used by @b{etypecase} and @b{ctypecase} can vary
  3747. between implementations. In situations where control of the specific wording
  3748. of the error message is important, it is better to use @b{typecase} with an
  3749. @i{otherwise-clause} that explicitly signals an error with an appropriate
  3750. message.
  3751. @node multiple-value-bind, multiple-value-call, typecase, Data and Control Flow Dictionary
  3752. @subsection multiple-value-bind [Macro]
  3753. @code{multiple-value-bind} @i{@r{(}@{@i{var}@}{*}@r{)}
  3754. @i{values-form}
  3755. @{@i{declaration}@}{*}
  3756. @{@i{form}@}{*}}@*
  3757. @result{} @i{@{@i{result}@}{*}}
  3758. @subsubheading Arguments and Values::
  3759. @i{var}---a @i{symbol} naming a variable; not evaluated.
  3760. @i{values-form}---a @i{form}; evaluated.
  3761. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  3762. @i{forms}---an @i{implicit progn}.
  3763. @i{results}---the @i{values} returned by the @i{forms}.
  3764. @subsubheading Description::
  3765. Creates new variable @i{bindings} for the @i{vars} and
  3766. executes a series of @i{forms} that use these @i{bindings}.
  3767. The variable @i{bindings} created are lexical unless
  3768. @b{special} declarations are specified.
  3769. @i{Values-form} is evaluated, and each of the @i{vars} is
  3770. bound to the respective value returned by that @i{form}. If there are more
  3771. @i{vars} than values returned, extra values of @b{nil} are given to the
  3772. remaining @i{vars}. If there are more values than
  3773. @i{vars}, the excess
  3774. values are discarded. The @i{vars} are bound to the values over
  3775. the execution of the @i{forms}, which make up an implicit @b{progn}.
  3776. The consequences are unspecified if a type @i{declaration} is specified
  3777. for a @i{var}, but the value to which
  3778. that @i{var} is bound is not consistent with
  3779. the type @i{declaration}.
  3780. The @i{scopes} of the name binding and @i{declarations}
  3781. do not include the @i{values-form}.
  3782. @subsubheading Examples::
  3783. @example
  3784. (multiple-value-bind (f r)
  3785. (floor 130 11)
  3786. (list f r)) @result{} (11 9)
  3787. @end example
  3788. @subsubheading See Also::
  3789. @ref{let; let*}
  3790. ,
  3791. @ref{multiple-value-call}
  3792. @subsubheading Notes::
  3793. @example
  3794. (multiple-value-bind (@{@i{var}@}{*}) @i{values-form} @{@i{form}@}{*})
  3795. @equiv{} (multiple-value-call #'(lambda (&optional @{@i{var}@}{*} &rest #1=#:ignore)
  3796. (declare (ignore #1#))
  3797. @{@i{form}@}{*})
  3798. @i{values-form})
  3799. @end example
  3800. @node multiple-value-call, multiple-value-list, multiple-value-bind, Data and Control Flow Dictionary
  3801. @subsection multiple-value-call [Special Operator]
  3802. @code{multiple-value-call} @i{@i{function-form} @i{form}@r{*}} @result{} @i{@{@i{result}@}{*}}
  3803. @subsubheading Arguments and Values::
  3804. @i{function-form}---a @i{form}; evaluated to produce @i{function}.
  3805. @i{function}---a @i{function designator}
  3806. resulting from the evaluation of @i{function-form}.
  3807. @i{form}---a @i{form}.
  3808. @i{results}---the @i{values} returned by the @i{function}.
  3809. @subsubheading Description::
  3810. Applies @i{function} to a @i{list} of the @i{objects} collected from groups of
  3811. @i{multiple values}_2.
  3812. @b{multiple-value-call} first evaluates the @i{function-form}
  3813. to obtain @i{function}, and then evaluates each @i{form}.
  3814. All the values
  3815. of each @i{form} are gathered together (not just one value from each)
  3816. and given as arguments to the @i{function}.
  3817. @subsubheading Examples::
  3818. @example
  3819. (multiple-value-call #'list 1 '/ (values 2 3) '/ (values) '/ (floor 2.5))
  3820. @result{} (1 / 2 3 / / 2 0.5)
  3821. (+ (floor 5 3) (floor 19 4)) @equiv{} (+ 1 4)
  3822. @result{} 5
  3823. (multiple-value-call #'+ (floor 5 3) (floor 19 4)) @equiv{} (+ 1 2 4 3)
  3824. @result{} 10
  3825. @end example
  3826. @subsubheading See Also::
  3827. @ref{multiple-value-list}
  3828. ,
  3829. @ref{multiple-value-bind}
  3830. @node multiple-value-list, multiple-value-prog1, multiple-value-call, Data and Control Flow Dictionary
  3831. @subsection multiple-value-list [Macro]
  3832. @code{multiple-value-list} @i{form} @result{} @i{list}
  3833. @subsubheading Arguments and Values::
  3834. @i{form}---a @i{form}; evaluated as described below.
  3835. @i{list}---a @i{list} of the @i{values} returned by @i{form}.
  3836. @subsubheading Description::
  3837. @b{multiple-value-list} evaluates @i{form}
  3838. and creates a @i{list} of the @i{multiple values}_2 it returns.
  3839. @subsubheading Examples::
  3840. @example
  3841. (multiple-value-list (floor -3 4)) @result{} (-1 1)
  3842. @end example
  3843. @subsubheading See Also::
  3844. @ref{values-list}
  3845. ,
  3846. @ref{multiple-value-call}
  3847. @subsubheading Notes::
  3848. @b{multiple-value-list} and @b{values-list} are inverses
  3849. of each other.
  3850. @example
  3851. (multiple-value-list form) @equiv{} (multiple-value-call #'list form)
  3852. @end example
  3853. @node multiple-value-prog1, multiple-value-setq, multiple-value-list, Data and Control Flow Dictionary
  3854. @subsection multiple-value-prog1 [Special Operator]
  3855. @code{multiple-value-prog} @i{1} @result{} @i{first-form @{@i{form}@}{*}}
  3856. {first-form-results}
  3857. @subsubheading Arguments and Values::
  3858. @i{first-form}---a @i{form}; evaluated as described below.
  3859. @i{form}---a @i{form}; evaluated as described below.
  3860. @i{first-form-results}---the @i{values} resulting from
  3861. the @i{evaluation} of @i{first-form}.
  3862. @subsubheading Description::
  3863. @b{multiple-value-prog1} evaluates @i{first-form} and saves
  3864. all the values produced by that @i{form}. It then evaluates each
  3865. @i{form} from left to right, discarding their values.
  3866. @subsubheading Examples::
  3867. @example
  3868. (setq temp '(1 2 3)) @result{} (1 2 3)
  3869. (multiple-value-prog1
  3870. (values-list temp)
  3871. (setq temp nil)
  3872. (values-list temp)) @result{} 1, 2, 3
  3873. @end example
  3874. @subsubheading See Also::
  3875. @ref{prog1; prog2}
  3876. @node multiple-value-setq, values, multiple-value-prog1, Data and Control Flow Dictionary
  3877. @subsection multiple-value-setq [Macro]
  3878. @code{multiple-value-setq} @i{vars form} @result{} @i{result}
  3879. @subsubheading Arguments and Values::
  3880. @i{vars}---a @i{list} of @i{symbols}
  3881. that are either @i{variable} @i{names}
  3882. or @i{names} of @i{symbol macros}.
  3883. @i{form}---a @i{form}.
  3884. @i{result}---The @i{primary value} returned by the @i{form}.
  3885. @subsubheading Description::
  3886. @b{multiple-value-setq} assigns values to @i{vars}.
  3887. The @i{form} is evaluated,
  3888. and each @i{var} is @i{assigned}
  3889. to the corresponding @i{value} returned by that @i{form}.
  3890. If there are more @i{vars} than @i{values} returned,
  3891. @b{nil} is @i{assigned} to the extra @i{vars}.
  3892. If there are more @i{values} than @i{vars},
  3893. the extra @i{values} are discarded.
  3894. If any @i{var} is the @i{name} of a @i{symbol macro},
  3895. then it is @i{assigned} as if by @b{setf}. Specifically,
  3896. @example
  3897. (multiple-value-setq (@i{symbol}_1 ... @i{symbol}_n) @i{value-producing-form})
  3898. @end example
  3899. is defined to always behave in the same way as
  3900. @example
  3901. (values (setf (values @i{symbol}_1 ... @i{symbol}_n) @i{value-producing-form}))
  3902. @end example
  3903. in order that the rules for order of evaluation and side-effects be consistent
  3904. with those used by @b{setf}.
  3905. @ITindex{order of evaluation}
  3906. @ITindex{evaluation order}
  3907. See @ref{VALUES Forms as Places}.
  3908. @subsubheading Examples::
  3909. @example
  3910. (multiple-value-setq (quotient remainder) (truncate 3.2 2)) @result{} 1
  3911. quotient @result{} 1
  3912. remainder @result{} 1.2
  3913. (multiple-value-setq (a b c) (values 1 2)) @result{} 1
  3914. a @result{} 1
  3915. b @result{} 2
  3916. c @result{} NIL
  3917. (multiple-value-setq (a b) (values 4 5 6)) @result{} 4
  3918. a @result{} 4
  3919. b @result{} 5
  3920. @end example
  3921. @subsubheading See Also::
  3922. @ref{setq}
  3923. ,
  3924. @ref{symbol-macrolet}
  3925. @node values, values-list, multiple-value-setq, Data and Control Flow Dictionary
  3926. @subsection values [Accessor]
  3927. @code{values} @i{{&rest} object} @result{} @i{@{@i{object}@}{*}}
  3928. (setf (@code{ values} @i{{&rest} place}) new-values)@*
  3929. @subsubheading Arguments and Values::
  3930. @i{object}---an @i{object}.
  3931. @i{place}---a @i{place}.
  3932. @i{new-value}---an @i{object}.
  3933. @subsubheading Description::
  3934. @b{values}
  3935. returns the @i{objects} as @i{multiple values}_2.
  3936. @b{setf} of @b{values} is used to store the
  3937. @i{multiple values}_2 @i{new-values} into the @i{places}.
  3938. See @ref{VALUES Forms as Places}.
  3939. @subsubheading Examples::
  3940. @example
  3941. (values) @result{} <@i{no @i{values}}>
  3942. (values 1) @result{} 1
  3943. (values 1 2) @result{} 1, 2
  3944. (values 1 2 3) @result{} 1, 2, 3
  3945. (values (values 1 2 3) 4 5) @result{} 1, 4, 5
  3946. (defun polar (x y)
  3947. (values (sqrt (+ (* x x) (* y y))) (atan y x))) @result{} POLAR
  3948. (multiple-value-bind (r theta) (polar 3.0 4.0)
  3949. (vector r theta))
  3950. @result{} #(5.0 0.927295)
  3951. @end example
  3952. Sometimes it is desirable to indicate explicitly that a function returns
  3953. exactly one value. For example, the function
  3954. @example
  3955. (defun foo (x y)
  3956. (floor (+ x y) y)) @result{} FOO
  3957. @end example
  3958. returns two values because @b{floor} returns
  3959. two values. It may be that the second value makes no sense,
  3960. or that for efficiency reasons it is desired not to compute the
  3961. second value. @b{values} is the standard idiom
  3962. for indicating that only one value is to be returned:
  3963. @example
  3964. (defun foo (x y)
  3965. (values (floor (+ x y) y))) @result{} FOO
  3966. @end example
  3967. This works because @b{values}
  3968. returns exactly one value for each of
  3969. @i{args}; as for any function call,
  3970. if any of @i{args} produces more than one value, all but the
  3971. first are discarded.
  3972. @subsubheading See Also::
  3973. @ref{values-list}
  3974. ,
  3975. @ref{multiple-value-bind}
  3976. ,
  3977. @ref{multiple-values-limit}
  3978. ,
  3979. @ref{Evaluation}
  3980. @subsubheading Notes::
  3981. Since @b{values} is a @i{function}, not a @i{macro} or @i{special form},
  3982. it receives as @i{arguments} only the @i{primary values} of
  3983. its @i{argument} @i{forms}.
  3984. @node values-list, multiple-values-limit, values, Data and Control Flow Dictionary
  3985. @subsection values-list [Function]
  3986. @code{values-list} @i{list} @result{} @i{@{@i{element}@}{*}}
  3987. @subsubheading Arguments and Values::
  3988. @i{list}---a @i{list}.
  3989. @i{elements}---the @i{elements} of the @i{list}.
  3990. @subsubheading Description::
  3991. Returns the @i{elements} of the @i{list} as @i{multiple values}_2.
  3992. @subsubheading Examples::
  3993. @example
  3994. (values-list nil) @result{} <@i{no @i{values}}>
  3995. (values-list '(1)) @result{} 1
  3996. (values-list '(1 2)) @result{} 1, 2
  3997. (values-list '(1 2 3)) @result{} 1, 2, 3
  3998. @end example
  3999. @subsubheading Exceptional Situations::
  4000. Should signal @b{type-error} if its argument is not a @i{proper list}.
  4001. @subsubheading See Also::
  4002. @ref{multiple-value-bind}
  4003. ,
  4004. @ref{multiple-value-list}
  4005. ,
  4006. @ref{multiple-values-limit}
  4007. ,
  4008. @ref{values}
  4009. @subsubheading Notes::
  4010. @example
  4011. (values-list @i{list}) @equiv{} (apply #'values @i{list})
  4012. @end example
  4013. @t{(equal @i{x} (multiple-value-list (values-list @i{x})))}
  4014. returns @i{true} for all @i{lists} @i{x}.
  4015. @node multiple-values-limit, nth-value, values-list, Data and Control Flow Dictionary
  4016. @subsection multiple-values-limit [Constant Variable]
  4017. @subsubheading Constant Value::
  4018. An @i{integer} not smaller than @t{20},
  4019. the exact magnitude of which is @i{implementation-dependent}.
  4020. @subsubheading Description::
  4021. The upper exclusive bound on the number of @i{values} that may be
  4022. returned from a @i{function},
  4023. bound or assigned by @b{multiple-value-bind} or @b{multiple-value-setq},
  4024. or passed as a first argument to @b{nth-value}.
  4025. (If these individual limits might differ, the minimum value is used.)
  4026. @subsubheading See Also::
  4027. @ref{lambda-parameters-limit}
  4028. ,
  4029. @ref{call-arguments-limit}
  4030. @subsubheading Notes::
  4031. Implementors are encouraged to make this limit as large as possible.
  4032. @node nth-value, prog, multiple-values-limit, Data and Control Flow Dictionary
  4033. @subsection nth-value [Macro]
  4034. @code{nth-value} @i{n form} @result{} @i{object}
  4035. @subsubheading Arguments and Values::
  4036. @i{n}---a non-negative @i{integer}; evaluated.
  4037. @i{form}---a @i{form}; evaluated as described below.
  4038. @i{object}---an @i{object}.
  4039. @subsubheading Description::
  4040. Evaluates @i{n} and then @i{form},
  4041. returning as its only value the @i{n}th value @i{yielded} by @i{form},
  4042. or @b{nil} if @i{n} is greater than or equal to the number of @i{values}
  4043. returned by @i{form}. (The first returned value is numbered @t{0}.)
  4044. @subsubheading Examples::
  4045. @example
  4046. (nth-value 0 (values 'a 'b)) @result{} A
  4047. (nth-value 1 (values 'a 'b)) @result{} B
  4048. (nth-value 2 (values 'a 'b)) @result{} NIL
  4049. (let* ((x 83927472397238947423879243432432432)
  4050. (y 32423489732)
  4051. (a (nth-value 1 (floor x y)))
  4052. (b (mod x y)))
  4053. (values a b (= a b)))
  4054. @result{} 3332987528, 3332987528, @i{true}
  4055. @end example
  4056. @subsubheading See Also::
  4057. @ref{multiple-value-list}
  4058. ,
  4059. @ref{nth}
  4060. @subsubheading Notes::
  4061. Operationally, the following relationship is true, although @b{nth-value}
  4062. might be more efficient in some @i{implementations}
  4063. because, for example, some @i{consing} might be avoided.
  4064. @example
  4065. (nth-value @i{n} @i{form}) @equiv{} (nth @i{n} (multiple-value-list @i{form}))
  4066. @end example
  4067. @node prog, prog1, nth-value, Data and Control Flow Dictionary
  4068. @subsection prog, prog* [Macro]
  4069. @code{prog} @i{@r{(}@{@i{var} |
  4070. @r{(}@i{var} @r{[}@i{init-form}@r{]}@r{)}@}{*}@r{)}
  4071. @{@i{declaration}@}{*}
  4072. @{@i{tag} | @i{statement}@}{*}}@*
  4073. @result{} @i{@{@i{result}@}{*}}
  4074. @code{prog*} @i{@r{(}@{@i{var} |
  4075. @r{(}@i{var} @r{[}@i{init-form}@r{]}@r{)}@}{*}@r{)}
  4076. @{@i{declaration}@}{*}
  4077. @{@i{tag} | @i{statement}@}{*}}@*
  4078. @result{} @i{@{@i{result}@}{*}}
  4079. @subsubheading Arguments and Values::
  4080. @i{var}---variable name.
  4081. @i{init-form}---a @i{form}.
  4082. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  4083. @i{tag}---a @i{go tag}; not evaluated.
  4084. @i{statement}---a @i{compound form}; evaluated as described below.
  4085. @i{results}---@b{nil} if a @i{normal return} occurs,
  4086. or else, if an @i{explicit return} occurs, the @i{values} that were transferred.
  4087. @subsubheading Description::
  4088. Three distinct operations are performed by @b{prog} and
  4089. @b{prog*}:
  4090. they bind local variables,
  4091. they permit use of the @b{return}
  4092. statement, and they permit use of the @b{go}
  4093. statement.
  4094. A typical @b{prog} looks like this:
  4095. @example
  4096. (prog (var1 var2 (var3 init-form-3) var4 (var5 init-form-5))
  4097. @{@i{declaration}@}{*}
  4098. statement1
  4099. tag1
  4100. statement2
  4101. statement3
  4102. statement4
  4103. tag2
  4104. statement5
  4105. ...
  4106. )
  4107. @end example
  4108. For @b{prog},
  4109. @i{init-forms} are evaluated first, in the order in which they are
  4110. supplied. The @i{vars} are then bound to the corresponding values in
  4111. parallel. If no @i{init-form}
  4112. is supplied for a given @i{var},
  4113. that @i{var} is bound to @b{nil}.
  4114. The body of @b{prog} is executed as if it were a @b{tagbody} @i{form};
  4115. the @b{go} statement can be used to transfer control
  4116. to a @i{tag}.
  4117. @i{Tags} label @i{statements}.
  4118. @b{prog} implicitly establishes a @b{block} named @b{nil} around
  4119. the entire @b{prog} @i{form}, so that @b{return} can be used
  4120. at any time to exit from the @b{prog} @i{form}.
  4121. The difference between @b{prog*} and @b{prog} is that
  4122. in @b{prog*} the @i{binding} and initialization of the @i{vars}
  4123. is done @i{sequentially}, so that the @i{init-form} for each
  4124. one can use the values of previous ones.
  4125. @subsubheading Examples::
  4126. @example
  4127. (prog* ((y z) (x (car y)))
  4128. (return x))
  4129. @end example
  4130. returns the @i{car} of the value of @t{z}.
  4131. @example
  4132. (setq a 1) @result{} 1
  4133. (prog ((a 2) (b a)) (return (if (= a b) '= '/=))) @result{} /=
  4134. (prog* ((a 2) (b a)) (return (if (= a b) '= '/=))) @result{} =
  4135. (prog () 'no-return-value) @result{} NIL
  4136. @end example
  4137. @example
  4138. (defun king-of-confusion (w)
  4139. "Take a cons of two lists and make a list of conses.
  4140. Think of this function as being like a zipper."
  4141. (prog (x y z) ;Initialize x, y, z to NIL
  4142. (setq y (car w) z (cdr w))
  4143. loop
  4144. (cond ((null y) (return x))
  4145. ((null z) (go err)))
  4146. rejoin
  4147. (setq x (cons (cons (car y) (car z)) x))
  4148. (setq y (cdr y) z (cdr z))
  4149. (go loop)
  4150. err
  4151. (cerror "Will self-pair extraneous items"
  4152. "Mismatch - gleep! ~S" y)
  4153. (setq z y)
  4154. (go rejoin))) @result{} KING-OF-CONFUSION
  4155. @end example
  4156. This can be accomplished more perspicuously as follows:
  4157. @example
  4158. (defun prince-of-clarity (w)
  4159. "Take a cons of two lists and make a list of conses.
  4160. Think of this function as being like a zipper."
  4161. (do ((y (car w) (cdr y))
  4162. (z (cdr w) (cdr z))
  4163. (x '@t{()} (cons (cons (car y) (car z)) x)))
  4164. ((null y) x)
  4165. (when (null z)
  4166. (cerror "Will self-pair extraneous items"
  4167. "Mismatch - gleep! ~S" y)
  4168. (setq z y)))) @result{} PRINCE-OF-CLARITY
  4169. @end example
  4170. @subsubheading See Also::
  4171. @ref{block}
  4172. ,
  4173. @ref{let; let*}
  4174. ,
  4175. @ref{tagbody}
  4176. ,
  4177. @ref{go}
  4178. ,
  4179. @ref{return}
  4180. , @ref{Evaluation}
  4181. @subsubheading Notes::
  4182. @b{prog} can be explained in terms of
  4183. @b{block}, @b{let}, and @b{tagbody} as
  4184. follows:
  4185. @example
  4186. (prog @i{variable-list} @i{declaration} . @i{body})
  4187. @equiv{} (block nil (let @i{variable-list} @i{declaration} (tagbody . @i{body})))
  4188. @end example
  4189. @node prog1, progn, prog, Data and Control Flow Dictionary
  4190. @subsection prog1, prog2 [Macro]
  4191. @code{prog} @i{1} @result{} @i{first-form @{@i{form}@}{*}}
  4192. {result-1}
  4193. @code{prog} @i{2} @result{} @i{first-form second-form @{@i{form}@}{*}}
  4194. {result-2}
  4195. @subsubheading Arguments and Values::
  4196. @i{first-form}---a @i{form}; evaluated as described below.
  4197. @i{second-form}---a @i{form}; evaluated as described below.
  4198. @i{forms}---an @i{implicit progn}; evaluated as described below.
  4199. @i{result-1}---the @i{primary value} resulting from
  4200. the @i{evaluation} of @i{first-form}.
  4201. @i{result-2}---the @i{primary value} resulting from
  4202. the @i{evaluation} of @i{second-form}.
  4203. @subsubheading Description::
  4204. @b{prog1} @i{evaluates} @i{first-form}
  4205. and then @i{forms},
  4206. @i{yielding} as its only @i{value}
  4207. the @i{primary value} @i{yielded} by @i{first-form}.
  4208. @b{prog2} @i{evaluates} @i{first-form},
  4209. then @i{second-form},
  4210. and then @i{forms},
  4211. @i{yielding} as its only @i{value}
  4212. the @i{primary value} @i{yielded} by @i{first-form}.
  4213. @subsubheading Examples::
  4214. @example
  4215. (setq temp 1) @result{} 1
  4216. (prog1 temp (print temp) (incf temp) (print temp))
  4217. @t{ |> } 1
  4218. @t{ |> } 2
  4219. @result{} 1
  4220. (prog1 temp (setq temp nil)) @result{} 2
  4221. temp @result{} NIL
  4222. (prog1 (values 1 2 3) 4) @result{} 1
  4223. (setq temp (list 'a 'b 'c))
  4224. (prog1 (car temp) (setf (car temp) 'alpha)) @result{} A
  4225. temp @result{} (ALPHA B C)
  4226. (flet ((swap-symbol-values (x y)
  4227. (setf (symbol-value x)
  4228. (prog1 (symbol-value y)
  4229. (setf (symbol-value y) (symbol-value x))))))
  4230. (let ((*foo* 1) (*bar* 2))
  4231. (declare (special *foo* *bar*))
  4232. (swap-symbol-values '*foo* '*bar*)
  4233. (values *foo* *bar*)))
  4234. @result{} 2, 1
  4235. (setq temp 1) @result{} 1
  4236. (prog2 (incf temp) (incf temp) (incf temp)) @result{} 3
  4237. temp @result{} 4
  4238. (prog2 1 (values 2 3 4) 5) @result{} 2
  4239. @end example
  4240. @subsubheading See Also::
  4241. @ref{multiple-value-prog1}
  4242. ,
  4243. @ref{progn}
  4244. @subsubheading Notes::
  4245. @b{prog1} and @b{prog2} are typically used to @i{evaluate}
  4246. one or more @i{forms} with side effects and return a @i{value} that
  4247. must be computed before some or all of the side effects happen.
  4248. @example
  4249. (prog1 @{@i{form}@}{*}) @equiv{} (values (multiple-value-prog1 @{@i{form}@}{*}))
  4250. (prog2 @i{form1} @{@i{form}@}{*}) @equiv{} (let () @i{form1} (prog1 @{@i{form}@}{*}))
  4251. @end example
  4252. @node progn, define-modify-macro, prog1, Data and Control Flow Dictionary
  4253. @subsection progn [Special Operator]
  4254. @code{progn} @i{@{@i{form}@}{*}} @result{} @i{@{@i{result}@}{*}}
  4255. @subsubheading Arguments and Values::
  4256. @i{forms}---an @i{implicit progn}.
  4257. @i{results}---the @i{values} of the @i{forms}.
  4258. @subsubheading Description::
  4259. @b{progn} evaluates @i{forms},
  4260. in the order in which they are given.
  4261. The values of each @i{form} but the last are discarded.
  4262. If @b{progn} appears as a @i{top level form}, then all @i{forms}
  4263. within that @b{progn} are considered by the compiler to be
  4264. @i{top level forms}.
  4265. @subsubheading Examples::
  4266. @example
  4267. (progn) @result{} NIL
  4268. (progn 1 2 3) @result{} 3
  4269. (progn (values 1 2 3)) @result{} 1, 2, 3
  4270. (setq a 1) @result{} 1
  4271. (if a
  4272. (progn (setq a nil) 'here)
  4273. (progn (setq a t) 'there)) @result{} HERE
  4274. a @result{} NIL
  4275. @end example
  4276. @subsubheading See Also::
  4277. @ref{prog1; prog2}
  4278. , @b{prog2}, @ref{Evaluation}
  4279. @subsubheading Notes::
  4280. Many places in @r{Common Lisp} involve syntax that uses @i{implicit progns}.
  4281. That is, part of their syntax allows many @i{forms} to be written
  4282. that are to be evaluated sequentially, discarding the results
  4283. of all @i{forms} but the last and returning the results of the last @i{form}.
  4284. Such places include, but are not limited to, the following:
  4285. the body of a @i{lambda expression};
  4286. the bodies of various control and conditional @i{forms}
  4287. (@i{e.g.}, @b{case}, @b{catch}, @b{progn}, and @b{when}).
  4288. @node define-modify-macro, defsetf, progn, Data and Control Flow Dictionary
  4289. @subsection define-modify-macro [Macro]
  4290. @code{define-modify-macro} @i{name lambda-list function @r{[}documentation@r{]}} @result{} @i{name}
  4291. @subsubheading Arguments and Values::
  4292. @i{name}---a @i{symbol}.
  4293. @i{lambda-list}---a @i{define-modify-macro lambda list}
  4294. @i{function}---a @i{symbol}.
  4295. @i{documentation}---a @i{string}; not evaluated.
  4296. @subsubheading Description::
  4297. @b{define-modify-macro} defines a @i{macro} named
  4298. @i{name} to @i{read} and @i{write} a @i{place}.
  4299. The arguments to the new @i{macro} are a @i{place},
  4300. followed
  4301. by the arguments that are supplied in @i{lambda-list}.
  4302. @i{Macros} defined with @b{define-modify-macro}
  4303. correctly pass the @i{environment parameter} to
  4304. @b{get-setf-expansion}.
  4305. When the @i{macro} is invoked, @i{function}
  4306. is applied to the old contents of the @i{place}
  4307. and the @i{lambda-list} arguments to obtain the new value,
  4308. and the @i{place} is updated to contain the result.
  4309. Except for the issue of avoiding multiple evaluation (see below), the expansion
  4310. of a @b{define-modify-macro} is equivalent to the following:
  4311. @example
  4312. (defmacro @i{name} (reference . @i{lambda-list})
  4313. @i{documentation}
  4314. `(setf ,reference
  4315. (@i{function} ,reference ,@i{arg1} ,@i{arg2} ...)))
  4316. @end example
  4317. where @i{arg1}, @i{arg2}, ...,
  4318. are the parameters appearing in @i{lambda-list};
  4319. appropriate provision is made for a @i{rest parameter}.
  4320. The @i{subforms} of the macro calls defined by @b{define-modify-macro}
  4321. are evaluated as specified in @ref{Evaluation of Subforms to Places}.
  4322. @i{Documentation} is attached as a @i{documentation string}
  4323. to @i{name} (as kind @b{function})
  4324. and to the @i{macro function}.
  4325. If a @b{define-modify-macro} @i{form} appears as a @i{top level form},
  4326. the @i{compiler} must store the @i{macro} definition at compile time,
  4327. so that occurrences of the macro later on in the file can be expanded correctly.
  4328. @subsubheading Examples::
  4329. @example
  4330. (define-modify-macro appendf (&rest args)
  4331. append "Append onto list") @result{} APPENDF
  4332. (setq x '(a b c) y x) @result{} (A B C)
  4333. (appendf x '(d e f) '(1 2 3)) @result{} (A B C D E F 1 2 3)
  4334. x @result{} (A B C D E F 1 2 3)
  4335. y @result{} (A B C)
  4336. (define-modify-macro new-incf (&optional (delta 1)) +)
  4337. (define-modify-macro unionf (other-set &rest keywords) union)
  4338. @end example
  4339. @subsubheading Side Effects::
  4340. A macro definition is assigned to @i{name}.
  4341. @subsubheading See Also::
  4342. @ref{defsetf}
  4343. ,
  4344. @ref{define-setf-expander}
  4345. ,
  4346. @ref{documentation; (setf documentation)}
  4347. ,
  4348. @ref{Syntactic Interaction of Documentation Strings and Declarations}
  4349. @node defsetf, define-setf-expander, define-modify-macro, Data and Control Flow Dictionary
  4350. @subsection defsetf [Macro]
  4351. The ``short form'':
  4352. @code{defsetf} @i{access-fn update-fn @r{[}documentation@r{]}}@*
  4353. @result{} @i{access-fn}
  4354. The ``long form'':
  4355. @code{defsetf} @i{access-fn lambda-list @r{(}@{@i{store-variable}@}{*}@r{)}
  4356. {[[@{@i{declaration}@}{*} | @i{documentation}]]} @{@i{form}@}{*}}@*
  4357. @result{} @i{access-fn}
  4358. @subsubheading Arguments and Values::
  4359. @i{access-fn}---a @i{symbol} which names a @i{function} or a @i{macro}.
  4360. @i{update-fn}---a @i{symbol} naming a @i{function} or @i{macro}.
  4361. @i{lambda-list}---a @i{defsetf lambda list}.
  4362. @i{store-variable}---a @i{symbol} (a @i{variable} @i{name}).
  4363. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  4364. @i{documentation}---a @i{string}; not evaluated.
  4365. @i{form}---a @i{form}.
  4366. @subsubheading Description::
  4367. @b{defsetf} defines how to
  4368. @b{setf} a @i{place}
  4369. of the form @t{(@i{access-fn} ...)} for relatively simple cases.
  4370. (See @b{define-setf-expander} for more general access to this facility.)
  4371. It must be the case that the @i{function} or @i{macro} named by @i{access-fn}
  4372. evaluates all of its arguments.
  4373. @b{defsetf} may take one of two forms, called the ``short form'' and the ``long form,''
  4374. which are distinguished by the @i{type} of the second @i{argument}.
  4375. When the short form is used,
  4376. @i{update-fn} must name
  4377. a @i{function} (or @i{macro}) that takes one more argument
  4378. than @i{access-fn} takes. When @b{setf} is given a @i{place}
  4379. that is a call on @i{access-fn}, it expands into
  4380. a call on @i{update-fn} that is given all the arguments to
  4381. @i{access-fn} and also, as its last argument, the new value
  4382. (which must be returned by @i{update-fn} as its value).
  4383. The long form @b{defsetf}
  4384. resembles @b{defmacro}.
  4385. The @i{lambda-list} describes the arguments of @i{access-fn}.
  4386. The @i{store-variables} describe the
  4387. value
  4388. or values
  4389. to be stored into the @i{place}.
  4390. The @i{body} must
  4391. compute the expansion of a @b{setf} of a call on @i{access-fn}.
  4392. The expansion function is defined in the same @i{lexical environment}
  4393. in which the @b{defsetf} @i{form} appears.
  4394. During the evaluation of the
  4395. @i{forms}, the variables in the @i{lambda-list} and the
  4396. @i{store-variables}
  4397. are bound to names of temporary variables,
  4398. generated as if by @b{gensym}
  4399. or @b{gentemp},
  4400. that will be bound by the
  4401. expansion of @b{setf}
  4402. to the values of those @i{subforms}. This binding
  4403. permits the
  4404. @i{forms} to be written without regard for order-of-evaluation
  4405. issues. @b{defsetf} arranges for the temporary variables to be
  4406. optimized out of the final result in cases where that is possible.
  4407. The body code in @b{defsetf} is implicitly enclosed in a
  4408. @i{block} whose name is
  4409. @i{access-fn}
  4410. @b{defsetf}
  4411. ensures that @i{subforms}
  4412. of the @i{place} are evaluated exactly once.
  4413. @i{Documentation} is attached to @i{access-fn} as a @i{documentation string}
  4414. of kind @b{setf}.
  4415. If a @b{defsetf} @i{form} appears as a @i{top level form},
  4416. the @i{compiler} must make the @i{setf expander} available so that
  4417. it may be used to expand calls to @b{setf} later on in the @i{file}.
  4418. Users must ensure that the @i{forms}, if any, can be evaluated
  4419. at compile time if the @i{access-fn} is used in a @i{place}
  4420. later in the same @i{file}.
  4421. The @i{compiler} must make these @i{setf expanders} available to
  4422. compile-time calls to @b{get-setf-expansion} when its @i{environment}
  4423. argument is a value received as the @i{environment parameter} of a @i{macro}.
  4424. @subsubheading Examples::
  4425. The effect of
  4426. @example
  4427. (defsetf symbol-value set)
  4428. @end example
  4429. is built into the @r{Common Lisp} system.
  4430. This causes the form @t{(setf (symbol-value foo) fu)}
  4431. to expand into @t{(set foo fu)}.
  4432. Note that
  4433. @example
  4434. (defsetf car rplaca)
  4435. @end example
  4436. would be incorrect because @b{rplaca} does not return its last argument.
  4437. @example
  4438. (defun middleguy (x) (nth (truncate (1- (list-length x)) 2) x)) @result{} MIDDLEGUY
  4439. (defun set-middleguy (x v)
  4440. (unless (null x)
  4441. (rplaca (nthcdr (truncate (1- (list-length x)) 2) x) v))
  4442. v) @result{} SET-MIDDLEGUY
  4443. (defsetf middleguy set-middleguy) @result{} MIDDLEGUY
  4444. (setq a (list 'a 'b 'c 'd)
  4445. b (list 'x)
  4446. c (list 1 2 3 (list 4 5 6) 7 8 9)) @result{} (1 2 3 (4 5 6) 7 8 9)
  4447. (setf (middleguy a) 3) @result{} 3
  4448. (setf (middleguy b) 7) @result{} 7
  4449. (setf (middleguy (middleguy c)) 'middleguy-symbol) @result{} MIDDLEGUY-SYMBOL
  4450. a @result{} (A 3 C D)
  4451. b @result{} (7)
  4452. c @result{} (1 2 3 (4 MIDDLEGUY-SYMBOL 6) 7 8 9)
  4453. @end example
  4454. An example of the use of the long form of @b{defsetf}:
  4455. @example
  4456. (defsetf subseq (sequence start &optional end) (new-sequence)
  4457. `(progn (replace ,sequence ,new-sequence
  4458. :start1 ,start :end1 ,end)
  4459. ,new-sequence)) @result{} SUBSEQ
  4460. @end example
  4461. @example
  4462. (defvar *xy* (make-array '(10 10)))
  4463. (defun xy (&key ((x x) 0) ((y y) 0)) (aref *xy* x y)) @result{} XY
  4464. (defun set-xy (new-value &key ((x x) 0) ((y y) 0))
  4465. (setf (aref *xy* x y) new-value)) @result{} SET-XY
  4466. (defsetf xy (&key ((x x) 0) ((y y) 0)) (store)
  4467. `(set-xy ,store 'x ,x 'y ,y)) @result{} XY
  4468. (get-setf-expansion '(xy a b))
  4469. @result{} (#:t0 #:t1),
  4470. (a b),
  4471. (#:store),
  4472. ((lambda (&key ((x #:x)) ((y #:y)))
  4473. (set-xy #:store 'x #:x 'y #:y))
  4474. #:t0 #:t1),
  4475. (xy #:t0 #:t1)
  4476. (xy 'x 1) @result{} NIL
  4477. (setf (xy 'x 1) 1) @result{} 1
  4478. (xy 'x 1) @result{} 1
  4479. (let ((a 'x) (b 'y))
  4480. (setf (xy a 1 b 2) 3)
  4481. (setf (xy b 5 a 9) 14))
  4482. @result{} 14
  4483. (xy 'y 0 'x 1) @result{} 1
  4484. (xy 'x 1 'y 2) @result{} 3
  4485. @end example
  4486. @subsubheading See Also::
  4487. @ref{documentation; (setf documentation)}
  4488. ,
  4489. @ref{setf; psetf}
  4490. ,
  4491. @ref{define-setf-expander}
  4492. ,
  4493. @ref{get-setf-expansion}
  4494. ,
  4495. @ref{Generalized Reference},
  4496. @ref{Syntactic Interaction of Documentation Strings and Declarations}
  4497. @subsubheading Notes::
  4498. @i{forms} must include provision
  4499. for returning the correct value (the value
  4500. or values
  4501. of @i{store-variable}).
  4502. This is
  4503. handled by @i{forms} rather than by @b{defsetf} because
  4504. in many cases this value can be returned at no extra cost, by calling a
  4505. function that simultaneously stores into the @i{place} and
  4506. returns the correct value.
  4507. A @b{setf} of a call on @i{access-fn} also evaluates
  4508. all of @i{access-fn}'s arguments; it cannot treat any of them specially.
  4509. This means that @b{defsetf}
  4510. cannot be used to describe how to store into
  4511. a @i{generalized reference} to a byte, such as @t{(ldb field reference)}.
  4512. @b{define-setf-expander}
  4513. is used to handle situations that
  4514. do not fit the restrictions imposed by @b{defsetf}
  4515. and gives the user additional control.
  4516. @node define-setf-expander, get-setf-expansion, defsetf, Data and Control Flow Dictionary
  4517. @subsection define-setf-expander [Macro]
  4518. @code{define-setf-expander} @i{access-fn lambda-list
  4519. {[[@{@i{declaration}@}{*} | @i{documentation}]]} @{@i{form}@}{*}}@*
  4520. @result{} @i{access-fn}
  4521. @subsubheading Arguments and Values::
  4522. @i{access-fn}---a @i{symbol} that @i{names} a @i{function} or @i{macro}.
  4523. @i{lambda-list} -- @i{macro lambda list}.
  4524. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  4525. @i{documentation}---a @i{string}; not evaluated.
  4526. @i{forms}---an @i{implicit progn}.
  4527. @subsubheading Description::
  4528. @b{define-setf-expander} specifies the means by which @b{setf}
  4529. updates a @i{place} that is referenced by @i{access-fn}.
  4530. When @b{setf} is given a @i{place} that is
  4531. specified in terms of @i{access-fn} and a new value for the
  4532. @i{place}, it is expanded into a form that performs
  4533. the appropriate update.
  4534. The @i{lambda-list} supports destructuring.
  4535. See @ref{Macro Lambda Lists}.
  4536. @i{Documentation} is attached to @i{access-fn} as a @i{documentation string}
  4537. of kind @b{setf}.
  4538. @i{Forms} constitute the body of the
  4539. @i{setf expander}
  4540. definition and must compute the @i{setf expansion} for a call on @b{setf}
  4541. that references the @i{place} by means of the given
  4542. @i{access-fn}.
  4543. The @i{setf expander} function is defined in the same @i{lexical environment}
  4544. in which the @b{define-setf-expander} @i{form} appears.
  4545. While @i{forms} are being executed,
  4546. the variables in @i{lambda-list} are bound to parts of the @i{place} @i{form}.
  4547. The body @i{forms} (but not the @i{lambda-list})
  4548. in a @b{define-setf-expander} @i{form} are implicitly enclosed in a
  4549. @i{block} whose name is
  4550. @i{access-fn}.
  4551. The evaluation of @i{forms} must result in the five values
  4552. described in @ref{Setf Expansions}.
  4553. If a @b{define-setf-expander} @i{form} appears as a @i{top level form},
  4554. the @i{compiler} must make the @i{setf expander} available so that
  4555. it may be used to expand calls to @b{setf} later on in the @i{file}.
  4556. @i{Programmers} must ensure that the @i{forms} can be evaluated
  4557. at compile time if the @i{access-fn} is used in a @i{place}
  4558. later in the same @i{file}.
  4559. The @i{compiler} must make these @i{setf expanders} available to
  4560. compile-time calls to @b{get-setf-expansion} when its @i{environment}
  4561. argument is a value received as the @i{environment parameter} of a @i{macro}.
  4562. @subsubheading Examples::
  4563. @example
  4564. (defun lastguy (x) (car (last x))) @result{} LASTGUY
  4565. (define-setf-expander lastguy (x &environment env)
  4566. "Set the last element in a list to the given value."
  4567. (multiple-value-bind (dummies vals newval setter getter)
  4568. (get-setf-expansion x env)
  4569. (let ((store (gensym)))
  4570. (values dummies
  4571. vals
  4572. `(,store)
  4573. `(progn (rplaca (last ,getter) ,store) ,store)
  4574. `(lastguy ,getter))))) @result{} LASTGUY
  4575. (setq a (list 'a 'b 'c 'd)
  4576. b (list 'x)
  4577. c (list 1 2 3 (list 4 5 6))) @result{} (1 2 3 (4 5 6))
  4578. (setf (lastguy a) 3) @result{} 3
  4579. (setf (lastguy b) 7) @result{} 7
  4580. (setf (lastguy (lastguy c)) 'lastguy-symbol) @result{} LASTGUY-SYMBOL
  4581. a @result{} (A B C 3)
  4582. b @result{} (7)
  4583. c @result{} (1 2 3 (4 5 LASTGUY-SYMBOL))
  4584. @end example
  4585. @example
  4586. ;;; Setf expander for the form (LDB bytespec int).
  4587. ;;; Recall that the int form must itself be suitable for SETF.
  4588. (define-setf-expander ldb (bytespec int &environment env)
  4589. (multiple-value-bind (temps vals stores
  4590. store-form access-form)
  4591. (get-setf-expansion int env);Get setf expansion for int.
  4592. (let ((btemp (gensym)) ;Temp var for byte specifier.
  4593. (store (gensym)) ;Temp var for byte to store.
  4594. (stemp (first stores))) ;Temp var for int to store.
  4595. (if (cdr stores) (error "Can't expand this."))
  4596. ;;; Return the setf expansion for LDB as five values.
  4597. (values (cons btemp temps) ;Temporary variables.
  4598. (cons bytespec vals) ;Value forms.
  4599. (list store) ;Store variables.
  4600. `(let ((,stemp (dpb ,store ,btemp ,access-form)))
  4601. ,store-form
  4602. ,store) ;Storing form.
  4603. `(ldb ,btemp ,access-form) ;Accessing form.
  4604. ))))
  4605. @end example
  4606. @subsubheading See Also::
  4607. @ref{setf; psetf}
  4608. ,
  4609. @ref{defsetf}
  4610. ,
  4611. @ref{documentation; (setf documentation)}
  4612. ,
  4613. @ref{get-setf-expansion}
  4614. ,
  4615. @ref{Syntactic Interaction of Documentation Strings and Declarations}
  4616. @subsubheading Notes::
  4617. @b{define-setf-expander} differs from the long form of @b{defsetf}
  4618. in that while the body is being executed the @i{variables}
  4619. in @i{lambda-list} are bound to parts of the @i{place} @i{form},
  4620. not to temporary variables that will be bound to the values of such parts.
  4621. In addition, @b{define-setf-expander} does not have @b{defsetf}'s
  4622. restriction that @i{access-fn} must be a @i{function}
  4623. or a function-like @i{macro}; an arbitrary @b{defmacro} destructuring
  4624. pattern is permitted in @i{lambda-list}.
  4625. @node get-setf-expansion, setf, define-setf-expander, Data and Control Flow Dictionary
  4626. @subsection get-setf-expansion [Function]
  4627. @code{get-setf-expansion} @i{place {&optional} environment}@*
  4628. @result{} @i{vars, vals, store-vars, writer-form, reader-form}
  4629. @subsubheading Arguments and Values::
  4630. @i{place}---a @i{place}.
  4631. @i{environment}---an @i{environment} @i{object}.
  4632. @i{vars, vals, store-vars, writer-form, reader-form}---a @i{setf expansion}.
  4633. @subsubheading Description::
  4634. Determines
  4635. five values constituting the @i{setf expansion} for @i{place}
  4636. in @i{environment}; see @ref{Setf Expansions}.
  4637. If @i{environment} is not supplied or @b{nil},
  4638. the environment is the @i{null lexical environment}.
  4639. @subsubheading Examples::
  4640. @example
  4641. (get-setf-expansion 'x)
  4642. @result{} NIL, NIL, (#:G0001), (SETQ X #:G0001), X
  4643. @end example
  4644. @example
  4645. ;;; This macro is like POP
  4646. (defmacro xpop (place &environment env)
  4647. (multiple-value-bind (dummies vals new setter getter)
  4648. (get-setf-expansion place env)
  4649. `(let* (,@@(mapcar #'list dummies vals) (,(car new) ,getter))
  4650. (if (cdr new) (error "Can't expand this."))
  4651. (prog1 (car ,(car new))
  4652. (setq ,(car new) (cdr ,(car new)))
  4653. ,setter))))
  4654. (defsetf frob (x) (value)
  4655. `(setf (car ,x) ,value)) @result{} FROB
  4656. ;;; The following is an error; an error might be signaled at macro expansion time
  4657. (flet ((frob (x) (cdr x))) ;Invalid
  4658. (xpop (frob z)))
  4659. @end example
  4660. @subsubheading See Also::
  4661. @ref{defsetf}
  4662. ,
  4663. @ref{define-setf-expander}
  4664. ,
  4665. @ref{setf; psetf}
  4666. @subsubheading Notes::
  4667. Any @i{compound form} is a valid @i{place},
  4668. since any @i{compound form} whose @i{operator} @i{f} has no @i{setf expander}
  4669. are expanded into a call to @t{(setf @i{f})}.
  4670. @node setf, shiftf, get-setf-expansion, Data and Control Flow Dictionary
  4671. @subsection setf, psetf [Macro]
  4672. @code{setf} @i{@{!@i{pair}@}{*}} @result{} @i{@{@i{result}@}{*}}
  4673. @code{psetf} @i{@{!@i{pair}@}{*}} @result{} @i{@b{nil}}
  4674. @w{@i{pair} ::=place newvalue}
  4675. @subsubheading Arguments and Values::
  4676. @i{place}---a @i{place}.
  4677. @i{newvalue}---a @i{form}.
  4678. @i{results}---the @i{multiple values}_2
  4679. returned by the storing form for the last @i{place},
  4680. or @b{nil} if there are no @i{pairs}.
  4681. @subsubheading Description::
  4682. @b{setf} changes the @i{value} of @i{place} to be @i{newvalue}.
  4683. @t{(setf place newvalue)}
  4684. expands into an update form that stores the
  4685. result
  4686. of evaluating
  4687. @i{newvalue} into the location referred to by @i{place}.
  4688. Some @i{place} forms
  4689. involve uses of accessors that take optional arguments.
  4690. Whether those optional arguments are permitted by
  4691. @b{setf}, or what their use
  4692. is, is up to the
  4693. @b{setf} expander function and is not under the control
  4694. of @b{setf}.
  4695. The documentation for any @i{function}
  4696. that accepts @b{&optional}, @b{&rest},
  4697. or @t{&key} arguments and that
  4698. claims to be usable with @b{setf} must specify
  4699. how those arguments are treated.
  4700. If more than one @i{pair} is supplied,
  4701. the @i{pairs} are processed sequentially; that is,
  4702. @example
  4703. (setf place-1 newvalue-1
  4704. place-2 newvalue-2
  4705. ...
  4706. place-N newvalue-N)
  4707. @end example
  4708. is precisely equivalent to
  4709. @example
  4710. (progn (setf place-1 newvalue-1)
  4711. (setf place-2 newvalue-2)
  4712. ...
  4713. (setf place-N newvalue-N))
  4714. @end example
  4715. For @b{psetf},
  4716. if more than one @i{pair} is supplied then the assignments of new values to places are
  4717. done in parallel. More precisely, all @i{subforms} (in both the @i{place}
  4718. and @i{newvalue} @i{forms}) that are to be evaluated
  4719. are evaluated from left to right; after all evaluations have been performed,
  4720. all of the assignments are performed in an unpredictable order.
  4721. For detailed treatment of the expansion of @b{setf} and @b{psetf},
  4722. see @ref{Kinds of Places}.
  4723. @subsubheading Examples::
  4724. @example
  4725. (setq x (cons 'a 'b) y (list 1 2 3)) @result{} (1 2 3)
  4726. (setf (car x) 'x (cadr y) (car x) (cdr x) y) @result{} (1 X 3)
  4727. x @result{} (X 1 X 3)
  4728. y @result{} (1 X 3)
  4729. (setq x (cons 'a 'b) y (list 1 2 3)) @result{} (1 2 3)
  4730. (psetf (car x) 'x (cadr y) (car x) (cdr x) y) @result{} NIL
  4731. x @result{} (X 1 A 3)
  4732. y @result{} (1 A 3)
  4733. @end example
  4734. @subsubheading Affected By::
  4735. @b{define-setf-expander},
  4736. @b{defsetf},
  4737. @b{*macroexpand-hook*}
  4738. @subsubheading See Also::
  4739. @ref{define-setf-expander}
  4740. ,
  4741. @ref{defsetf}
  4742. ,
  4743. @b{macroexpand-1},
  4744. @ref{rotatef}
  4745. ,
  4746. @ref{shiftf}
  4747. ,
  4748. @ref{Generalized Reference}
  4749. @node shiftf, rotatef, setf, Data and Control Flow Dictionary
  4750. @subsection shiftf [Macro]
  4751. @code{shiftf} @i{@{@i{place}@}^+ newvalue} @result{} @i{old-value-1}
  4752. @subsubheading Arguments and Values::
  4753. @i{place}---a @i{place}.
  4754. @i{newvalue}---a @i{form}; evaluated.
  4755. @i{old-value-1}---an @i{object} (the old @i{value} of the first @i{place}).
  4756. @subsubheading Description::
  4757. @b{shiftf} modifies the values of each
  4758. @i{place} by storing @i{newvalue}
  4759. into the last @i{place}, and shifting the
  4760. values of the second through the last @i{place}
  4761. into the remaining @i{places}.
  4762. If @i{newvalue} produces more values than there
  4763. are store variables, the extra values are ignored. If @i{newvalue}
  4764. produces fewer values than there are store variables, the missing values
  4765. are set to @b{nil}.
  4766. In the form @t{(shiftf @i{place1} @i{place2} ... @i{placen} @i{newvalue})},
  4767. the values in @i{place1} through @i{placen} are @i{read} and saved,
  4768. and @i{newvalue} is evaluated, for a total of @t{n}+1 values in all.
  4769. Values 2 through @t{n}+1 are then stored into @i{place1} through @i{placen}, respectively.
  4770. It is as if all the @i{places} form a shift register; the @i{newvalue}
  4771. is shifted in from the right, all values shift over to the left one place,
  4772. and the value shifted out of @i{place1} is returned.
  4773. For information about the @i{evaluation} of @i{subforms} of @i{places},
  4774. see @ref{Evaluation of Subforms to Places}.
  4775. @subsubheading Examples::
  4776. @example
  4777. (setq x (list 1 2 3) y 'trash) @result{} TRASH
  4778. (shiftf y x (cdr x) '(hi there)) @result{} TRASH
  4779. x @result{} (2 3)
  4780. y @result{} (1 HI THERE)
  4781. (setq x (list 'a 'b 'c)) @result{} (A B C)
  4782. (shiftf (cadr x) 'z) @result{} B
  4783. x @result{} (A Z C)
  4784. (shiftf (cadr x) (cddr x) 'q) @result{} Z
  4785. x @result{} (A (C) . Q)
  4786. (setq n 0) @result{} 0
  4787. (setq x (list 'a 'b 'c 'd)) @result{} (A B C D)
  4788. (shiftf (nth (setq n (+ n 1)) x) 'z) @result{} B
  4789. x @result{} (A Z C D)
  4790. @end example
  4791. @subsubheading Affected By::
  4792. @b{define-setf-expander},
  4793. @b{defsetf},
  4794. @b{*macroexpand-hook*}
  4795. @subsubheading See Also::
  4796. @ref{setf; psetf}
  4797. ,
  4798. @ref{rotatef}
  4799. , @ref{Generalized Reference}
  4800. @subsubheading Notes::
  4801. The effect of
  4802. @t{(shiftf @i{place1} @i{place2} ... @i{placen} @i{newvalue})}
  4803. is roughly equivalent to
  4804. @example
  4805. (let ((var1 @i{place1})
  4806. (var2 @i{place2})
  4807. ...
  4808. (varn @i{placen})
  4809. (var0 @i{newvalue}))
  4810. (setf @i{place1} var2)
  4811. (setf @i{place2} var3)
  4812. ...
  4813. (setf @i{placen} var0)
  4814. var1)
  4815. @end example
  4816. except that the latter would evaluate any @i{subforms}
  4817. of each @t{place} twice, whereas @b{shiftf} evaluates them once.
  4818. For example,
  4819. @example
  4820. (setq n 0) @result{} 0
  4821. (setq x (list 'a 'b 'c 'd)) @result{} (A B C D)
  4822. (prog1 (nth (setq n (+ n 1)) x)
  4823. (setf (nth (setq n (+ n 1)) x) 'z)) @result{} B
  4824. x @result{} (A B Z D)
  4825. @end example
  4826. @node rotatef, control-error, shiftf, Data and Control Flow Dictionary
  4827. @subsection rotatef [Macro]
  4828. @code{rotatef} @i{@{@i{place}@}{*}} @result{} @i{@b{nil}}
  4829. @subsubheading Arguments and Values::
  4830. @i{place}---a @i{place}.
  4831. @subsubheading Description::
  4832. @b{rotatef} modifies the values of each @i{place} by
  4833. rotating values from one @i{place} into another.
  4834. If a @i{place} produces more values than there
  4835. are store variables, the extra values are ignored. If a @i{place}
  4836. produces fewer values than there are store variables, the missing values
  4837. are set to @b{nil}.
  4838. In the form @t{(rotatef @i{place1} @i{place2} ... @i{placen})},
  4839. the values in @i{place1} through @i{placen} are @i{read} and @i{written}.
  4840. Values 2 through @i{n}
  4841. and value 1 are then stored into @i{place1} through @i{placen}.
  4842. It is as if all the places form an end-around shift register
  4843. that is rotated one place to the left, with the value of @i{place1}
  4844. being shifted around the end to @i{placen}.
  4845. For information about the @i{evaluation} of @i{subforms} of @i{places},
  4846. see @ref{Evaluation of Subforms to Places}.
  4847. @subsubheading Examples::
  4848. @example
  4849. (let ((n 0)
  4850. (x (list 'a 'b 'c 'd 'e 'f 'g)))
  4851. (rotatef (nth (incf n) x)
  4852. (nth (incf n) x)
  4853. (nth (incf n) x))
  4854. x) @result{} (A C D B E F G)
  4855. @end example
  4856. @subsubheading See Also::
  4857. @ref{define-setf-expander}
  4858. ,
  4859. @ref{defsetf}
  4860. ,
  4861. @ref{setf; psetf}
  4862. ,
  4863. @ref{shiftf}
  4864. ,
  4865. @b{*macroexpand-hook*},
  4866. @ref{Generalized Reference}
  4867. @subsubheading Notes::
  4868. The effect of
  4869. @t{(rotatef @i{place1} @i{place2} ... @i{placen})}
  4870. is roughly equivalent to
  4871. @example
  4872. (psetf @i{place1} @i{place2}
  4873. @i{place2} @i{place3}
  4874. ...
  4875. @i{placen} @i{place1})
  4876. @end example
  4877. except that the latter would evaluate any @i{subforms}
  4878. of each @t{place} twice, whereas @b{rotatef} evaluates them once.
  4879. @node control-error, program-error, rotatef, Data and Control Flow Dictionary
  4880. @subsection control-error [Condition Type]
  4881. @subsubheading Class Precedence List::
  4882. @b{control-error},
  4883. @b{error},
  4884. @b{serious-condition},
  4885. @b{condition},
  4886. @b{t}
  4887. @subsubheading Description::
  4888. The @i{type} @b{control-error} consists of error conditions that result from
  4889. invalid dynamic transfers of control in a program. The errors that
  4890. result from giving @b{throw} a tag that is not active or from
  4891. giving @b{go} or @b{return-from} a tag that is no longer
  4892. dynamically available are of @i{type} @b{control-error}.
  4893. @node program-error, undefined-function, control-error, Data and Control Flow Dictionary
  4894. @subsection program-error [Condition Type]
  4895. @subsubheading Class Precedence List::
  4896. @b{program-error},
  4897. @b{error},
  4898. @b{serious-condition},
  4899. @b{condition},
  4900. @b{t}
  4901. @subsubheading Description::
  4902. The @i{type} @b{program-error}
  4903. consists of error conditions related to incorrect program syntax. The
  4904. errors that result from naming a @i{go tag} or a @i{block tag}
  4905. that is not lexically apparent are of @i{type} @b{program-error}.
  4906. @node undefined-function, , program-error, Data and Control Flow Dictionary
  4907. @subsection undefined-function [Condition Type]
  4908. @subsubheading Class Precedence List::
  4909. @b{undefined-function},
  4910. @b{cell-error},
  4911. @b{error},
  4912. @b{serious-condition},
  4913. @b{condition},
  4914. @b{t}
  4915. @subsubheading Description::
  4916. The @i{type} @b{undefined-function} consists of @i{error} @i{conditions}
  4917. that represent attempts to @i{read} the definition of an @i{undefined function}.
  4918. The name of the cell (see @b{cell-error}) is the @i{function name}
  4919. which was @i{funbound}.
  4920. @subsubheading See Also::
  4921. @ref{cell-error-name}
  4922. @c end of including dict-flow
  4923. @c %**end of chapter