r5rs.texi 251 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538
  1. \input texinfo @c -*-texinfo-*-
  2. @c %**start of header
  3. @setfilename r5rs.info
  4. @settitle Revised(5) Scheme
  5. @c This copy of r5rs.texi differs from Aubrey Jaffer's master copy
  6. @c by a set of changes to allow the building of r5rs.dvi from r5rs.texi.
  7. @c Aubrey Jaffer's view - which I agree with - is that, given that
  8. @c people have the option of building r5rs.dvi from the original
  9. @c LaTeX distribution for R5RS, it is not worth fixing his master
  10. @c copy of r5rs.texi and the tool which autogenerates it. On the
  11. @c other hand, it is a marginal convenience for people to be able to
  12. @c build hardcopy from r5rs.texi, even if the results are less good
  13. @c than with the original LaTeX. Hence the following fixes.
  14. @c (lines 714, 725, 728, 1614, 2258): Remove invalid parentheses from
  15. @c @deffn statements.
  16. @c (line 2316): Change @deffnx to @deffn, and insert `@end deffn' to
  17. @c terminate preceding @deffn.
  18. @c (line 7320): Insert `@c ' at beginning of lines that are intended
  19. @c to be @ignore'd.
  20. @c
  21. @c NJ 2001/1/26
  22. @c \documentclass[twoside]{algol60}
  23. @c \pagestyle{headings}
  24. @c \showboxdepth=0
  25. @c \def\headertitle{Revised$^{5}$ Scheme}
  26. @c \def\integerversion{5}
  27. @c Sizes and dimensions
  28. @c \topmargin -.375in % Nominal distance from top of page to top of
  29. @c box containing running head.
  30. @c \headsep 15pt % Space between running head and text.
  31. @c \textheight 663pt % Height of text (including footnotes and figures,
  32. @c excluding running head and foot).
  33. @c \textwidth 523pt % Width of text line.
  34. @c \columnsep 15pt % Space between columns
  35. @c \columnseprule 0pt % Width of rule between columns.
  36. @c \parskip 5pt plus 2pt minus 2pt % Extra vertical space between paragraphs.
  37. @c \parindent 0pt % Width of paragraph indentation.
  38. @c \topsep 0pt plus 2pt % Extra vertical space, in addition to
  39. @c \parskip, added above and below list and
  40. @c paragraphing environments.
  41. @c \oddsidemargin -.5in % Left margin on odd-numbered pages.
  42. @c \evensidemargin -.5in % Left margin on even-numbered pages.
  43. @c % End of sizes and dimensions
  44. @paragraphindent 0
  45. @c %**end of header
  46. @c syncodeindex fn cp
  47. @ifinfo
  48. @dircategory The Algorithmic Language Scheme
  49. @direntry
  50. * R5RS: (r5rs). The Revised(5) Report on Scheme.
  51. @end direntry
  52. @end ifinfo
  53. @c \parindent 0pt %!! 15pt % Width of paragraph indentation.
  54. @b{20 February 1998}
  55. @c \hfil \today{}
  56. @c @include{first}
  57. @titlepage
  58. @c HTML first page
  59. @title Scheme
  60. @subtitle Revised(5) Report on the Algorithmic Language Scheme
  61. @c First page
  62. @c \thispagestyle{empty}
  63. @c \todo{"another" report?}
  64. @author R@sc{ICHARD} K@sc{ELSEY}, W@sc{ILLIAM} C@sc{LINGER, AND} J@sc{ONATHAN} R@sc{EES} (@i{Editors})
  65. @author H. A@sc{BELSON}
  66. @author R. K. D@sc{YBVIG}
  67. @author C. T. H@sc{AYNES}
  68. @author G. J. R@sc{OZAS}
  69. @author N. I. A@sc{DAMS IV}
  70. @author D. P. F@sc{RIEDMAN}
  71. @author E. K@sc{OHLBECKER}
  72. @author G. L. S@sc{TEELE} J@sc{R}.
  73. @author D. H. B@sc{ARTLEY}
  74. @author R. H@sc{ALSTEAD}
  75. @author D. O@sc{XLEY}
  76. @author G. J. S@sc{USSMAN}
  77. @author G. B@sc{ROOKS}
  78. @author C. H@sc{ANSON}
  79. @author K. M. P@sc{ITMAN}
  80. @author M. W@sc{AND}
  81. @author
  82. @c {\it Dedicated to the Memory of ALGOL 60}
  83. @i{Dedicated to the Memory of Robert Hieb}
  84. @c [For the macros in R5RS -RK]
  85. @unnumbered Summary
  86. The report gives a defining description of the programming language
  87. Scheme. Scheme is a statically scoped and properly tail-recursive
  88. dialect of the Lisp programming language invented by Guy Lewis
  89. Steele Jr.@: and Gerald Jay Sussman. It was designed to have an
  90. exceptionally clear and simple semantics and few different ways to
  91. form expressions. A wide variety of programming paradigms, including
  92. imperative, functional, and message passing styles, find convenient
  93. expression in Scheme.
  94. The introduction offers a brief history of the language and of
  95. the report.
  96. The first three chapters present the fundamental ideas of the
  97. language and describe the notational conventions used for describing the
  98. language and for writing programs in the language.
  99. Chapters @ref{Expressions} and @ref{Program structure} describe
  100. the syntax and semantics of expressions, programs, and definitions.
  101. Chapter @ref{Standard procedures} describes Scheme's built-in
  102. procedures, which include all of the language's data manipulation and
  103. input/output primitives.
  104. Chapter @ref{Formal syntax and semantics} provides a formal syntax for Scheme
  105. written in extended BNF, along with a formal denotational semantics.
  106. An example of the use of the language follows the formal syntax and
  107. semantics.
  108. The report concludes with a list of references and an
  109. alphabetic index.
  110. @ignore todo
  111. expand the summary so that it fills up the column.
  112. @end ignore
  113. @c \vfill
  114. @c \begin{center}
  115. @c {\large \bf
  116. @c *** DRAFT*** \\
  117. @c %August 31, 1989
  118. @c \today
  119. @c }\end{center}
  120. @c \addvspace{3.5pt} % don't shrink this gap
  121. @c \renewcommand{\tocshrink}{-3.5pt} % value determined experimentally
  122. @page
  123. @end titlepage
  124. @c INFO first page
  125. @ifnottex
  126. @c First page
  127. @c \thispagestyle{empty}
  128. @c \todo{"another" report?}
  129. @node top, Introduction, (dir), (dir)
  130. @top Revised(5) Report on the Algorithmic Language Scheme
  131. @sp 1
  132. @quotation
  133. R@sc{ichard} K@sc{elsey}, W@sc{illiam} C@sc{linger, and} J@sc{onathan} R@sc{ees} (@i{Editors})
  134. @sp 1
  135. @multitable @columnfractions 0.25 0.25 0.25 0.25
  136. @item H. A@sc{belson} @tab R. K. D@sc{ybvig} @tab C. T. H@sc{aynes} @tab G. J. R@sc{ozas}
  137. @item N. I. A@sc{dams IV} @tab D. P. F@sc{riedman} @tab E. K@sc{ohlbecker} @tab G. L. S@sc{teele} J@sc{r}.
  138. @item D. H. B@sc{artley} @tab R. H@sc{alstead} @tab D. O@sc{xley} @tab G. J. S@sc{ussman}
  139. @item G. B@sc{rooks} @tab C. H@sc{anson} @tab K. M. P@sc{itman} @tab M. W@sc{and}
  140. @item
  141. @end multitable
  142. @end quotation
  143. @sp 2
  144. @c {\it Dedicated to the Memory of ALGOL 60}
  145. @i{Dedicated to the Memory of Robert Hieb}
  146. @c [For the macros in R5RS -RK]
  147. @sp 3
  148. @majorheading Summary
  149. The report gives a defining description of the programming language
  150. Scheme. Scheme is a statically scoped and properly tail-recursive
  151. dialect of the Lisp programming language invented by Guy Lewis
  152. Steele Jr.@: and Gerald Jay Sussman. It was designed to have an
  153. exceptionally clear and simple semantics and few different ways to
  154. form expressions. A wide variety of programming paradigms, including
  155. imperative, functional, and message passing styles, find convenient
  156. expression in Scheme.
  157. The introduction offers a brief history of the language and of
  158. the report.
  159. The first three chapters present the fundamental ideas of the
  160. language and describe the notational conventions used for describing the
  161. language and for writing programs in the language.
  162. Chapters @ref{Expressions} and @ref{Program structure} describe
  163. the syntax and semantics of expressions, programs, and definitions.
  164. Chapter @ref{Standard procedures} describes Scheme's built-in
  165. procedures, which include all of the language's data manipulation and
  166. input/output primitives.
  167. Chapter @ref{Formal syntax and semantics} provides a formal syntax for Scheme
  168. written in extended BNF, along with a formal denotational semantics.
  169. An example of the use of the language follows the formal syntax and
  170. semantics.
  171. The report concludes with a list of references and an
  172. alphabetic index.
  173. @ignore todo
  174. expand the summary so that it fills up the column.
  175. @end ignore
  176. @c \vfill
  177. @c \begin{center}
  178. @c {\large \bf
  179. @c *** DRAFT*** \\
  180. @c %August 31, 1989
  181. @c \today
  182. @c }\end{center}
  183. @c \addvspace{3.5pt} % don't shrink this gap
  184. @c \renewcommand{\tocshrink}{-3.5pt} % value determined experimentally
  185. @unnumbered Contents
  186. @menu
  187. * Introduction::
  188. * Overview of Scheme::
  189. * Lexical conventions::
  190. * Basic concepts::
  191. * Expressions::
  192. * Program structure::
  193. * Standard procedures::
  194. * Formal syntax and semantics::
  195. * Notes::
  196. * Additional material::
  197. * Example::
  198. * Bibliography::
  199. * Index::
  200. @end menu
  201. @page
  202. @end ifnottex
  203. @c @include{intro}
  204. @node Introduction, Overview of Scheme, top, top
  205. @unnumbered Introduction
  206. @menu
  207. * Background::
  208. * Acknowledgements::
  209. @end menu
  210. Programming languages should be designed not by piling feature on top of
  211. feature, but by removing the weaknesses and restrictions that make additional
  212. features appear necessary. Scheme demonstrates that a very small number
  213. of rules for forming expressions, with no restrictions on how they are
  214. composed, suffice to form a practical and efficient programming language
  215. that is flexible enough to support most of the major programming
  216. paradigms in use today.
  217. @c Scheme has influenced the evolution of Lisp.
  218. Scheme
  219. was one of the first programming languages to incorporate first class
  220. procedures as in the lambda calculus, thereby proving the usefulness of
  221. static scope rules and block structure in a dynamically typed language.
  222. Scheme was the first major dialect of Lisp to distinguish procedures
  223. from lambda expressions and symbols, to use a single lexical
  224. environment for all variables, and to evaluate the operator position
  225. of a procedure call in the same way as an operand position. By relying
  226. entirely on procedure calls to express iteration, Scheme emphasized the
  227. fact that tail-recursive procedure calls are essentially goto's that
  228. pass arguments. Scheme was the first widely used programming language to
  229. embrace first class escape procedures, from which all previously known
  230. sequential control structures can be synthesized. A subsequent
  231. version of Scheme introduced the concept of exact and inexact numbers,
  232. an extension of Common Lisp's generic arithmetic.
  233. More recently, Scheme became the first programming language to support
  234. hygienic macros, which permit the syntax of a block-structured language
  235. to be extended in a consistent and reliable manner.
  236. @c A few
  237. @c of these innovations have recently been incorporated into Common Lisp, while
  238. @c others remain to be adopted.
  239. @ignore todo
  240. Ramsdell:
  241. I would like to make a few comments on presentation. The most
  242. important comment is about section organization. Newspaper writers
  243. spend most of their time writing the first three paragraphs of any
  244. article. This part of the article is often the only part read by
  245. readers, and is important in enticing readers to continue. In the
  246. same way, The first page is most likely to be the only page read by
  247. many SIGPLAN readers. If I had my choice of what I would ask them to
  248. read, it would be the material in section 1.1, the Semantics section
  249. that notes that scheme is lexically scoped, tail recursive, weakly
  250. typed, ... etc. I would expand on the discussion on continuations,
  251. as they represent one important difference between Scheme and other
  252. languages. The introduction, with its history of scheme, its history
  253. of scheme reports and meetings, and acknowledgements giving names of
  254. people that the reader will not likely know, is not that one page I
  255. would like all to read. I suggest moving the history to the back of
  256. the report, and use the first couple of pages to convince the reader
  257. that the language documented in this report is worth studying.
  258. @end ignore
  259. @node Background, Acknowledgements, Introduction, Introduction
  260. @unnumberedsec Background
  261. The first description of Scheme was written in
  262. 1975 [Scheme75]. A revised report [Scheme78]
  263. @ignore todo
  264. italicize or not?
  265. @end ignore
  266. appeared in 1978, which described the evolution
  267. of the language as its MIT implementation was upgraded to support an
  268. innovative compiler [Rabbit]. Three distinct projects began in
  269. 1981 and 1982 to use variants of Scheme for courses at MIT, Yale, and
  270. Indiana University [Rees82], [MITScheme], [Scheme311]. An introductory
  271. computer science textbook using Scheme was published in
  272. 1984 [SICP].
  273. @c \vest As might be expected of a language used primarily for education and
  274. @c research, Scheme has always evolved rapidly. This was no problem when
  275. @c Scheme was used only within MIT, but
  276. As Scheme became more widespread,
  277. local dialects began to diverge until students and researchers
  278. occasionally found it difficult to understand code written at other
  279. sites.
  280. Fifteen representatives of the major implementations of Scheme therefore
  281. met in October 1984 to work toward a better and more widely accepted
  282. standard for Scheme.
  283. @c Participating in this workshop were Hal Abelson, Norman Adams, David
  284. @c Bartley, Gary Brooks, William Clinger, Daniel Friedman, Robert Halstead,
  285. @c Chris Hanson, Christopher Haynes, Eugene Kohlbecker, Don Oxley, Jonathan Rees,
  286. @c Guillermo Rozas, Gerald Jay Sussman, and Mitchell Wand. Kent Pitman
  287. @c made valuable contributions to the agenda for the workshop but was
  288. @c unable to attend the sessions.
  289. @c Subsequent electronic mail discussions and committee work completed the
  290. @c definition of the language.
  291. @c Gerry Sussman drafted the section on numbers, Chris Hanson drafted the
  292. @c sections on characters and strings, and Gary Brooks and William Clinger
  293. @c drafted the sections on input and output.
  294. @c William Clinger recorded the decisions of the workshop and
  295. @c compiled the pieces into a coherent document.
  296. @c The ``Revised revised report on Scheme''~\cite{RRRS}
  297. Their report [RRRS]
  298. was published at MIT and Indiana University in the summer of 1985.
  299. Further revision took place in the spring of 1986 [R3RS],
  300. @c , again accomplished
  301. @c almost entirely by electronic mail, resulted in the present report.
  302. and in the spring of 1988 [R4RS].
  303. The present report reflects further revisions agreed upon in a meeting
  304. at Xerox PARC in June 1992.
  305. @c \vest The number 3 in the title is part of the title, not a reference to
  306. @c a footnote. The word ``revised'' is raised to the third power because
  307. @c the report is a revision of a report that was already twice revised.
  308. @ignore todo
  309. Write an editors' note?
  310. @end ignore
  311. @sp 3
  312. We intend this report to belong to the entire Scheme community, and so
  313. we grant permission to copy it in whole or in part without fee. In
  314. particular, we encourage implementors of Scheme to use this report as
  315. a starting point for manuals and other documentation, modifying it as
  316. necessary.
  317. @node Acknowledgements, , Background, Introduction
  318. @unnumberedsec Acknowledgements
  319. We would like to thank the following people for their help: Alan Bawden, Michael
  320. Blair, George Carrette, Andy Cromarty, Pavel Curtis, Jeff Dalton, Olivier Danvy,
  321. Ken Dickey, Bruce Duba, Marc Feeley,
  322. Andy Freeman, Richard Gabriel, Yekta G"ursel, Ken Haase, Robert
  323. Hieb, Paul Hudak, Morry Katz, Chris Lindblad, Mark Meyer, Jim Miller, Jim Philbin,
  324. John Ramsdell, Mike Shaff, Jonathan Shapiro, Julie Sussman,
  325. Perry Wagle, Daniel Weise, Henry Wu, and Ozan Yigit.
  326. We thank Carol Fessenden, Daniel
  327. Friedman, and Christopher Haynes for permission to use text from the Scheme 311
  328. version 4 reference manual. We thank Texas Instruments, Inc. for permission to
  329. use text from the @emph{TI Scheme Language Reference Manual}[TImanual85].
  330. We gladly acknowledge the influence of manuals for MIT Scheme[MITScheme],
  331. T[Rees84], Scheme 84[Scheme84],Common Lisp[CLtL],
  332. and Algol 60[Naur63].
  333. We also thank Betty Dexter for the extreme effort she put into
  334. setting this report in @TeX{}, and Donald Knuth for designing the program
  335. that caused her troubles.
  336. The Artificial Intelligence Laboratory of the
  337. Massachusetts Institute of Technology, the Computer Science
  338. Department of Indiana University, the Computer and Information
  339. Sciences Department of the University of Oregon, and the NEC Research
  340. Institute supported the preparation of this report. Support for the MIT
  341. work was provided in part by
  342. the Advanced Research Projects Agency of the Department of Defense under Office
  343. of Naval Research contract N00014-80-C-0505. Support for the Indiana
  344. University work was provided by NSF grants NCS 83-04567 and NCS
  345. 83-03325.
  346. @sp 2
  347. @c \clearchapterstar{Description of the language} %\unskip\vskip -2ex
  348. @c @include{struct}
  349. @c 1. Structure of the language
  350. @node Overview of Scheme, Lexical conventions, Introduction, top
  351. @chapter Overview of Scheme
  352. @menu
  353. * Semantics::
  354. * Syntax::
  355. * Notation and terminology::
  356. @end menu
  357. @node Semantics, Syntax, Overview of Scheme, Overview of Scheme
  358. @section Semantics
  359. This section gives an overview of Scheme's semantics. A
  360. detailed informal semantics is the subject of
  361. chapters @ref{Basic concepts} through @ref{Standard procedures}. For reference
  362. purposes, section @ref{Formal semantics} provides a formal
  363. semantics of Scheme.
  364. Following Algol, Scheme is a statically scoped programming
  365. language. Each use of a variable is associated with a lexically
  366. apparent binding of that variable.
  367. Scheme has latent as opposed to manifest types. Types
  368. are associated with values (also called objects) rather than
  369. @cindex @w{object}
  370. with variables. (Some authors refer to languages with latent types as
  371. weakly typed or dynamically typed languages.) Other languages with
  372. latent types are APL, Snobol, and other dialects of Lisp. Languages
  373. with manifest types (sometimes referred to as strongly typed or
  374. statically typed languages) include Algol 60, Pascal, and C.
  375. All objects created in the course of a Scheme computation, including
  376. procedures and continuations, have unlimited extent.
  377. No Scheme object is ever destroyed. The reason that
  378. implementations of Scheme do not (usually!) run out of storage is that
  379. they are permitted to reclaim the storage occupied by an object if
  380. they can prove that the object cannot possibly matter to any future
  381. computation. Other languages in which most objects have unlimited
  382. extent include APL and other Lisp dialects.
  383. Implementations of Scheme are required to be properly tail-recursive.
  384. This allows the execution of an iterative computation in constant space,
  385. even if the iterative computation is described by a syntactically
  386. recursive procedure. Thus with a properly tail-recursive implementation,
  387. iteration can be expressed using the ordinary procedure-call
  388. mechanics, so that special iteration constructs are useful only as
  389. syntactic sugar. See section @ref{Proper tail recursion}.
  390. Scheme procedures are objects in their own right. Procedures can be
  391. created dynamically, stored in data structures, returned as results of
  392. procedures, and so on. Other languages with these properties include
  393. Common Lisp and ML.
  394. @ignore todo
  395. Rozas: Scheme had them first.
  396. @end ignore
  397. One distinguishing feature of Scheme is that continuations, which
  398. in most other languages only operate behind the scenes, also have
  399. ``first-class'' status. Continuations are useful for implementing a
  400. wide variety of advanced control constructs, including non-local exits,
  401. backtracking, and coroutines. See section @ref{Control features}.
  402. Arguments to Scheme procedures are always passed by value, which
  403. means that the actual argument expressions are evaluated before the
  404. procedure gains control, whether the procedure needs the result of the
  405. evaluation or not. ML, C, and APL are three other languages that always
  406. pass arguments by value.
  407. This is distinct from the lazy-evaluation semantics of Haskell,
  408. or the call-by-name semantics of Algol 60, where an argument
  409. expression is not evaluated unless its value is needed by the
  410. procedure.
  411. @ignore todo
  412. Lisp's call by value should be explained more
  413. accurately. What's funny is that all values are references.
  414. @end ignore
  415. Scheme's model of arithmetic is designed to remain as independent as
  416. possible of the particular ways in which numbers are represented within a
  417. computer. In Scheme, every integer is a rational number, every rational is a
  418. real, and every real is a complex number. Thus the distinction between integer
  419. and real arithmetic, so important to many programming languages, does not
  420. appear in Scheme. In its place is a distinction between exact arithmetic,
  421. which corresponds to the mathematical ideal, and inexact arithmetic on
  422. approximations. As in Common Lisp, exact arithmetic is not limited to
  423. integers.
  424. @node Syntax, Notation and terminology, Semantics, Overview of Scheme
  425. @section Syntax
  426. Scheme, like most dialects of Lisp, employs a fully parenthesized prefix
  427. notation for programs and (other) data; the grammar of Scheme generates a
  428. sublanguage of the language used for data. An important
  429. consequence of this simple, uniform representation is the susceptibility of
  430. Scheme programs and data to uniform treatment by other Scheme programs.
  431. For example, the @samp{eval} procedure evaluates a Scheme program expressed
  432. as data.
  433. The @samp{read} procedure performs syntactic as well as lexical decomposition of
  434. the data it reads. The @samp{read} procedure parses its input as data
  435. (section @pxref{External representation}), not as program.
  436. The formal syntax of Scheme is described in section @ref{Formal syntax}.
  437. @node Notation and terminology, , Syntax, Overview of Scheme
  438. @section Notation and terminology
  439. @menu
  440. * Primitive; library; and optional features::
  441. * Error situations and unspecified behavior::
  442. * Entry format::
  443. * Evaluation examples::
  444. * Naming conventions::
  445. @end menu
  446. @node Primitive; library; and optional features, Error situations and unspecified behavior, Notation and terminology, Notation and terminology
  447. @subsection Primitive; library; and optional features
  448. It is required that every implementation of Scheme support all
  449. features that are not marked as being @dfn{optional}. Implementations are
  450. @cindex @w{optional}
  451. free to omit optional features of Scheme or to add extensions,
  452. provided the extensions are not in conflict with the language reported
  453. here. In particular, implementations must support portable code by
  454. providing a syntactic mode that preempts no lexical conventions of this
  455. report.
  456. To aid in understanding and implementing Scheme, some features are marked
  457. as @dfn{library}. These can be easily implemented in terms of the other,
  458. @cindex @w{library}
  459. primitive, features. They are redundant in the strict sense of
  460. the word, but they capture common patterns of usage, and are therefore
  461. provided as convenient abbreviations.
  462. @node Error situations and unspecified behavior, Entry format, Primitive; library; and optional features, Notation and terminology
  463. @subsection Error situations and unspecified behavior
  464. @cindex @w{error}
  465. When speaking of an error situation, this report uses the phrase ``an
  466. error is signalled'' to indicate that implementations must detect and
  467. report the error. If such wording does not appear in the discussion of
  468. an error, then implementations are not required to detect or report the
  469. error, though they are encouraged to do so. An error situation that
  470. implementations are not required to detect is usually referred to simply
  471. as ``an error.''
  472. For example, it is an error for a procedure to be passed an argument that
  473. the procedure is not explicitly specified to handle, even though such
  474. domain errors are seldom mentioned in this report. Implementations may
  475. extend a procedure's domain of definition to include such arguments.
  476. This report uses the phrase ``may report a violation of an
  477. implementation restriction'' to indicate circumstances under which an
  478. implementation is permitted to report that it is unable to continue
  479. execution of a correct program because of some restriction imposed by the
  480. implementation. Implementation restrictions are of course discouraged,
  481. but implementations are encouraged to report violations of implementation
  482. restrictions.
  483. @cindex @w{implementation restriction}
  484. For example, an implementation may report a violation of an
  485. implementation restriction if it does not have enough storage to run a
  486. program.
  487. If the value of an expression is said to be ``unspecified,'' then
  488. the expression must evaluate to some object without signalling an error,
  489. but the value depends on the implementation; this report explicitly does
  490. not say what value should be returned.
  491. @cindex @w{unspecified}
  492. @ignore todo
  493. Talk about unspecified behavior vs. unspecified values.
  494. @end ignore
  495. @ignore todo
  496. Look at KMP's situations paper.
  497. @end ignore
  498. @node Entry format, Evaluation examples, Error situations and unspecified behavior, Notation and terminology
  499. @subsection Entry format
  500. Chapters @ref{Expressions} and @ref{Standard procedures} are organized
  501. into entries. Each entry describes one language feature or a group of
  502. related features, where a feature is either a syntactic construct or a
  503. built-in procedure. An entry begins with one or more header lines of the form
  504. @noindent
  505. @deffn {@var{category}} @var{template}
  506. @end deffn
  507. for required, primitive features, or
  508. @noindent
  509. @deffn {@var{qualifier} @var{category}} @var{template}
  510. @end deffn
  511. where @var{qualifier} is either ``library'' or ``optional'' as defined
  512. in section @ref{Primitive; library; and optional features}.
  513. If @var{category} is ``syntax'', the entry describes an expression
  514. type, and the template gives the syntax of the expression type.
  515. Components of expressions are designated by syntactic variables, which
  516. are written using angle brackets, for example, @r{<expression>},
  517. @r{<variable>}. Syntactic variables should be understood to denote segments of
  518. program text; for example, @r{<expression>} stands for any string of
  519. characters which is a syntactically valid expression. The notation
  520. @format
  521. @r{<thing1>} @dots{}
  522. @end format
  523. indicates zero or more occurrences of a @r{<thing>}, and
  524. @format
  525. @r{<thing1>} @r{<thing2>} @dots{}
  526. @end format
  527. indicates one or more occurrences of a @r{<thing>}.
  528. If @var{category} is ``procedure'', then the entry describes a procedure, and
  529. the header line gives a template for a call to the procedure. Argument
  530. names in the template are @var{italicized}. Thus the header line
  531. @noindent
  532. @deffn {procedure} vector-ref @var{vector} @var{k}
  533. @end deffn
  534. indicates that the built-in procedure @t{vector-ref} takes
  535. two arguments, a vector @var{vector} and an exact non-negative integer
  536. @var{k} (see below). The header lines
  537. @noindent
  538. @deffn {procedure} make-vector @var{k}
  539. @deffnx {procedure} make-vector @var{k} @var{fill}
  540. @end deffn
  541. indicate that the @t{make-vector} procedure must be defined to take
  542. either one or two arguments.
  543. It is an error for an operation to be presented with an argument that it
  544. is not specified to handle. For succinctness, we follow the convention
  545. that if an argument name is also the name of a type listed in
  546. section @ref{Disjointness of types}, then that argument must be of the named type.
  547. For example, the header line for @t{vector-ref} given above dictates that the
  548. first argument to @t{vector-ref} must be a vector. The following naming
  549. conventions also imply type restrictions:
  550. @c \newcommand{\foo}[1]{\vr{#1}, \vri{#1}, $\ldots$ \vrj{#1}, $\ldots$}
  551. @center @c begin-tabular
  552. @quotation
  553. @table @asis
  554. @item @var{obj}
  555. any object
  556. @item @var{list}, @var{list1}, @dots{} @var{listj}, @dots{}
  557. list (see section @pxref{Pairs and lists})
  558. @item @var{z}, @var{z1}, @dots{} @var{zj}, @dots{}
  559. complex number
  560. @item @var{x}, @var{x1}, @dots{} @var{xj}, @dots{}
  561. real number
  562. @item @var{y}, @var{y1}, @dots{} @var{yj}, @dots{}
  563. real number
  564. @item @var{q}, @var{q1}, @dots{} @var{qj}, @dots{}
  565. rational number
  566. @item @var{n}, @var{n1}, @dots{} @var{nj}, @dots{}
  567. integer
  568. @item @var{k}, @var{k1}, @dots{} @var{kj}, @dots{}
  569. exact non-negative integer
  570. @item
  571. @end table
  572. @end quotation
  573. @ignore todo
  574. Provide an example entry??
  575. @end ignore
  576. @node Evaluation examples, Naming conventions, Entry format, Notation and terminology
  577. @subsection Evaluation examples
  578. The symbol ``@result{}'' used in program examples should be read
  579. ``evaluates to.'' For example,
  580. @example
  581. (* 5 8) ==> 40
  582. @end example
  583. means that the expression @t{(* 5 8)} evaluates to the object @t{40}.
  584. Or, more precisely: the expression given by the sequence of characters
  585. ``@t{(* 5 8)}'' evaluates, in the initial environment, to an object
  586. that may be represented externally by the sequence of characters ``@t{40}''. See section @ref{External representations} for a discussion of external
  587. representations of objects.
  588. @node Naming conventions, , Evaluation examples, Notation and terminology
  589. @subsection Naming conventions
  590. By convention, the names of procedures that always return a boolean
  591. value usually end
  592. in ``@code{?}''. Such procedures are called predicates.
  593. @vindex @w{?}
  594. By convention, the names of procedures that store values into previously
  595. allocated locations (see section @pxref{Storage model}) usually end in
  596. ``@code{!}''.
  597. @vindex @w{!}
  598. Such procedures are called mutation procedures.
  599. By convention, the value returned by a mutation procedure is unspecified.
  600. By convention, ``@code{->}'' appears within the names of procedures that
  601. @vindex @w{->}
  602. take an object of one type and return an analogous object of another type.
  603. For example, @samp{list->vector} takes a list and returns a vector whose
  604. elements are the same as those of the list.
  605. @ignore todo
  606. Terms that need defining: thunk, command (what else?).
  607. @end ignore
  608. @c @include{lex}
  609. @c Lexical structure
  610. @c %\vfill\eject
  611. @node Lexical conventions, Basic concepts, Overview of Scheme, top
  612. @chapter Lexical conventions
  613. @menu
  614. * Identifiers::
  615. * Whitespace and comments::
  616. * Other notations::
  617. @end menu
  618. This section gives an informal account of some of the lexical
  619. conventions used in writing Scheme programs. For a formal syntax of
  620. Scheme, see section @ref{Formal syntax}.
  621. Upper and lower case forms of a letter are never distinguished
  622. except within character and string constants. For example, @samp{Foo} is
  623. the same identifier as @samp{FOO}, and @t{#x1AB} is the same number as
  624. @t{#X1ab}.
  625. @node Identifiers, Whitespace and comments, Lexical conventions, Lexical conventions
  626. @section Identifiers
  627. Most identifiers allowed by other programming
  628. @cindex @w{identifier}
  629. languages are also acceptable to Scheme. The precise rules for forming
  630. identifiers vary among implementations of Scheme, but in all
  631. implementations a sequence of letters, digits, and ``extended alphabetic
  632. characters'' that begins with a character that cannot begin a number is
  633. an identifier. In addition, @code{+}, @code{-}, and @code{...} are identifiers.
  634. @vindex @w{...}
  635. @vindex @w{-}
  636. @vindex @w{+}
  637. Here are some examples of identifiers:
  638. @example
  639. lambda q
  640. list->vector soup
  641. + V17a
  642. <=? a34kTMNs
  643. the-word-recursion-has-many-meanings
  644. @end example
  645. Extended alphabetic characters may be used within identifiers as if
  646. they were letters. The following are extended alphabetic characters:
  647. @example
  648. ! $ % & * + - . / : < = > ? @@ ^ _ ~
  649. @end example
  650. See section @ref{Lexical structure} for a formal syntax of identifiers.
  651. Identifiers have two uses within Scheme programs:
  652. @itemize @bullet
  653. @item
  654. Any identifier may be used as a variable
  655. or as a syntactic keyword
  656. (see sections @pxref{Variables; syntactic keywords; and regions} and @pxref{Macros}).
  657. @item
  658. When an identifier appears as a literal or within a literal
  659. (see section @pxref{Literal expressions}), it is being used to denote a @emph{symbol}
  660. (see section @pxref{Symbols}).
  661. @end itemize
  662. @cindex @w{syntactic keyword}
  663. @cindex @w{variable}
  664. @c \label{keywordsection}
  665. @c The following identifiers are syntactic keywords, and should not be used
  666. @c as variables:
  667. @c \begin{scheme}
  668. @c => do or
  669. @c and else quasiquote
  670. @c begin if quote
  671. @c case lambda set!
  672. @c cond let unquote
  673. @c define let* unquote-splicing
  674. @c delay letrec%
  675. @c \end{scheme}
  676. @c Some implementations allow all identifiers, including syntactic
  677. @c keywords, to be used as variables. This is a compatible extension to
  678. @c the language, but ambiguities in the language result when the
  679. @c restriction is relaxed, and the ways in which these ambiguities are
  680. @c resolved vary between implementations.
  681. @node Whitespace and comments, Other notations, Identifiers, Lexical conventions
  682. @section Whitespace and comments
  683. @dfn{Whitespace} characters are spaces and newlines.
  684. @cindex @w{Whitespace}
  685. (Implementations typically provide additional whitespace characters such
  686. as tab or page break.) Whitespace is used for improved readability and
  687. as necessary to separate tokens from each other, a token being an
  688. indivisible lexical unit such as an identifier or number, but is
  689. otherwise insignificant. Whitespace may occur between any two tokens,
  690. but not within a token. Whitespace may also occur inside a string,
  691. where it is significant.
  692. A semicolon (@t{;}) indicates the start of a
  693. comment. The comment continues to the
  694. @cindex @w{;}
  695. @cindex @w{comment}
  696. end of the line on which the semicolon appears. Comments are invisible
  697. to Scheme, but the end of the line is visible as whitespace. This
  698. prevents a comment from appearing in the middle of an identifier or
  699. number.
  700. @example
  701. ;;; The FACT procedure computes the factorial
  702. ;;; of a non-negative integer.
  703. (define fact
  704. (lambda (n)
  705. (if (= n 0)
  706. 1 ;Base case: return 1
  707. (* n (fact (- n 1))))))
  708. @end example
  709. @node Other notations, , Whitespace and comments, Lexical conventions
  710. @section Other notations
  711. @ignore todo
  712. Rewrite?
  713. @end ignore
  714. For a description of the notations used for numbers, see
  715. section @ref{Numbers}.
  716. @table @t
  717. @item @t{.@: + -}
  718. These are used in numbers, and may also occur anywhere in an identifier
  719. except as the first character. A delimited plus or minus sign by itself
  720. is also an identifier.
  721. A delimited period (not occurring within a number or identifier) is used
  722. in the notation for pairs (section @pxref{Pairs and lists}), and to indicate a
  723. rest-parameter in a formal parameter list (section @pxref{Procedures}).
  724. A delimited sequence of three successive periods is also an identifier.
  725. @item @t{( )}
  726. Parentheses are used for grouping and to notate lists
  727. (section @pxref{Pairs and lists}).
  728. @item @t{'}
  729. The single quote character is used to indicate literal data (section @pxref{Literal expressions}).
  730. @item @t{`}
  731. The backquote character is used to indicate almost-constant
  732. data (section @pxref{Quasiquotation}).
  733. @item @t{, ,@@}
  734. The character comma and the sequence comma at-sign are used in conjunction
  735. with backquote (section @pxref{Quasiquotation}).
  736. @item @t{"}
  737. The double quote character is used to delimit strings (section @pxref{Strings}).
  738. @item \
  739. Backslash is used in the syntax for character constants
  740. (section @pxref{Characters}) and as an escape character within string
  741. constants (section @pxref{Strings}).
  742. @c A box used because \verb is not allowed in command arguments.
  743. @item @w{@t{[ ] @{ @} |}}
  744. Left and right square brackets and curly braces and vertical bar
  745. are reserved for possible future extensions to the language.
  746. @item #
  747. Sharp sign is used for a variety of purposes depending on
  748. the character that immediately follows it:
  749. @item @t{#t} @t{#f}
  750. These are the boolean constants (section @pxref{Booleans}).
  751. @item #\
  752. This introduces a character constant (section @pxref{Characters}).
  753. @item #@t{(}
  754. This introduces a vector constant (section @pxref{Vectors}). Vector constants
  755. are terminated by @t{)} .
  756. @item @t{#e #i #b #o #d #x}
  757. These are used in the notation for numbers (section @pxref{Syntax of numerical constants}).
  758. @end table
  759. @c @include{basic}
  760. @c \vfill\eject
  761. @node Basic concepts, Expressions, Lexical conventions, top
  762. @chapter Basic concepts
  763. @menu
  764. * Variables; syntactic keywords; and regions::
  765. * Disjointness of types::
  766. * External representations::
  767. * Storage model::
  768. * Proper tail recursion::
  769. @end menu
  770. @node Variables; syntactic keywords; and regions, Disjointness of types, Basic concepts, Basic concepts
  771. @section Variables; syntactic keywords; and regions
  772. An identifier may name a type of syntax, or it may name
  773. @cindex @w{identifier}
  774. a location where a value can be stored. An identifier that names a type
  775. of syntax is called a @emph{syntactic keyword}
  776. @cindex @w{syntactic keyword}
  777. and is said to be @emph{bound} to that syntax. An identifier that names a
  778. location is called a @emph{variable} and is said to be
  779. @cindex @w{variable}
  780. @emph{bound} to that location. The set of all visible
  781. bindings in effect at some point in a program is
  782. @cindex @w{binding}
  783. known as the @emph{environment} in effect at that point. The value
  784. stored in the location to which a variable is bound is called the
  785. variable's value. By abuse of terminology, the variable is sometimes
  786. said to name the value or to be bound to the value. This is not quite
  787. accurate, but confusion rarely results from this practice.
  788. @ignore todo
  789. Define ``assigned'' and ``unassigned'' perhaps?
  790. @end ignore
  791. @ignore todo
  792. In programs without side effects, one can safely pretend that the
  793. variables are bound directly to the arguments. Or:
  794. In programs without @code{set!}, one can safely pretend that the
  795. @vindex @w{set!}
  796. variable is bound directly to the value.
  797. @end ignore
  798. Certain expression types are used to create new kinds of syntax
  799. and bind syntactic keywords to those new syntaxes, while other
  800. expression types create new locations and bind variables to those
  801. locations. These expression types are called @emph{binding constructs}.
  802. @cindex @w{binding construct}
  803. Those that bind syntactic keywords are listed in section @ref{Macros}.
  804. The most fundamental of the variable binding constructs is the
  805. @samp{lambda} expression, because all other variable binding constructs
  806. can be explained in terms of @samp{lambda} expressions. The other
  807. variable binding constructs are @samp{let}, @samp{let*}, @samp{letrec},
  808. and @samp{do} expressions (see sections @pxref{Procedures}, @pxref{Binding constructs}, and
  809. @pxref{Iteration}).
  810. @c Note: internal definitions not mentioned here.
  811. Like Algol and Pascal, and unlike most other dialects of Lisp
  812. except for Common Lisp, Scheme is a statically scoped language with
  813. block structure. To each place where an identifier is bound in a program
  814. there corresponds a @dfn{region} of the program text within which
  815. @cindex @w{region}
  816. the binding is visible. The region is determined by the particular
  817. binding construct that establishes the binding; if the binding is
  818. established by a @samp{lambda} expression, for example, then its region
  819. is the entire @samp{lambda} expression. Every mention of an identifier
  820. refers to the binding of the identifier that established the
  821. innermost of the regions containing the use. If there is no binding of
  822. the identifier whose region contains the use, then the use refers to the
  823. binding for the variable in the top level environment, if any
  824. (chapters @pxref{Expressions} and @pxref{Standard procedures}); if there is no
  825. binding for the identifier,
  826. it is said to be @dfn{unbound}.
  827. @cindex @w{top level environment}
  828. @cindex @w{bound}
  829. @cindex @w{unbound}
  830. @ignore todo
  831. Mention that some implementations have multiple top level environments?
  832. @end ignore
  833. @ignore todo
  834. Pitman sez: needs elaboration in case of @t{(let ...)}
  835. @end ignore
  836. @ignore todo
  837. Pitman asks: say something about vars created after scheme starts?
  838. @t{(define x 3) (define (f) x) (define (g) y) (define y 4)}
  839. Clinger replies: The language was explicitly
  840. designed to permit a view in which no variables are created after
  841. Scheme starts. In files, you can scan out the definitions beforehand.
  842. I think we're agreed on the principle that interactive use should
  843. approximate that behavior as closely as possible, though we don't yet
  844. agree on which programming environment provides the best approximation.
  845. @end ignore
  846. @node Disjointness of types, External representations, Variables; syntactic keywords; and regions, Basic concepts
  847. @section Disjointness of types
  848. No object satisfies more than one of the following predicates:
  849. @example
  850. boolean? pair?
  851. symbol? number?
  852. char? string?
  853. vector? port?
  854. procedure?
  855. @end example
  856. These predicates define the types @emph{boolean}, @emph{pair}, @emph{symbol}, @emph{number}, @emph{char} (or @emph{character}), @emph{string}, @emph{vector}, @emph{port}, and @emph{procedure}. The empty list is a special
  857. object of its own type; it satisfies none of the above predicates.
  858. @vindex symbol?
  859. @vindex pair?
  860. @vindex boolean?
  861. @cindex @w{type}
  862. @vindex vector?
  863. @vindex string?
  864. @vindex char?
  865. @vindex number?
  866. @cindex @w{empty list}
  867. @vindex procedure?
  868. @vindex port?
  869. Although there is a separate boolean type,
  870. any Scheme value can be used as a boolean value for the purpose of a
  871. conditional test. As explained in section @ref{Booleans}, all
  872. values count as true in such a test except for @t{#f}.
  873. @c and possibly the empty list.
  874. @c The only value that is guaranteed to count as
  875. @c false is \schfalse{}. It is explicitly unspecified whether the empty list
  876. @c counts as true or as false.
  877. This report uses the word ``true'' to refer to any
  878. Scheme value except @t{#f}, and the word ``false'' to refer to
  879. @t{#f}.
  880. @cindex @w{false}
  881. @cindex @w{true}
  882. @node External representations, Storage model, Disjointness of types, Basic concepts
  883. @section External representations
  884. An important concept in Scheme (and Lisp) is that of the @emph{external
  885. representation} of an object as a sequence of characters. For example,
  886. an external representation of the integer 28 is the sequence of
  887. characters ``@t{28}'', and an external representation of a list consisting
  888. of the integers 8 and 13 is the sequence of characters ``@t{(8 13)}''.
  889. The external representation of an object is not necessarily unique. The
  890. integer 28 also has representations ``@t{#e28.000}'' and ``@t{#x1c}'', and the
  891. list in the previous paragraph also has the representations ``@t{( 08 13
  892. )}'' and ``@t{(8 .@: (13 .@: ()))}'' (see section @pxref{Pairs and lists}).
  893. Many objects have standard external representations, but some, such as
  894. procedures, do not have standard representations (although particular
  895. implementations may define representations for them).
  896. An external representation may be written in a program to obtain the
  897. corresponding object (see @samp{quote}, section @pxref{Literal expressions}).
  898. External representations can also be used for input and output. The
  899. procedure @samp{read} (section @pxref{Input}) parses external
  900. representations, and the procedure @samp{write} (section @pxref{Output})
  901. generates them. Together, they provide an elegant and powerful
  902. input/output facility.
  903. Note that the sequence of characters ``@t{(+ 2 6)}'' is @emph{not} an
  904. external representation of the integer 8, even though it @emph{is} an
  905. expression evaluating to the integer 8; rather, it is an external
  906. representation of a three-element list, the elements of which are the symbol
  907. @t{+} and the integers 2 and 6. Scheme's syntax has the property that
  908. any sequence of characters that is an expression is also the external
  909. representation of some object. This can lead to confusion, since it may
  910. not be obvious out of context whether a given sequence of characters is
  911. intended to denote data or program, but it is also a source of power,
  912. since it facilitates writing programs such as interpreters and
  913. compilers that treat programs as data (or vice versa).
  914. The syntax of external representations of various kinds of objects
  915. accompanies the description of the primitives for manipulating the
  916. objects in the appropriate sections of chapter @ref{Standard procedures}.
  917. @node Storage model, Proper tail recursion, External representations, Basic concepts
  918. @section Storage model
  919. Variables and objects such as pairs, vectors, and strings implicitly
  920. denote locations or sequences of locations. A string, for
  921. @cindex @w{location}
  922. example, denotes as many locations as there are characters in the string.
  923. (These locations need not correspond to a full machine word.) A new value may be
  924. stored into one of these locations using the @t{string-set!} procedure, but
  925. the string continues to denote the same locations as before.
  926. An object fetched from a location, by a variable reference or by
  927. a procedure such as @samp{car}, @samp{vector-ref}, or @samp{string-ref}, is
  928. equivalent in the sense of @code{eqv?}
  929. @c and \ide{eq?} ??
  930. (section @pxref{Equivalence predicates})
  931. @vindex @w{eqv?}
  932. to the object last stored in the location before the fetch.
  933. Every location is marked to show whether it is in use.
  934. No variable or object ever refers to a location that is not in use.
  935. Whenever this report speaks of storage being allocated for a variable
  936. or object, what is meant is that an appropriate number of locations are
  937. chosen from the set of locations that are not in use, and the chosen
  938. locations are marked to indicate that they are now in use before the variable
  939. or object is made to denote them.
  940. In many systems it is desirable for constants (i.e. the values of
  941. @cindex @w{constant}
  942. literal expressions) to reside in read-only-memory. To express this, it is
  943. convenient to imagine that every object that denotes locations is associated
  944. with a flag telling whether that object is mutable or
  945. @cindex @w{mutable}
  946. immutable. In such systems literal constants and the strings
  947. @cindex @w{immutable}
  948. returned by @code{symbol->string} are immutable objects, while all objects
  949. @vindex @w{symbol->string}
  950. created by the other procedures listed in this report are mutable. It is an
  951. error to attempt to store a new value into a location that is denoted by an
  952. immutable object.
  953. @node Proper tail recursion, , Storage model, Basic concepts
  954. @section Proper tail recursion
  955. Implementations of Scheme are required to be
  956. @emph{properly tail-recursive}.
  957. @cindex @w{proper tail recursion}
  958. Procedure calls that occur in certain syntactic
  959. contexts defined below are `tail calls'. A Scheme implementation is
  960. properly tail-recursive if it supports an unbounded number of active
  961. tail calls. A call is @emph{active} if the called procedure may still
  962. return. Note that this includes calls that may be returned from either
  963. by the current continuation or by continuations captured earlier by
  964. @samp{call-with-current-continuation} that are later invoked.
  965. In the absence of captured continuations, calls could
  966. return at most once and the active calls would be those that had not
  967. yet returned.
  968. A formal definition of proper tail recursion can be found
  969. in [propertailrecursion].
  970. @quotation
  971. @emph{Rationale:}
  972. Intuitively, no space is needed for an active tail call because the
  973. continuation that is used in the tail call has the same semantics as the
  974. continuation passed to the procedure containing the call. Although an improper
  975. implementation might use a new continuation in the call, a return
  976. to this new continuation would be followed immediately by a return
  977. to the continuation passed to the procedure. A properly tail-recursive
  978. implementation returns to that continuation directly.
  979. Proper tail recursion was one of the central ideas in Steele and
  980. Sussman's original version of Scheme. Their first Scheme interpreter
  981. implemented both functions and actors. Control flow was expressed using
  982. actors, which differed from functions in that they passed their results
  983. on to another actor instead of returning to a caller. In the terminology
  984. of this section, each actor finished with a tail call to another actor.
  985. Steele and Sussman later observed that in their interpreter the code
  986. for dealing with actors was identical to that for functions and thus
  987. there was no need to include both in the language.
  988. @end quotation
  989. A @emph{tail call} is a procedure call that occurs
  990. @cindex @w{tail call}
  991. in a @emph{tail context}. Tail contexts are defined inductively. Note
  992. that a tail context is always determined with respect to a particular lambda
  993. expression.
  994. @itemize @bullet
  995. @item
  996. The last expression within the body of a lambda expression,
  997. shown as @r{<tail expression>} below, occurs in a tail context.
  998. @format
  999. @t{(lambda <formals>
  1000. <definition>* <expression>* <tail expression>)
  1001. }
  1002. @end format
  1003. @item
  1004. If one of the following expressions is in a tail context,
  1005. then the subexpressions shown as <tail expression> are in a tail context.
  1006. These were derived from rules in the grammar given in
  1007. chapter @ref{Formal syntax and semantics} by replacing some occurrences of <expression>
  1008. with <tail expression>. Only those rules that contain tail contexts
  1009. are shown here.
  1010. @format
  1011. @t{(if <expression> <tail expression> <tail expression>)
  1012. (if <expression> <tail expression>)
  1013. (cond <cond clause>+)
  1014. (cond <cond clause>* (else <tail sequence>))
  1015. (case <expression>
  1016. <case clause>+)
  1017. (case <expression>
  1018. <case clause>*
  1019. (else <tail sequence>))
  1020. (and <expression>* <tail expression>)
  1021. (or <expression>* <tail expression>)
  1022. (let (<binding spec>*) <tail body>)
  1023. (let <variable> (<binding spec>*) <tail body>)
  1024. (let* (<binding spec>*) <tail body>)
  1025. (letrec (<binding spec>*) <tail body>)
  1026. (let-syntax (<syntax spec>*) <tail body>)
  1027. (letrec-syntax (<syntax spec>*) <tail body>)
  1028. (begin <tail sequence>)
  1029. (do (<iteration spec>*)
  1030. (<test> <tail sequence>)
  1031. <expression>*)
  1032. @r{where}
  1033. <cond clause> --> (<test> <tail sequence>)
  1034. <case clause> --> ((<datum>*) <tail sequence>)
  1035. <tail body> --> <definition>* <tail sequence>
  1036. <tail sequence> --> <expression>* <tail expression>
  1037. }
  1038. @end format
  1039. @item
  1040. If a @samp{cond} expression is in a tail context, and has a clause of
  1041. the form @samp{(@r{<expression1>} => @r{<expression2>})}
  1042. then the (implied) call to
  1043. the procedure that results from the evaluation of @r{<expression2>} is in a
  1044. tail context. @r{<expression2>} itself is not in a tail context.
  1045. @end itemize
  1046. Certain built-in procedures are also required to perform tail calls.
  1047. The first argument passed to @code{apply} and to
  1048. @vindex @w{apply}
  1049. @code{call-with-current-continuation}, and the second argument passed to
  1050. @vindex @w{call-with-current-continuation}
  1051. @code{call-with-values}, must be called via a tail call.
  1052. @vindex @w{call-with-values}
  1053. Similarly, @code{eval} must evaluate its argument as if it
  1054. @vindex @w{eval}
  1055. were in tail position within the @code{eval} procedure.
  1056. @vindex @w{eval}
  1057. In the following example the only tail call is the call to @samp{f}.
  1058. None of the calls to @samp{g} or @samp{h} are tail calls. The reference to
  1059. @samp{x} is in a tail context, but it is not a call and thus is not a
  1060. tail call.
  1061. @example
  1062. (lambda ()
  1063. (if (g)
  1064. (let ((x (h)))
  1065. x)
  1066. (and (g) (f))))
  1067. @end example
  1068. @quotation
  1069. @emph{Note:}
  1070. Implementations are allowed, but not required, to
  1071. recognize that some non-tail calls, such as the call to @samp{h}
  1072. above, can be evaluated as though they were tail calls.
  1073. In the example above, the @samp{let} expression could be compiled
  1074. as a tail call to @samp{h}. (The possibility of @samp{h} returning
  1075. an unexpected number of values can be ignored, because in that
  1076. case the effect of the @samp{let} is explicitly unspecified and
  1077. implementation-dependent.)
  1078. @end quotation
  1079. @c @include{expr}
  1080. @c \vfill\eject
  1081. @node Expressions, Program structure, Basic concepts, top
  1082. @chapter Expressions
  1083. @menu
  1084. * Primitive expression types::
  1085. * Derived expression types::
  1086. * Macros::
  1087. @end menu
  1088. @c \newcommand{\syntax}{{\em Syntax: }}
  1089. @c \newcommand{\semantics}{{\em Semantics: }}
  1090. @c [Deleted for R5RS because of multiple-value returns. -RK]
  1091. @c A Scheme expression is a construct that returns a value, such as a
  1092. @c variable reference, literal, procedure call, or conditional.
  1093. Expression types are categorized as @emph{primitive} or @emph{derived}.
  1094. Primitive expression types include variables and procedure calls.
  1095. Derived expression types are not semantically primitive, but can instead
  1096. be defined as macros.
  1097. With the exception of @samp{quasiquote}, whose macro definition is complex,
  1098. the derived expressions are classified as library features.
  1099. Suitable definitions are given in section @ref{Derived expression type}.
  1100. @node Primitive expression types, Derived expression types, Expressions, Expressions
  1101. @section Primitive expression types
  1102. @menu
  1103. * Variable references::
  1104. * Literal expressions::
  1105. * Procedure calls::
  1106. * Procedures::
  1107. * Conditionals::
  1108. * Assignments::
  1109. @end menu
  1110. @node Variable references, Literal expressions, Primitive expression types, Primitive expression types
  1111. @subsection Variable references
  1112. @deffn {syntax} @r{<variable>}
  1113. An expression consisting of a variable
  1114. @cindex @w{variable}
  1115. (section @pxref{Variables; syntactic keywords; and regions}) is a variable reference. The value of
  1116. the variable reference is the value stored in the location to which the
  1117. variable is bound. It is an error to reference an
  1118. unbound variable.
  1119. @cindex @w{unbound}
  1120. @format
  1121. @t{(define x 28)
  1122. x ==> 28
  1123. }
  1124. @end format
  1125. @end deffn
  1126. @node Literal expressions, Procedure calls, Variable references, Primitive expression types
  1127. @subsection Literal expressions
  1128. @deffn {syntax} quote @r{<datum>}
  1129. @deffnx {syntax} @t{'}@r{<datum>}
  1130. @deffnx {syntax} @r{<constant>}
  1131. @samp{(quote @r{<datum>})} evaluates to @r{<datum>}.
  1132. @cindex @w{'}
  1133. @r{<Datum>}
  1134. may be any external representation of a Scheme object (see
  1135. section @pxref{External representations}). This notation is used to include literal
  1136. constants in Scheme code.
  1137. @format
  1138. @t{
  1139. (quote a) ==> a
  1140. (quote #(a b c)) ==> #(a b c)
  1141. (quote (+ 1 2)) ==> (+ 1 2)
  1142. }
  1143. @end format
  1144. @samp{(quote @r{<datum>})} may be abbreviated as
  1145. @t{'}@r{<datum>}. The two notations are equivalent in all
  1146. respects.
  1147. @format
  1148. @t{'a ==> a
  1149. '#(a b c) ==> #(a b c)
  1150. '() ==> ()
  1151. '(+ 1 2) ==> (+ 1 2)
  1152. '(quote a) ==> (quote a)
  1153. ''a ==> (quote a)
  1154. }
  1155. @end format
  1156. Numerical constants, string constants, character constants, and boolean
  1157. constants evaluate ``to themselves''; they need not be quoted.
  1158. @format
  1159. @t{'"abc" ==> "abc"
  1160. "abc" ==> "abc"
  1161. '145932 ==> 145932
  1162. 145932 ==> 145932
  1163. '#t ==> #t
  1164. #t ==> #t
  1165. }
  1166. @end format
  1167. As noted in section @ref{Storage model}, it is an error to alter a constant
  1168. (i.e. the value of a literal expression) using a mutation procedure like
  1169. @samp{set-car!} or @samp{string-set!}.
  1170. @end deffn
  1171. @node Procedure calls, Procedures, Literal expressions, Primitive expression types
  1172. @subsection Procedure calls
  1173. @deffn {syntax} @r{<operator>} @r{<operand1>} @dots{},
  1174. A procedure call is written by simply enclosing in parentheses
  1175. expressions for the procedure to be called and the arguments to be
  1176. passed to it. The operator and operand expressions are evaluated (in an
  1177. unspecified order) and the resulting procedure is passed the resulting
  1178. arguments.
  1179. @cindex @w{procedure call}
  1180. @cindex @w{call}
  1181. @format
  1182. @t{
  1183. (+ 3 4) ==> 7
  1184. ((if #f + *) 3 4) ==> 12
  1185. }
  1186. @end format
  1187. A number of procedures are available as the values of variables in the
  1188. initial environment; for example, the addition and multiplication
  1189. procedures in the above examples are the values of the variables @samp{+}
  1190. and @samp{*}. New procedures are created by evaluating lambda expressions
  1191. (see section @pxref{Procedures}).
  1192. @ignore todo
  1193. At Friedman's request, flushed mention of other ways.
  1194. @end ignore
  1195. @c or definitions (see section~\ref{define}).
  1196. Procedure calls may return any number of values (see @code{values} in
  1197. @vindex @w{values}
  1198. section @pxref{Control features}). With the exception of @samp{values}
  1199. the procedures available in the initial environment return one
  1200. value or, for procedures such as @samp{apply}, pass on the values returned
  1201. by a call to one of their arguments.
  1202. Procedure calls are also called @emph{combinations}.
  1203. @cindex @w{combination}
  1204. @quotation
  1205. @emph{Note:} In contrast to other dialects of Lisp, the order of
  1206. evaluation is unspecified, and the operator expression and the operand
  1207. expressions are always evaluated with the same evaluation rules.
  1208. @end quotation
  1209. @quotation
  1210. @emph{Note:}
  1211. Although the order of evaluation is otherwise unspecified, the effect of
  1212. any concurrent evaluation of the operator and operand expressions is
  1213. constrained to be consistent with some sequential order of evaluation.
  1214. The order of evaluation may be chosen differently for each procedure call.
  1215. @end quotation
  1216. @quotation
  1217. @emph{Note:} In many dialects of Lisp, the empty combination, @t{()}, is a legitimate expression. In Scheme, combinations must have at
  1218. least one subexpression, so @t{()} is not a syntactically valid
  1219. expression.
  1220. @ignore todo
  1221. Dybvig: ``it should be obvious from the syntax.''
  1222. @end ignore
  1223. @end quotation
  1224. @ignore todo
  1225. Freeman:
  1226. I think an explanation as to why evaluation order is not specified
  1227. should be included. It should not include any reference to parallel
  1228. evaluation. Does any existing compiler generate better code because
  1229. the evaluation order is unspecified? Clinger: yes: T3, MacScheme v2,
  1230. probably MIT Scheme and Chez Scheme. But that's not the main reason
  1231. for leaving the order unspecified.
  1232. @end ignore
  1233. @end deffn
  1234. @node Procedures, Conditionals, Procedure calls, Primitive expression types
  1235. @subsection Procedures
  1236. @deffn {syntax} lambda @r{<formals>} @r{<body>}
  1237. @emph{Syntax:}
  1238. @r{<Formals>} should be a formal arguments list as described below,
  1239. and @r{<body>} should be a sequence of one or more expressions.
  1240. @emph{Semantics:}
  1241. A lambda expression evaluates to a procedure. The environment in
  1242. effect when the lambda expression was evaluated is remembered as part of the
  1243. procedure. When the procedure is later called with some actual
  1244. arguments, the environment in which the lambda expression was evaluated will
  1245. be extended by binding the variables in the formal argument list to
  1246. fresh locations, the corresponding actual argument values will be stored
  1247. in those locations, and the expressions in the body of the lambda expression
  1248. will be evaluated sequentially in the extended environment.
  1249. The result(s) of the last expression in the body will be returned as
  1250. the result(s) of the procedure call.
  1251. @format
  1252. @t{(lambda (x) (+ x x)) ==> @emph{}a procedure
  1253. ((lambda (x) (+ x x)) 4) ==> 8
  1254. (define reverse-subtract
  1255. (lambda (x y) (- y x)))
  1256. (reverse-subtract 7 10) ==> 3
  1257. (define add4
  1258. (let ((x 4))
  1259. (lambda (y) (+ x y))))
  1260. (add4 6) ==> 10
  1261. }
  1262. @end format
  1263. @r{<Formals>} should have one of the following forms:
  1264. @itemize @bullet
  1265. @item
  1266. @t{(@r{<variable1>} @dots{},)}:
  1267. The procedure takes a fixed number of arguments; when the procedure is
  1268. called, the arguments will be stored in the bindings of the
  1269. corresponding variables.
  1270. @item
  1271. @r{<variable>}:
  1272. The procedure takes any number of arguments; when the procedure is
  1273. called, the sequence of actual arguments is converted into a newly
  1274. allocated list, and the list is stored in the binding of the
  1275. @r{<variable>}.
  1276. @item
  1277. @t{(@r{<variable1>} @dots{}, @r{<variable_n>} @b{.}
  1278. @r{<variable_n+1>})}:
  1279. If a space-delimited period precedes the last variable, then
  1280. the procedure takes n or more arguments, where n is the
  1281. number of formal arguments before the period (there must
  1282. be at least one).
  1283. The value stored in the binding of the last variable will be a
  1284. newly allocated
  1285. list of the actual arguments left over after all the other actual
  1286. arguments have been matched up against the other formal arguments.
  1287. @end itemize
  1288. It is an error for a @r{<variable>} to appear more than once in
  1289. @r{<formals>}.
  1290. @format
  1291. @t{((lambda x x) 3 4 5 6) ==> (3 4 5 6)
  1292. ((lambda (x y . z) z)
  1293. 3 4 5 6) ==> (5 6)
  1294. }
  1295. @end format
  1296. Each procedure created as the result of evaluating a lambda expression is
  1297. (conceptually) tagged
  1298. with a storage location, in order to make @code{eqv?} and
  1299. @vindex @w{eqv?}
  1300. @code{eq?} work on procedures (see section @pxref{Equivalence predicates}).
  1301. @vindex @w{eq?}
  1302. @end deffn
  1303. @node Conditionals, Assignments, Procedures, Primitive expression types
  1304. @subsection Conditionals
  1305. @deffn {syntax} if @r{<test>} @r{<consequent>} @r{<alternate>}
  1306. @deffnx {syntax} if @r{<test>} @r{<consequent>}
  1307. @c \/ if hyper = italic
  1308. @emph{Syntax:}
  1309. @r{<Test>}, @r{<consequent>}, and @r{<alternate>} may be arbitrary
  1310. expressions.
  1311. @emph{Semantics:}
  1312. An @samp{if} expression is evaluated as follows: first,
  1313. @r{<test>} is evaluated. If it yields a true value (see
  1314. @cindex @w{true}
  1315. section @pxref{Booleans}), then @r{<consequent>} is evaluated and
  1316. its value(s) is(are) returned. Otherwise @r{<alternate>} is evaluated and its
  1317. value(s) is(are) returned. If @r{<test>} yields a false value and no
  1318. @r{<alternate>} is specified, then the result of the expression is
  1319. unspecified.
  1320. @format
  1321. @t{(if (> 3 2) 'yes 'no) ==> yes
  1322. (if (> 2 3) 'yes 'no) ==> no
  1323. (if (> 3 2)
  1324. (- 3 2)
  1325. (+ 3 2)) ==> 1
  1326. }
  1327. @end format
  1328. @end deffn
  1329. @node Assignments, , Conditionals, Primitive expression types
  1330. @subsection Assignments
  1331. @deffn {syntax} set! @r{<variable>} @r{<expression>}
  1332. @r{<Expression>} is evaluated, and the resulting value is stored in
  1333. the location to which @r{<variable>} is bound. @r{<Variable>} must
  1334. be bound either in some region enclosing the @samp{set!} expression
  1335. @cindex @w{region}
  1336. or at top level. The result of the @samp{set!} expression is
  1337. unspecified.
  1338. @format
  1339. @t{(define x 2)
  1340. (+ x 1) ==> 3
  1341. (set! x 4) ==> @emph{unspecified}
  1342. (+ x 1) ==> 5
  1343. }
  1344. @end format
  1345. @end deffn
  1346. @node Derived expression types, Macros, Primitive expression types, Expressions
  1347. @section Derived expression types
  1348. @menu
  1349. * Conditional::
  1350. * Binding constructs::
  1351. * Sequencing::
  1352. * Iteration::
  1353. * Delayed evaluation::
  1354. * Quasiquotation::
  1355. @end menu
  1356. The constructs in this section are hygienic, as discussed in
  1357. section @ref{Macros}.
  1358. For reference purposes, section @ref{Derived expression type} gives macro definitions
  1359. that will convert most of the constructs described in this section
  1360. into the primitive constructs described in the previous section.
  1361. @ignore todo
  1362. Mention that no definition of backquote is provided?
  1363. @end ignore
  1364. @node Conditional, Binding constructs, Derived expression types, Derived expression types
  1365. @subsection Conditionals
  1366. @deffn {library syntax} cond <clause1> <clause2> @dots{},
  1367. @emph{Syntax:}
  1368. Each @r{<clause>} should be of the form
  1369. @format
  1370. @t{(@r{<test>} @r{<expression1>} @dots{},)
  1371. }
  1372. @end format
  1373. where @r{<test>} is any expression. Alternatively, a @r{<clause>} may be
  1374. of the form
  1375. @format
  1376. @t{(@r{<test>} => @r{<expression>})
  1377. }
  1378. @end format
  1379. The last @r{<clause>} may be
  1380. an ``else clause,'' which has the form
  1381. @format
  1382. @t{(else @r{<expression1>} @r{<expression2>} @dots{},)@r{.}
  1383. }
  1384. @end format
  1385. @cindex @w{else}
  1386. @cindex @w{=>}
  1387. @emph{Semantics:}
  1388. A @samp{cond} expression is evaluated by evaluating the @r{<test>}
  1389. expressions of successive @r{<clause>}s in order until one of them
  1390. evaluates to a true value (see
  1391. @cindex @w{true}
  1392. section @pxref{Booleans}). When a @r{<test>} evaluates to a true
  1393. value, then the remaining @r{<expression>}s in its @r{<clause>} are
  1394. evaluated in order, and the result(s) of the last @r{<expression>} in the
  1395. @r{<clause>} is(are) returned as the result(s) of the entire @samp{cond}
  1396. expression. If the selected @r{<clause>} contains only the
  1397. @r{<test>} and no @r{<expression>}s, then the value of the
  1398. @r{<test>} is returned as the result. If the selected @r{<clause>} uses the
  1399. @code{=>} alternate form, then the @r{<expression>} is evaluated.
  1400. @vindex @w{=>}
  1401. Its value must be a procedure that accepts one argument; this procedure is then
  1402. called on the value of the @r{<test>} and the value(s) returned by this
  1403. procedure is(are) returned by the @samp{cond} expression.
  1404. If all @r{<test>}s evaluate
  1405. to false values, and there is no else clause, then the result of
  1406. the conditional expression is unspecified; if there is an else
  1407. clause, then its @r{<expression>}s are evaluated, and the value(s) of
  1408. the last one is(are) returned.
  1409. @format
  1410. @t{(cond ((> 3 2) 'greater)
  1411. ((< 3 2) 'less)) ==> greater
  1412. (cond ((> 3 3) 'greater)
  1413. ((< 3 3) 'less)
  1414. (else 'equal)) ==> equal
  1415. (cond ((assv 'b '((a 1) (b 2))) => cadr)
  1416. (else #f)) ==> 2
  1417. }
  1418. @end format
  1419. @end deffn
  1420. @deffn {library syntax} case @r{<key>} <clause1> <clause2> @dots{},
  1421. @emph{Syntax:}
  1422. @r{<Key>} may be any expression. Each @r{<clause>} should have
  1423. the form
  1424. @format
  1425. @t{((@r{<datum1>} @dots{},) @r{<expression1>} @r{<expression2>} @dots{},)@r{,}
  1426. }
  1427. @end format
  1428. where each @r{<datum>} is an external representation of some object.
  1429. All the @r{<datum>}s must be distinct.
  1430. The last @r{<clause>} may be an ``else clause,'' which has the form
  1431. @format
  1432. @t{(else @r{<expression1>} @r{<expression2>} @dots{},)@r{.}
  1433. }
  1434. @end format
  1435. @vindex else
  1436. @emph{Semantics:}
  1437. A @samp{case} expression is evaluated as follows. @r{<Key>} is
  1438. evaluated and its result is compared against each @r{<datum>}. If the
  1439. result of evaluating @r{<key>} is equivalent (in the sense of
  1440. @samp{eqv?}; see section @pxref{Equivalence predicates}) to a @r{<datum>}, then the
  1441. expressions in the corresponding @r{<clause>} are evaluated from left
  1442. to right and the result(s) of the last expression in the @r{<clause>} is(are)
  1443. returned as the result(s) of the @samp{case} expression. If the result of
  1444. evaluating @r{<key>} is different from every @r{<datum>}, then if
  1445. there is an else clause its expressions are evaluated and the
  1446. result(s) of the last is(are) the result(s) of the @samp{case} expression;
  1447. otherwise the result of the @samp{case} expression is unspecified.
  1448. @format
  1449. @t{(case (* 2 3)
  1450. ((2 3 5 7) 'prime)
  1451. ((1 4 6 8 9) 'composite)) ==> composite
  1452. (case (car '(c d))
  1453. ((a) 'a)
  1454. ((b) 'b)) ==> @emph{unspecified}
  1455. (case (car '(c d))
  1456. ((a e i o u) 'vowel)
  1457. ((w y) 'semivowel)
  1458. (else 'consonant)) ==> consonant
  1459. }
  1460. @end format
  1461. @end deffn
  1462. @deffn {library syntax} and <test1> @dots{},
  1463. The @r{<test>} expressions are evaluated from left to right, and the
  1464. value of the first expression that evaluates to a false value (see
  1465. section @pxref{Booleans}) is returned. Any remaining expressions
  1466. are not evaluated. If all the expressions evaluate to true values, the
  1467. value of the last expression is returned. If there are no expressions
  1468. then @t{#t} is returned.
  1469. @format
  1470. @t{(and (= 2 2) (> 2 1)) ==> #t
  1471. (and (= 2 2) (< 2 1)) ==> #f
  1472. (and 1 2 'c '(f g)) ==> (f g)
  1473. (and) ==> #t
  1474. }
  1475. @end format
  1476. @end deffn
  1477. @deffn {library syntax} or <test1> @dots{},
  1478. The @r{<test>} expressions are evaluated from left to right, and the value of the
  1479. first expression that evaluates to a true value (see
  1480. section @pxref{Booleans}) is returned. Any remaining expressions
  1481. are not evaluated. If all expressions evaluate to false values, the
  1482. value of the last expression is returned. If there are no
  1483. expressions then @t{#f} is returned.
  1484. @format
  1485. @t{(or (= 2 2) (> 2 1)) ==> #t
  1486. (or (= 2 2) (< 2 1)) ==> #t
  1487. (or #f #f #f) ==> #f
  1488. (or (memq 'b '(a b c))
  1489. (/ 3 0)) ==> (b c)
  1490. }
  1491. @end format
  1492. @end deffn
  1493. @node Binding constructs, Sequencing, Conditional, Derived expression types
  1494. @subsection Binding constructs
  1495. The three binding constructs @samp{let}, @samp{let*}, and @samp{letrec}
  1496. give Scheme a block structure, like Algol 60. The syntax of the three
  1497. constructs is identical, but they differ in the regions they establish
  1498. @cindex @w{region}
  1499. for their variable bindings. In a @samp{let} expression, the initial
  1500. values are computed before any of the variables become bound; in a
  1501. @samp{let*} expression, the bindings and evaluations are performed
  1502. sequentially; while in a @samp{letrec} expression, all the bindings are in
  1503. effect while their initial values are being computed, thus allowing
  1504. mutually recursive definitions.
  1505. @deffn {library syntax} let @r{<bindings>} @r{<body>}
  1506. @emph{Syntax:}
  1507. @r{<Bindings>} should have the form
  1508. @format
  1509. @t{((@r{<variable1>} @r{<init1>}) @dots{},)@r{,}
  1510. }
  1511. @end format
  1512. where each @r{<init>} is an expression, and @r{<body>} should be a
  1513. sequence of one or more expressions. It is
  1514. an error for a @r{<variable>} to appear more than once in the list of variables
  1515. being bound.
  1516. @emph{Semantics:}
  1517. The @r{<init>}s are evaluated in the current environment (in some
  1518. unspecified order), the @r{<variable>}s are bound to fresh locations
  1519. holding the results, the @r{<body>} is evaluated in the extended
  1520. environment, and the value(s) of the last expression of @r{<body>}
  1521. is(are) returned. Each binding of a @r{<variable>} has @r{<body>} as its
  1522. region.
  1523. @cindex @w{region}
  1524. @format
  1525. @t{(let ((x 2) (y 3))
  1526. (* x y)) ==> 6
  1527. (let ((x 2) (y 3))
  1528. (let ((x 7)
  1529. (z (+ x y)))
  1530. (* z x))) ==> 35
  1531. }
  1532. @end format
  1533. See also named @samp{let}, section @ref{Iteration}.
  1534. @end deffn
  1535. @deffn {library syntax} let* @r{<bindings>} @r{<body>}
  1536. @emph{Syntax:}
  1537. @r{<Bindings>} should have the form
  1538. @format
  1539. @t{((@r{<variable1>} @r{<init1>}) @dots{},)@r{,}
  1540. }
  1541. @end format
  1542. and @r{<body>} should be a sequence of
  1543. one or more expressions.
  1544. @emph{Semantics:}
  1545. @samp{Let*} is similar to @samp{let}, but the bindings are performed
  1546. sequentially from left to right, and the region of a binding indicated
  1547. @cindex @w{region}
  1548. by @samp{(@r{<variable>} @r{<init>})} is that part of the @samp{let*}
  1549. expression to the right of the binding. Thus the second binding is done
  1550. in an environment in which the first binding is visible, and so on.
  1551. @format
  1552. @t{(let ((x 2) (y 3))
  1553. (let* ((x 7)
  1554. (z (+ x y)))
  1555. (* z x))) ==> 70
  1556. }
  1557. @end format
  1558. @end deffn
  1559. @deffn {library syntax} letrec @r{<bindings>} @r{<body>}
  1560. @emph{Syntax:}
  1561. @r{<Bindings>} should have the form
  1562. @format
  1563. @t{((@r{<variable1>} @r{<init1>}) @dots{},)@r{,}
  1564. }
  1565. @end format
  1566. and @r{<body>} should be a sequence of
  1567. one or more expressions. It is an error for a @r{<variable>} to appear more
  1568. than once in the list of variables being bound.
  1569. @emph{Semantics:}
  1570. The @r{<variable>}s are bound to fresh locations holding undefined
  1571. values, the @r{<init>}s are evaluated in the resulting environment (in
  1572. some unspecified order), each @r{<variable>} is assigned to the result
  1573. of the corresponding @r{<init>}, the @r{<body>} is evaluated in the
  1574. resulting environment, and the value(s) of the last expression in
  1575. @r{<body>} is(are) returned. Each binding of a @r{<variable>} has the
  1576. entire @samp{letrec} expression as its region, making it possible to
  1577. @cindex @w{region}
  1578. define mutually recursive procedures.
  1579. @format
  1580. @t{(letrec ((even?
  1581. (lambda (n)
  1582. (if (zero? n)
  1583. #t
  1584. (odd? (- n 1)))))
  1585. (odd?
  1586. (lambda (n)
  1587. (if (zero? n)
  1588. #f
  1589. (even? (- n 1))))))
  1590. (even? 88))
  1591. ==> #t
  1592. }
  1593. @end format
  1594. One restriction on @samp{letrec} is very important: it must be possible
  1595. to evaluate each @r{<init>} without assigning or referring to the value of any
  1596. @r{<variable>}. If this restriction is violated, then it is an error. The
  1597. restriction is necessary because Scheme passes arguments by value rather than by
  1598. name. In the most common uses of @samp{letrec}, all the @r{<init>}s are
  1599. lambda expressions and the restriction is satisfied automatically.
  1600. @c \todo{use or uses? --- Jinx.}
  1601. @end deffn
  1602. @node Sequencing, Iteration, Binding constructs, Derived expression types
  1603. @subsection Sequencing
  1604. @deffn {library syntax} begin <expression1> <expression2> @dots{},
  1605. The @r{<expression>}s are evaluated sequentially from left to right,
  1606. and the value(s) of the last @r{<expression>} is(are) returned. This
  1607. expression type is used to sequence side effects such as input and
  1608. output.
  1609. @format
  1610. @t{(define x 0)
  1611. (begin (set! x 5)
  1612. (+ x 1)) ==> 6
  1613. (begin (display "4 plus 1 equals ")
  1614. (display (+ 4 1))) ==> @emph{unspecified}
  1615. @emph{and prints} 4 plus 1 equals 5
  1616. }
  1617. @end format
  1618. @end deffn
  1619. @node Iteration, Delayed evaluation, Sequencing, Derived expression types
  1620. @subsection Iteration
  1621. @c \unsection
  1622. @noindent
  1623. @deffn {library syntax} do ((@r{<variable1>} @r{<init1>} @r{<step1>}) @dots{}) (@r{<test>} @r{<expression>} @dots{}) @r{<command>} @dots{}
  1624. @cindex @w{do}
  1625. @samp{Do} is an iteration construct. It specifies a set of variables to
  1626. be bound, how they are to be initialized at the start, and how they are
  1627. to be updated on each iteration. When a termination condition is met,
  1628. the loop exits after evaluating the @r{<expression>}s.
  1629. @samp{Do} expressions are evaluated as follows:
  1630. The @r{<init>} expressions are evaluated (in some unspecified order),
  1631. the @r{<variable>}s are bound to fresh locations, the results of the
  1632. @r{<init>} expressions are stored in the bindings of the
  1633. @r{<variable>}s, and then the iteration phase begins.
  1634. Each iteration begins by evaluating @r{<test>}; if the result is
  1635. false (see section @pxref{Booleans}), then the @r{<command>}
  1636. expressions are evaluated in order for effect, the @r{<step>}
  1637. expressions are evaluated in some unspecified order, the
  1638. @r{<variable>}s are bound to fresh locations, the results of the
  1639. @r{<step>}s are stored in the bindings of the
  1640. @r{<variable>}s, and the next iteration begins.
  1641. If @r{<test>} evaluates to a true value, then the
  1642. @r{<expression>}s are evaluated from left to right and the value(s) of
  1643. the last @r{<expression>} is(are) returned. If no @r{<expression>}s
  1644. are present, then the value of the @samp{do} expression is unspecified.
  1645. The region of the binding of a @r{<variable>}
  1646. @cindex @w{region}
  1647. consists of the entire @samp{do} expression except for the @r{<init>}s.
  1648. It is an error for a @r{<variable>} to appear more than once in the
  1649. list of @samp{do} variables.
  1650. A @r{<step>} may be omitted, in which case the effect is the
  1651. same as if @samp{(@r{<variable>} @r{<init>} @r{<variable>})} had
  1652. been written instead of @samp{(@r{<variable>} @r{<init>})}.
  1653. @format
  1654. @t{(do ((vec (make-vector 5))
  1655. (i 0 (+ i 1)))
  1656. ((= i 5) vec)
  1657. (vector-set! vec i i)) ==> #(0 1 2 3 4)
  1658. (let ((x '(1 3 5 7 9)))
  1659. (do ((x x (cdr x))
  1660. (sum 0 (+ sum (car x))))
  1661. ((null? x) sum))) ==> 25
  1662. }
  1663. @end format
  1664. @c \end{entry}
  1665. @end deffn
  1666. @deffn {library syntax} let @r{<variable>} @r{<bindings>} @r{<body>}
  1667. ``Named @samp{let}'' is a variant on the syntax of @code{let} which provides
  1668. @vindex @w{let}
  1669. a more general looping construct than @samp{do} and may also be used to express
  1670. recursions.
  1671. It has the same syntax and semantics as ordinary @samp{let}
  1672. except that @r{<variable>} is bound within @r{<body>} to a procedure
  1673. whose formal arguments are the bound variables and whose body is
  1674. @r{<body>}. Thus the execution of @r{<body>} may be repeated by
  1675. invoking the procedure named by @r{<variable>}.
  1676. @c | <-- right margin
  1677. @format
  1678. @t{(let loop ((numbers '(3 -2 1 6 -5))
  1679. (nonneg '())
  1680. (neg '()))
  1681. (cond ((null? numbers) (list nonneg neg))
  1682. ((>= (car numbers) 0)
  1683. (loop (cdr numbers)
  1684. (cons (car numbers) nonneg)
  1685. neg))
  1686. ((< (car numbers) 0)
  1687. (loop (cdr numbers)
  1688. nonneg
  1689. (cons (car numbers) neg)))))
  1690. ==> ((6 1 3) (-5 -2))
  1691. }
  1692. @end format
  1693. @end deffn
  1694. @node Delayed evaluation, Quasiquotation, Iteration, Derived expression types
  1695. @subsection Delayed evaluation
  1696. @deffn {library syntax} delay @r{<expression>}
  1697. @ignore todo
  1698. Fix.
  1699. @end ignore
  1700. The @samp{delay} construct is used together with the procedure @code{force} to
  1701. @vindex @w{force}
  1702. implement @dfn{lazy evaluation} or @dfn{call by need}.
  1703. @cindex @w{call by need}
  1704. @cindex @w{lazy evaluation}
  1705. @t{(delay @r{<expression>})} returns an object called a
  1706. @dfn{promise} which at some point in the future may be asked (by
  1707. @cindex @w{promise}
  1708. the @samp{force} procedure)
  1709. @ignore todo
  1710. Bartley's white lie; OK?
  1711. @end ignore
  1712. to evaluate
  1713. @r{<expression>}, and deliver the resulting value.
  1714. The effect of @r{<expression>} returning multiple values
  1715. is unspecified.
  1716. See the description of @samp{force} (section @pxref{Control features}) for a
  1717. more complete description of @samp{delay}.
  1718. @end deffn
  1719. @node Quasiquotation, , Delayed evaluation, Derived expression types
  1720. @subsection Quasiquotation
  1721. @deffn {syntax} quasiquote @r{<qq template>}
  1722. @deffnx {syntax} @t{`}@r{<qq template>}
  1723. ``Backquote'' or ``quasiquote'' expressions are useful
  1724. @cindex @w{backquote}
  1725. for constructing a list or vector structure when most but not all of the
  1726. desired structure is known in advance. If no
  1727. commas appear within the @r{<qq template>}, the result of
  1728. @cindex @w{comma}
  1729. evaluating
  1730. @t{`}@r{<qq template>} is equivalent to the result of evaluating
  1731. @t{'}@r{<qq template>}. If a comma appears within the
  1732. @cindex @w{,}
  1733. @r{<qq template>}, however, the expression following the comma is
  1734. evaluated (``unquoted'') and its result is inserted into the structure
  1735. instead of the comma and the expression. If a comma appears followed
  1736. immediately by an at-sign (@@), then the following
  1737. @cindex @w{,@@}
  1738. expression must evaluate to a list; the opening and closing parentheses
  1739. of the list are then ``stripped away'' and the elements of the list are
  1740. inserted in place of the comma at-sign expression sequence. A comma
  1741. at-sign should only appear within a list or vector @r{<qq template>}.
  1742. @c struck: "(in the sense of {\cf equal?})" after "equivalent"
  1743. @format
  1744. @t{`(list ,(+ 1 2) 4) ==> (list 3 4)
  1745. (let ((name 'a)) `(list ,name ',name))
  1746. ==> (list a (quote a))
  1747. `(a ,(+ 1 2) ,@@(map abs '(4 -5 6)) b)
  1748. ==> (a 3 4 5 6 b)
  1749. `((@samp{foo} ,(- 10 3)) ,@@(cdr '(c)) . ,(car '(cons)))
  1750. ==> ((foo 7) . cons)
  1751. `#(10 5 ,(sqrt 4) ,@@(map sqrt '(16 9)) 8)
  1752. ==> #(10 5 2 4 3 8)
  1753. }
  1754. @end format
  1755. Quasiquote forms may be nested. Substitutions are made only for
  1756. unquoted components appearing at the same nesting level
  1757. as the outermost backquote. The nesting level increases by one inside
  1758. each successive quasiquotation, and decreases by one inside each
  1759. unquotation.
  1760. @format
  1761. @t{`(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f)
  1762. ==> (a `(b ,(+ 1 2) ,(foo 4 d) e) f)
  1763. (let ((name1 'x)
  1764. (name2 'y))
  1765. `(a `(b ,,name1 ,',name2 d) e))
  1766. ==> (a `(b ,x ,'y d) e)
  1767. }
  1768. @end format
  1769. The two notations
  1770. @t{`}@r{<qq template>} and @t{(quasiquote @r{<qq template>})}
  1771. are identical in all respects.
  1772. @samp{,@r{<expression>}} is identical to @samp{(unquote @r{<expression>})},
  1773. and
  1774. @samp{,@@@r{<expression>}} is identical to @samp{(unquote-splicing @r{<expression>})}.
  1775. The external syntax generated by @code{write} for two-element lists whose
  1776. @vindex @w{write}
  1777. car is one of these symbols may vary between implementations.
  1778. @cindex @w{`}
  1779. @format
  1780. @t{(quasiquote (list (unquote (+ 1 2)) 4))
  1781. ==> (list 3 4)
  1782. '(quasiquote (list (unquote (+ 1 2)) 4))
  1783. ==> `(list ,(+ 1 2) 4)
  1784. @emph{}i.e., (quasiquote (list (unquote (+ 1 2)) 4))
  1785. }
  1786. @end format
  1787. Unpredictable behavior can result if any of the symbols
  1788. @code{quasiquote}, @code{unquote}, or @code{unquote-splicing} appear in
  1789. @vindex @w{unquote-splicing}
  1790. @vindex @w{unquote}
  1791. @vindex @w{quasiquote}
  1792. positions within a @r{<qq template>} otherwise than as described above.
  1793. @end deffn
  1794. @node Macros, , Derived expression types, Expressions
  1795. @section Macros
  1796. @menu
  1797. * Binding constructs for syntactic keywords::
  1798. * Pattern language::
  1799. @end menu
  1800. Scheme programs can define and use new derived expression types,
  1801. called @emph{macros}.
  1802. @cindex @w{macro}
  1803. Program-defined expression types have the syntax
  1804. @example
  1805. (@r{<keyword>} @r{<datum>} ...)
  1806. @end example
  1807. where @r{<keyword>} is an identifier that uniquely determines the
  1808. expression type. This identifier is called the @emph{syntactic
  1809. keyword}, or simply @emph{keyword}, of the macro. The
  1810. @cindex @w{macro keyword}
  1811. @cindex @w{keyword}
  1812. @cindex @w{syntactic keyword}
  1813. number of the @r{<datum>}s, and their syntax, depends on the
  1814. expression type.
  1815. Each instance of a macro is called a @emph{use}
  1816. @cindex @w{macro use}
  1817. of the macro.
  1818. The set of rules that specifies
  1819. how a use of a macro is transcribed into a more primitive expression
  1820. is called the @emph{transformer}
  1821. @cindex @w{macro transformer}
  1822. of the macro.
  1823. The macro definition facility consists of two parts:
  1824. @itemize @bullet
  1825. @item
  1826. A set of expressions used to establish that certain identifiers
  1827. are macro keywords, associate them with macro transformers, and control
  1828. the scope within which a macro is defined, and
  1829. @item
  1830. a pattern language for specifying macro transformers.
  1831. @end itemize
  1832. The syntactic keyword of a macro may shadow variable bindings, and local
  1833. variable bindings may shadow keyword bindings. All macros
  1834. @cindex @w{keyword}
  1835. defined using the pattern language are ``hygienic'' and ``referentially
  1836. transparent'' and thus preserve Scheme's lexical scoping [Kohlbecker86], [
  1837. hygienic], [Bawden88], [macrosthatwork], [syntacticabstraction]:
  1838. @cindex @w{hygienic}
  1839. @cindex @w{referentially transparent}
  1840. @itemize @bullet
  1841. @item
  1842. If a macro transformer inserts a binding for an identifier
  1843. (variable or keyword), the identifier will in effect be renamed
  1844. throughout its scope to avoid conflicts with other identifiers.
  1845. Note that a @code{define} at top level may or may not introduce a binding;
  1846. see section @ref{Definitions}.
  1847. @item
  1848. If a macro transformer inserts a free reference to an
  1849. identifier, the reference refers to the binding that was visible
  1850. where the transformer was specified, regardless of any local
  1851. bindings that may surround the use of the macro.
  1852. @end itemize
  1853. @vindex @w{define}
  1854. @c The low-level facility permits non-hygienic macros to be written,
  1855. @c and may be used to implement the high-level pattern language.
  1856. @c The fourth section describes some features that would make the
  1857. @c low-level macro facility easier to use directly.
  1858. @node Binding constructs for syntactic keywords, Pattern language, Macros, Macros
  1859. @subsection Binding constructs for syntactic keywords
  1860. @samp{Let-syntax} and @samp{letrec-syntax} are
  1861. analogous to @samp{let} and @samp{letrec}, but they bind
  1862. syntactic keywords to macro transformers instead of binding variables
  1863. to locations that contain values. Syntactic keywords may also be
  1864. bound at top level; see section @ref{Syntax definitions}.
  1865. @deffn {syntax} let-syntax @r{<bindings>} @r{<body>}
  1866. @emph{Syntax:}
  1867. @r{<Bindings>} should have the form
  1868. @format
  1869. @t{((@r{<keyword>} @r{<transformer spec>}) @dots{},)
  1870. }
  1871. @end format
  1872. Each @r{<keyword>} is an identifier,
  1873. each @r{<transformer spec>} is an instance of @samp{syntax-rules}, and
  1874. @r{<body>} should be a sequence of one or more expressions. It is an error
  1875. for a @r{<keyword>} to appear more than once in the list of keywords
  1876. being bound.
  1877. @emph{Semantics:}
  1878. The @r{<body>} is expanded in the syntactic environment
  1879. obtained by extending the syntactic environment of the
  1880. @samp{let-syntax} expression with macros whose keywords are
  1881. the @r{<keyword>}s, bound to the specified transformers.
  1882. Each binding of a @r{<keyword>} has @r{<body>} as its region.
  1883. @format
  1884. @t{(let-syntax ((when (syntax-rules ()
  1885. ((when test stmt1 stmt2 ...)
  1886. (if test
  1887. (begin stmt1
  1888. stmt2 ...))))))
  1889. (let ((if #t))
  1890. (when if (set! if 'now))
  1891. if)) ==> now
  1892. (let ((x 'outer))
  1893. (let-syntax ((m (syntax-rules () ((m) x))))
  1894. (let ((x 'inner))
  1895. (m)))) ==> outer
  1896. }
  1897. @end format
  1898. @end deffn
  1899. @deffn {syntax} letrec-syntax @r{<bindings>} @r{<body>}
  1900. @emph{Syntax:}
  1901. Same as for @samp{let-syntax}.
  1902. @emph{Semantics:}
  1903. The @r{<body>} is expanded in the syntactic environment obtained by
  1904. extending the syntactic environment of the @samp{letrec-syntax}
  1905. expression with macros whose keywords are the
  1906. @r{<keyword>}s, bound to the specified transformers.
  1907. Each binding of a @r{<keyword>} has the @r{<bindings>}
  1908. as well as the @r{<body>} within its region,
  1909. so the transformers can
  1910. transcribe expressions into uses of the macros
  1911. introduced by the @samp{letrec-syntax} expression.
  1912. @format
  1913. @t{(letrec-syntax
  1914. ((my-or (syntax-rules ()
  1915. ((my-or) #f)
  1916. ((my-or e) e)
  1917. ((my-or e1 e2 ...)
  1918. (let ((temp e1))
  1919. (if temp
  1920. temp
  1921. (my-or e2 ...)))))))
  1922. (let ((x #f)
  1923. (y 7)
  1924. (temp 8)
  1925. (let odd?)
  1926. (if even?))
  1927. (my-or x
  1928. (let temp)
  1929. (if y)
  1930. y))) ==> 7
  1931. }
  1932. @end format
  1933. @end deffn
  1934. @node Pattern language, , Binding constructs for syntactic keywords, Macros
  1935. @subsection Pattern language
  1936. A @r{<transformer spec>} has the following form:
  1937. @deffn {} syntax-rules @r{<literals>} @r{<syntax rule>} @dots{},
  1938. @emph{Syntax:}
  1939. @r{<Literals>} is a list of identifiers and each @r{<syntax rule>}
  1940. should be of the form
  1941. @format
  1942. @t{(@r{<pattern>} @r{<template>})
  1943. }
  1944. @end format
  1945. The @r{<pattern>} in a @r{<syntax rule>} is a list @r{<pattern>}
  1946. that begins with the keyword for the macro.
  1947. A @r{<pattern>} is either an identifier, a constant, or one of the
  1948. following
  1949. @format
  1950. @t{(@r{<pattern>} @dots{})
  1951. (@r{<pattern>} @r{<pattern>} @dots{} . @r{<pattern>})
  1952. (@r{<pattern>} @dots{} @r{<pattern>} @r{<ellipsis>})
  1953. #(@r{<pattern>} @dots{})
  1954. #(@r{<pattern>} @dots{} @r{<pattern>} @r{<ellipsis>})
  1955. }
  1956. @end format
  1957. and a template is either an identifier, a constant, or one of the following
  1958. @format
  1959. @t{(@r{<element>} @dots{})
  1960. (@r{<element>} @r{<element>} @dots{} . @r{<template>})
  1961. #(@r{<element>} @dots{})
  1962. }
  1963. @end format
  1964. where an @r{<element>} is a @r{<template>} optionally
  1965. followed by an @r{<ellipsis>} and
  1966. an @r{<ellipsis>} is the identifier ``@samp{...}'' (which cannot be used as
  1967. an identifier in either a template or a pattern).
  1968. @vindex ...
  1969. @emph{Semantics:} An instance of @samp{syntax-rules} produces a new macro
  1970. transformer by specifying a sequence of hygienic rewrite rules. A use
  1971. of a macro whose keyword is associated with a transformer specified by
  1972. @samp{syntax-rules} is matched against the patterns contained in the
  1973. @r{<syntax rule>}s, beginning with the leftmost @r{<syntax rule>}.
  1974. When a match is found, the macro use is transcribed hygienically
  1975. according to the template.
  1976. An identifier that appears in the pattern of a @r{<syntax rule>} is
  1977. a @emph{pattern variable}, unless it is the keyword that begins the pattern,
  1978. is listed in @r{<literals>}, or is the identifier ``@samp{...}''.
  1979. Pattern variables match arbitrary input elements and
  1980. are used to refer to elements of the input in the template. It is an
  1981. error for the same pattern variable to appear more than once in a
  1982. @r{<pattern>}.
  1983. The keyword at the beginning of the pattern in a
  1984. @r{<syntax rule>} is not involved in the matching and
  1985. is not considered a pattern variable or literal identifier.
  1986. @quotation
  1987. @emph{Rationale:}
  1988. The scope of the keyword is determined by the expression or syntax
  1989. definition that binds it to the associated macro transformer.
  1990. If the keyword were a pattern variable or literal
  1991. identifier, then
  1992. the template that follows the pattern would be within its scope
  1993. regardless of whether the keyword were bound by @samp{let-syntax}
  1994. or by @samp{letrec-syntax}.
  1995. @end quotation
  1996. Identifiers that appear in @r{<literals>} are interpreted as literal
  1997. identifiers to be matched against corresponding subforms of the input.
  1998. A subform
  1999. in the input matches a literal identifier if and only if it is an
  2000. identifier
  2001. and either both its occurrence in the macro expression and its
  2002. occurrence in the macro definition have the same lexical binding, or
  2003. the two identifiers are equal and both have no lexical binding.
  2004. @c [Bill Rozas suggested the term "noise word" for these literal
  2005. @c identifiers, but in their most interesting uses, such as a setf
  2006. @c macro, they aren't noise words at all. -- Will]
  2007. A subpattern followed by @samp{...} can match zero or more elements of the
  2008. input. It is an error for @samp{...} to appear in @r{<literals>}.
  2009. Within a pattern the identifier @samp{...} must follow the last element of
  2010. a nonempty sequence of subpatterns.
  2011. More formally, an input form F matches a pattern P if and only if:
  2012. @itemize @bullet
  2013. @item
  2014. P is a non-literal identifier; or
  2015. @item
  2016. P is a literal identifier and F is an identifier with the same
  2017. binding; or
  2018. @item
  2019. P is a list @samp{(P_1 @dots{} P_n)} and F is a
  2020. list of n
  2021. forms that match P_1 through P_n, respectively; or
  2022. @item
  2023. P is an improper list
  2024. @samp{(P_1 P_2 @dots{} P_n . P_n+1)}
  2025. and F is a list or
  2026. improper list of n or more forms that match P_1 through P_n,
  2027. respectively, and whose nth ``cdr'' matches P_n+1; or
  2028. @item
  2029. P is of the form
  2030. @samp{(P_1 @dots{} P_n P_n+1 <ellipsis>)}
  2031. where <ellipsis> is the identifier @samp{...}
  2032. and F is
  2033. a proper list of at least n forms, the first n of which match
  2034. P_1 through P_n, respectively, and each remaining element of F
  2035. matches P_n+1; or
  2036. @item
  2037. P is a vector of the form @samp{#(P_1 @dots{} P_n)}
  2038. and F is a vector
  2039. of n forms that match P_1 through P_n; or
  2040. @item
  2041. P is of the form
  2042. @samp{#(P_1 @dots{} P_n P_n+1 <ellipsis>)}
  2043. where <ellipsis> is the identifier @samp{...}
  2044. and F is a vector of n
  2045. or more forms the first n of which match
  2046. P_1 through P_n, respectively, and each remaining element of F
  2047. matches P_n+1; or
  2048. @item
  2049. P is a datum and F is equal to P in the sense of
  2050. the @samp{equal?} procedure.
  2051. @end itemize
  2052. It is an error to use a macro keyword, within the scope of its
  2053. binding, in an expression that does not match any of the patterns.
  2054. When a macro use is transcribed according to the template of the
  2055. matching @r{<syntax rule>}, pattern variables that occur in the
  2056. template are replaced by the subforms they match in the input.
  2057. Pattern variables that occur in subpatterns followed by one or more
  2058. instances of the identifier
  2059. @samp{...} are allowed only in subtemplates that are
  2060. followed by as many instances of @samp{...}.
  2061. They are replaced in the
  2062. output by all of the subforms they match in the input, distributed as
  2063. indicated. It is an error if the output cannot be built up as
  2064. specified.
  2065. @c %% This description of output construction is very vague. It should
  2066. @c %% probably be formalized, but that is not easy...
  2067. Identifiers that appear in the template but are not pattern variables
  2068. or the identifier
  2069. @samp{...} are inserted into the output as literal identifiers. If a
  2070. literal identifier is inserted as a free identifier then it refers to the
  2071. binding of that identifier within whose scope the instance of
  2072. @samp{syntax-rules} appears.
  2073. If a literal identifier is inserted as a bound identifier then it is
  2074. in effect renamed to prevent inadvertent captures of free identifiers.
  2075. As an example, if @code{let} and @code{cond} are defined as in
  2076. @vindex @w{cond}
  2077. @vindex @w{let}
  2078. section @ref{Derived expression type} then they are hygienic (as required) and
  2079. the following is not an error.
  2080. @format
  2081. @t{(let ((=> #f))
  2082. (cond (#t => 'ok))) ==> ok
  2083. }
  2084. @end format
  2085. The macro transformer for @samp{cond} recognizes @samp{=>}
  2086. as a local variable, and hence an expression, and not as the
  2087. top-level identifier @samp{=>}, which the macro transformer treats
  2088. as a syntactic keyword. Thus the example expands into
  2089. @format
  2090. @t{(let ((=> #f))
  2091. (if #t (begin => 'ok)))
  2092. }
  2093. @end format
  2094. instead of
  2095. @format
  2096. @t{(let ((=> #f))
  2097. (let ((temp #t))
  2098. (if temp ('ok temp))))
  2099. }
  2100. @end format
  2101. which would result in an invalid procedure call.
  2102. @end deffn
  2103. @page
  2104. @c @include{prog}
  2105. @node Program structure, Standard procedures, Expressions, top
  2106. @chapter Program structure
  2107. @menu
  2108. * Programs::
  2109. * Definitions::
  2110. * Syntax definitions::
  2111. @end menu
  2112. @node Programs, Definitions, Program structure, Program structure
  2113. @section Programs
  2114. A Scheme program consists of a sequence of expressions, definitions,
  2115. and syntax definitions.
  2116. Expressions are described in chapter @ref{Expressions};
  2117. definitions and syntax definitions are the subject of the rest of the
  2118. present chapter.
  2119. Programs are typically stored in files or entered interactively to a
  2120. running Scheme system, although other paradigms are possible;
  2121. questions of user interface lie outside the scope of this report.
  2122. (Indeed, Scheme would still be useful as a notation for expressing
  2123. computational methods even in the absence of a mechanical
  2124. implementation.)
  2125. Definitions and syntax definitions occurring at the top level of a program
  2126. can be interpreted
  2127. declaratively.
  2128. They cause bindings to be created in the top level
  2129. environment or modify the value of existing top-level bindings.
  2130. Expressions occurring at the top level of a program are
  2131. interpreted imperatively; they are executed in order when the program is
  2132. invoked or loaded, and typically perform some kind of initialization.
  2133. At the top level of a program @t{(begin @r{<form1>} @dots{},)} is
  2134. equivalent to the sequence of expressions, definitions, and syntax definitions
  2135. that form the body of the @code{begin}.
  2136. @vindex @w{begin}
  2137. @ignore todo
  2138. Cromarty, etc.: disclaimer about top level?
  2139. @end ignore
  2140. @node Definitions, Syntax definitions, Programs, Program structure
  2141. @section Definitions
  2142. @menu
  2143. * Top level definitions::
  2144. * Internal definitions::
  2145. @end menu
  2146. Definitions are valid in some, but not all, contexts where expressions
  2147. are allowed. They are valid only at the top level of a @r{<program>}
  2148. and at the beginning of a @r{<body>}.
  2149. @cindex @w{definition}
  2150. A definition should have one of the following forms:
  2151. @cindex @w{define}
  2152. @itemize @bullet
  2153. @item @t{(define @r{<variable>} @r{<expression>})}
  2154. @item @t{(define (@r{<variable>} @r{<formals>}) @r{<body>})}
  2155. @r{<Formals>} should be either a
  2156. sequence of zero or more variables, or a sequence of one or more
  2157. variables followed by a space-delimited period and another variable (as
  2158. in a lambda expression). This form is equivalent to
  2159. @example
  2160. (define @r{<variable>}
  2161. (lambda (@r{<formals>}) @r{<body>}))@r{.}
  2162. @end example
  2163. @item @t{(define (@r{<variable>} .@: @r{<formal>}) @r{<body>})}
  2164. @r{<Formal>} should be a single
  2165. variable. This form is equivalent to
  2166. @example
  2167. (define @r{<variable>}
  2168. (lambda @r{<formal>} @r{<body>}))@r{.}
  2169. @end example
  2170. @end itemize
  2171. @node Top level definitions, Internal definitions, Definitions, Definitions
  2172. @subsection Top level definitions
  2173. At the top level of a program, a definition
  2174. @example
  2175. (define @r{<variable>} @r{<expression>})
  2176. @end example
  2177. has essentially the same effect as the assignment expression
  2178. @example
  2179. (set! @r{<variable>} @r{<expression>})
  2180. @end example
  2181. if @r{<variable>} is bound. If @r{<variable>} is not bound,
  2182. however, then the definition will bind @r{<variable>} to a new
  2183. location before performing the assignment, whereas it would be an error
  2184. to perform a @samp{set!} on an unbound variable.
  2185. @cindex @w{unbound}
  2186. @example
  2187. (define add3
  2188. (lambda (x) (+ x 3)))
  2189. (add3 3) ==> 6
  2190. (define first car)
  2191. (first '(1 2)) ==> 1
  2192. @end example
  2193. Some implementations of Scheme use an initial environment in
  2194. which all possible variables are bound to locations, most of
  2195. which contain undefined values. Top level definitions in
  2196. such an implementation are truly equivalent to assignments.
  2197. @ignore todo
  2198. Rozas: equal time for opposition semantics?
  2199. @end ignore
  2200. @node Internal definitions, , Top level definitions, Definitions
  2201. @subsection Internal definitions
  2202. Definitions may occur at the
  2203. beginning of a @r{<body>} (that is, the body of a @code{lambda},
  2204. @vindex @w{lambda}
  2205. @code{let}, @code{let*}, @code{letrec}, @code{let-syntax}, or @code{letrec-syntax}
  2206. @vindex @w{letrec-syntax}
  2207. @vindex @w{let-syntax}
  2208. @vindex @w{letrec}
  2209. @vindex @w{let*}
  2210. @vindex @w{let}
  2211. expression or that of a definition of an appropriate form).
  2212. Such definitions are known as @emph{internal definitions} as opposed to the top level definitions described above.
  2213. @cindex @w{internal definition}
  2214. The variable defined by an internal definition is local to the
  2215. @r{<body>}. That is, @r{<variable>} is bound rather than assigned,
  2216. and the region of the binding is the entire @r{<body>}. For example,
  2217. @example
  2218. (let ((x 5))
  2219. (define foo (lambda (y) (bar x y)))
  2220. (define bar (lambda (a b) (+ (* a b) a)))
  2221. (foo (+ x 3))) ==> 45
  2222. @end example
  2223. A @r{<body>} containing internal definitions can always be converted
  2224. into a completely equivalent @samp{letrec} expression. For example, the
  2225. @samp{let} expression in the above example is equivalent to
  2226. @example
  2227. (let ((x 5))
  2228. (letrec ((foo (lambda (y) (bar x y)))
  2229. (bar (lambda (a b) (+ (* a b) a))))
  2230. (foo (+ x 3))))
  2231. @end example
  2232. Just as for the equivalent @samp{letrec} expression, it must be
  2233. possible to evaluate each @r{<expression>} of every internal
  2234. definition in a @r{<body>} without assigning or referring to
  2235. the value of any @r{<variable>} being defined.
  2236. Wherever an internal definition may occur
  2237. @t{(begin @r{<definition1>} @dots{},)}
  2238. is equivalent to the sequence of definitions
  2239. that form the body of the @code{begin}.
  2240. @vindex @w{begin}
  2241. @node Syntax definitions, , Definitions, Program structure
  2242. @section Syntax definitions
  2243. Syntax definitions are valid only at the top level of a @r{<program>}.
  2244. @cindex @w{syntax definition}
  2245. They have the following form:
  2246. @cindex @w{define-syntax}
  2247. @t{(define-syntax @r{<keyword>} @r{<transformer spec>})}
  2248. @r{<Keyword>} is an identifier, and
  2249. the @r{<transformer spec>} should be an instance of @code{syntax-rules}.
  2250. @vindex @w{syntax-rules}
  2251. The top-level syntactic environment is extended by binding the
  2252. @r{<keyword>} to the specified transformer.
  2253. There is no @samp{define-syntax} analogue of internal definitions.
  2254. @c [Rationale flushed because it may or may not be true and isn't the
  2255. @c real rationale anyway. -RK]
  2256. @c \begin{rationale}
  2257. @c As discussed below, the syntax and scope rules for syntax definitions
  2258. @c can give rise to syntactic ambiguities when syntactic keywords are
  2259. @c shadowed.
  2260. @c Further ambiguities would arise if {\cf define-syntax}
  2261. @c were permitted at the beginning of a \meta{body}, with scope
  2262. @c rules analogous to those for internal definitions.
  2263. @c \end{rationale}
  2264. @c It is an error for a program to contain more than one top-level
  2265. @c \meta{definition} or \meta{syntax definition} of any identifier.
  2266. @c [I flushed this because it isn't an error for a program to
  2267. @c contain more than one top-level definition of an identifier,
  2268. @c and I didn't want to introduce any gratuitous incompatibilities
  2269. @c with the existing Scheme language. -- Will]
  2270. Although macros may expand into definitions and syntax definitions in
  2271. any context that permits them, it is an error for a definition or syntax
  2272. definition to shadow a syntactic keyword whose meaning is needed to
  2273. determine whether some form in the group of forms that contains the
  2274. shadowing definition is in fact a definition, or, for internal definitions,
  2275. is needed to determine the boundary between the group and the expressions
  2276. that follow the group. For example, the following are errors:
  2277. @example
  2278. (define define 3)
  2279. (begin (define begin list))
  2280. (let-syntax
  2281. ((foo (syntax-rules ()
  2282. ((foo (proc args ...) body ...)
  2283. (define proc
  2284. (lambda (args ...)
  2285. body ...))))))
  2286. (let ((x 3))
  2287. (foo (plus x y) (+ x y))
  2288. (define foo x)
  2289. (plus foo x)))
  2290. @end example
  2291. @c @include{procs}
  2292. @c Initial environment
  2293. @c \vfill\eject
  2294. @node Standard procedures, Formal syntax and semantics, Program structure, top
  2295. @chapter Standard procedures
  2296. @menu
  2297. * Equivalence predicates::
  2298. * Numbers::
  2299. * Other data types::
  2300. * Control features::
  2301. * Eval::
  2302. * Input and output::
  2303. @end menu
  2304. @cindex @w{initial environment}
  2305. @cindex @w{top level environment}
  2306. @cindex @w{library procedure}
  2307. This chapter describes Scheme's built-in procedures. The initial (or
  2308. ``top level'') Scheme environment starts out with a number of variables
  2309. bound to locations containing useful values, most of which are primitive
  2310. procedures that manipulate data. For example, the variable @samp{abs} is
  2311. bound to (a location initially containing) a procedure of one argument
  2312. that computes the absolute value of a number, and the variable @samp{+}
  2313. is bound to a procedure that computes sums. Built-in procedures that
  2314. can easily be written in terms of other built-in procedures are identified as
  2315. ``library procedures''.
  2316. A program may use a top-level definition to bind any variable. It may
  2317. subsequently alter any such binding by an assignment (see @pxref{Assignments}).
  2318. These operations do not modify the behavior of Scheme's built-in
  2319. procedures. Altering any top-level binding that has not been introduced by a
  2320. definition has an unspecified effect on the behavior of the built-in procedures.
  2321. @node Equivalence predicates, Numbers, Standard procedures, Standard procedures
  2322. @section Equivalence predicates
  2323. A @dfn{predicate} is a procedure that always returns a boolean
  2324. @cindex @w{predicate}
  2325. value (@t{#t} or @t{#f}). An @dfn{equivalence predicate} is
  2326. @cindex @w{equivalence predicate}
  2327. the computational analogue of a mathematical equivalence relation (it is
  2328. symmetric, reflexive, and transitive). Of the equivalence predicates
  2329. described in this section, @samp{eq?} is the finest or most
  2330. discriminating, and @samp{equal?} is the coarsest. @samp{Eqv?} is
  2331. slightly less discriminating than @samp{eq?}.
  2332. @ignore todo
  2333. Pitman doesn't like
  2334. this paragraph. Lift the discussion from the Maclisp manual. Explain
  2335. why there's more than one predicate.
  2336. @end ignore
  2337. @deffn {procedure} eqv? obj1 obj2
  2338. The @samp{eqv?} procedure defines a useful equivalence relation on objects.
  2339. Briefly, it returns @t{#t} if @var{obj1} and @var{obj2} should
  2340. normally be regarded as the same object. This relation is left slightly
  2341. open to interpretation, but the following partial specification of
  2342. @samp{eqv?} holds for all implementations of Scheme.
  2343. The @samp{eqv?} procedure returns @t{#t} if:
  2344. @itemize @bullet
  2345. @item
  2346. @var{obj1} and @var{obj2} are both @t{#t} or both @t{#f}.
  2347. @item
  2348. @var{obj1} and @var{obj2} are both symbols and
  2349. @format
  2350. @t{(string=? (symbol->string obj1)
  2351. (symbol->string obj2))
  2352. ==> #t
  2353. }
  2354. @end format
  2355. @quotation
  2356. @emph{Note:}
  2357. This assumes that neither @var{obj1} nor @var{obj2} is an ``uninterned
  2358. symbol'' as alluded to in section @ref{Symbols}. This report does
  2359. not presume to specify the behavior of @samp{eqv?} on implementation-dependent
  2360. extensions.
  2361. @end quotation
  2362. @item
  2363. @var{obj1} and @var{obj2} are both numbers, are numerically
  2364. equal (see @samp{=}, section @pxref{Numbers}), and are either both
  2365. exact or both inexact.
  2366. @item
  2367. @var{obj1} and @var{obj2} are both characters and are the same
  2368. character according to the @samp{char=?} procedure
  2369. (section @pxref{Characters}).
  2370. @item
  2371. both @var{obj1} and @var{obj2} are the empty list.
  2372. @item
  2373. @var{obj1} and @var{obj2} are pairs, vectors, or strings that denote the
  2374. same locations in the store (section @pxref{Storage model}).
  2375. @item
  2376. @var{obj1} and @var{obj2} are procedures whose location tags are
  2377. equal (section @pxref{Procedures}).
  2378. @end itemize
  2379. @cindex @w{inexact}
  2380. @cindex @w{exact}
  2381. The @samp{eqv?} procedure returns @t{#f} if:
  2382. @itemize @bullet
  2383. @item
  2384. @var{obj1} and @var{obj2} are of different types
  2385. (section @pxref{Disjointness of types}).
  2386. @item
  2387. one of @var{obj1} and @var{obj2} is @t{#t} but the other is
  2388. @t{#f}.
  2389. @item
  2390. @var{obj1} and @var{obj2} are symbols but
  2391. @format
  2392. @t{(string=? (symbol->string @var{obj1})
  2393. (symbol->string @var{obj2}))
  2394. ==> #f
  2395. }
  2396. @end format
  2397. @item
  2398. one of @var{obj1} and @var{obj2} is an exact number but the other
  2399. is an inexact number.
  2400. @item
  2401. @var{obj1} and @var{obj2} are numbers for which the @samp{=}
  2402. procedure returns @t{#f}.
  2403. @item
  2404. @var{obj1} and @var{obj2} are characters for which the @samp{char=?}
  2405. procedure returns @t{#f}.
  2406. @item
  2407. one of @var{obj1} and @var{obj2} is the empty list but the other
  2408. is not.
  2409. @item
  2410. @var{obj1} and @var{obj2} are pairs, vectors, or strings that denote
  2411. distinct locations.
  2412. @item
  2413. @var{obj1} and @var{obj2} are procedures that would behave differently
  2414. (return different value(s) or have different side effects) for some arguments.
  2415. @end itemize
  2416. @format
  2417. @t{(eqv? 'a 'a) ==> #t
  2418. (eqv? 'a 'b) ==> #f
  2419. (eqv? 2 2) ==> #t
  2420. (eqv? '() '()) ==> #t
  2421. (eqv? 100000000 100000000) ==> #t
  2422. (eqv? (cons 1 2) (cons 1 2)) ==> #f
  2423. (eqv? (lambda () 1)
  2424. (lambda () 2)) ==> #f
  2425. (eqv? #f 'nil) ==> #f
  2426. (let ((p (lambda (x) x)))
  2427. (eqv? p p)) ==> #t
  2428. }
  2429. @end format
  2430. The following examples illustrate cases in which the above rules do
  2431. not fully specify the behavior of @samp{eqv?}. All that can be said
  2432. about such cases is that the value returned by @samp{eqv?} must be a
  2433. boolean.
  2434. @format
  2435. @t{(eqv? "" "") ==> @emph{unspecified}
  2436. (eqv? '#() '#()) ==> @emph{unspecified}
  2437. (eqv? (lambda (x) x)
  2438. (lambda (x) x)) ==> @emph{unspecified}
  2439. (eqv? (lambda (x) x)
  2440. (lambda (y) y)) ==> @emph{unspecified}
  2441. }
  2442. @end format
  2443. The next set of examples shows the use of @samp{eqv?} with procedures
  2444. that have local state. @samp{Gen-counter} must return a distinct
  2445. procedure every time, since each procedure has its own internal counter.
  2446. @samp{Gen-loser}, however, returns equivalent procedures each time, since
  2447. the local state does not affect the value or side effects of the
  2448. procedures.
  2449. @format
  2450. @t{(define gen-counter
  2451. (lambda ()
  2452. (let ((n 0))
  2453. (lambda () (set! n (+ n 1)) n))))
  2454. (let ((g (gen-counter)))
  2455. (eqv? g g)) ==> #t
  2456. (eqv? (gen-counter) (gen-counter))
  2457. ==> #f
  2458. (define gen-loser
  2459. (lambda ()
  2460. (let ((n 0))
  2461. (lambda () (set! n (+ n 1)) 27))))
  2462. (let ((g (gen-loser)))
  2463. (eqv? g g)) ==> #t
  2464. (eqv? (gen-loser) (gen-loser))
  2465. ==> @emph{unspecified}
  2466. (letrec ((f (lambda () (if (eqv? f g) 'both 'f)))
  2467. (g (lambda () (if (eqv? f g) 'both 'g))))
  2468. (eqv? f g))
  2469. ==> @emph{unspecified}
  2470. (letrec ((f (lambda () (if (eqv? f g) 'f 'both)))
  2471. (g (lambda () (if (eqv? f g) 'g 'both))))
  2472. (eqv? f g))
  2473. ==> #f
  2474. }
  2475. @end format
  2476. @c Objects of distinct types must never be regarded as the same object,
  2477. @c except that \schfalse{} and the empty list\index{empty list} are permitted to
  2478. @c be identical.
  2479. @c \begin{scheme}
  2480. @c (eqv? '() \schfalse) \ev \unspecified%
  2481. @c \end{scheme}
  2482. Since it is an error to modify constant objects (those returned by
  2483. literal expressions), implementations are permitted, though not
  2484. required, to share structure between constants where appropriate. Thus
  2485. the value of @samp{eqv?} on constants is sometimes
  2486. implementation-dependent.
  2487. @format
  2488. @t{(eqv? '(a) '(a)) ==> @emph{unspecified}
  2489. (eqv? "a" "a") ==> @emph{unspecified}
  2490. (eqv? '(b) (cdr '(a b))) ==> @emph{unspecified}
  2491. (let ((x '(a)))
  2492. (eqv? x x)) ==> #t
  2493. }
  2494. @end format
  2495. @quotation
  2496. @emph{Rationale:}
  2497. The above definition of @samp{eqv?} allows implementations latitude in
  2498. their treatment of procedures and literals: implementations are free
  2499. either to detect or to fail to detect that two procedures or two literals
  2500. are equivalent to each other, and can decide whether or not to
  2501. merge representations of equivalent objects by using the same pointer or
  2502. bit pattern to represent both.
  2503. @end quotation
  2504. @end deffn
  2505. @deffn {procedure} eq? obj1 obj2
  2506. @samp{Eq?} is similar to @samp{eqv?} except that in some cases it is
  2507. capable of discerning distinctions finer than those detectable by
  2508. @samp{eqv?}.
  2509. @samp{Eq?} and @samp{eqv?} are guaranteed to have the same
  2510. behavior on symbols, booleans, the empty list, pairs, procedures,
  2511. and non-empty
  2512. strings and vectors. @samp{Eq?}'s behavior on numbers and characters is
  2513. implementation-dependent, but it will always return either true or
  2514. false, and will return true only when @samp{eqv?} would also return
  2515. true. @samp{Eq?} may also behave differently from @samp{eqv?} on empty
  2516. vectors and empty strings.
  2517. @format
  2518. @t{(eq? 'a 'a) ==> #t
  2519. (eq? '(a) '(a)) ==> @emph{unspecified}
  2520. (eq? (list 'a) (list 'a)) ==> #f
  2521. (eq? "a" "a") ==> @emph{unspecified}
  2522. (eq? "" "") ==> @emph{unspecified}
  2523. (eq? '() '()) ==> #t
  2524. (eq? 2 2) ==> @emph{unspecified}
  2525. (eq? #\A #\A) ==> @emph{unspecified}
  2526. (eq? car car) ==> #t
  2527. (let ((n (+ 2 3)))
  2528. (eq? n n)) ==> @emph{unspecified}
  2529. (let ((x '(a)))
  2530. (eq? x x)) ==> #t
  2531. (let ((x '#()))
  2532. (eq? x x)) ==> #t
  2533. (let ((p (lambda (x) x)))
  2534. (eq? p p)) ==> #t
  2535. }
  2536. @end format
  2537. @ignore todo
  2538. Needs to be explained better above. How can this be made to be
  2539. not confusing? A table maybe?
  2540. @end ignore
  2541. @quotation
  2542. @emph{Rationale:} It will usually be possible to implement @samp{eq?} much
  2543. more efficiently than @samp{eqv?}, for example, as a simple pointer
  2544. comparison instead of as some more complicated operation. One reason is
  2545. that it may not be possible to compute @samp{eqv?} of two numbers in
  2546. constant time, whereas @samp{eq?} implemented as pointer comparison will
  2547. always finish in constant time. @samp{Eq?} may be used like @samp{eqv?}
  2548. in applications using procedures to implement objects with state since
  2549. it obeys the same constraints as @samp{eqv?}.
  2550. @end quotation
  2551. @end deffn
  2552. @deffn {library procedure} equal? obj1 obj2
  2553. @samp{Equal?} recursively compares the contents of pairs, vectors, and
  2554. strings, applying @samp{eqv?} on other objects such as numbers and symbols.
  2555. A rule of thumb is that objects are generally @samp{equal?} if they print
  2556. the same. @samp{Equal?} may fail to terminate if its arguments are
  2557. circular data structures.
  2558. @format
  2559. @t{(equal? 'a 'a) ==> #t
  2560. (equal? '(a) '(a)) ==> #t
  2561. (equal? '(a (b) c)
  2562. '(a (b) c)) ==> #t
  2563. (equal? "abc" "abc") ==> #t
  2564. (equal? 2 2) ==> #t
  2565. (equal? (make-vector 5 'a)
  2566. (make-vector 5 'a)) ==> #t
  2567. (equal? (lambda (x) x)
  2568. (lambda (y) y)) ==> @emph{unspecified}
  2569. }
  2570. @end format
  2571. @end deffn
  2572. @node Numbers, Other data types, Equivalence predicates, Standard procedures
  2573. @section Numbers
  2574. @menu
  2575. * Numerical types::
  2576. * Exactness::
  2577. * Implementation restrictions::
  2578. * Syntax of numerical constants::
  2579. * Numerical operations::
  2580. * Numerical input and output::
  2581. @end menu
  2582. @cindex @w{number}
  2583. @c %R4%% The excessive use of the code font in this section was
  2584. @c confusing, somewhat obnoxious, and inconsistent with the rest
  2585. @c of the report and with parts of the section itself. I added
  2586. @c a \tupe no-op, and changed most old uses of \type to \tupe,
  2587. @c to make it easier to change the fonts back if people object
  2588. @c to the change.
  2589. @c \newcommand{\type}[1]{{\it#1}}
  2590. @c \newcommand{\tupe}[1]{{#1}}
  2591. Numerical computation has traditionally been neglected by the Lisp
  2592. community. Until Common Lisp there was no carefully thought out
  2593. strategy for organizing numerical computation, and with the exception of
  2594. the MacLisp system [Pitman83] little effort was made to
  2595. execute numerical code efficiently. This report recognizes the excellent work
  2596. of the Common Lisp committee and accepts many of their recommendations.
  2597. In some ways this report simplifies and generalizes their proposals in a manner
  2598. consistent with the purposes of Scheme.
  2599. It is important to distinguish between the mathematical numbers, the
  2600. Scheme numbers that attempt to model them, the machine representations
  2601. used to implement the Scheme numbers, and notations used to write numbers.
  2602. This report uses the types @i{number}, @i{complex}, @i{real},
  2603. @i{rational}, and @i{integer} to refer to both mathematical numbers
  2604. and Scheme numbers. Machine representations such as fixed point and
  2605. floating point are referred to by names such as @i{fixnum} and
  2606. @i{flonum}.
  2607. @c %R4%% I did some reorganizing here to move the discussion of mathematical
  2608. @c numbers before the discussion of the Scheme numbers, hoping that this
  2609. @c would help to motivate the discussion of representation independence.
  2610. @node Numerical types, Exactness, Numbers, Numbers
  2611. @subsection Numerical types
  2612. @cindex @w{numerical types}
  2613. @c %R4%% A Scheme system provides data of type \type{number}, which is the most
  2614. @c general numerical type supported by that system.
  2615. @c \type{Number} is
  2616. @c likely to be a complicated union type implemented in terms of
  2617. @c \type{fixnum}s, \type{bignum}s, \type{flonum}s, and so forth, but this
  2618. @c should not be apparent to a naive user. What the user should see is
  2619. @c that the usual operations on numbers produce the mathematically
  2620. @c expected results, within the limits of the implementation.
  2621. @c %R4%% I rewrote the following paragraph to make the various levels of
  2622. @c the tower into subsets of each other, instead of relating them by
  2623. @c injections. I think the injections tended to put people in the frame
  2624. @c of mind of thinking about coercions between non-overlapping numeric
  2625. @c types in mainstream programming languages.
  2626. Mathematically, numbers may be arranged into a tower of subtypes
  2627. @c %R4%% with injections relating adjacent levels of the tower:
  2628. in which each level is a subset of the level above it:
  2629. @format
  2630. @r{number}
  2631. @r{complex}
  2632. @r{real}
  2633. @r{rational}
  2634. @r{integer}
  2635. @end format
  2636. For example, 3 is an integer. Therefore 3 is also a rational,
  2637. a real, and a complex. The same is true of the Scheme numbers
  2638. that model 3. For Scheme numbers, these types are defined by the
  2639. predicates @code{number?}, @code{complex?}, @code{real?}, @code{rational?},
  2640. @vindex @w{rational?}
  2641. @vindex @w{real?}
  2642. @vindex @w{complex?}
  2643. @vindex @w{number?}
  2644. and @code{integer?}.
  2645. @vindex @w{integer?}
  2646. There is no simple relationship between a number's type and its
  2647. representation inside a computer. Although most implementations of
  2648. Scheme will offer at least two different representations of 3, these
  2649. different representations denote the same integer.
  2650. @c %R4%% I moved "Implementations of Scheme are not required to implement
  2651. @c the whole tower..." to the subsection on implementation restrictions.
  2652. Scheme's numerical operations treat numbers as abstract data, as
  2653. independent of their representation as possible. Although an implementation
  2654. of Scheme may use fixnum, flonum, and perhaps other representations for
  2655. numbers, this should not be apparent to a casual programmer writing
  2656. simple programs.
  2657. It is necessary, however, to distinguish between numbers that are
  2658. represented exactly and those that may not be. For example, indexes
  2659. into data structures must be known exactly, as must some polynomial
  2660. coefficients in a symbolic algebra system. On the other hand, the
  2661. results of measurements are inherently inexact, and irrational numbers
  2662. may be approximated by rational and therefore inexact approximations.
  2663. In order to catch uses of inexact numbers where exact numbers are
  2664. required, Scheme explicitly distinguishes exact from inexact numbers.
  2665. This distinction is orthogonal to the dimension of type.
  2666. @node Exactness, Implementation restrictions, Numerical types, Numbers
  2667. @subsection Exactness
  2668. @c %R4%% I tried to direct the following paragraph away from philosophizing
  2669. @c about the exactness of mathematical numbers, and toward philosophizing
  2670. @c about the exactness of Scheme numbers.
  2671. @cindex @w{exactness}
  2672. Scheme numbers are either @i{exact} or @i{inexact}. A number is
  2673. @r{exact} if it was written as an exact constant or was derived from
  2674. @r{exact} numbers using only @r{exact} operations. A number is
  2675. @r{inexact} if it was written as an inexact constant,
  2676. @c %R4%% models a quantity (e.g., a measurement) known only approximately,
  2677. if it was
  2678. derived using @r{inexact} ingredients, or if it was derived using
  2679. @r{inexact} operations. Thus @r{inexact}ness is a contagious
  2680. property of a number.
  2681. @c %R4%% The rest of this paragraph (from R3RS) has been dropped.
  2682. If two implementations produce @r{exact} results for a
  2683. computation that did not involve @r{inexact} intermediate results,
  2684. the two ultimate results will be mathematically equivalent. This is
  2685. generally not true of computations involving @r{inexact} numbers
  2686. since approximate methods such as floating point arithmetic may be used,
  2687. but it is the duty of each implementation to make the result as close as
  2688. practical to the mathematically ideal result.
  2689. Rational operations such as @samp{+} should always produce
  2690. @r{exact} results when given @r{exact} arguments.
  2691. @c %R4%%If an implementation is
  2692. @c unable to represent an \tupe{exact} result (for example, if it does not
  2693. @c support infinite precision integers and rationals)
  2694. If the operation is unable to produce an @r{exact} result,
  2695. then it may either report the violation of an implementation restriction
  2696. or it may silently coerce its
  2697. result to an @r{inexact} value.
  2698. @c %R4%%Such a coercion may cause an error later.
  2699. See section @ref{Implementation restrictions}.
  2700. With the exception of @code{inexact->exact}, the operations described in
  2701. @vindex @w{inexact->exact}
  2702. this section must generally return inexact results when given any inexact
  2703. arguments. An operation may, however, return an @r{exact} result if it can
  2704. prove that the value of the result is unaffected by the inexactness of its
  2705. arguments. For example, multiplication of any number by an @r{exact} zero
  2706. may produce an @r{exact} zero result, even if the other argument is
  2707. @r{inexact}.
  2708. @node Implementation restrictions, Syntax of numerical constants, Exactness, Numbers
  2709. @subsection Implementation restrictions
  2710. @cindex @w{implementation restriction}
  2711. Implementations of Scheme are not required to implement the whole
  2712. tower of subtypes given in section @ref{Numerical types},
  2713. but they must implement a coherent subset consistent with both the
  2714. purposes of the implementation and the spirit of the Scheme language.
  2715. For example, an implementation in which all numbers are @r{real}
  2716. may still be quite useful.
  2717. Implementations may also support only a limited range of numbers of
  2718. any type, subject to the requirements of this section. The supported
  2719. range for @r{exact} numbers of any type may be different from the
  2720. supported range for @r{inexact} numbers of that type. For example,
  2721. an implementation that uses flonums to represent all its
  2722. @r{inexact} @r{real} numbers may
  2723. support a practically unbounded range of @r{exact} @r{integer}s
  2724. and @r{rational}s
  2725. while limiting the range of @r{inexact} @r{real}s (and therefore
  2726. the range of @r{inexact} @r{integer}s and @r{rational}s)
  2727. to the dynamic range of the flonum format.
  2728. Furthermore
  2729. the gaps between the representable @r{inexact} @r{integer}s and
  2730. @r{rational}s are
  2731. likely to be very large in such an implementation as the limits of this
  2732. range are approached.
  2733. An implementation of Scheme must support exact integers
  2734. throughout the range of numbers that may be used for indexes of
  2735. lists, vectors, and strings or that may result from computing the length of a
  2736. list, vector, or string. The @code{length}, @code{vector-length},
  2737. @vindex @w{vector-length}
  2738. @vindex @w{length}
  2739. and @code{string-length} procedures must return an exact
  2740. @vindex @w{string-length}
  2741. integer, and it is an error to use anything but an exact integer as an
  2742. index. Furthermore any integer constant within the index range, if
  2743. expressed by an exact integer syntax, will indeed be read as an exact
  2744. integer, regardless of any implementation restrictions that may apply
  2745. outside this range. Finally, the procedures listed below will always
  2746. return an exact integer result provided all their arguments are exact integers
  2747. and the mathematically expected result is representable as an exact integer
  2748. within the implementation:
  2749. @example
  2750. + - *
  2751. quotient remainder modulo
  2752. max min abs
  2753. numerator denominator gcd
  2754. lcm floor ceiling
  2755. truncate round rationalize
  2756. expt
  2757. @end example
  2758. Implementations are encouraged, but not required, to support
  2759. @r{exact} @r{integer}s and @r{exact} @r{rational}s of
  2760. practically unlimited size and precision, and to implement the
  2761. above procedures and the @samp{/} procedure in
  2762. such a way that they always return @r{exact} results when given @r{exact}
  2763. arguments. If one of these procedures is unable to deliver an @r{exact}
  2764. result when given @r{exact} arguments, then it may either report a
  2765. violation of an
  2766. implementation restriction or it may silently coerce its result to an
  2767. @r{inexact} number. Such a coercion may cause an error later.
  2768. @c %R4%% I moved this stuff here.
  2769. @c It seems to me that the only thing that this requires is that
  2770. @c implementations that support inexact numbers have to have both
  2771. @c exact and inexact representations for the integers 0 through 15.
  2772. @c If that's what it's saying, I'd rather say it that way.
  2773. @c On the other hand, letting the limit be as small as 15 sounds a
  2774. @c tad silly, though I think I understand how that number was arrived at.
  2775. @c (Or is 35 the number?)
  2776. @c Implementations are encouraged, but not required, to support \tupe{inexact}
  2777. @c numbers. For any implementation that supports \tupe{inexact} numbers,
  2778. @c there is a subset of the integers for which there are both \tupe{exact} and
  2779. @c \tupe{inexact} representations. This subset must include all non-negative
  2780. @c integers up to some limit specified by the implementation. This limit
  2781. @c must be 16 or greater. The
  2782. @c \ide{exact\coerce{}inexact} and \ide{inexact\coerce{}exact}
  2783. @c procedures implement the natural one-to-one correspondence between
  2784. @c the \tupe{inexact} and \tupe{exact} integers within this range.
  2785. An implementation may use floating point and other approximate
  2786. representation strategies for @r{inexact} numbers.
  2787. @c %R4%% The following sentence seemed a bit condescending as well as
  2788. @c awkward. It didn't seem to be very enforceable, so I flushed it.
  2789. @c This is not to
  2790. @c say that implementors need not use the best known algorithms for
  2791. @c \tupe{inexact} computations---only that approximate methods of high
  2792. @c quality are allowed.
  2793. This report recommends, but does not require, that the IEEE 32-bit
  2794. and 64-bit floating point standards be followed by implementations that use
  2795. flonum representations, and that implementations using
  2796. other representations should match or exceed the precision achievable
  2797. using these floating point standards [IEEE].
  2798. In particular, implementations that use flonum representations
  2799. must follow these rules: A @r{flonum} result
  2800. must be represented with at least as much precision as is used to express any of
  2801. the inexact arguments to that operation. It is desirable (but not required) for
  2802. potentially inexact operations such as @samp{sqrt}, when applied to @r{exact}
  2803. arguments, to produce @r{exact} answers whenever possible (for example the
  2804. square root of an @r{exact} 4 ought to be an @r{exact} 2).
  2805. If, however, an
  2806. @r{exact} number is operated upon so as to produce an @r{inexact} result
  2807. (as by @samp{sqrt}), and if the result is represented as a @r{flonum}, then
  2808. the most precise @r{flonum} format available must be used; but if the result
  2809. is represented in some other way then the representation must have at least as
  2810. much precision as the most precise @r{flonum} format available.
  2811. Although Scheme allows a variety of written
  2812. @c %R4%% representations of
  2813. notations for
  2814. numbers, any particular implementation may support only some of them.
  2815. @c %R4%%
  2816. For example, an implementation in which all numbers are @r{real}
  2817. need not support the rectangular and polar notations for complex
  2818. numbers. If an implementation encounters an @r{exact} numerical constant that
  2819. it cannot represent as an @r{exact} number, then it may either report a
  2820. violation of an implementation restriction or it may silently represent the
  2821. constant by an @r{inexact} number.
  2822. @node Syntax of numerical constants, Numerical operations, Implementation restrictions, Numbers
  2823. @subsection Syntax of numerical constants
  2824. @c @@@@LOSE@@@@
  2825. @c %R4%% I removed the following paragraph in an attempt to tighten up
  2826. @c this subsection. Except for its first sentence, which I moved to
  2827. @c the subsection on implementation restrictions, I think its content
  2828. @c is implied by the rest of the section.
  2829. @c Although Scheme allows a variety of written representations of numbers,
  2830. @c any particular implementation may support only some of them.
  2831. @c These syntaxes are intended to be purely notational; any kind of number
  2832. @c may be written in any form that the user deems convenient. Of course,
  2833. @c writing 1/7 as a limited-precision decimal fraction will not express the
  2834. @c number exactly, but this approximate form of expression may be just what
  2835. @c the user wants to see.
  2836. The syntax of the written representations for numbers is described formally in
  2837. section @ref{Lexical structure}. Note that case is not significant in numerical
  2838. constants.
  2839. @c %R4%% See section~\ref{numberformats} for many examples.
  2840. A number may be written in binary, octal, decimal, or
  2841. hexadecimal by the use of a radix prefix. The radix prefixes are @samp{#b} (binary), @samp{#o} (octal), @samp{#d} (decimal), and @samp{#x} (hexadecimal). With
  2842. @vindex #x
  2843. @vindex #d
  2844. @vindex #o
  2845. @vindex #b
  2846. no radix prefix, a number is assumed to be expressed in decimal.
  2847. A
  2848. @c %R4%%
  2849. @c simple
  2850. numerical constant may be specified to be either @r{exact} or
  2851. @r{inexact} by a prefix. The prefixes are @samp{#e}
  2852. @vindex #e
  2853. for @r{exact}, and @samp{#i} for @r{inexact}. An exactness
  2854. @vindex #i
  2855. prefix may appear before or after any radix prefix that is used. If
  2856. the written representation of a number has no exactness prefix, the
  2857. constant may be either @r{inexact} or @r{exact}. It is
  2858. @r{inexact} if it contains a decimal point, an
  2859. exponent, or a ``#'' character in the place of a digit,
  2860. otherwise it is @r{exact}.
  2861. @c %R4%% With our new syntax, the following sentence is redundant:
  2862. @c The written representation of a
  2863. @c compound number, such as a ratio or a complex, is exact if and only if
  2864. @c all of its constituents are exact.
  2865. In systems with @r{inexact} numbers
  2866. of varying precisions it may be useful to specify
  2867. the precision of a constant. For this purpose, numerical constants
  2868. may be written with an exponent marker that indicates the
  2869. desired precision of the @r{inexact}
  2870. representation. The letters @samp{s}, @samp{f},
  2871. @samp{d}, and @samp{l} specify the use of @var{short}, @var{single},
  2872. @var{double}, and @var{long} precision, respectively. (When fewer
  2873. than four internal
  2874. @c %R4%%\tupe{flonum}
  2875. @r{inexact}
  2876. representations exist, the four size
  2877. specifications are mapped onto those available. For example, an
  2878. implementation with two internal representations may map short and
  2879. single together and long and double together.) In addition, the
  2880. exponent marker @samp{e} specifies the default precision for the
  2881. implementation. The default precision has at least as much precision
  2882. as @var{double}, but
  2883. implementations may wish to allow this default to be set by the user.
  2884. @example
  2885. 3.14159265358979F0
  2886. @r{Round to single ---} 3.141593
  2887. 0.6L0
  2888. @r{Extend to long ---} .600000000000000
  2889. @end example
  2890. @node Numerical operations, Numerical input and output, Syntax of numerical constants, Numbers
  2891. @subsection Numerical operations
  2892. The reader is referred to section @ref{Entry format} for a summary
  2893. of the naming conventions used to specify restrictions on the types of
  2894. arguments to numerical routines.
  2895. @c %R4%% The following sentence has already been said twice, and the
  2896. @c term "exactness-preserving" is no longer defined by the Report.
  2897. @c Remember that
  2898. @c an exactness-preserving operation may coerce its result to inexact if the
  2899. @c implementation is unable to represent it exactly.
  2900. The examples used in this section assume that any numerical constant written
  2901. using an @r{exact} notation is indeed represented as an @r{exact}
  2902. number. Some examples also assume that certain numerical constants written
  2903. using an @r{inexact} notation can be represented without loss of
  2904. accuracy; the @r{inexact} constants were chosen so that this is
  2905. likely to be true in implementations that use flonums to represent
  2906. inexact numbers.
  2907. @ignore todo
  2908. Scheme provides the usual set of operations for manipulating
  2909. numbers, etc.
  2910. @end ignore
  2911. @deffn {procedure} number? obj
  2912. @deffnx {procedure} complex? obj
  2913. @deffnx {procedure} real? obj
  2914. @deffnx {procedure} rational? obj
  2915. @deffnx {procedure} integer? obj
  2916. These numerical type predicates can be applied to any kind of
  2917. argument, including non-numbers. They return @t{#t} if the object is
  2918. of the named type, and otherwise they return @t{#f}.
  2919. In general, if a type predicate is true of a number then all higher
  2920. type predicates are also true of that number. Consequently, if a type
  2921. predicate is false of a number, then all lower type predicates are
  2922. also false of that number.
  2923. @c %R4%% The new section on implementation restrictions subsumes:
  2924. @c Not every system
  2925. @c supports all of these types; for example, it is entirely possible to have a
  2926. @c Scheme system that has only \tupe{integer}s. Nonetheless every implementation
  2927. @c of Scheme must have all of these predicates.
  2928. If @var{z} is an inexact complex number, then @samp{(real? @var{z})} is true if
  2929. and only if @samp{(zero? (imag-part @var{z}))} is true. If @var{x} is an inexact
  2930. real number, then @samp{(integer? @var{x})} is true if and only if
  2931. @samp{(= @var{x} (round @var{x}))}.
  2932. @format
  2933. @t{(complex? 3+4i) ==> #t
  2934. (complex? 3) ==> #t
  2935. (real? 3) ==> #t
  2936. (real? -2.5+0.0i) ==> #t
  2937. (real? #e1e10) ==> #t
  2938. (rational? 6/10) ==> #t
  2939. (rational? 6/3) ==> #t
  2940. (integer? 3+0i) ==> #t
  2941. (integer? 3.0) ==> #t
  2942. (integer? 8/4) ==> #t
  2943. }
  2944. @end format
  2945. @quotation
  2946. @emph{Note:}
  2947. The behavior of these type predicates on @r{inexact} numbers
  2948. is unreliable, since any inaccuracy may affect the result.
  2949. @end quotation
  2950. @quotation
  2951. @emph{Note:}
  2952. In many implementations the @code{rational?} procedure will be the same
  2953. @vindex @w{rational?}
  2954. as @code{real?}, and the @code{complex?} procedure will be the same as
  2955. @vindex @w{complex?}
  2956. @vindex @w{real?}
  2957. @code{number?}, but unusual implementations may be able to represent
  2958. @vindex @w{number?}
  2959. some irrational numbers exactly or may extend the number system to
  2960. support some kind of non-complex numbers.
  2961. @end quotation
  2962. @end deffn
  2963. @deffn {procedure} exact? @var{z}
  2964. @deffnx {procedure} inexact? @var{z}
  2965. These numerical predicates provide tests for the exactness of a
  2966. quantity. For any Scheme number, precisely one of these predicates
  2967. is true.
  2968. @end deffn
  2969. @deffn {procedure} = z1 z2 z3 @dots{},
  2970. @deffnx {procedure} < x1 x2 x3 @dots{},
  2971. @deffnx {procedure} > x1 x2 x3 @dots{},
  2972. @deffnx {procedure} <= x1 x2 x3 @dots{},
  2973. @deffnx {procedure} >= x1 x2 x3 @dots{},
  2974. @c - Some implementations allow these procedures to take many arguments, to
  2975. @c - facilitate range checks.
  2976. These procedures return @t{#t} if their arguments are (respectively):
  2977. equal, monotonically increasing, monotonically decreasing,
  2978. monotonically nondecreasing, or monotonically nonincreasing.
  2979. These predicates are required to be transitive.
  2980. @quotation
  2981. @emph{Note:}
  2982. The traditional implementations of these predicates in Lisp-like
  2983. languages are not transitive.
  2984. @end quotation
  2985. @quotation
  2986. @emph{Note:}
  2987. While it is not an error to compare @r{inexact} numbers using these
  2988. predicates, the results may be unreliable because a small inaccuracy
  2989. may affect the result; this is especially true of @code{=} and @code{zero?}.
  2990. @vindex @w{zero?}
  2991. @vindex @w{=}
  2992. When in doubt, consult a numerical analyst.
  2993. @end quotation
  2994. @end deffn
  2995. @deffn {library procedure} zero? @var{z}
  2996. @deffnx {library procedure} positive? @var{x}
  2997. @deffnx {library procedure} negative? @var{x}
  2998. @deffnx {library procedure} odd? @var{n}
  2999. @deffnx {library procedure} even? @var{n}
  3000. These numerical predicates test a number for a particular property,
  3001. returning @t{#t} or @t{#f}. See note above.
  3002. @end deffn
  3003. @deffn {library procedure} max x1 x2 @dots{},
  3004. @deffnx {library procedure} min x1 x2 @dots{},
  3005. These procedures return the maximum or minimum of their arguments.
  3006. @format
  3007. @t{(max 3 4) ==> 4 ; exact
  3008. (max 3.9 4) ==> 4.0 ; inexact
  3009. }
  3010. @end format
  3011. @quotation
  3012. @emph{Note:}
  3013. If any argument is inexact, then the result will also be inexact (unless
  3014. the procedure can prove that the inaccuracy is not large enough to affect the
  3015. result, which is possible only in unusual implementations). If @samp{min} or
  3016. @samp{max} is used to compare numbers of mixed exactness, and the numerical
  3017. value of the result cannot be represented as an inexact number without loss of
  3018. accuracy, then the procedure may report a violation of an implementation
  3019. restriction.
  3020. @end quotation
  3021. @end deffn
  3022. @deffn {procedure} + z1 @dots{},
  3023. @deffnx {procedure} * z1 @dots{},
  3024. These procedures return the sum or product of their arguments.
  3025. @c - These procedures are exactness preserving.
  3026. @format
  3027. @t{(+ 3 4) ==> 7
  3028. (+ 3) ==> 3
  3029. (+) ==> 0
  3030. (* 4) ==> 4
  3031. (*) ==> 1
  3032. }
  3033. @end format
  3034. @end deffn
  3035. @deffn {procedure} - z1 z2
  3036. @deffnx {procedure} - @var{z}
  3037. @deffnx {optional procedure} - z1 z2 @dots{},
  3038. @deffnx {procedure} / z1 z2
  3039. @deffnx {procedure} / @var{z}
  3040. @deffnx {optional procedure} / z1 z2 @dots{},
  3041. With two or more arguments, these procedures return the difference or
  3042. quotient of their arguments, associating to the left. With one argument,
  3043. however, they return the additive or multiplicative inverse of their argument.
  3044. @c - These procedures are exactness preserving, except that division may
  3045. @c - coerce its result to inexact in implementations that do not support
  3046. @c - \tupe{ratnum}s.
  3047. @format
  3048. @t{(- 3 4) ==> -1
  3049. (- 3 4 5) ==> -6
  3050. (- 3) ==> -3
  3051. (/ 3 4 5) ==> 3/20
  3052. (/ 3) ==> 1/3
  3053. }
  3054. @end format
  3055. @end deffn
  3056. @deffn {library procedure} abs x
  3057. @samp{Abs} returns the absolute value of its argument.
  3058. @c - {\cf Abs} is exactness preserving when its argument is real.
  3059. @format
  3060. @t{(abs -7) ==> 7
  3061. }
  3062. @end format
  3063. @end deffn
  3064. @deffn {procedure} quotient n1 n2
  3065. @deffnx {procedure} remainder n1 n2
  3066. @deffnx {procedure} modulo n1 n2
  3067. These procedures implement number-theoretic (integer)
  3068. division. @var{n2} should be non-zero. All three procedures
  3069. return integers. If @var{n1}/@var{n2} is an integer:
  3070. @format
  3071. @t{ (quotient @var{n1} @var{n2}) ==> @var{n1}/@var{n2}
  3072. (remainder @var{n1} @var{n2}) ==> 0
  3073. (modulo @var{n1} @var{n2}) ==> 0
  3074. }
  3075. @end format
  3076. If @var{n1}/@var{n2} is not an integer:
  3077. @format
  3078. @t{ (quotient @var{n1} @var{n2}) ==> @var{n_q}
  3079. (remainder @var{n1} @var{n2}) ==> @var{n_r}
  3080. (modulo @var{n1} @var{n2}) ==> @var{n_m}
  3081. }
  3082. @end format
  3083. where @var{n_q} is @var{n1}/@var{n2} rounded towards zero,
  3084. 0 < |@var{n_r}| < |@var{n2}|, 0 < |@var{n_m}| < |@var{n2}|,
  3085. @var{n_r} and @var{n_m} differ from @var{n1} by a multiple of @var{n2},
  3086. @var{n_r} has the same sign as @var{n1}, and
  3087. @var{n_m} has the same sign as @var{n2}.
  3088. From this we can conclude that for integers @var{n1} and @var{n2} with
  3089. @var{n2} not equal to 0,
  3090. @format
  3091. @t{ (= @var{n1} (+ (* @var{n2} (quotient @var{n1} @var{n2}))
  3092. (remainder @var{n1} @var{n2})))
  3093. ==> #t
  3094. }
  3095. @end format
  3096. provided all numbers involved in that computation are exact.
  3097. @format
  3098. @t{(modulo 13 4) ==> 1
  3099. (remainder 13 4) ==> 1
  3100. (modulo -13 4) ==> 3
  3101. (remainder -13 4) ==> -1
  3102. (modulo 13 -4) ==> -3
  3103. (remainder 13 -4) ==> 1
  3104. (modulo -13 -4) ==> -1
  3105. (remainder -13 -4) ==> -1
  3106. (remainder -13 -4.0) ==> -1.0 ; inexact
  3107. }
  3108. @end format
  3109. @end deffn
  3110. @deffn {library procedure} gcd n1 @dots{},
  3111. @deffnx {library procedure} lcm n1 @dots{},
  3112. These procedures return the greatest common divisor or least common
  3113. multiple of their arguments. The result is always non-negative.
  3114. @c - These procedures are exactness preserving.
  3115. @c %R4%% I added the inexact example.
  3116. @format
  3117. @t{(gcd 32 -36) ==> 4
  3118. (gcd) ==> 0
  3119. (lcm 32 -36) ==> 288
  3120. (lcm 32.0 -36) ==> 288.0 ; inexact
  3121. (lcm) ==> 1
  3122. }
  3123. @end format
  3124. @end deffn
  3125. @deffn {procedure} numerator @var{q}
  3126. @deffnx {procedure} denominator @var{q}
  3127. These procedures return the numerator or denominator of their
  3128. argument; the result is computed as if the argument was represented as
  3129. a fraction in lowest terms. The denominator is always positive. The
  3130. denominator of 0 is defined to be 1.
  3131. @c - The remarks about denominators are new.
  3132. @c - Clearly, they are exactness-preserving procedures.
  3133. @ignore todo
  3134. More description and examples needed.
  3135. @end ignore
  3136. @format
  3137. @t{(numerator (/ 6 4)) ==> 3
  3138. (denominator (/ 6 4)) ==> 2
  3139. (denominator
  3140. (exact->inexact (/ 6 4))) ==> 2.0
  3141. }
  3142. @end format
  3143. @end deffn
  3144. @deffn {procedure} floor x
  3145. @deffnx {procedure} ceiling x
  3146. @deffnx {procedure} truncate x
  3147. @deffnx {procedure} round x
  3148. These procedures return integers.
  3149. @samp{Floor} returns the largest integer not larger than @var{x}.
  3150. @samp{Ceiling} returns the smallest integer not smaller than @var{x}.
  3151. @samp{Truncate} returns the integer closest to @var{x} whose absolute
  3152. value is not larger than the absolute value of @var{x}. @samp{Round} returns the
  3153. closest integer to @var{x}, rounding to even when @var{x} is halfway between two
  3154. integers.
  3155. @quotation
  3156. @emph{Rationale:}
  3157. @samp{Round} rounds to even for consistency with the default rounding
  3158. mode specified by the IEEE floating point standard.
  3159. @end quotation
  3160. @quotation
  3161. @emph{Note:}
  3162. If the argument to one of these procedures is inexact, then the result
  3163. will also be inexact. If an exact value is needed, the
  3164. result should be passed to the @samp{inexact->exact} procedure.
  3165. @end quotation
  3166. @format
  3167. @t{(floor -4.3) ==> -5.0
  3168. (ceiling -4.3) ==> -4.0
  3169. (truncate -4.3) ==> -4.0
  3170. (round -4.3) ==> -4.0
  3171. (floor 3.5) ==> 3.0
  3172. (ceiling 3.5) ==> 4.0
  3173. (truncate 3.5) ==> 3.0
  3174. (round 3.5) ==> 4.0 ; inexact
  3175. (round 7/2) ==> 4 ; exact
  3176. (round 7) ==> 7
  3177. }
  3178. @end format
  3179. @end deffn
  3180. @deffn {library procedure} rationalize x y
  3181. @c - \proto{rationalize}{ x}{procedure}
  3182. @samp{Rationalize} returns the @emph{simplest} rational number
  3183. differing from @var{x} by no more than @var{y}. A rational number r_1 is
  3184. @emph{simpler} than another rational number
  3185. @cindex @w{simplest rational}
  3186. r_2 if r_1 = p_1/q_1 and r_2 = p_2/q_2 (in lowest terms) and |p_1|<= |p_2| and |q_1| <= |q_2|. Thus 3/5 is simpler than 4/7.
  3187. Although not all rationals are comparable in this ordering (consider 2/7
  3188. and 3/5) any interval contains a rational number that is simpler than
  3189. every other rational number in that interval (the simpler 2/5 lies
  3190. between 2/7 and 3/5). Note that 0 = 0/1 is the simplest rational of
  3191. all.
  3192. @format
  3193. @t{(rationalize
  3194. (inexact->exact .3) 1/10) ==> 1/3 ; exact
  3195. (rationalize .3 1/10) ==> #i1/3 ; inexact
  3196. }
  3197. @end format
  3198. @end deffn
  3199. @deffn {procedure} exp @var{z}
  3200. @deffnx {procedure} log @var{z}
  3201. @deffnx {procedure} sin @var{z}
  3202. @deffnx {procedure} cos @var{z}
  3203. @deffnx {procedure} tan @var{z}
  3204. @deffnx {procedure} asin @var{z}
  3205. @deffnx {procedure} acos @var{z}
  3206. @deffnx {procedure} atan @var{z}
  3207. @deffnx {procedure} atan @var{y} @var{x}
  3208. These procedures are part of every implementation that supports
  3209. @c %R4%%
  3210. general
  3211. real numbers; they compute the usual transcendental functions. @samp{Log}
  3212. computes the natural logarithm of @var{z} (not the base ten logarithm).
  3213. @samp{Asin}, @samp{acos}, and @samp{atan} compute arcsine (sin^-1),
  3214. arccosine (cos^-1), and arctangent (tan^-1), respectively.
  3215. The two-argument variant of @samp{atan} computes @t{(angle
  3216. (make-rectangular @var{x} @var{y}))} (see below), even in implementations
  3217. that don't support general complex numbers.
  3218. In general, the mathematical functions log, arcsine, arccosine, and
  3219. arctangent are multiply defined.
  3220. The value of log z is defined to be the one whose imaginary
  3221. part lies in the range from -pi (exclusive) to pi (inclusive).
  3222. log 0 is undefined.
  3223. With log defined this way, the values of sin^-1 z, cos^-1 z,
  3224. and tan^-1 z are according to the following formulae:
  3225. @center sin^-1 z = -i log (i z + sqrt1 - z^2)
  3226. @center cos^-1 z = pi / 2 - sin^-1 z
  3227. @center tan^-1 z = (log (1 + i z) - log (1 - i z)) / (2 i)
  3228. The above specification follows [CLtL], which in turn
  3229. cites [Penfield81]; refer to these sources for more detailed
  3230. discussion of branch cuts, boundary conditions, and implementation of
  3231. these functions. When it is possible these procedures produce a real
  3232. result from a real argument.
  3233. @c %R4%%
  3234. @ignore todo
  3235. The cited references are likely to change their branch cuts
  3236. soon to allow for the possibility of distinct positive and negative
  3237. zeroes, as in IEEE floating point. We may not want to follow those
  3238. changes, since we may want a complex number with zero imaginary part
  3239. (whether positive or negative zero) to be treated as a real. I don't
  3240. think there are any better standards for complex arithmetic than the
  3241. ones cited, so we're really on our own here.
  3242. @end ignore
  3243. @end deffn
  3244. @deffn {procedure} sqrt @var{z}
  3245. Returns the principal square root of @var{z}. The result will have
  3246. either positive real part, or zero real part and non-negative imaginary
  3247. part.
  3248. @end deffn
  3249. @deffn {procedure} expt z1 z2
  3250. Returns @var{z1} raised to the power @var{z2}. For z_1 ~= 0
  3251. @center z_1^z_2 = e^z_2 log z_1
  3252. 0^z is 1 if z = 0 and 0 otherwise.
  3253. @end deffn
  3254. @c - \begin{entry}{%-
  3255. @c - \proto{approximate}{ z x}{procedure}}
  3256. @c -
  3257. @c - Returns an approximation to \vr{z} in a representation whose precision is
  3258. @c - the same as that
  3259. @c - of the representation of \vr{x}, which must be an inexact number. The
  3260. @c - result is always inexact.
  3261. @c -
  3262. @c - \begin{scheme}
  3263. @c - (approximate 3.1415926535 1F10)
  3264. @c - \ev 3.14159F0
  3265. @c - (approximate 3.1415926535 \#I65535)
  3266. @c - \ev \#I3
  3267. @c - (approximate 3.14F0 1L8)
  3268. @c - \ev 3.14L0
  3269. @c - (approximate 3.1415926535F0 1L8)
  3270. @c - \ev 3.14159L0
  3271. @c - \end{scheme}
  3272. @c - \end{entry}
  3273. @deffn {procedure} make-rectangular x1 x2
  3274. @deffnx {procedure} make-polar x3 x4
  3275. @deffnx {procedure} real-part @var{z}
  3276. @deffnx {procedure} imag-part @var{z}
  3277. @deffnx {procedure} magnitude @var{z}
  3278. @deffnx {procedure} angle @var{z}
  3279. These procedures are part of every implementation that supports
  3280. @c %R4%%
  3281. general
  3282. complex numbers. Suppose @var{x1}, @var{x2}, @var{x3}, and @var{x4} are
  3283. real numbers and @var{z} is a complex number such that
  3284. @center @var{z} = @var{x1} + @var{x2}@w{i} = @var{x3} . e^@w{i} @var{x4}
  3285. Then
  3286. @format
  3287. @t{(make-rectangular @var{x1} @var{x2}) ==> @var{z}
  3288. (make-polar @var{x3} @var{x4}) ==> @var{z}
  3289. (real-part @var{z}) ==> @var{x1}
  3290. (imag-part @var{z}) ==> @var{x2}
  3291. (magnitude @var{z}) ==> |@var{x3}|
  3292. (angle @var{z}) ==> x_angle
  3293. }
  3294. @end format
  3295. where -pi < x_angle <= pi with x_angle = @var{x4} + 2pi n
  3296. for some integer n.
  3297. @quotation
  3298. @emph{Rationale:}
  3299. @samp{Magnitude} is the same as @code{abs} for a real argument,
  3300. @vindex @w{abs}
  3301. but @samp{abs} must be present in all implementations, whereas
  3302. @samp{magnitude} need only be present in implementations that support
  3303. general complex numbers.
  3304. @end quotation
  3305. @end deffn
  3306. @deffn {procedure} exact->inexact @var{z}
  3307. @deffnx {procedure} inexact->exact @var{z}
  3308. @samp{Exact->inexact} returns an @r{inexact} representation of @var{z}.
  3309. The value returned is the
  3310. @r{inexact} number that is numerically closest to the argument.
  3311. @c %R4%%For
  3312. @c \tupe{exact} arguments which have no reasonably close \tupe{inexact} equivalent,
  3313. @c it is permissible to signal an error.
  3314. If an @r{exact} argument has no reasonably close @r{inexact} equivalent,
  3315. then a violation of an implementation restriction may be reported.
  3316. @samp{Inexact->exact} returns an @r{exact} representation of
  3317. @var{z}. The value returned is the @r{exact} number that is numerically
  3318. closest to the argument.
  3319. @c %R4%% For \tupe{inexact} arguments which have no
  3320. @c reasonably close \tupe{exact} equivalent, it is permissible to signal
  3321. @c an error.
  3322. If an @r{inexact} argument has no reasonably close @r{exact} equivalent,
  3323. then a violation of an implementation restriction may be reported.
  3324. @c %R%% I moved this to the section on implementation restrictions.
  3325. @c For any implementation that supports \tupe{inexact} quantities,
  3326. @c there is a subset of the integers for which there are both \tupe{exact} and
  3327. @c \tupe{inexact} representations. This subset must include the non-negative
  3328. @c integers up to a limit specified by the implementation. The limit
  3329. @c must be big enough to represent all digits in reasonable radices, and
  3330. @c may correspond to some natural word size for the implementation. For
  3331. @c such integers, these procedures implement the natural one-to-one
  3332. @c correspondence between the representations.
  3333. These procedures implement the natural one-to-one correspondence between
  3334. @r{exact} and @r{inexact} integers throughout an
  3335. implementation-dependent range. See section @ref{Implementation restrictions}.
  3336. @end deffn
  3337. @sp 3
  3338. @node Numerical input and output, , Numerical operations, Numbers
  3339. @subsection Numerical input and output
  3340. @deffn {procedure} number->string z
  3341. @deffnx {procedure} number->string z radix
  3342. @var{Radix} must be an exact integer, either 2, 8, 10, or 16. If omitted,
  3343. @var{radix} defaults to 10.
  3344. The procedure @samp{number->string} takes a
  3345. number and a radix and returns as a string an external representation of
  3346. the given number in the given radix such that
  3347. @format
  3348. @t{(let ((number @var{number})
  3349. (radix @var{radix}))
  3350. (eqv? number
  3351. (string->number (number->string number
  3352. radix)
  3353. radix)))
  3354. }
  3355. @end format
  3356. is true. It is an error if no possible result makes this expression true.
  3357. If @var{z} is inexact, the radix is 10, and the above expression
  3358. can be satisfied by a result that contains a decimal point,
  3359. then the result contains a decimal point and is expressed using the
  3360. minimum number of digits (exclusive of exponent and trailing
  3361. zeroes) needed to make the above expression
  3362. true [howtoprint], [howtoread];
  3363. otherwise the format of the result is unspecified.
  3364. The result returned by @samp{number->string}
  3365. never contains an explicit radix prefix.
  3366. @quotation
  3367. @emph{Note:}
  3368. The error case can occur only when @var{z} is not a complex number
  3369. or is a complex number with a non-rational real or imaginary part.
  3370. @end quotation
  3371. @quotation
  3372. @emph{Rationale:}
  3373. If @var{z} is an inexact number represented using flonums, and
  3374. the radix is 10, then the above expression is normally satisfied by
  3375. a result containing a decimal point. The unspecified case
  3376. allows for infinities, NaNs, and non-flonum representations.
  3377. @end quotation
  3378. @end deffn
  3379. @deffn {procedure} string->number string
  3380. @deffnx {procedure} string->number string radix
  3381. @c %R4%% I didn't include the (string->number string radix exactness)
  3382. @c case, since I haven't heard any resolution of the coding to be used
  3383. @c for the third argument.
  3384. Returns a number of the maximally precise representation expressed by the
  3385. given @var{string}. @var{Radix} must be an exact integer, either 2, 8, 10,
  3386. or 16. If supplied, @var{radix} is a default radix that may be overridden
  3387. by an explicit radix prefix in @var{string} (e.g. @t{"#o177"}). If @var{radix}
  3388. is not supplied, then the default radix is 10. If @var{string} is not
  3389. a syntactically valid notation for a number, then @samp{string->number}
  3390. returns @t{#f}.
  3391. @format
  3392. @t{(string->number "100") ==> 100
  3393. (string->number "100" 16) ==> 256
  3394. (string->number "1e2") ==> 100.0
  3395. (string->number "15##") ==> 1500.0
  3396. }
  3397. @end format
  3398. @quotation
  3399. @emph{Note:}
  3400. The domain of @samp{string->number} may be restricted by implementations
  3401. in the following ways. @samp{String->number} is permitted to return
  3402. @t{#f} whenever @var{string} contains an explicit radix prefix.
  3403. If all numbers supported by an implementation are real, then
  3404. @samp{string->number} is permitted to return @t{#f} whenever
  3405. @var{string} uses the polar or rectangular notations for complex
  3406. numbers. If all numbers are integers, then
  3407. @samp{string->number} may return @t{#f} whenever
  3408. the fractional notation is used. If all numbers are exact, then
  3409. @samp{string->number} may return @t{#f} whenever
  3410. an exponent marker or explicit exactness prefix is used, or if
  3411. a @t{#} appears in place of a digit. If all inexact
  3412. numbers are integers, then
  3413. @samp{string->number} may return @t{#f} whenever
  3414. a decimal point is used.
  3415. @end quotation
  3416. @end deffn
  3417. @node Other data types, Control features, Numbers, Standard procedures
  3418. @section Other data types
  3419. @menu
  3420. * Booleans::
  3421. * Pairs and lists::
  3422. * Symbols::
  3423. * Characters::
  3424. * Strings::
  3425. * Vectors::
  3426. @end menu
  3427. This section describes operations on some of Scheme's non-numeric data types:
  3428. booleans, pairs, lists, symbols, characters, strings and vectors.
  3429. @node Booleans, Pairs and lists, Other data types, Other data types
  3430. @subsection Booleans
  3431. The standard boolean objects for true and false are written as
  3432. @t{#t} and @t{#f}. What really
  3433. @vindex #f
  3434. @vindex #t
  3435. matters, though, are the objects that the Scheme conditional expressions
  3436. (@samp{if}, @samp{cond}, @samp{and}, @samp{or}, @samp{do}) treat as
  3437. true or false. The phrase ``a true value''
  3438. @cindex @w{false}
  3439. @cindex @w{true}
  3440. (or sometimes just ``true'') means any object treated as true by the
  3441. conditional expressions, and the phrase ``a false value'' (or
  3442. @cindex @w{false}
  3443. ``false'') means any object treated as false by the conditional expressions.
  3444. Of all the standard Scheme values, only @t{#f}
  3445. @c is guaranteed to count
  3446. counts as false in conditional expressions.
  3447. @c It is not
  3448. @c specified whether the empty list\index{empty list} counts as false
  3449. @c or as true in conditional expressions.
  3450. Except for @t{#f},
  3451. @c and possibly the empty list,
  3452. all standard Scheme values, including @t{#t},
  3453. pairs, the empty list, symbols, numbers, strings, vectors, and procedures,
  3454. count as true.
  3455. @c \begin{note}
  3456. @c In some implementations the empty list counts as false, contrary
  3457. @c to the above.
  3458. @c Nonetheless a few examples in this report assume that the
  3459. @c empty list counts as true, as in \cite{IEEEScheme}.
  3460. @c \end{note}
  3461. @c \begin{rationale}
  3462. @c For historical reasons some implementations regard \schfalse{} and the
  3463. @c empty list as the same object. These implementations therefore cannot
  3464. @c make the empty list count as true in conditional expressions.
  3465. @c \end{rationale}
  3466. @quotation
  3467. @emph{Note:}
  3468. Programmers accustomed to other dialects of Lisp should be aware that
  3469. Scheme distinguishes both @t{#f} and the empty list
  3470. @cindex @w{empty list}
  3471. from the symbol @code{nil}.
  3472. @vindex @w{nil}
  3473. @end quotation
  3474. Boolean constants evaluate to themselves, so they do not need to be quoted
  3475. in programs.
  3476. @example
  3477. #t ==> #t
  3478. #f ==> #f
  3479. '#f ==> #f
  3480. @end example
  3481. @deffn {library procedure} not obj
  3482. @samp{Not} returns @t{#t} if @var{obj} is false, and returns
  3483. @t{#f} otherwise.
  3484. @format
  3485. @t{(not #t) ==> #f
  3486. (not 3) ==> #f
  3487. (not (list 3)) ==> #f
  3488. (not #f) ==> #t
  3489. (not '()) ==> #f
  3490. (not (list)) ==> #f
  3491. (not 'nil) ==> #f
  3492. }
  3493. @end format
  3494. @end deffn
  3495. @deffn {library procedure} boolean? obj
  3496. @samp{Boolean?} returns @t{#t} if @var{obj} is either @t{#t} or
  3497. @t{#f} and returns @t{#f} otherwise.
  3498. @format
  3499. @t{(boolean? #f) ==> #t
  3500. (boolean? 0) ==> #f
  3501. (boolean? '()) ==> #f
  3502. }
  3503. @end format
  3504. @end deffn
  3505. @node Pairs and lists, Symbols, Booleans, Other data types
  3506. @subsection Pairs and lists
  3507. A @dfn{pair} (sometimes called a @dfn{dotted pair}) is a
  3508. @cindex @w{dotted pair}
  3509. @cindex @w{pair}
  3510. record structure with two fields called the car and cdr fields (for
  3511. historical reasons). Pairs are created by the procedure @samp{cons}.
  3512. The car and cdr fields are accessed by the procedures @samp{car} and
  3513. @samp{cdr}. The car and cdr fields are assigned by the procedures
  3514. @samp{set-car!} and @samp{set-cdr!}.
  3515. Pairs are used primarily to represent lists. A list can
  3516. be defined recursively as either the empty list or a pair whose
  3517. @cindex @w{empty list}
  3518. cdr is a list. More precisely, the set of lists is defined as the smallest
  3519. set @var{X} such that
  3520. @itemize @bullet
  3521. @item
  3522. The empty list is in @var{X}.
  3523. @item
  3524. If @var{list} is in @var{X}, then any pair whose cdr field contains
  3525. @var{list} is also in @var{X}.
  3526. @end itemize
  3527. The objects in the car fields of successive pairs of a list are the
  3528. elements of the list. For example, a two-element list is a pair whose car
  3529. is the first element and whose cdr is a pair whose car is the second element
  3530. and whose cdr is the empty list. The length of a list is the number of
  3531. elements, which is the same as the number of pairs.
  3532. The empty list is a special object of its own type
  3533. @cindex @w{empty list}
  3534. (it is not a pair); it has no elements and its length is zero.
  3535. @quotation
  3536. @emph{Note:}
  3537. The above definitions imply that all lists have finite length and are
  3538. terminated by the empty list.
  3539. @end quotation
  3540. The most general notation (external representation) for Scheme pairs is
  3541. the ``dotted'' notation @w{@samp{(@var{c1} .@: @var{c2})}} where
  3542. @var{c1} is the value of the car field and @var{c2} is the value of the
  3543. cdr field. For example @samp{(4 .@: 5)} is a pair whose car is 4 and whose
  3544. cdr is 5. Note that @samp{(4 .@: 5)} is the external representation of a
  3545. pair, not an expression that evaluates to a pair.
  3546. A more streamlined notation can be used for lists: the elements of the
  3547. list are simply enclosed in parentheses and separated by spaces. The
  3548. empty list is written @t{()} . For example,
  3549. @cindex @w{empty list}
  3550. @example
  3551. (a b c d e)
  3552. @end example
  3553. and
  3554. @example
  3555. (a . (b . (c . (d . (e . ())))))
  3556. @end example
  3557. are equivalent notations for a list of symbols.
  3558. A chain of pairs not ending in the empty list is called an
  3559. @dfn{improper list}. Note that an improper list is not a list.
  3560. @cindex @w{improper list}
  3561. The list and dotted notations can be combined to represent
  3562. improper lists:
  3563. @example
  3564. (a b c . d)
  3565. @end example
  3566. is equivalent to
  3567. @example
  3568. (a . (b . (c . d)))
  3569. @end example
  3570. Whether a given pair is a list depends upon what is stored in the cdr
  3571. field. When the @code{set-cdr!} procedure is used, an object can be a
  3572. @vindex @w{set-cdr!}
  3573. list one moment and not the next:
  3574. @example
  3575. (define x (list 'a 'b 'c))
  3576. (define y x)
  3577. y ==> (a b c)
  3578. (list? y) ==> #t
  3579. (set-cdr! x 4) ==> @emph{unspecified}
  3580. x ==> (a . 4)
  3581. (eqv? x y) ==> #t
  3582. y ==> (a . 4)
  3583. (list? y) ==> #f
  3584. (set-cdr! x x) ==> @emph{unspecified}
  3585. (list? x) ==> #f
  3586. @end example
  3587. @c It is often convenient to speak of a homogeneous list of objects
  3588. @c of some particular data type, as for example \hbox{\cf (1 2 3)} is a list of
  3589. @c integers. To be more precise, suppose \var{D} is some data type. (Any
  3590. @c predicate defines a data type consisting of those objects of which the
  3591. @c predicate is true.) Then
  3592. @c \begin{itemize}
  3593. @c \item The empty list is a list of \var{D}.
  3594. @c \item If \var{list} is a list of \var{D}, then any pair whose cdr is
  3595. @c \var{list} and whose car is an element of the data type \var{D} is also a
  3596. @c list of \var{D}.
  3597. @c \item There are no other lists of \var{D}.
  3598. @c \end{itemize}
  3599. Within literal expressions and representations of objects read by the
  3600. @code{read} procedure, the forms @t{'}@r{<datum>},
  3601. @vindex '
  3602. @vindex @w{read}
  3603. @t{`}@r{<datum>}, @t{,}@r{<datum>}, and
  3604. @vindex ,
  3605. @t{,@@}@r{<datum>} denote two-ele@-ment lists whose first elements are
  3606. the symbols @code{quote}, @code{quasiquote}, @w{@code{unquote}}, and
  3607. @vindex @w{unquote}
  3608. @vindex @w{quasiquote}
  3609. @vindex @w{quote}
  3610. @code{unquote-splicing}, respectively. The second element in each case
  3611. @vindex @w{unquote-splicing}
  3612. is @r{<datum>}. This convention is supported so that arbitrary Scheme
  3613. programs may be represented as lists.
  3614. @ignore todo
  3615. Can or need this be stated
  3616. more carefully?
  3617. @end ignore
  3618. That is, according to Scheme's grammar, every
  3619. <expression> is also a <datum> (see section @pxref{External representation}).
  3620. Among other things, this permits the use of the @samp{read} procedure to
  3621. parse Scheme programs. See section @ref{External representations}.
  3622. @deffn {procedure} pair? obj
  3623. @samp{Pair?} returns @t{#t} if @var{obj} is a pair, and otherwise
  3624. returns @t{#f}.
  3625. @format
  3626. @t{(pair? '(a . b)) ==> #t
  3627. (pair? '(a b c)) ==> #t
  3628. (pair? '()) ==> #f
  3629. (pair? '#(a b)) ==> #f
  3630. }
  3631. @end format
  3632. @end deffn
  3633. @deffn {procedure} cons obj1 obj2
  3634. Returns a newly allocated pair whose car is @var{obj1} and whose cdr is
  3635. @var{obj2}. The pair is guaranteed to be different (in the sense of
  3636. @samp{eqv?}) from every existing object.
  3637. @format
  3638. @t{(cons 'a '()) ==> (a)
  3639. (cons '(a) '(b c d)) ==> ((a) b c d)
  3640. (cons "a" '(b c)) ==> ("a" b c)
  3641. (cons 'a 3) ==> (a . 3)
  3642. (cons '(a b) 'c) ==> ((a b) . c)
  3643. }
  3644. @end format
  3645. @end deffn
  3646. @deffn {procedure} car pair
  3647. @ignore nodomain
  3648. @var{Pair} must be a pair.
  3649. @end ignore
  3650. Returns the contents of the car field of @var{pair}. Note that it is an
  3651. error to take the car of the empty list.
  3652. @cindex @w{empty list}
  3653. @format
  3654. @t{(car '(a b c)) ==> a
  3655. (car '((a) b c d)) ==> (a)
  3656. (car '(1 . 2)) ==> 1
  3657. (car '()) ==> @emph{error}
  3658. }
  3659. @end format
  3660. @end deffn
  3661. @deffn {procedure} cdr pair
  3662. @ignore nodomain
  3663. @var{Pair} must be a pair.
  3664. @end ignore
  3665. Returns the contents of the cdr field of @var{pair}.
  3666. Note that it is an error to take the cdr of the empty list.
  3667. @format
  3668. @t{(cdr '((a) b c d)) ==> (b c d)
  3669. (cdr '(1 . 2)) ==> 2
  3670. (cdr '()) ==> @emph{error}
  3671. }
  3672. @end format
  3673. @end deffn
  3674. @deffn {procedure} set-car! pair obj
  3675. @ignore nodomain
  3676. @var{Pair} must be a pair.
  3677. @end ignore
  3678. Stores @var{obj} in the car field of @var{pair}.
  3679. The value returned by @samp{set-car!} is unspecified.
  3680. @c <!>
  3681. @c This procedure can be very confusing if used indiscriminately.
  3682. @format
  3683. @t{(define (f) (list 'not-a-constant-list))
  3684. (define (g) '(constant-list))
  3685. (set-car! (f) 3) ==> @emph{unspecified}
  3686. (set-car! (g) 3) ==> @emph{error}
  3687. }
  3688. @end format
  3689. @end deffn
  3690. @deffn {procedure} set-cdr! pair obj
  3691. @ignore nodomain
  3692. @var{Pair} must be a pair.
  3693. @end ignore
  3694. Stores @var{obj} in the cdr field of @var{pair}.
  3695. The value returned by @samp{set-cdr!} is unspecified.
  3696. @c <!>
  3697. @c This procedure can be very confusing if used indiscriminately.
  3698. @end deffn
  3699. @deffn {library procedure} caar pair
  3700. @deffnx {library procedure} cadr pair
  3701. @deffnx { @w{ @dots{}}} @w{ @dots{}}
  3702. @deffnx {library procedure} cdddar pair
  3703. @deffnx {library procedure} cddddr pair
  3704. These procedures are compositions of @samp{car} and @samp{cdr}, where
  3705. for example @samp{caddr} could be defined by
  3706. @format
  3707. @t{(define caddr (lambda (x) (car (cdr (cdr x)))))@r{.}
  3708. }
  3709. @end format
  3710. Arbitrary compositions, up to four deep, are provided. There are
  3711. twenty-eight of these procedures in all.
  3712. @end deffn
  3713. @deffn {library procedure} null? obj
  3714. Returns @t{#t} if @var{obj} is the empty list,
  3715. @cindex @w{empty list}
  3716. otherwise returns @t{#f}.
  3717. @c \begin{note}
  3718. @c In implementations in which the empty
  3719. @c list is the same as \schfalse{}, {\cf null?} will return \schtrue{}
  3720. @c if \var{obj} is \schfalse{}.
  3721. @c \end{note}
  3722. @end deffn
  3723. @deffn {library procedure} list? obj
  3724. Returns @t{#t} if @var{obj} is a list, otherwise returns @t{#f}.
  3725. By definition, all lists have finite length and are terminated by
  3726. the empty list.
  3727. @format
  3728. @t{ (list? '(a b c)) ==> #t
  3729. (list? '()) ==> #t
  3730. (list? '(a . b)) ==> #f
  3731. (let ((x (list 'a)))
  3732. (set-cdr! x x)
  3733. (list? x)) ==> #f
  3734. }
  3735. @end format
  3736. @end deffn
  3737. @deffn {library procedure} list @var{obj} @dots{},
  3738. Returns a newly allocated list of its arguments.
  3739. @format
  3740. @t{(list 'a (+ 3 4) 'c) ==> (a 7 c)
  3741. (list) ==> ()
  3742. }
  3743. @end format
  3744. @end deffn
  3745. @deffn {library procedure} length list
  3746. @ignore nodomain
  3747. @var{List} must be a list.
  3748. @end ignore
  3749. Returns the length of @var{list}.
  3750. @format
  3751. @t{(length '(a b c)) ==> 3
  3752. (length '(a (b) (c d e))) ==> 3
  3753. (length '()) ==> 0
  3754. }
  3755. @end format
  3756. @end deffn
  3757. @deffn {library procedure} append list @dots{},
  3758. @ignore nodomain
  3759. All @var{list}s should be lists.
  3760. @end ignore
  3761. Returns a list consisting of the elements of the first @var{list}
  3762. followed by the elements of the other @var{list}s.
  3763. @format
  3764. @t{(append '(x) '(y)) ==> (x y)
  3765. (append '(a) '(b c d)) ==> (a b c d)
  3766. (append '(a (b)) '((c))) ==> (a (b) (c))
  3767. }
  3768. @end format
  3769. The resulting list is always newly allocated, except that it shares
  3770. structure with the last @var{list} argument. The last argument may
  3771. actually be any object; an improper list results if the last argument is not a
  3772. proper list.
  3773. @ignore todo
  3774. This is pretty awkward. I should get Bartley to fix this.
  3775. @end ignore
  3776. @format
  3777. @t{(append '(a b) '(c . d)) ==> (a b c . d)
  3778. (append '() 'a) ==> a
  3779. }
  3780. @end format
  3781. @end deffn
  3782. @deffn {library procedure} reverse list
  3783. @ignore nodomain
  3784. @var{List} must be a list.
  3785. @end ignore
  3786. Returns a newly allocated list consisting of the elements of @var{list}
  3787. in reverse order.
  3788. @format
  3789. @t{(reverse '(a b c)) ==> (c b a)
  3790. (reverse '(a (b c) d (e (f))))
  3791. ==> ((e (f)) d (b c) a)
  3792. }
  3793. @end format
  3794. @end deffn
  3795. @deffn {library procedure} list-tail list @var{k}
  3796. Returns the sublist of @var{list} obtained by omitting the first @var{k}
  3797. elements. It is an error if @var{list} has fewer than @var{k} elements.
  3798. @samp{List-tail} could be defined by
  3799. @format
  3800. @t{(define list-tail
  3801. (lambda (x k)
  3802. (if (zero? k)
  3803. x
  3804. (list-tail (cdr x) (- k 1)))))
  3805. }
  3806. @end format
  3807. @end deffn
  3808. @deffn {library procedure} list-ref list @var{k}
  3809. Returns the @var{k}th element of @var{list}. (This is the same
  3810. as the car of @t{(list-tail @var{list} @var{k})}.)
  3811. It is an error if @var{list} has fewer than @var{k} elements.
  3812. @format
  3813. @t{(list-ref '(a b c d) 2) ==> c
  3814. (list-ref '(a b c d)
  3815. (inexact->exact (round 1.8)))
  3816. ==> c
  3817. }
  3818. @end format
  3819. @end deffn
  3820. @c \begin{entry}{%
  3821. @c \proto{last-pair}{ list}{library procedure}}
  3822. @c Returns the last pair in the nonempty, possibly improper, list \var{list}.
  3823. @c {\cf Last-pair} could be defined by
  3824. @c \begin{scheme}
  3825. @c (define last-pair
  3826. @c (lambda (x)
  3827. @c (if (pair? (cdr x))
  3828. @c (last-pair (cdr x))
  3829. @c x)))%
  3830. @c \end{scheme}
  3831. @c \end{entry}
  3832. @deffn {library procedure} memq obj list
  3833. @deffnx {library procedure} memv obj list
  3834. @deffnx {library procedure} member obj list
  3835. These procedures return the first sublist of @var{list} whose car is
  3836. @var{obj}, where the sublists of @var{list} are the non-empty lists
  3837. returned by @t{(list-tail @var{list} @var{k})} for @var{k} less
  3838. than the length of @var{list}. If
  3839. @var{obj} does not occur in @var{list}, then @t{#f} (not the empty list) is
  3840. returned. @samp{Memq} uses @samp{eq?} to compare @var{obj} with the elements of
  3841. @var{list}, while @samp{memv} uses @samp{eqv?} and @samp{member} uses @samp{equal?}.
  3842. @format
  3843. @t{(memq 'a '(a b c)) ==> (a b c)
  3844. (memq 'b '(a b c)) ==> (b c)
  3845. (memq 'a '(b c d)) ==> #f
  3846. (memq (list 'a) '(b (a) c)) ==> #f
  3847. (member (list 'a)
  3848. '(b (a) c)) ==> ((a) c)
  3849. (memq 101 '(100 101 102)) ==> @emph{unspecified}
  3850. (memv 101 '(100 101 102)) ==> (101 102)
  3851. }
  3852. @end format
  3853. @end deffn
  3854. @deffn {library procedure} assq obj alist
  3855. @deffnx {library procedure} assv obj alist
  3856. @deffnx {library procedure} assoc obj alist
  3857. @var{Alist} (for ``association list'') must be a list of
  3858. pairs. These procedures find the first pair in @var{alist} whose car field is @var{obj},
  3859. and returns that pair. If no pair in @var{alist} has @var{obj} as its
  3860. car, then @t{#f} (not the empty list) is returned. @samp{Assq} uses
  3861. @samp{eq?} to compare @var{obj} with the car fields of the pairs in @var{alist},
  3862. while @samp{assv} uses @samp{eqv?} and @samp{assoc} uses @samp{equal?}.
  3863. @format
  3864. @t{(define e '((a 1) (b 2) (c 3)))
  3865. (assq 'a e) ==> (a 1)
  3866. (assq 'b e) ==> (b 2)
  3867. (assq 'd e) ==> #f
  3868. (assq (list 'a) '(((a)) ((b)) ((c))))
  3869. ==> #f
  3870. (assoc (list 'a) '(((a)) ((b)) ((c))))
  3871. ==> ((a))
  3872. (assq 5 '((2 3) (5 7) (11 13)))
  3873. ==> @emph{unspecified}
  3874. (assv 5 '((2 3) (5 7) (11 13)))
  3875. ==> (5 7)
  3876. }
  3877. @end format
  3878. @quotation
  3879. @emph{Rationale:}
  3880. Although they are ordinarily used as predicates,
  3881. @samp{memq}, @samp{memv}, @samp{member}, @samp{assq}, @samp{assv}, and @samp{assoc} do not
  3882. have question marks in their names because they return useful values rather
  3883. than just @t{#t} or @t{#f}.
  3884. @end quotation
  3885. @end deffn
  3886. @node Symbols, Characters, Pairs and lists, Other data types
  3887. @subsection Symbols
  3888. Symbols are objects whose usefulness rests on the fact that two
  3889. symbols are identical (in the sense of @samp{eqv?}) if and only if their
  3890. names are spelled the same way. This is exactly the property needed to
  3891. represent identifiers in programs, and so most
  3892. @cindex @w{identifier}
  3893. implementations of Scheme use them internally for that purpose. Symbols
  3894. are useful for many other applications; for instance, they may be used
  3895. the way enumerated values are used in Pascal.
  3896. The rules for writing a symbol are exactly the same as the rules for
  3897. writing an identifier; see sections @ref{Identifiers}
  3898. and @ref{Lexical structure}.
  3899. It is guaranteed that any symbol that has been returned as part of
  3900. a literal expression, or read using the @samp{read} procedure, and
  3901. subsequently written out using the @samp{write} procedure, will read back
  3902. in as the identical symbol (in the sense of @samp{eqv?}). The
  3903. @samp{string->symbol} procedure, however, can create symbols for
  3904. which this write/read invariance may not hold because their names
  3905. contain special characters or letters in the non-standard case.
  3906. @quotation
  3907. @emph{Note:}
  3908. Some implementations of Scheme have a feature known as ``slashification''
  3909. in order to guarantee write/read invariance for all symbols, but
  3910. historically the most important use of this feature has been to
  3911. compensate for the lack of a string data type.
  3912. Some implementations also have ``uninterned symbols'', which
  3913. defeat write/read invariance even in implementations with slashification,
  3914. and also generate exceptions to the rule that two symbols are the same
  3915. if and only if their names are spelled the same.
  3916. @end quotation
  3917. @deffn {procedure} symbol? obj
  3918. Returns @t{#t} if @var{obj} is a symbol, otherwise returns @t{#f}.
  3919. @format
  3920. @t{(symbol? 'foo) ==> #t
  3921. (symbol? (car '(a b))) ==> #t
  3922. (symbol? "bar") ==> #f
  3923. (symbol? 'nil) ==> #t
  3924. (symbol? '()) ==> #f
  3925. (symbol? #f) ==> #f
  3926. }
  3927. @end format
  3928. @end deffn
  3929. @deffn {procedure} symbol->string symbol
  3930. Returns the name of @var{symbol} as a string. If the symbol was part of
  3931. an object returned as the value of a literal expression
  3932. (section @pxref{Literal expressions}) or by a call to the @samp{read} procedure,
  3933. and its name contains alphabetic characters, then the string returned
  3934. will contain characters in the implementation's preferred standard
  3935. case---some implementations will prefer upper case, others lower case.
  3936. If the symbol was returned by @samp{string->symbol}, the case of
  3937. characters in the string returned will be the same as the case in the
  3938. string that was passed to @samp{string->symbol}. It is an error
  3939. to apply mutation procedures like @code{string-set!} to strings returned
  3940. @vindex @w{string-set!}
  3941. by this procedure.
  3942. The following examples assume that the implementation's standard case is
  3943. lower case:
  3944. @format
  3945. @t{(symbol->string 'flying-fish)
  3946. ==> "flying-fish"
  3947. (symbol->string 'Martin) ==> "martin"
  3948. (symbol->string
  3949. (string->symbol "Malvina"))
  3950. ==> "Malvina"
  3951. }
  3952. @end format
  3953. @end deffn
  3954. @deffn {procedure} string->symbol string
  3955. Returns the symbol whose name is @var{string}. This procedure can
  3956. create symbols with names containing special characters or letters in
  3957. the non-standard case, but it is usually a bad idea to create such
  3958. symbols because in some implementations of Scheme they cannot be read as
  3959. themselves. See @samp{symbol->string}.
  3960. The following examples assume that the implementation's standard case is
  3961. lower case:
  3962. @format
  3963. @t{(eq? 'mISSISSIppi 'mississippi)
  3964. ==> #t
  3965. (string->symbol "mISSISSIppi")
  3966. ==>
  3967. @r{}the symbol with name "mISSISSIppi"
  3968. (eq? 'bitBlt (string->symbol "bitBlt"))
  3969. ==> #f
  3970. (eq? 'JollyWog
  3971. (string->symbol
  3972. (symbol->string 'JollyWog)))
  3973. ==> #t
  3974. (string=? "K. Harper, M.D."
  3975. (symbol->string
  3976. (string->symbol "K. Harper, M.D.")))
  3977. ==> #t
  3978. }
  3979. @end format
  3980. @end deffn
  3981. @node Characters, Strings, Symbols, Other data types
  3982. @subsection Characters
  3983. Characters are objects that represent printed characters such as
  3984. letters and digits.
  3985. @c There is no requirement that the data type of
  3986. @c characters be disjoint from other data types; implementations are
  3987. @c encouraged to have a separate character data type, but may choose to
  3988. @c represent characters as integers, strings, or some other type.
  3989. Characters are written using the notation #\@r{<character>}
  3990. or #\@r{<character name>}.
  3991. For example:
  3992. @center @c begin-tabular
  3993. @quotation
  3994. @table @asis
  3995. @item @t{#\a}
  3996. ; lower case letter
  3997. @item @t{#\A}
  3998. ; upper case letter
  3999. @item @t{#\(}
  4000. ; left parenthesis
  4001. @item @t{#\ }
  4002. ; the space character
  4003. @item @t{#\space}
  4004. ; the preferred way to write a space
  4005. @item @t{#\newline}
  4006. ; the newline character
  4007. @item
  4008. @end table
  4009. @end quotation
  4010. Case is significant in #\@r{<character>}, but not in
  4011. #\@r{<character name>}.
  4012. @c \hyper doesn't
  4013. @c allow a linebreak
  4014. If @r{<character>} in
  4015. #\@r{<character>} is alphabetic, then the character
  4016. following @r{<character>} must be a delimiter character such as a
  4017. space or parenthesis. This rule resolves the ambiguous case where, for
  4018. example, the sequence of characters ``@t{#\ space}''
  4019. could be taken to be either a representation of the space character or a
  4020. representation of the character ``@t{#\ s}'' followed
  4021. by a representation of the symbol ``@t{pace}.''
  4022. @ignore todo
  4023. Fix
  4024. @end ignore
  4025. Characters written in the #\ notation are self-evaluating.
  4026. That is, they do not have to be quoted in programs.
  4027. @c The \sharpsign\backwhack{}
  4028. @c notation is not an essential part of Scheme, however. Even implementations
  4029. @c that support the \sharpsign\backwhack{} notation for input do not have to
  4030. @c support it for output.
  4031. Some of the procedures that operate on characters ignore the
  4032. difference between upper case and lower case. The procedures that
  4033. ignore case have @w{``@t{-ci}''} (for ``case
  4034. insensitive'') embedded in their names.
  4035. @deffn {procedure} char? obj
  4036. Returns @t{#t} if @var{obj} is a character, otherwise returns @t{#f}.
  4037. @end deffn
  4038. @deffn {procedure} char=? char1 char2
  4039. @deffnx {procedure} char<? char1 char2
  4040. @deffnx {procedure} char>? char1 char2
  4041. @deffnx {procedure} char<=? char1 char2
  4042. @deffnx {procedure} char>=? char1 char2
  4043. @ignore nodomain
  4044. Both @var{char1} and @var{char2} must be characters.
  4045. @end ignore
  4046. These procedures impose a total ordering on the set of characters. It
  4047. is guaranteed that under this ordering:
  4048. @itemize @bullet
  4049. @item
  4050. The upper case characters are in order. For example, @samp{(char<? #\A #\B)} returns @t{#t}.
  4051. @item
  4052. The lower case characters are in order. For example, @samp{(char<? #\a #\b)} returns @t{#t}.
  4053. @item
  4054. The digits are in order. For example, @samp{(char<? #\0 #\9)} returns @t{#t}.
  4055. @item
  4056. Either all the digits precede all the upper case letters, or vice versa.
  4057. @item
  4058. Either all the digits precede all the lower case letters, or vice versa.
  4059. @end itemize
  4060. Some implementations may generalize these procedures to take more than
  4061. two arguments, as with the corresponding numerical predicates.
  4062. @end deffn
  4063. @deffn {library procedure} char-ci=? char1 char2
  4064. @deffnx {library procedure} char-ci<? char1 char2
  4065. @deffnx {library procedure} char-ci>? char1 char2
  4066. @deffnx {library procedure} char-ci<=? char1 char2
  4067. @deffnx {library procedure} char-ci>=? char1 char2
  4068. @ignore nodomain
  4069. Both @var{char1} and @var{char2} must be characters.
  4070. @end ignore
  4071. These procedures are similar to @samp{char=?} et cetera, but they treat
  4072. upper case and lower case letters as the same. For example, @samp{(char-ci=? #\A #\a)} returns @t{#t}. Some
  4073. implementations may generalize these procedures to take more than two
  4074. arguments, as with the corresponding numerical predicates.
  4075. @end deffn
  4076. @deffn {library procedure} char-alphabetic? char
  4077. @deffnx {library procedure} char-numeric? char
  4078. @deffnx {library procedure} char-whitespace? char
  4079. @deffnx {library procedure} char-upper-case? letter
  4080. @deffnx {library procedure} char-lower-case? letter
  4081. These procedures return @t{#t} if their arguments are alphabetic,
  4082. numeric, whitespace, upper case, or lower case characters, respectively,
  4083. otherwise they return @t{#f}. The following remarks, which are specific to
  4084. the ASCII character set, are intended only as a guide: The alphabetic characters
  4085. are the 52 upper and lower case letters. The numeric characters are the
  4086. ten decimal digits. The whitespace characters are space, tab, line
  4087. feed, form feed, and carriage return.
  4088. @end deffn
  4089. @c %R4%%\begin{entry}{%
  4090. @c \proto{char-upper-case?}{ letter}{procedure}
  4091. @c \proto{char-lower-case?}{ letter}{procedure}}
  4092. @c \domain{\var{Letter} must be an alphabetic character.}
  4093. @c These procedures return \schtrue{} if their arguments are upper case or
  4094. @c lower case characters, respectively, otherwise they return \schfalse.
  4095. @c \end{entry}
  4096. @deffn {procedure} char->integer char
  4097. @deffnx {procedure} integer->char @var{n}
  4098. Given a character, @samp{char->integer} returns an exact integer
  4099. representation of the character. Given an exact integer that is the image of
  4100. a character under @samp{char->integer}, @samp{integer->char}
  4101. returns that character. These procedures implement order-preserving isomorphisms
  4102. between the set of characters under the @code{char<=?} ordering and some
  4103. @vindex @w{char<=?}
  4104. subset of the integers under the @samp{<=} ordering. That is, if
  4105. @format
  4106. @t{(char<=? @var{a} @var{b}) @result{} #t @r{}and (<= @var{x} @var{y}) @result{} #t
  4107. }
  4108. @end format
  4109. @noindent
  4110. and @var{x} and @var{y} are in the domain of
  4111. @samp{integer->char}, then
  4112. @format
  4113. @t{(<= (char->integer @var{a})
  4114. (char->integer @var{b})) ==> #t
  4115. (char<=? (integer->char @var{x})
  4116. (integer->char @var{y})) ==> #t
  4117. }
  4118. @end format
  4119. @end deffn
  4120. @deffn {library procedure} char-upcase char
  4121. @deffnx {library procedure} char-downcase char
  4122. @ignore nodomain
  4123. @var{Char} must be a character.
  4124. @end ignore
  4125. These procedures return a character @var{char2} such that @samp{(char-ci=? @var{char} @var{char2})}. In addition, if @var{char} is
  4126. alphabetic, then the result of @samp{char-upcase} is upper case and the
  4127. result of @samp{char-downcase} is lower case.
  4128. @end deffn
  4129. @node Strings, Vectors, Characters, Other data types
  4130. @subsection Strings
  4131. Strings are sequences of characters.
  4132. @c In some implementations of Scheme
  4133. @c they are immutable; other implementations provide destructive procedures
  4134. @c such as {\cf string-set!}\ that alter string objects.
  4135. Strings are written as sequences of characters enclosed within doublequotes
  4136. (@samp{"}). A doublequote can be written inside a string only by escaping
  4137. it with a backslash (\), as in
  4138. @example
  4139. "The word \"recursion\" has many meanings."
  4140. @end example
  4141. A backslash can be written inside a string only by escaping it with another
  4142. backslash. Scheme does not specify the effect of a backslash within a
  4143. string that is not followed by a doublequote or backslash.
  4144. A string constant may continue from one line to the next, but
  4145. the exact contents of such a string are unspecified.
  4146. @c this is
  4147. @c usually a bad idea because
  4148. @c the exact effect may vary from one computer
  4149. @c system to another.
  4150. The @emph{length} of a string is the number of characters that it
  4151. contains. This number is an exact, non-negative integer that is fixed when the
  4152. string is created. The @dfn{valid indexes} of a string are the
  4153. @cindex @w{valid indexes}
  4154. exact non-negative integers less than the length of the string. The first
  4155. character of a string has index 0, the second has index 1, and so on.
  4156. In phrases such as ``the characters of @var{string} beginning with
  4157. index @var{start} and ending with index @var{end},'' it is understood
  4158. that the index @var{start} is inclusive and the index @var{end} is
  4159. exclusive. Thus if @var{start} and @var{end} are the same index, a null
  4160. substring is referred to, and if @var{start} is zero and @var{end} is
  4161. the length of @var{string}, then the entire string is referred to.
  4162. Some of the procedures that operate on strings ignore the
  4163. difference between upper and lower case. The versions that ignore case
  4164. have @w{``@samp{-ci}''} (for ``case insensitive'') embedded in their
  4165. names.
  4166. @deffn {procedure} string? obj
  4167. Returns @t{#t} if @var{obj} is a string, otherwise returns @t{#f}.
  4168. @end deffn
  4169. @deffn {procedure} make-string @var{k}
  4170. @deffnx {procedure} make-string @var{k} char
  4171. @c \domain{\vr{k} must be a non-negative integer, and \var{char} must be
  4172. @c a character.}
  4173. @samp{Make-string} returns a newly allocated string of
  4174. length @var{k}. If @var{char} is given, then all elements of the string
  4175. are initialized to @var{char}, otherwise the contents of the
  4176. @var{string} are unspecified.
  4177. @end deffn
  4178. @deffn {library procedure} string char @dots{},
  4179. Returns a newly allocated string composed of the arguments.
  4180. @end deffn
  4181. @deffn {procedure} string-length string
  4182. Returns the number of characters in the given @var{string}.
  4183. @end deffn
  4184. @deffn {procedure} string-ref string @var{k}
  4185. @var{k} must be a valid index of @var{string}.
  4186. @samp{String-ref} returns character @var{k} of @var{string} using zero-origin indexing.
  4187. @end deffn
  4188. @deffn {procedure} string-set! string k char
  4189. @c \var{String} must be a string,
  4190. @var{k} must be a valid index of @var{string}
  4191. @c , and \var{char} must be a character
  4192. .
  4193. @samp{String-set!} stores @var{char} in element @var{k} of @var{string}
  4194. and returns an unspecified value.
  4195. @c <!>
  4196. @format
  4197. @t{(define (f) (make-string 3 #\*))
  4198. (define (g) "***")
  4199. (string-set! (f) 0 #\?) ==> @emph{unspecified}
  4200. (string-set! (g) 0 #\?) ==> @emph{error}
  4201. (string-set! (symbol->string 'immutable)
  4202. 0
  4203. #\?) ==> @emph{error}
  4204. }
  4205. @end format
  4206. @end deffn
  4207. @deffn {library procedure} string=? string1 string2
  4208. @deffnx {library procedure} string-ci=? string1 string2
  4209. Returns @t{#t} if the two strings are the same length and contain the same
  4210. characters in the same positions, otherwise returns @t{#f}.
  4211. @samp{String-ci=?} treats
  4212. upper and lower case letters as though they were the same character, but
  4213. @samp{string=?} treats upper and lower case as distinct characters.
  4214. @end deffn
  4215. @deffn {library procedure} string<? string1 string2
  4216. @deffnx {library procedure} string>? string1 string2
  4217. @deffnx {library procedure} string<=? string1 string2
  4218. @deffnx {library procedure} string>=? string1 string2
  4219. @deffnx {library procedure} string-ci<? string1 string2
  4220. @deffnx {library procedure} string-ci>? string1 string2
  4221. @deffnx {library procedure} string-ci<=? string1 string2
  4222. @deffnx {library procedure} string-ci>=? string1 string2
  4223. These procedures are the lexicographic extensions to strings of the
  4224. corresponding orderings on characters. For example, @samp{string<?} is
  4225. the lexicographic ordering on strings induced by the ordering
  4226. @samp{char<?} on characters. If two strings differ in length but
  4227. are the same up to the length of the shorter string, the shorter string
  4228. is considered to be lexicographically less than the longer string.
  4229. Implementations may generalize these and the @samp{string=?} and
  4230. @samp{string-ci=?} procedures to take more than two arguments, as with
  4231. the corresponding numerical predicates.
  4232. @end deffn
  4233. @deffn {library procedure} substring string start end
  4234. @var{String} must be a string, and @var{start} and @var{end}
  4235. must be exact integers satisfying
  4236. @center 0 <= @var{start} <= @var{end} <= @w{@t{(string-length @var{string})@r{.}}}
  4237. @samp{Substring} returns a newly allocated string formed from the characters of
  4238. @var{string} beginning with index @var{start} (inclusive) and ending with index
  4239. @var{end} (exclusive).
  4240. @end deffn
  4241. @deffn {library procedure} string-append @var{string} @dots{},
  4242. Returns a newly allocated string whose characters form the concatenation of the
  4243. given strings.
  4244. @end deffn
  4245. @deffn {library procedure} string->list string
  4246. @deffnx {library procedure} list->string list
  4247. @samp{String->list} returns a newly allocated list of the
  4248. characters that make up the given string. @samp{List->string}
  4249. returns a newly allocated string formed from the characters in the list
  4250. @var{list}, which must be a list of characters. @samp{String->list}
  4251. and @samp{list->string} are
  4252. inverses so far as @samp{equal?} is concerned.
  4253. @c Implementations that provide
  4254. @c destructive operations on strings should ensure that the result of
  4255. @c {\cf list\coerce{}string} is newly allocated.
  4256. @end deffn
  4257. @deffn {library procedure} string-copy string
  4258. Returns a newly allocated copy of the given @var{string}.
  4259. @end deffn
  4260. @deffn {library procedure} string-fill! string char
  4261. Stores @var{char} in every element of the given @var{string} and returns an
  4262. unspecified value.
  4263. @c <!>
  4264. @end deffn
  4265. @node Vectors, , Strings, Other data types
  4266. @subsection Vectors
  4267. Vectors are heterogenous structures whose elements are indexed
  4268. by integers. A vector typically occupies less space than a list
  4269. of the same length, and the average time required to access a randomly
  4270. chosen element is typically less for the vector than for the list.
  4271. The @emph{length} of a vector is the number of elements that it
  4272. contains. This number is a non-negative integer that is fixed when the
  4273. vector is created. The @emph{valid indexes} of a
  4274. @cindex @w{valid indexes}
  4275. vector are the exact non-negative integers less than the length of the
  4276. vector. The first element in a vector is indexed by zero, and the last
  4277. element is indexed by one less than the length of the vector.
  4278. Vectors are written using the notation @t{#(@var{obj} @dots{},)}.
  4279. For example, a vector of length 3 containing the number zero in element
  4280. 0, the list @samp{(2 2 2 2)} in element 1, and the string @samp{"Anna"} in
  4281. element 2 can be written as following:
  4282. @example
  4283. #(0 (2 2 2 2) "Anna")
  4284. @end example
  4285. Note that this is the external representation of a vector, not an
  4286. expression evaluating to a vector. Like list constants, vector
  4287. constants must be quoted:
  4288. @example
  4289. '#(0 (2 2 2 2) "Anna")
  4290. ==> #(0 (2 2 2 2) "Anna")
  4291. @end example
  4292. @ignore todo
  4293. Pitman sez: The visual similarity to lists is bound to be confusing
  4294. to some. Elaborate on the distinction.
  4295. @end ignore
  4296. @deffn {procedure} vector? obj
  4297. Returns @t{#t} if @var{obj} is a vector, otherwise returns @t{#f}.
  4298. @end deffn
  4299. @deffn {procedure} make-vector k
  4300. @deffnx {procedure} make-vector k fill
  4301. Returns a newly allocated vector of @var{k} elements. If a second
  4302. argument is given, then each element is initialized to @var{fill}.
  4303. Otherwise the initial contents of each element is unspecified.
  4304. @end deffn
  4305. @deffn {library procedure} vector obj @dots{},
  4306. Returns a newly allocated vector whose elements contain the given
  4307. arguments. Analogous to @samp{list}.
  4308. @format
  4309. @t{(vector 'a 'b 'c) ==> #(a b c)
  4310. }
  4311. @end format
  4312. @end deffn
  4313. @deffn {procedure} vector-length vector
  4314. Returns the number of elements in @var{vector} as an exact integer.
  4315. @end deffn
  4316. @deffn {procedure} vector-ref vector k
  4317. @var{k} must be a valid index of @var{vector}.
  4318. @samp{Vector-ref} returns the contents of element @var{k} of
  4319. @var{vector}.
  4320. @format
  4321. @t{(vector-ref '#(1 1 2 3 5 8 13 21)
  4322. 5)
  4323. ==> 8
  4324. (vector-ref '#(1 1 2 3 5 8 13 21)
  4325. (let ((i (round (* 2 (acos -1)))))
  4326. (if (inexact? i)
  4327. (inexact->exact i)
  4328. i)))
  4329. ==> 13
  4330. }
  4331. @end format
  4332. @end deffn
  4333. @deffn {procedure} vector-set! vector k obj
  4334. @var{k} must be a valid index of @var{vector}.
  4335. @samp{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.
  4336. The value returned by @samp{vector-set!} is unspecified.
  4337. @c <!>
  4338. @format
  4339. @t{(let ((vec (vector 0 '(2 2 2 2) "Anna")))
  4340. (vector-set! vec 1 '("Sue" "Sue"))
  4341. vec)
  4342. ==> #(0 ("Sue" "Sue") "Anna")
  4343. (vector-set! '#(0 1 2) 1 "doe")
  4344. ==> @emph{error} ; constant vector
  4345. }
  4346. @end format
  4347. @end deffn
  4348. @deffn {library procedure} vector->list vector
  4349. @deffnx {library procedure} list->vector list
  4350. @samp{Vector->list} returns a newly allocated list of the objects contained
  4351. in the elements of @var{vector}. @samp{List->vector} returns a newly
  4352. created vector initialized to the elements of the list @var{list}.
  4353. @format
  4354. @t{(vector->list '#(dah dah didah))
  4355. ==> (dah dah didah)
  4356. (list->vector '(dididit dah))
  4357. ==> #(dididit dah)
  4358. }
  4359. @end format
  4360. @end deffn
  4361. @deffn {library procedure} vector-fill! vector fill
  4362. Stores @var{fill} in every element of @var{vector}.
  4363. The value returned by @samp{vector-fill!} is unspecified.
  4364. @c <!>
  4365. @end deffn
  4366. @node Control features, Eval, Other data types, Standard procedures
  4367. @section Control features
  4368. @c Intro flushed; not very a propos any more.
  4369. @c Procedures should be discussed somewhere, however.
  4370. This chapter describes various primitive procedures which control the
  4371. flow of program execution in special ways.
  4372. The @samp{procedure?} predicate is also described here.
  4373. @ignore todo
  4374. @t{Procedure?} doesn't belong in a section with the name
  4375. ``control features.'' What to do?
  4376. @end ignore
  4377. @deffn {procedure} procedure? obj
  4378. Returns @t{#t} if @var{obj} is a procedure, otherwise returns @t{#f}.
  4379. @format
  4380. @t{(procedure? car) ==> #t
  4381. (procedure? 'car) ==> #f
  4382. (procedure? (lambda (x) (* x x)))
  4383. ==> #t
  4384. (procedure? '(lambda (x) (* x x)))
  4385. ==> #f
  4386. (call-with-current-continuation procedure?)
  4387. ==> #t
  4388. }
  4389. @end format
  4390. @end deffn
  4391. @deffn {procedure} apply proc arg1 @dots{} args
  4392. @var{Proc} must be a procedure and @var{args} must be a list.
  4393. Calls @var{proc} with the elements of the list
  4394. @samp{(append (list @var{arg1} @dots{},) @var{args})} as the actual
  4395. arguments.
  4396. @format
  4397. @t{(apply + (list 3 4)) ==> 7
  4398. (define compose
  4399. (lambda (f g)
  4400. (lambda args
  4401. (f (apply g args)))))
  4402. ((compose sqrt *) 12 75) ==> 30
  4403. }
  4404. @end format
  4405. @end deffn
  4406. @deffn {library procedure} map proc list1 list2 @dots{},
  4407. The @var{list}s must be lists, and @var{proc} must be a
  4408. procedure taking as many arguments as there are @i{list}s
  4409. and returning a single value. If more
  4410. than one @var{list} is given, then they must all be the same length.
  4411. @samp{Map} applies @var{proc} element-wise to the elements of the
  4412. @var{list}s and returns a list of the results, in order.
  4413. The dynamic order in which @var{proc} is applied to the elements of the
  4414. @var{list}s is unspecified.
  4415. @format
  4416. @t{(map cadr '((a b) (d e) (g h)))
  4417. ==> (b e h)
  4418. (map (lambda (n) (expt n n))
  4419. '(1 2 3 4 5))
  4420. ==> (1 4 27 256 3125)
  4421. (map + '(1 2 3) '(4 5 6)) ==> (5 7 9)
  4422. (let ((count 0))
  4423. (map (lambda (ignored)
  4424. (set! count (+ count 1))
  4425. count)
  4426. '(a b))) ==> (1 2) @var{or} (2 1)
  4427. }
  4428. @end format
  4429. @end deffn
  4430. @deffn {library procedure} for-each proc list1 list2 @dots{},
  4431. The arguments to @samp{for-each} are like the arguments to @samp{map}, but
  4432. @samp{for-each} calls @var{proc} for its side effects rather than for its
  4433. values. Unlike @samp{map}, @samp{for-each} is guaranteed to call @var{proc} on
  4434. the elements of the @var{list}s in order from the first element(s) to the
  4435. last, and the value returned by @samp{for-each} is unspecified.
  4436. @format
  4437. @t{(let ((v (make-vector 5)))
  4438. (for-each (lambda (i)
  4439. (vector-set! v i (* i i)))
  4440. '(0 1 2 3 4))
  4441. v) ==> #(0 1 4 9 16)
  4442. }
  4443. @end format
  4444. @end deffn
  4445. @deffn {library procedure} force promise
  4446. Forces the value of @var{promise} (see @code{delay},
  4447. @vindex @w{delay}
  4448. section @pxref{Delayed evaluation}). If no value has been computed for
  4449. @cindex @w{promise}
  4450. the promise, then a value is computed and returned. The value of the
  4451. promise is cached (or ``memoized'') so that if it is forced a second
  4452. time, the previously computed value is returned.
  4453. @c without any recomputation.
  4454. @c [As pointed out by Marc Feeley, the "without any recomputation"
  4455. @c isn't necessarily true. --Will]
  4456. @format
  4457. @t{(force (delay (+ 1 2))) ==> 3
  4458. (let ((p (delay (+ 1 2))))
  4459. (list (force p) (force p)))
  4460. ==> (3 3)
  4461. (define a-stream
  4462. (letrec ((next
  4463. (lambda (n)
  4464. (cons n (delay (next (+ n 1)))))))
  4465. (next 0)))
  4466. (define head car)
  4467. (define tail
  4468. (lambda (stream) (force (cdr stream))))
  4469. (head (tail (tail a-stream)))
  4470. ==> 2
  4471. }
  4472. @end format
  4473. @samp{Force} and @samp{delay} are mainly intended for programs written in
  4474. functional style. The following examples should not be considered to
  4475. illustrate good programming style, but they illustrate the property that
  4476. only one value is computed for a promise, no matter how many times it is
  4477. forced.
  4478. @c the value of a promise is computed at most once.
  4479. @c [As pointed out by Marc Feeley, it may be computed more than once,
  4480. @c but as I observed we can at least insist that only one value be
  4481. @c used! -- Will]
  4482. @format
  4483. @t{(define count 0)
  4484. (define p
  4485. (delay (begin (set! count (+ count 1))
  4486. (if (> count x)
  4487. count
  4488. (force p)))))
  4489. (define x 5)
  4490. p ==> @i{}a promise
  4491. (force p) ==> 6
  4492. p ==> @i{}a promise, still
  4493. (begin (set! x 10)
  4494. (force p)) ==> 6
  4495. }
  4496. @end format
  4497. Here is a possible implementation of @samp{delay} and @samp{force}.
  4498. Promises are implemented here as procedures of no arguments,
  4499. and @samp{force} simply calls its argument:
  4500. @format
  4501. @t{(define force
  4502. (lambda (object)
  4503. (object)))
  4504. }
  4505. @end format
  4506. We define the expression
  4507. @format
  4508. @t{(delay @r{<expression>})
  4509. }
  4510. @end format
  4511. to have the same meaning as the procedure call
  4512. @format
  4513. @t{(make-promise (lambda () @r{<expression>}))@r{}
  4514. }
  4515. @end format
  4516. as follows
  4517. @format
  4518. @t{(define-syntax delay
  4519. (syntax-rules ()
  4520. ((delay expression)
  4521. (make-promise (lambda () expression))))),
  4522. }
  4523. @end format
  4524. where @samp{make-promise} is defined as follows:
  4525. @c \begin{scheme}
  4526. @c (define make-promise
  4527. @c (lambda (proc)
  4528. @c (let ((already-run? \schfalse) (result \schfalse))
  4529. @c (lambda ()
  4530. @c (cond ((not already-run?)
  4531. @c (set! result (proc))
  4532. @c (set! already-run? \schtrue)))
  4533. @c result))))%
  4534. @c \end{scheme}
  4535. @format
  4536. @t{(define make-promise
  4537. (lambda (proc)
  4538. (let ((result-ready? #f)
  4539. (result #f))
  4540. (lambda ()
  4541. (if result-ready?
  4542. result
  4543. (let ((x (proc)))
  4544. (if result-ready?
  4545. result
  4546. (begin (set! result-ready? #t)
  4547. (set! result x)
  4548. result))))))))
  4549. }
  4550. @end format
  4551. @quotation
  4552. @emph{Rationale:}
  4553. A promise may refer to its own value, as in the last example above.
  4554. Forcing such a promise may cause the promise to be forced a second time
  4555. before the value of the first force has been computed.
  4556. This complicates the definition of @samp{make-promise}.
  4557. @end quotation
  4558. Various extensions to this semantics of @samp{delay} and @samp{force}
  4559. are supported in some implementations:
  4560. @itemize @bullet
  4561. @item
  4562. Calling @samp{force} on an object that is not a promise may simply
  4563. return the object.
  4564. @item
  4565. It may be the case that there is no means by which a promise can be
  4566. operationally distinguished from its forced value. That is, expressions
  4567. like the following may evaluate to either @t{#t} or to @t{#f},
  4568. depending on the implementation:
  4569. @format
  4570. @t{(eqv? (delay 1) 1) ==> @emph{unspecified}
  4571. (pair? (delay (cons 1 2))) ==> @emph{unspecified}
  4572. }
  4573. @end format
  4574. @item
  4575. Some implementations may implement ``implicit forcing,'' where
  4576. the value of a promise is forced by primitive procedures like @samp{cdr}
  4577. and @samp{+}:
  4578. @format
  4579. @t{(+ (delay (* 3 7)) 13) ==> 34
  4580. }
  4581. @end format
  4582. @end itemize
  4583. @end deffn
  4584. @deffn {procedure} call-with-current-continuation proc
  4585. @var{Proc} must be a procedure of one
  4586. argument. The procedure @samp{call-with-current-continuation} packages
  4587. up the current continuation (see the rationale below) as an ``escape
  4588. procedure'' and passes it as an argument to
  4589. @cindex @w{escape procedure}
  4590. @var{proc}. The escape procedure is a Scheme procedure that, if it is
  4591. later called, will abandon whatever continuation is in effect at that later
  4592. time and will instead use the continuation that was in effect
  4593. when the escape procedure was created. Calling the escape procedure
  4594. may cause the invocation of @var{before} and @var{after} thunks installed using
  4595. @code{dynamic-wind}.
  4596. @vindex @w{dynamic-wind}
  4597. The escape procedure accepts the same number of arguments as the continuation to
  4598. the original call to @t{call-with-current-continuation}.
  4599. Except for continuations created by the @samp{call-with-values}
  4600. procedure, all continuations take exactly one value. The
  4601. effect of passing no value or more than one value to continuations
  4602. that were not created by @t{call-with-values} is unspecified.
  4603. The escape procedure that is passed to @var{proc} has
  4604. unlimited extent just like any other procedure in Scheme. It may be stored
  4605. in variables or data structures and may be called as many times as desired.
  4606. The following examples show only the most common ways in which
  4607. @samp{call-with-current-continuation} is used. If all real uses were as
  4608. simple as these examples, there would be no need for a procedure with
  4609. the power of @samp{call-with-current-continuation}.
  4610. @format
  4611. @t{(call-with-current-continuation
  4612. (lambda (exit)
  4613. (for-each (lambda (x)
  4614. (if (negative? x)
  4615. (exit x)))
  4616. '(54 0 37 -3 245 19))
  4617. #t)) ==> -3
  4618. (define list-length
  4619. (lambda (obj)
  4620. (call-with-current-continuation
  4621. (lambda (return)
  4622. (letrec ((r
  4623. (lambda (obj)
  4624. (cond ((null? obj) 0)
  4625. ((pair? obj)
  4626. (+ (r (cdr obj)) 1))
  4627. (else (return #f))))))
  4628. (r obj))))))
  4629. (list-length '(1 2 3 4)) ==> 4
  4630. (list-length '(a b . c)) ==> #f
  4631. }
  4632. @end format
  4633. @quotation
  4634. @emph{Rationale:}
  4635. A common use of @samp{call-with-current-continuation} is for
  4636. structured, non-local exits from loops or procedure bodies, but in fact
  4637. @samp{call-with-current-continuation} is extremely useful for implementing a
  4638. wide variety of advanced control structures.
  4639. Whenever a Scheme expression is evaluated there is a
  4640. @dfn{continuation} wanting the result of the expression. The continuation
  4641. @cindex @w{continuation}
  4642. represents an entire (default) future for the computation. If the expression is
  4643. evaluated at top level, for example, then the continuation might take the
  4644. result, print it on the screen, prompt for the next input, evaluate it, and
  4645. so on forever. Most of the time the continuation includes actions
  4646. specified by user code, as in a continuation that will take the result,
  4647. multiply it by the value stored in a local variable, add seven, and give
  4648. the answer to the top level continuation to be printed. Normally these
  4649. ubiquitous continuations are hidden behind the scenes and programmers do not
  4650. think much about them. On rare occasions, however, a programmer may
  4651. need to deal with continuations explicitly.
  4652. @samp{Call-with-current-continuation} allows Scheme programmers to do
  4653. that by creating a procedure that acts just like the current
  4654. continuation.
  4655. Most programming languages incorporate one or more special-purpose
  4656. escape constructs with names like @t{exit}, @w{@samp{return}}, or
  4657. even @t{goto}. In 1965, however, Peter Landin [Landin65]
  4658. invented a general purpose escape operator called the J-operator. John
  4659. Reynolds [Reynolds72] described a simpler but equally powerful
  4660. construct in 1972. The @samp{catch} special form described by Sussman
  4661. and Steele in the 1975 report on Scheme is exactly the same as
  4662. Reynolds's construct, though its name came from a less general construct
  4663. in MacLisp. Several Scheme implementors noticed that the full power of the
  4664. @code{catch} construct could be provided by a procedure instead of by a
  4665. @vindex @w{catch}
  4666. special syntactic construct, and the name
  4667. @samp{call-with-current-continuation} was coined in 1982. This name is
  4668. descriptive, but opinions differ on the merits of such a long name, and
  4669. some people use the name @code{call/cc} instead.
  4670. @vindex @w{call/cc}
  4671. @end quotation
  4672. @end deffn
  4673. @deffn {procedure} values obj @dots{}
  4674. Delivers all of its arguments to its continuation.
  4675. Except for continuations created by the @code{call-with-values}
  4676. @vindex @w{call-with-values}
  4677. procedure, all continuations take exactly one value.
  4678. @t{Values} might be defined as follows:
  4679. @format
  4680. @t{(define (values . things)
  4681. (call-with-current-continuation
  4682. (lambda (cont) (apply cont things))))
  4683. }
  4684. @end format
  4685. @end deffn
  4686. @deffn {procedure} call-with-values producer consumer
  4687. Calls its @var{producer} argument with no values and
  4688. a continuation that, when passed some values, calls the
  4689. @var{consumer} procedure with those values as arguments.
  4690. The continuation for the call to @var{consumer} is the
  4691. continuation of the call to @t{call-with-values}.
  4692. @format
  4693. @t{(call-with-values (lambda () (values 4 5))
  4694. (lambda (a b) b))
  4695. ==> 5
  4696. (call-with-values * -) ==> -1
  4697. }
  4698. @end format
  4699. @end deffn
  4700. @deffn {procedure} dynamic-wind before thunk after
  4701. Calls @var{thunk} without arguments, returning the result(s) of this call.
  4702. @var{Before} and @var{after} are called, also without arguments, as required
  4703. by the following rules (note that in the absence of calls to continuations
  4704. captured using @code{call-with-current-continuation} the three arguments are
  4705. @vindex @w{call-with-current-continuation}
  4706. called once each, in order). @var{Before} is called whenever execution
  4707. enters the dynamic extent of the call to @var{thunk} and @var{after} is called
  4708. whenever it exits that dynamic extent. The dynamic extent of a procedure
  4709. call is the period between when the call is initiated and when it
  4710. returns. In Scheme, because of @samp{call-with-current-continuation}, the
  4711. dynamic extent of a call may not be a single, connected time period.
  4712. It is defined as follows:
  4713. @itemize @bullet
  4714. @item
  4715. The dynamic extent is entered when execution of the body of the
  4716. called procedure begins.
  4717. @item
  4718. The dynamic extent is also entered when execution is not within
  4719. the dynamic extent and a continuation is invoked that was captured
  4720. (using @samp{call-with-current-continuation}) during the dynamic extent.
  4721. @item
  4722. It is exited when the called procedure returns.
  4723. @item
  4724. It is also exited when execution is within the dynamic extent and
  4725. a continuation is invoked that was captured while not within the
  4726. dynamic extent.
  4727. @end itemize
  4728. If a second call to @samp{dynamic-wind} occurs within the dynamic extent of the
  4729. call to @var{thunk} and then a continuation is invoked in such a way that the
  4730. @var{after}s from these two invocations of @samp{dynamic-wind} are both to be
  4731. called, then the @var{after} associated with the second (inner) call to
  4732. @samp{dynamic-wind} is called first.
  4733. If a second call to @samp{dynamic-wind} occurs within the dynamic extent of the
  4734. call to @var{thunk} and then a continuation is invoked in such a way that the
  4735. @var{before}s from these two invocations of @samp{dynamic-wind} are both to be
  4736. called, then the @var{before} associated with the first (outer) call to
  4737. @samp{dynamic-wind} is called first.
  4738. If invoking a continuation requires calling the @var{before} from one call
  4739. to @samp{dynamic-wind} and the @var{after} from another, then the @var{after}
  4740. is called first.
  4741. The effect of using a captured continuation to enter or exit the dynamic
  4742. extent of a call to @var{before} or @var{after} is undefined.
  4743. @format
  4744. @t{(let ((path '())
  4745. (c #f))
  4746. (let ((add (lambda (s)
  4747. (set! path (cons s path)))))
  4748. (dynamic-wind
  4749. (lambda () (add 'connect))
  4750. (lambda ()
  4751. (add (call-with-current-continuation
  4752. (lambda (c0)
  4753. (set! c c0)
  4754. 'talk1))))
  4755. (lambda () (add 'disconnect)))
  4756. (if (< (length path) 4)
  4757. (c 'talk2)
  4758. (reverse path))))
  4759. ==> (connect talk1 disconnect
  4760. connect talk2 disconnect)
  4761. }
  4762. @end format
  4763. @end deffn
  4764. @node Eval, Input and output, Control features, Standard procedures
  4765. @section Eval
  4766. @deffn {procedure} eval expression environment-specifier
  4767. Evaluates @var{expression} in the specified environment and returns its value.
  4768. @var{Expression} must be a valid Scheme expression represented as data,
  4769. and @var{environment-specifier} must be a value returned by one of the
  4770. three procedures described below.
  4771. Implementations may extend @samp{eval} to allow non-expression programs
  4772. (definitions) as the first argument and to allow other
  4773. values as environments, with the restriction that @samp{eval} is not
  4774. allowed to create new bindings in the environments associated with
  4775. @samp{null-environment} or @samp{scheme-report-environment}.
  4776. @format
  4777. @t{(eval '(* 7 3) (scheme-report-environment 5))
  4778. ==> 21
  4779. (let ((f (eval '(lambda (f x) (f x x))
  4780. (null-environment 5))))
  4781. (f + 10))
  4782. ==> 20
  4783. }
  4784. @end format
  4785. @end deffn
  4786. @deffn {procedure} scheme-report-environment version
  4787. @deffnx {procedure} null-environment version
  4788. @var{Version} must be the exact integer @samp{5},
  4789. corresponding to this revision of the Scheme report (the
  4790. Revised^5 Report on Scheme).
  4791. @samp{Scheme-report-environment} returns a specifier for an
  4792. environment that is empty except for all bindings defined in
  4793. this report that are either required or both optional and
  4794. supported by the implementation. @samp{Null-environment} returns
  4795. a specifier for an environment that is empty except for the
  4796. (syntactic) bindings for all syntactic keywords defined in
  4797. this report that are either required or both optional and
  4798. supported by the implementation.
  4799. Other values of @var{version} can be used to specify environments
  4800. matching past revisions of this report, but their support is not
  4801. required. An implementation will signal an error if @var{version}
  4802. is neither @samp{5} nor another value supported by
  4803. the implementation.
  4804. The effect of assigning (through the use of @samp{eval}) a variable
  4805. bound in a @samp{scheme-report-environment}
  4806. (for example @samp{car}) is unspecified. Thus the environments specified
  4807. by @samp{scheme-report-environment} may be immutable.
  4808. @end deffn
  4809. @deffn {optional procedure} interaction-environment
  4810. This procedure returns a specifier for the environment that
  4811. contains imple@-men@-ta@-tion-defined bindings, typically a superset of
  4812. those listed in the report. The intent is that this procedure
  4813. will return the environment in which the implementation would evaluate
  4814. expressions dynamically typed by the user.
  4815. @end deffn
  4816. @node Input and output, , Eval, Standard procedures
  4817. @section Input and output
  4818. @menu
  4819. * Ports::
  4820. * Input::
  4821. * Output::
  4822. * System interface::
  4823. @end menu
  4824. @node Ports, Input, Input and output, Input and output
  4825. @subsection Ports
  4826. Ports represent input and output devices. To Scheme, an input port is a
  4827. Scheme object that can deliver characters upon command, while an output port
  4828. is a Scheme object that can accept characters.
  4829. @cindex @w{port}
  4830. @ignore todo
  4831. Haase: Mention that there are alternatives to files?
  4832. @end ignore
  4833. @deffn {library procedure} call-with-input-file string proc
  4834. @deffnx {library procedure} call-with-output-file string proc
  4835. @var{String} should be a string naming a file, and
  4836. @var{proc} should be a procedure that accepts one argument.
  4837. For @samp{call-with-input-file},
  4838. the file should already exist; for
  4839. @samp{call-with-output-file},
  4840. the effect is unspecified if the file
  4841. already exists. These procedures call @var{proc} with one argument: the
  4842. port obtained by opening the named file for input or output. If the
  4843. file cannot be opened, an error is signalled. If @var{proc} returns,
  4844. then the port is closed automatically and the value(s) yielded by the
  4845. @var{proc} is(are) returned. If @var{proc} does not return, then
  4846. the port will not be closed automatically unless it is possible to
  4847. prove that the port will never again be used for a read or write
  4848. operation.
  4849. @c Scheme
  4850. @c will not close the port unless it can prove that the port will never
  4851. @c again be used for a read or write operation.
  4852. @quotation
  4853. @emph{Rationale:}
  4854. Because Scheme's escape procedures have unlimited extent, it is
  4855. possible to escape from the current continuation but later to escape back in.
  4856. If implementations were permitted to close the port on any escape from the
  4857. current continuation, then it would be impossible to write portable code using
  4858. both @samp{call-with-current-continuation} and @samp{call-with-input-file} or
  4859. @samp{call-with-output-file}.
  4860. @ignore todo
  4861. Pitman wants more said here; maybe encourage users to call
  4862. @var{close-foo-port}; maybe talk about process switches (?).
  4863. @end ignore
  4864. @end quotation
  4865. @end deffn
  4866. @deffn {procedure} input-port? obj
  4867. @deffnx {procedure} output-port? obj
  4868. Returns @t{#t} if @var{obj} is an input port or output port
  4869. respectively, otherwise returns @t{#f}.
  4870. @ignore todo
  4871. Won't necessarily return true after port is closed.
  4872. @end ignore
  4873. @end deffn
  4874. @deffn {procedure} current-input-port
  4875. @deffnx {procedure} current-output-port
  4876. Returns the current default input or output port.
  4877. @end deffn
  4878. @deffn {optional procedure} with-input-from-file string thunk
  4879. @deffnx {optional procedure} with-output-to-file string thunk
  4880. @var{String} should be a string naming a file, and
  4881. @var{proc} should be a procedure of no arguments.
  4882. For @samp{with-input-from-file},
  4883. the file should already exist; for
  4884. @samp{with-output-to-file},
  4885. the effect is unspecified if the file
  4886. already exists.
  4887. The file is opened for input or output, an input or output port
  4888. connected to it is made the default value returned by
  4889. @samp{current-input-port} or @samp{current-output-port}
  4890. (and is used by @t{(read)}, @t{(write @var{obj})}, and so forth),
  4891. and the
  4892. @var{thunk} is called with no arguments. When the @var{thunk} returns,
  4893. the port is closed and the previous default is restored.
  4894. @samp{With-input-from-file} and @samp{with-output-to-file} return(s) the
  4895. value(s) yielded by @var{thunk}.
  4896. If an escape procedure
  4897. is used to escape from the continuation of these procedures, their
  4898. behavior is implementation dependent.
  4899. @ignore todo
  4900. OK this with authors??
  4901. @end ignore
  4902. @c current continuation changes in such a way
  4903. @c as to make it doubtful that the \var{thunk} will ever return.
  4904. @ignore todo
  4905. Freeman:
  4906. Throughout this section I wanted to see ``the value of @t{(current-input-port)}''
  4907. instead of ``the value returned by @var{current-input-port}''. (Same for
  4908. @var{current-output-port}.)
  4909. @end ignore
  4910. @end deffn
  4911. @deffn {procedure} open-input-file filename
  4912. Takes a string naming an existing file and returns an input port capable of
  4913. delivering characters from the file. If the file cannot be opened, an error is
  4914. signalled.
  4915. @end deffn
  4916. @deffn {procedure} open-output-file filename
  4917. Takes a string naming an output file to be created and returns an output
  4918. port capable of writing characters to a new file by that name. If the file
  4919. cannot be opened, an error is signalled. If a file with the given name
  4920. already exists, the effect is unspecified.
  4921. @end deffn
  4922. @deffn {procedure} close-input-port port
  4923. @deffnx {procedure} close-output-port port
  4924. Closes the file associated with @var{port}, rendering the @var{port}
  4925. incapable of delivering or accepting characters.
  4926. @ignore todo
  4927. But maybe a no-op
  4928. on some ports, e.g. terminals or editor buffers.
  4929. @end ignore
  4930. These routines have no effect if the file has already been closed.
  4931. The value returned is unspecified.
  4932. @ignore todo
  4933. Ramsdell: Some note is needed explaining why there are two
  4934. different close procedures.
  4935. @end ignore
  4936. @ignore todo
  4937. A port isn't necessarily still a port after it has been closed?
  4938. @end ignore
  4939. @end deffn
  4940. @node Input, Output, Ports, Input and output
  4941. @subsection Input
  4942. @noindent
  4943. @w{ }
  4944. @c ???
  4945. @sp 5
  4946. @ignore todo
  4947. The input routines have some things in common, maybe explain here.
  4948. @end ignore
  4949. @deffn {library procedure} read
  4950. @deffnx {library procedure} read port
  4951. @samp{Read} converts external representations of Scheme objects into the
  4952. objects themselves. That is, it is a parser for the nonterminal
  4953. <datum> (see sections @pxref{External representation} and
  4954. @pxref{Pairs and lists}). @samp{Read} returns the next
  4955. object parsable from the given input @var{port}, updating @var{port} to point to
  4956. the first character past the end of the external representation of the object.
  4957. If an end of file is encountered in the input before any
  4958. characters are found that can begin an object, then an end of file
  4959. object is returned.
  4960. @ignore todo
  4961. @end ignore
  4962. The port remains open, and further attempts
  4963. to read will also return an end of file object. If an end of file is
  4964. encountered after the beginning of an object's external representation,
  4965. but the external representation is incomplete and therefore not parsable,
  4966. an error is signalled.
  4967. The @var{port} argument may be omitted, in which case it defaults to the
  4968. value returned by @samp{current-input-port}. It is an error to read from
  4969. a closed port.
  4970. @end deffn
  4971. @deffn {procedure} read-char
  4972. @deffnx {procedure} read-char port
  4973. Returns the next character available from the input @var{port}, updating
  4974. the @var{port} to point to the following character. If no more characters
  4975. are available, an end of file object is returned. @var{Port} may be
  4976. omitted, in which case it defaults to the value returned by @samp{current-input-port}.
  4977. @end deffn
  4978. @deffn {procedure} peek-char
  4979. @deffnx {procedure} peek-char port
  4980. Returns the next character available from the input @var{port},
  4981. @emph{without} updating
  4982. the @var{port} to point to the following character. If no more characters
  4983. are available, an end of file object is returned. @var{Port} may be
  4984. omitted, in which case it defaults to the value returned by @samp{current-input-port}.
  4985. @quotation
  4986. @emph{Note:}
  4987. The value returned by a call to @samp{peek-char} is the same as the
  4988. value that would have been returned by a call to @samp{read-char} with the
  4989. same @var{port}. The only difference is that the very next call to
  4990. @samp{read-char} or @samp{peek-char} on that @var{port} will return the
  4991. value returned by the preceding call to @samp{peek-char}. In particular, a call
  4992. to @samp{peek-char} on an interactive port will hang waiting for input
  4993. whenever a call to @samp{read-char} would have hung.
  4994. @end quotation
  4995. @end deffn
  4996. @deffn {procedure} eof-object? obj
  4997. Returns @t{#t} if @var{obj} is an end of file object, otherwise returns
  4998. @t{#f}. The precise set of end of file objects will vary among
  4999. implementations, but in any case no end of file object will ever be an object
  5000. that can be read in using @samp{read}.
  5001. @end deffn
  5002. @deffn {procedure} char-ready?
  5003. @deffnx {procedure} char-ready? port
  5004. Returns @t{#t} if a character is ready on the input @var{port} and
  5005. returns @t{#f} otherwise. If @samp{char-ready} returns @t{#t} then
  5006. the next @samp{read-char} operation on the given @var{port} is guaranteed
  5007. not to hang. If the @var{port} is at end of file then @samp{char-ready?}
  5008. returns @t{#t}. @var{Port} may be omitted, in which case it defaults to
  5009. the value returned by @samp{current-input-port}.
  5010. @quotation
  5011. @emph{Rationale:}
  5012. @samp{Char-ready?} exists to make it possible for a program to
  5013. accept characters from interactive ports without getting stuck waiting for
  5014. input. Any input editors associated with such ports must ensure that
  5015. characters whose existence has been asserted by @samp{char-ready?} cannot
  5016. be rubbed out. If @samp{char-ready?} were to return @t{#f} at end of
  5017. file, a port at end of file would be indistinguishable from an interactive
  5018. port that has no ready characters.
  5019. @end quotation
  5020. @end deffn
  5021. @node Output, System interface, Input, Input and output
  5022. @subsection Output
  5023. @c We've got to put something here to fix the indentation!!
  5024. @noindent
  5025. @w{}
  5026. @sp 5
  5027. @deffn {library procedure} write obj
  5028. @deffnx {library procedure} write obj port
  5029. Writes a written representation of @var{obj} to the given @var{port}. Strings
  5030. that appear in the written representation are enclosed in doublequotes, and
  5031. within those strings backslash and doublequote characters are
  5032. escaped by backslashes.
  5033. Character objects are written using the @samp{#\} notation.
  5034. @samp{Write} returns an unspecified value. The
  5035. @var{port} argument may be omitted, in which case it defaults to the value
  5036. returned by @samp{current-output-port}.
  5037. @end deffn
  5038. @deffn {library procedure} display obj
  5039. @deffnx {library procedure} display obj port
  5040. Writes a representation of @var{obj} to the given @var{port}. Strings
  5041. that appear in the written representation are not enclosed in
  5042. doublequotes, and no characters are escaped within those strings. Character
  5043. objects appear in the representation as if written by @samp{write-char}
  5044. instead of by @samp{write}. @samp{Display} returns an unspecified value.
  5045. The @var{port} argument may be omitted, in which case it defaults to the
  5046. value returned by @samp{current-output-port}.
  5047. @quotation
  5048. @emph{Rationale:}
  5049. @samp{Write} is intended
  5050. for producing mach@-ine-readable output and @samp{display} is for producing
  5051. human-readable output. Implementations that allow ``slashification''
  5052. within symbols will probably want @samp{write} but not @samp{display} to
  5053. slashify funny characters in symbols.
  5054. @end quotation
  5055. @end deffn
  5056. @deffn {library procedure} newline
  5057. @deffnx {library procedure} newline port
  5058. Writes an end of line to @var{port}. Exactly how this is done differs
  5059. from one operating system to another. Returns an unspecified value.
  5060. The @var{port} argument may be omitted, in which case it defaults to the
  5061. value returned by @samp{current-output-port}.
  5062. @end deffn
  5063. @deffn {procedure} write-char char
  5064. @deffnx {procedure} write-char char port
  5065. Writes the character @var{char} (not an external representation of the
  5066. character) to the given @var{port} and returns an unspecified value. The
  5067. @var{port} argument may be omitted, in which case it defaults to the value
  5068. returned by @samp{current-output-port}.
  5069. @end deffn
  5070. @node System interface, , Output, Input and output
  5071. @subsection System interface
  5072. Questions of system interface generally fall outside of the domain of this
  5073. report. However, the following operations are important enough to
  5074. deserve description here.
  5075. @deffn {optional procedure} load filename
  5076. @ignore todo
  5077. Fix
  5078. @end ignore
  5079. @c \domain{\var{Filename} should be a string naming an existing file
  5080. @c containing Scheme source code.} The {\cf load} procedure reads
  5081. @var{Filename} should be a string naming an existing file
  5082. containing Scheme source code. The @samp{load} procedure reads
  5083. expressions and definitions from the file and evaluates them
  5084. sequentially. It is unspecified whether the results of the expressions
  5085. are printed. The @samp{load} procedure does not affect the values
  5086. returned by @samp{current-input-port} and @samp{current-output-port}.
  5087. @samp{Load} returns an unspecified value.
  5088. @quotation
  5089. @emph{Rationale:}
  5090. For portability, @samp{load} must operate on source files.
  5091. Its operation on other kinds of files necessarily varies among
  5092. implementations.
  5093. @end quotation
  5094. @end deffn
  5095. @deffn {optional procedure} transcript-on filename
  5096. @deffnx {optional procedure} transcript-off
  5097. @var{Filename} must be a string naming an output file to be
  5098. created. The effect of @samp{transcript-on} is to open the named file
  5099. for output, and to cause a transcript of subsequent interaction between
  5100. the user and the Scheme system to be written to the file. The
  5101. transcript is ended by a call to @samp{transcript-off}, which closes the
  5102. transcript file. Only one transcript may be in progress at any time,
  5103. though some implementations may relax this restriction. The values
  5104. returned by these procedures are unspecified.
  5105. @c \begin{note}
  5106. @c These procedures are redundant in some systems, but
  5107. @c systems that need them should provide them.
  5108. @c \end{note}
  5109. @end deffn
  5110. @page
  5111. @c @include{syn}
  5112. @node Formal syntax and semantics, Notes, Standard procedures, top
  5113. @chapter Formal syntax and semantics
  5114. @menu
  5115. * Formal syntax::
  5116. * Formal semantics::
  5117. * Derived expression type::
  5118. @end menu
  5119. This chapter provides formal descriptions of what has already been
  5120. described informally in previous chapters of this report.
  5121. @ignore todo
  5122. Allow grammar to say that else clause needn't be last?
  5123. @end ignore
  5124. @node Formal syntax, Formal semantics, Formal syntax and semantics, Formal syntax and semantics
  5125. @section Formal syntax
  5126. @menu
  5127. * Lexical structure::
  5128. * External representation::
  5129. * Expression::
  5130. * Quasiquotations::
  5131. * Transformers::
  5132. * Programs and definitions::
  5133. @end menu
  5134. This section provides a formal syntax for Scheme written in an extended
  5135. BNF.
  5136. All spaces in the grammar are for legibility. Case is insignificant;
  5137. for example, @samp{#x1A} and @samp{#X1a} are equivalent. <empty>
  5138. stands for the empty string.
  5139. The following extensions to BNF are used to make the description more
  5140. concise: <thing>* means zero or more occurrences of
  5141. <thing>; and <thing>+ means at least one
  5142. <thing>.
  5143. @node Lexical structure, External representation, Formal syntax, Formal syntax
  5144. @subsection Lexical structure
  5145. This section describes how individual tokens (identifiers,
  5146. @cindex @w{token}
  5147. numbers, etc.) are formed from sequences of characters. The following
  5148. sections describe how expressions and programs are formed from sequences
  5149. of tokens.
  5150. <Intertoken space> may occur on either side of any token, but not
  5151. within a token.
  5152. Tokens which require implicit termination (identifiers, numbers,
  5153. characters, and dot) may be terminated by any <delimiter>, but not
  5154. necessarily by anything else.
  5155. The following five characters are reserved for future extensions to the
  5156. language: @t{[ ] @{ @} |}
  5157. @format
  5158. @t{<token> --> <identifier> | <boolean> | <number>
  5159. @cindex @w{identifier}
  5160. | <character> | <string>
  5161. | ( | ) | #( | @t{'} | @t{`} | , | ,@@ | @b{.}
  5162. <delimiter> --> <whitespace> | ( | ) | " | ;
  5163. <whitespace> --> <space or newline>
  5164. <comment> --> ; <@r{all subsequent characters up to a}
  5165. @r{line break>}
  5166. @cindex @w{comment}
  5167. <atmosphere> --> <whitespace> | <comment>
  5168. <intertoken space> --> <atmosphere>*}
  5169. @end format
  5170. @c This is a kludge, but \multicolumn doesn't work in tabbing environments.
  5171. @format
  5172. @t{<identifier> --> <initial> <subsequent>*
  5173. | <peculiar identifier>
  5174. <initial> --> <letter> | <special initial>
  5175. <letter> --> a | b | c | ... | z
  5176. <special initial> --> ! | $ | % | & | * | / | : | < | =
  5177. | > | ? | ^ | _ | ~
  5178. <subsequent> --> <initial> | <digit>
  5179. | <special subsequent>
  5180. <digit> --> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
  5181. <special subsequent> --> + | - | .@: | @@
  5182. <peculiar identifier> --> + | - | ...
  5183. <syntactic keyword> --> <expression keyword>
  5184. @cindex @w{syntactic keyword}
  5185. @cindex @w{keyword}
  5186. | else | => | define
  5187. | unquote | unquote-splicing
  5188. <expression keyword> --> quote | lambda | if
  5189. | set! | begin | cond | and | or | case
  5190. | let | let* | letrec | do | delay
  5191. | quasiquote
  5192. @w{@samp{<variable> @result{} <}}@r{any <identifier> that isn't}
  5193. @cindex @w{variable}
  5194. @w{ @r{also a <syntactic keyword>>}}
  5195. <boolean> --> #t | #f
  5196. <character> --> #\ <any character>
  5197. | #\ <character name>
  5198. <character name> --> space | newline
  5199. <string> --> " <string element>* "
  5200. <string element> --> <any character other than " or \>
  5201. | \" | \\ }
  5202. @end format
  5203. @format
  5204. @t{<number> --> <num 2>| <num 8>
  5205. | <num 10>| <num 16>
  5206. }
  5207. @end format
  5208. The following rules for <num R>, <complex R>, <real
  5209. R>, <ureal R>, <uinteger R>, and <prefix R>
  5210. should be replicated for @w{R = 2, 8, 10,}
  5211. and 16. There are no rules for <decimal 2>, <decimal
  5212. 8>, and <decimal 16>, which means that numbers containing
  5213. decimal points or exponents must be in decimal radix.
  5214. @ignore todo
  5215. Mark Meyer and David Bartley want to fix this. (What? -- Will)
  5216. @end ignore
  5217. @format
  5218. @t{<num R> --> <prefix R> <complex R>
  5219. <complex R> --> <real R> | <real R> @@ <real R>
  5220. | <real R> + <ureal R> i | <real R> - <ureal R> i
  5221. | <real R> + i | <real R> - i
  5222. | + <ureal R> i | - <ureal R> i | + i | - i
  5223. <real R> --> <sign> <ureal R>
  5224. <ureal R> --> <uinteger R>
  5225. | <uinteger R> / <uinteger R>
  5226. | <decimal R>
  5227. <decimal 10> --> <uinteger 10> <suffix>
  5228. | . <digit 10>+ #* <suffix>
  5229. | <digit 10>+ . <digit 10>* #* <suffix>
  5230. | <digit 10>+ #+ . #* <suffix>
  5231. <uinteger R> --> <digit R>+ #*
  5232. <prefix R> --> <radix R> <exactness>
  5233. | <exactness> <radix R>
  5234. }
  5235. @end format
  5236. @format
  5237. @t{<suffix> --> <empty>
  5238. | <exponent marker> <sign> <digit 10>+
  5239. <exponent marker> --> e | s | f | d | l
  5240. <sign> --> <empty> | + | -
  5241. <exactness> --> <empty> | #i | #e
  5242. @vindex #e
  5243. @vindex #i
  5244. <radix 2> --> #b
  5245. @vindex #b
  5246. <radix 8> --> #o
  5247. @vindex #o
  5248. <radix 10> --> <empty> | #d
  5249. <radix 16> --> #x
  5250. @vindex #x
  5251. <digit 2> --> 0 | 1
  5252. <digit 8> --> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7
  5253. <digit 10> --> <digit>
  5254. <digit 16> --> <digit 10> | a | b | c | d | e | f }
  5255. @end format
  5256. @ignore todo
  5257. Mark Meyer of TI sez, shouldn't we allow @t{1e3/2}?
  5258. @end ignore
  5259. @node External representation, Expression, Lexical structure, Formal syntax
  5260. @subsection External representations
  5261. <Datum> is what the @code{read} procedure (section @pxref{Input})
  5262. @vindex @w{read}
  5263. successfully parses. Note that any string that parses as an
  5264. <ex@-pres@-sion> will also parse as a <datum>.
  5265. @format
  5266. @t{<datum> --> <simple datum> | <compound datum>
  5267. <simple datum> --> <boolean> | <number>
  5268. | <character> | <string> | <symbol>
  5269. <symbol> --> <identifier>
  5270. <compound datum> --> <list> | <vector>
  5271. <list> --> (<datum>*) | (<datum>+ .@: <datum>)
  5272. | <abbreviation>
  5273. <abbreviation> --> <abbrev prefix> <datum>
  5274. <abbrev prefix> --> ' | ` | , | ,@@
  5275. <vector> --> #(<datum>*) }
  5276. @end format
  5277. @node Expression, Quasiquotations, External representation, Formal syntax
  5278. @subsection Expressions
  5279. @format
  5280. @t{<expression> --> <variable>
  5281. | <literal>
  5282. | <procedure call>
  5283. | <lambda expression>
  5284. | <conditional>
  5285. | <assignment>
  5286. | <derived expression>
  5287. | <macro use>
  5288. | <macro block>
  5289. <literal> --> <quotation> | <self-evaluating>
  5290. <self-evaluating> --> <boolean> | <number>
  5291. | <character> | <string>
  5292. <quotation> --> '<datum> | (quote <datum>)
  5293. <procedure call> --> (<operator> <operand>*)
  5294. <operator> --> <expression>
  5295. <operand> --> <expression>
  5296. <lambda expression> --> (lambda <formals> <body>)
  5297. <formals> --> (<variable>*) | <variable>
  5298. | (<variable>+ .@: <variable>)
  5299. <body> --> <definition>* <sequence>
  5300. <sequence> --> <command>* <expression>
  5301. <command> --> <expression>
  5302. <conditional> --> (if <test> <consequent> <alternate>)
  5303. <test> --> <expression>
  5304. <consequent> --> <expression>
  5305. <alternate> --> <expression> | <empty>
  5306. <assignment> --> (set! <variable> <expression>)
  5307. <derived expression> -->
  5308. (cond <cond clause>+)
  5309. | (cond <cond clause>* (else <sequence>))
  5310. | (case <expression>
  5311. <case clause>+)
  5312. | (case <expression>
  5313. <case clause>*
  5314. (else <sequence>))
  5315. | (and <test>*)
  5316. | (or <test>*)
  5317. | (let (<binding spec>*) <body>)
  5318. | (let <variable> (<binding spec>*) <body>)
  5319. | (let* (<binding spec>*) <body>)
  5320. | (letrec (<binding spec>*) <body>)
  5321. | (begin <sequence>)
  5322. | (do (<iteration spec>*)
  5323. (<test> <do result>)
  5324. <command>*)
  5325. | (delay <expression>)
  5326. | <quasiquotation>
  5327. <cond clause> --> (<test> <sequence>)
  5328. | (<test>)
  5329. | (<test> => <recipient>)
  5330. <recipient> --> <expression>
  5331. <case clause> --> ((<datum>*) <sequence>)
  5332. <binding spec> --> (<variable> <expression>)
  5333. <iteration spec> --> (<variable> <init> <step>)
  5334. | (<variable> <init>)
  5335. <init> --> <expression>
  5336. <step> --> <expression>
  5337. <do result> --> <sequence> | <empty>
  5338. <macro use> --> (<keyword> <datum>*)
  5339. <keyword> --> <identifier>
  5340. <macro block> -->
  5341. (let-syntax (<syntax spec>*) <body>)
  5342. | (letrec-syntax (<syntax spec>*) <body>)
  5343. <syntax spec> --> (<keyword> <transformer spec>)
  5344. }
  5345. @end format
  5346. @node Quasiquotations, Transformers, Expression, Formal syntax
  5347. @subsection Quasiquotations
  5348. The following grammar for quasiquote expressions is not context-free.
  5349. It is presented as a recipe for generating an infinite number of
  5350. production rules. Imagine a copy of the following rules for D = 1, 2,3, @dots{}. D keeps track of the nesting depth.
  5351. @format
  5352. @t{<quasiquotation> --> <quasiquotation 1>
  5353. <qq template 0> --> <expression>
  5354. <quasiquotation D> --> `<qq template D>
  5355. | (quasiquote <qq template D>)
  5356. <qq template D> --> <simple datum>
  5357. | <list qq template D>
  5358. | <vector qq template D>
  5359. | <unquotation D>
  5360. <list qq template D> --> (<qq template or splice D>*)
  5361. | (<qq template or splice D>+ .@: <qq template D>)
  5362. | '<qq template D>
  5363. | <quasiquotation D+1>
  5364. <vector qq template D> --> #(<qq template or splice D>*)
  5365. <unquotation D> --> ,<qq template D-1>
  5366. | (unquote <qq template D-1>)
  5367. <qq template or splice D> --> <qq template D>
  5368. | <splicing unquotation D>
  5369. <splicing unquotation D> --> ,@@<qq template D-1>
  5370. | (unquote-splicing <qq template D-1>) }
  5371. @end format
  5372. In <quasiquotation>s, a <list qq template D> can sometimes
  5373. be confused with either an <un@-quota@-tion D> or a <splicing
  5374. un@-quo@-ta@-tion D>. The interpretation as an
  5375. <un@-quo@-ta@-tion> or <splicing
  5376. un@-quo@-ta@-tion D> takes precedence.
  5377. @node Transformers, Programs and definitions, Quasiquotations, Formal syntax
  5378. @subsection Transformers
  5379. @format
  5380. @t{<transformer spec> -->
  5381. (syntax-rules (<identifier>*) <syntax rule>*)
  5382. <syntax rule> --> (<pattern> <template>)
  5383. <pattern> --> <pattern identifier>
  5384. | (<pattern>*)
  5385. | (<pattern>+ . <pattern>)
  5386. | (<pattern>* <pattern> <ellipsis>)
  5387. | #(<pattern>*)
  5388. | #(<pattern>* <pattern> <ellipsis>)
  5389. | <pattern datum>
  5390. <pattern datum> --> <string>
  5391. | <character>
  5392. | <boolean>
  5393. | <number>
  5394. <template> --> <pattern identifier>
  5395. | (<template element>*)
  5396. | (<template element>+ . <template>)
  5397. | #(<template element>*)
  5398. | <template datum>
  5399. <template element> --> <template>
  5400. | <template> <ellipsis>
  5401. <template datum> --> <pattern datum>
  5402. <pattern identifier> --> <any identifier except @samp{...}>
  5403. <ellipsis> --> <the identifier @samp{...}>
  5404. }
  5405. @end format
  5406. @node Programs and definitions, , Transformers, Formal syntax
  5407. @subsection Programs and definitions
  5408. @format
  5409. @t{<program> --> <command or definition>*
  5410. <command or definition> --> <command>
  5411. | <definition>
  5412. | <syntax definition>
  5413. | (begin <command or definition>+)
  5414. <definition> --> (define <variable> <expression>)
  5415. | (define (<variable> <def formals>) <body>)
  5416. | (begin <definition>*)
  5417. <def formals> --> <variable>*
  5418. | <variable>* .@: <variable>
  5419. <syntax definition> -->
  5420. (define-syntax <keyword> <transformer spec>)
  5421. }
  5422. @end format
  5423. @node Formal semantics, Derived expression type, Formal syntax, Formal syntax and semantics
  5424. @section Formal semantics
  5425. This section provides a formal denotational semantics for the primitive
  5426. expressions of Scheme and selected built-in procedures. The concepts
  5427. and notation used here are described in @sc{[Stoy77]}.
  5428. @quotation
  5429. @emph{Note:} The formal semantics section was written in La@TeX{} which
  5430. is incompatible with @TeX{}info. See the Formal semantics section of
  5431. the original document from which this was derived.
  5432. @end quotation
  5433. @c @include{derive}
  5434. @node Derived expression type, , Formal semantics, Formal syntax and semantics
  5435. @section Derived expression types
  5436. This section gives macro definitions for the derived expression types in
  5437. terms of the primitive expression types (literal, variable, call, @samp{lambda},
  5438. @samp{if}, @samp{set!}). See section @ref{Control features} for a possible
  5439. definition of @samp{delay}.
  5440. @example
  5441. (define-syntax cond
  5442. (syntax-rules (else =>)
  5443. ((cond (else result1 result2 ...))
  5444. (begin result1 result2 ...))
  5445. ((cond (test => result))
  5446. (let ((temp test))
  5447. (if temp (result temp))))
  5448. ((cond (test => result) clause1 clause2 ...)
  5449. (let ((temp test))
  5450. (if temp
  5451. (result temp)
  5452. (cond clause1 clause2 ...))))
  5453. ((cond (test)) test)
  5454. ((cond (test) clause1 clause2 ...)
  5455. (let ((temp test))
  5456. (if temp
  5457. temp
  5458. (cond clause1 clause2 ...))))
  5459. ((cond (test result1 result2 ...))
  5460. (if test (begin result1 result2 ...)))
  5461. ((cond (test result1 result2 ...)
  5462. clause1 clause2 ...)
  5463. (if test
  5464. (begin result1 result2 ...)
  5465. (cond clause1 clause2 ...)))))
  5466. @end example
  5467. @example
  5468. (define-syntax case
  5469. (syntax-rules (else)
  5470. ((case (key ...)
  5471. clauses ...)
  5472. (let ((atom-key (key ...)))
  5473. (case atom-key clauses ...)))
  5474. ((case key
  5475. (else result1 result2 ...))
  5476. (begin result1 result2 ...))
  5477. ((case key
  5478. ((atoms ...) result1 result2 ...))
  5479. (if (memv key '(atoms ...))
  5480. (begin result1 result2 ...)))
  5481. ((case key
  5482. ((atoms ...) result1 result2 ...)
  5483. clause clauses ...)
  5484. (if (memv key '(atoms ...))
  5485. (begin result1 result2 ...)
  5486. (case key clause clauses ...)))))
  5487. @end example
  5488. @example
  5489. (define-syntax and
  5490. (syntax-rules ()
  5491. ((and) #t)
  5492. ((and test) test)
  5493. ((and test1 test2 ...)
  5494. (if test1 (and test2 ...) #f))))
  5495. @end example
  5496. @example
  5497. (define-syntax or
  5498. (syntax-rules ()
  5499. ((or) #f)
  5500. ((or test) test)
  5501. ((or test1 test2 ...)
  5502. (let ((x test1))
  5503. (if x x (or test2 ...))))))
  5504. @end example
  5505. @example
  5506. (define-syntax let
  5507. (syntax-rules ()
  5508. ((let ((name val) ...) body1 body2 ...)
  5509. ((lambda (name ...) body1 body2 ...)
  5510. val ...))
  5511. ((let tag ((name val) ...) body1 body2 ...)
  5512. ((letrec ((tag (lambda (name ...)
  5513. body1 body2 ...)))
  5514. tag)
  5515. val ...))))
  5516. @end example
  5517. @example
  5518. (define-syntax let*
  5519. (syntax-rules ()
  5520. ((let* () body1 body2 ...)
  5521. (let () body1 body2 ...))
  5522. ((let* ((name1 val1) (name2 val2) ...)
  5523. body1 body2 ...)
  5524. (let ((name1 val1))
  5525. (let* ((name2 val2) ...)
  5526. body1 body2 ...)))))
  5527. @end example
  5528. The following @samp{letrec} macro uses the symbol @samp{<undefined>}
  5529. in place of an expression which returns something that when stored in
  5530. a location makes it an error to try to obtain the value stored in the
  5531. location (no such expression is defined in Scheme).
  5532. A trick is used to generate the temporary names needed to avoid
  5533. specifying the order in which the values are evaluated.
  5534. This could also be accomplished by using an auxiliary macro.
  5535. @example
  5536. (define-syntax letrec
  5537. (syntax-rules ()
  5538. ((letrec ((var1 init1) ...) body ...)
  5539. (letrec "generate temp names"
  5540. (var1 ...)
  5541. ()
  5542. ((var1 init1) ...)
  5543. body ...))
  5544. ((letrec "generate temp names"
  5545. ()
  5546. (temp1 ...)
  5547. ((var1 init1) ...)
  5548. body ...)
  5549. (let ((var1 <undefined>) ...)
  5550. (let ((temp1 init1) ...)
  5551. (set! var1 temp1)
  5552. ...
  5553. body ...)))
  5554. ((letrec "generate temp names"
  5555. (x y ...)
  5556. (temp ...)
  5557. ((var1 init1) ...)
  5558. body ...)
  5559. (letrec "generate temp names"
  5560. (y ...)
  5561. (newtemp temp ...)
  5562. ((var1 init1) ...)
  5563. body ...))))
  5564. @end example
  5565. @example
  5566. (define-syntax begin
  5567. (syntax-rules ()
  5568. ((begin exp ...)
  5569. ((lambda () exp ...)))))
  5570. @end example
  5571. The following alternative expansion for @samp{begin} does not make use of
  5572. the ability to write more than one expression in the body of a lambda
  5573. expression. In any case, note that these rules apply only if the body
  5574. of the @samp{begin} contains no definitions.
  5575. @example
  5576. (define-syntax begin
  5577. (syntax-rules ()
  5578. ((begin exp)
  5579. exp)
  5580. ((begin exp1 exp2 ...)
  5581. (let ((x exp1))
  5582. (begin exp2 ...)))))
  5583. @end example
  5584. The following definition
  5585. of @samp{do} uses a trick to expand the variable clauses.
  5586. As with @samp{letrec} above, an auxiliary macro would also work.
  5587. The expression @samp{(if #f #f)} is used to obtain an unspecific
  5588. value.
  5589. @example
  5590. (define-syntax do
  5591. (syntax-rules ()
  5592. ((do ((var init step ...) ...)
  5593. (test expr ...)
  5594. command ...)
  5595. (letrec
  5596. ((loop
  5597. (lambda (var ...)
  5598. (if test
  5599. (begin
  5600. (if #f #f)
  5601. expr ...)
  5602. (begin
  5603. command
  5604. ...
  5605. (loop (do "step" var step ...)
  5606. ...))))))
  5607. (loop init ...)))
  5608. ((do "step" x)
  5609. x)
  5610. ((do "step" x y)
  5611. y)))
  5612. @end example
  5613. @c `a = Q_1[a]
  5614. @c `(a b c ... . z) = `(a . (b c ...))
  5615. @c `(a . b) = (append Q*_0[a] `b)
  5616. @c `(a) = Q*_0[a]
  5617. @c Q*_0[a] = (list 'a)
  5618. @c Q*_0[,a] = (list a)
  5619. @c Q*_0[,@a] = a
  5620. @c Q*_0[`a] = (list 'quasiquote Q*_1[a])
  5621. @c `#(a b ...) = (list->vector `(a b ...))
  5622. @c ugh.
  5623. @page
  5624. @c @include{notes}
  5625. @node Notes, Additional material, Formal syntax and semantics, top
  5626. @unnumbered Notes
  5627. @menu
  5628. * Language changes::
  5629. @end menu
  5630. @ignore todo
  5631. Perhaps this section should be made to disappear.
  5632. Can these remarks be moved somewhere else?
  5633. @end ignore
  5634. @node Language changes, , Notes, Notes
  5635. @unnumberedsec Language changes
  5636. This section enumerates the changes that have been made to Scheme since
  5637. the ``Revised^4 report'' [R4RS] was published.
  5638. @itemize @bullet
  5639. @item
  5640. The report is now a superset of the IEEE standard for Scheme
  5641. [IEEEScheme]: implementations that conform to the report will
  5642. also conform to the standard. This required the following changes:
  5643. @itemize @bullet
  5644. @item
  5645. The empty list is now required to count as true.
  5646. @item
  5647. The classification of features as essential or inessential has been
  5648. removed. There are now three classes of built-in procedures: primitive,
  5649. library, and optional. The optional procedures are @samp{load},
  5650. @samp{with-input-from-file}, @samp{with-output-to-file},
  5651. @samp{transcript-on}, @samp{transcript-off}, and
  5652. @samp{interaction-environment},
  5653. and @samp{-} and @samp{/} with more than two arguments.
  5654. None of these are in the IEEE standard.
  5655. @item
  5656. Programs are allowed to redefine built-in procedures. Doing so
  5657. will not change the behavior of other built-in procedures.
  5658. @end itemize
  5659. @item
  5660. @emph{Port} has been added to the list of disjoint types.
  5661. @item
  5662. The macro appendix has been removed. High-level macros are now part
  5663. of the main body of the report. The rewrite rules for derived expressions
  5664. have been replaced with macro definitions. There are no reserved identifiers.
  5665. @item
  5666. @samp{Syntax-rules} now allows vector patterns.
  5667. @item
  5668. Multiple-value returns, @samp{eval}, and @samp{dynamic-wind} have
  5669. been added.
  5670. @item
  5671. The calls that are required to be implemented in a properly tail-recursive
  5672. fashion are defined explicitly.
  5673. @item
  5674. `@samp{@@}' can be used within identifiers. `@samp{|}' is reserved
  5675. for possible future extensions.
  5676. @end itemize
  5677. @c %R4%%
  5678. @c \subsection*{Keywords as variable names}
  5679. @c Some implementations allow arbitrary syntactic
  5680. @c keywords \index{keyword}\index{syntactic keyword}to be used as variable
  5681. @c names, instead of reserving them, as this report would have
  5682. @c it.\index{variable} But this creates ambiguities in the interpretation
  5683. @c of expressions: for example, in the following, it's not clear whether
  5684. @c the expression {\tt (if 1 2 3)} should be treated as a procedure call or
  5685. @c as a conditional.
  5686. @c \begin{scheme}
  5687. @c (define if list)
  5688. @c (if 1 2 3) \ev 2 {\em{}or} (1 2 3)%
  5689. @c \end{scheme}
  5690. @c These ambiguities are usually resolved in some consistent way within any
  5691. @c given implementation, but no particular treatment stands out as being
  5692. @c clearly superior to any other, so these situations were excluded for the
  5693. @c purposes of this report.
  5694. @c %R4%%
  5695. @c \subsection*{Macros}
  5696. @c Scheme does not have any standard facility for defining new kinds of
  5697. @c expressions.\index{macros}
  5698. @c \vest The ability to alter the syntax of the language creates
  5699. @c numerous problems. All current implementations of Scheme have macro
  5700. @c facilities that solve those problems to one degree or another, but the
  5701. @c solutions are quite different and it isn't clear at this time which
  5702. @c solution is best, or indeed whether any of the solutions are truly
  5703. @c adequate. Rather than standardize, we are encouraging implementations
  5704. @c to continue to experiment with different solutions.
  5705. @c \vest The main problems with traditional macros are: They must be
  5706. @c defined to the system before any code using them is loaded; this is a
  5707. @c common source of obscure bugs. They are usually global; macros can be
  5708. @c made to follow lexical scope rules \todo{flushed: ``as in Common
  5709. @c Lisp's {\tt macrolet}''; OK?}, but many people find the resulting scope rules
  5710. @c confusing. Unless they are written very carefully, macros are
  5711. @c vulnerable to inadvertent capture of free variables; to get around this,
  5712. @c for example, macros may have to generate code in which procedure values
  5713. @c appear as quoted constants. There is a similar problem with syntactic
  5714. @c keywords if the keywords of special forms are not reserved. If keywords
  5715. @c are reserved, then either macros introduce new reserved words,
  5716. @c invalidating old code, or else special forms defined by the programmer
  5717. @c do not have the same status as special forms defined by the system.
  5718. @c \todo{Refer to Pitman's special forms paper.}
  5719. @c \todo{Pitman sez: Discuss importance of having a small number of special forms
  5720. @c so that programs can inspect each other.}
  5721. @ignore todo
  5722. Move cwcc history back here? --- Andy Cromarty is concerned about
  5723. confusion over who the audience is.
  5724. @end ignore
  5725. @ignore todo
  5726. Cromarty:
  5727. 23. NOTES, p.35ff.: This material should stay somehow. We need to
  5728. make it clear that R^3 Scheme is not being touted as Yet Another
  5729. Ultimate Solution To The Programming Language Problem, but rather
  5730. as a snapshot of a *process* of good design, for which not all
  5731. answers have yet been found. We also ought to use the opportunity
  5732. for publicity afforded us by SIGPLAN to advertise some of the thorny
  5733. unsolved problems that need further research, and encourage
  5734. language designers to work on them.
  5735. @end ignore
  5736. @c @include{repository}
  5737. @node Additional material, Example, Notes, top
  5738. @unnumbered Additional material
  5739. The Internet Scheme Repository at
  5740. @center
  5741. @center @url{http://www.cs.indiana.edu/scheme-repository/}
  5742. @center
  5743. contains an extensive Scheme bibliography, as well as papers,
  5744. programs, implementations, and other material related to Scheme.
  5745. @page
  5746. @c @include{example}
  5747. @node Example, Bibliography, Additional material, top
  5748. @unnumbered Example
  5749. @c -*- Mode: Lisp; Package: SCHEME; Syntax: Common-lisp -*-
  5750. @samp{Integrate-system} integrates the system
  5751. @center y_k^^ = f_k(y_1, y_2, @dots{}, y_n), k = 1, @dots{}, n
  5752. of differential equations with the method of Runge-Kutta.
  5753. The parameter @t{system-derivative} is a function that takes a system
  5754. state (a vector of values for the state variables y_1, @dots{}, y_n)
  5755. and produces a system derivative (the values y_1^^, @dots{},y_n^^). The parameter @t{initial-state} provides an initial
  5756. system state, and @t{h} is an initial guess for the length of the
  5757. integration step.
  5758. The value returned by @samp{integrate-system} is an infinite stream of
  5759. system states.
  5760. @example
  5761. (define integrate-system
  5762. (lambda (system-derivative initial-state h)
  5763. (let ((next (runge-kutta-4 system-derivative h)))
  5764. (letrec ((states
  5765. (cons initial-state
  5766. (delay (map-streams next
  5767. states)))))
  5768. states))))
  5769. @end example
  5770. @samp{Runge-Kutta-4} takes a function, @t{f}, that produces a
  5771. system derivative from a system state. @samp{Runge-Kutta-4}
  5772. produces a function that takes a system state and
  5773. produces a new system state.
  5774. @example
  5775. (define runge-kutta-4
  5776. (lambda (f h)
  5777. (let ((*h (scale-vector h))
  5778. (*2 (scale-vector 2))
  5779. (*1/2 (scale-vector (/ 1 2)))
  5780. (*1/6 (scale-vector (/ 1 6))))
  5781. (lambda (y)
  5782. ;; y @r{}is a system state
  5783. (let* ((k0 (*h (f y)))
  5784. (k1 (*h (f (add-vectors y (*1/2 k0)))))
  5785. (k2 (*h (f (add-vectors y (*1/2 k1)))))
  5786. (k3 (*h (f (add-vectors y k2)))))
  5787. (add-vectors y
  5788. (*1/6 (add-vectors k0
  5789. (*2 k1)
  5790. (*2 k2)
  5791. k3))))))))
  5792. @c |--------------------------------------------------|
  5793. (define elementwise
  5794. (lambda (f)
  5795. (lambda vectors
  5796. (generate-vector
  5797. (vector-length (car vectors))
  5798. (lambda (i)
  5799. (apply f
  5800. (map (lambda (v) (vector-ref v i))
  5801. vectors)))))))
  5802. @c |--------------------------------------------------|
  5803. (define generate-vector
  5804. (lambda (size proc)
  5805. (let ((ans (make-vector size)))
  5806. (letrec ((loop
  5807. (lambda (i)
  5808. (cond ((= i size) ans)
  5809. (else
  5810. (vector-set! ans i (proc i))
  5811. (loop (+ i 1)))))))
  5812. (loop 0)))))
  5813. (define add-vectors (elementwise +))
  5814. (define scale-vector
  5815. (lambda (s)
  5816. (elementwise (lambda (x) (* x s)))))
  5817. @end example
  5818. @samp{Map-streams} is analogous to @samp{map}: it applies its first
  5819. argument (a procedure) to all the elements of its second argument (a
  5820. stream).
  5821. @example
  5822. (define map-streams
  5823. (lambda (f s)
  5824. (cons (f (head s))
  5825. (delay (map-streams f (tail s))))))
  5826. @end example
  5827. Infinite streams are implemented as pairs whose car holds the first
  5828. element of the stream and whose cdr holds a promise to deliver the rest
  5829. of the stream.
  5830. @example
  5831. (define head car)
  5832. (define tail
  5833. (lambda (stream) (force (cdr stream))))
  5834. @end example
  5835. @sp 6
  5836. The following illustrates the use of @samp{integrate-system} in
  5837. integrating the system
  5838. @center C dv_C / dt = -i_L - v_C / R
  5839. @center L di_L / dt = v_C
  5840. which models a damped oscillator.
  5841. @example
  5842. (define damped-oscillator
  5843. (lambda (R L C)
  5844. (lambda (state)
  5845. (let ((Vc (vector-ref state 0))
  5846. (Il (vector-ref state 1)))
  5847. (vector (- 0 (+ (/ Vc (* R C)) (/ Il C)))
  5848. (/ Vc L))))))
  5849. (define the-states
  5850. (integrate-system
  5851. (damped-oscillator 10000 1000 .001)
  5852. '#(1 0)
  5853. .01))
  5854. @end example
  5855. @ignore todo
  5856. Show some output?
  5857. @end ignore
  5858. @c (letrec ((loop (lambda (s)
  5859. @c (newline)
  5860. @c (write (head s))
  5861. @c (loop (tail s)))))
  5862. @c (loop the-states))
  5863. @c #(1 0)
  5864. @c #(0.99895054 9.994835e-6)
  5865. @c #(0.99780226 1.9978681e-5)
  5866. @c #(0.9965554 2.9950552e-5)
  5867. @c #(0.9952102 3.990946e-5)
  5868. @c #(0.99376684 4.985443e-5)
  5869. @c #(0.99222565 5.9784474e-5)
  5870. @c #(0.9905868 6.969862e-5)
  5871. @c #(0.9888506 7.9595884e-5)
  5872. @c #(0.9870173 8.94753e-5)
  5873. @page
  5874. @c \newpage % Put bib on it's own page (it's just one)
  5875. @c \twocolumn[\vspace{-.18in}]% Last bib item was on a page by itself.
  5876. @c \renewcommand{\bibname}{References}
  5877. @c @include{bib}
  5878. @c My reference for proper reference format is:
  5879. @c Mary-Claire van Leunen.
  5880. @c {\em A Handbook for Scholars.}
  5881. @c Knopf, 1978.
  5882. @c I think the references list would look better in ``open'' format,
  5883. @c i.e. with the three blocks for each entry appearing on separate
  5884. @c lines. I used the compressed format for SIGPLAN in the interest of
  5885. @c space. In open format, when a block runs over one line,
  5886. @c continuation lines should be indented; this could probably be done
  5887. @c using some flavor of latex list environment. Maybe the right thing
  5888. @c to do in the long run would be to convert to Bibtex, which probably
  5889. @c does the right thing, since it was implemented by one of van
  5890. @c Leunen's colleagues at DEC SRC.
  5891. @c -- Jonathan
  5892. @c I tried to follow Jonathan's format, insofar as I understood it.
  5893. @c I tried to order entries lexicographically by authors (with singly
  5894. @c authored papers first), then by date.
  5895. @c In some cases I replaced a technical report or conference paper
  5896. @c by a subsequent journal article, but I think there are several
  5897. @c more such replacements that ought to be made.
  5898. @c -- Will, 1991.
  5899. @c This is just a personal remark on your question on the RRRS:
  5900. @c The language CUCH (Curry-Church) was implemented by 1964 and
  5901. @c is a practical version of the lambda-calculus (call-by-name).
  5902. @c One reference you may find in Formal Language Description Languages
  5903. @c for Computer Programming T.~B.~Steele, 1965 (or so).
  5904. @c -- Matthias Felleisen
  5905. @c Rather than try to keep the bibliography up-to-date, which is hopeless
  5906. @c given the time between updates, I replaced the bulk of the references
  5907. @c with a pointer to the Scheme Repository. Ozan Yigit's bibliography in
  5908. @c the repository is a superset of the R4RS one.
  5909. @c The bibliography now contains only items referenced within the report.
  5910. @c -- Richard, 1996.
  5911. @node Bibliography, Index, Example, top
  5912. @unnumbered Bibliography
  5913. @itemize @bullet
  5914. @c 999
  5915. @item [SICP]
  5916. @pindex SICP
  5917. Harold Abelson and Gerald Jay Sussman with Julie Sussman.
  5918. @emph{Structure and Interpretation of Computer Programs, second edition.}
  5919. MIT Press, Cambridge, 1996.
  5920. @item [Bawden88]
  5921. @c new
  5922. Alan Bawden and Jonathan Rees.
  5923. @pindex Bawden88
  5924. Syntactic closures.
  5925. In @emph{Proceedings of the 1988 ACM Symposium on Lisp and
  5926. Functional Programming}, pages 86--95.
  5927. @item [howtoprint]
  5928. @pindex howtoprint
  5929. Robert G. Burger and R. Kent Dybvig.
  5930. Printing floating-point numbers quickly and accurately.
  5931. In @emph{Proceedings of the ACM SIGPLAN '96 Conference
  5932. on Programming Language Design and Implementation}, pages 108--116.
  5933. @item [RRRS]
  5934. @pindex RRRS
  5935. William Clinger, editor.
  5936. The revised revised report on Scheme, or an uncommon Lisp.
  5937. MIT Artificial Intelligence Memo 848, August 1985.
  5938. Also published as Computer Science Department Technical Report 174,
  5939. Indiana University, June 1985.
  5940. @item [howtoread]
  5941. @c new
  5942. William Clinger.
  5943. @pindex howtoread
  5944. How to read floating point numbers accurately.
  5945. In @emph{Proceedings of the ACM SIGPLAN '90 Conference
  5946. on Programming Language Design and Implementation}, pages 92--101.
  5947. Proceedings published as @emph{SIGPLAN Notices} 25(6), June 1990.
  5948. @item [R4RS]
  5949. @pindex R4RS
  5950. William Clinger and Jonathan Rees, editors.
  5951. The revised^4 report on the algorithmic language Scheme.
  5952. In @emph{ACM Lisp Pointers} 4(3), pages 1--55, 1991.
  5953. @item [macrosthatwork]
  5954. @c new
  5955. William Clinger and Jonathan Rees.
  5956. @pindex macrosthatwork
  5957. Macros that work.
  5958. In @emph{Proceedings of the 1991 ACM Conference on Principles of
  5959. Programming Languages}, pages 155--162.
  5960. @item [propertailrecursion]
  5961. @c new
  5962. William Clinger.
  5963. @pindex propertailrecursion
  5964. Proper Tail Recursion and Space Efficiency.
  5965. To appear in @emph{Proceedings of the 1998 ACM Conference on Programming
  5966. Language Design and Implementation}, June 1998.
  5967. @item [syntacticabstraction]
  5968. @pindex syntacticabstraction
  5969. R. Kent Dybvig, Robert Hieb, and Carl Bruggeman.
  5970. Syntactic abstraction in Scheme.
  5971. @emph{Lisp and Symbolic Computation} 5(4):295--326, 1993.
  5972. @item [Scheme311]
  5973. @pindex Scheme311
  5974. Carol Fessenden, William Clinger, Daniel P. Friedman, and Christopher Haynes.
  5975. Scheme 311 version 4 reference manual.
  5976. Indiana University Computer Science Technical Report 137, February 1983.
  5977. Superseded by [Scheme84].
  5978. @item [Scheme84]
  5979. @pindex Scheme84
  5980. D. Friedman, C. Haynes, E. Kohlbecker, and M. Wand.
  5981. Scheme 84 interim reference manual.
  5982. Indiana University Computer Science Technical Report 153, January 1985.
  5983. @item [IEEE]
  5984. @pindex IEEE
  5985. @emph{IEEE Standard 754-1985. IEEE Standard for Binary Floating-Point
  5986. Arithmetic.} IEEE, New York, 1985.
  5987. @item [IEEEScheme]
  5988. @pindex IEEEScheme
  5989. @emph{IEEE Standard 1178-1990. IEEE Standard for the Scheme
  5990. Programming Language.} IEEE, New York, 1991.
  5991. @item [Kohlbecker86]
  5992. @pindex Kohlbecker86
  5993. Eugene E. Kohlbecker Jr.
  5994. @emph{Syntactic Extensions in the Programming Language Lisp.}
  5995. PhD thesis, Indiana University, August 1986.
  5996. @item [hygienic]
  5997. @pindex hygienic
  5998. Eugene E. Kohlbecker Jr., Daniel P. Friedman, Matthias Felleisen, and Bruce Duba.
  5999. Hygienic macro expansion.
  6000. In @emph{Proceedings of the 1986 ACM Conference on Lisp
  6001. and Functional Programming}, pages 151--161.
  6002. @item [Landin65]
  6003. @pindex Landin65
  6004. Peter Landin.
  6005. A correspondence between Algol 60 and Church's lambda notation: Part I.
  6006. @emph{Communications of the ACM} 8(2):89--101, February 1965.
  6007. @item [MITScheme]
  6008. @pindex MITScheme
  6009. MIT Department of Electrical Engineering and Computer Science.
  6010. Scheme manual, seventh edition.
  6011. September 1984.
  6012. @item [Naur63]
  6013. @pindex Naur63
  6014. Peter Naur et al.
  6015. Revised report on the algorithmic language Algol 60.
  6016. @emph{Communications of the ACM} 6(1):1--17, January 1963.
  6017. @item [Penfield81]
  6018. @pindex Penfield81
  6019. Paul Penfield, Jr.
  6020. Principal values and branch cuts in complex APL.
  6021. In @emph{APL '81 Conference Proceedings,} pages 248--256.
  6022. ACM SIGAPL, San Francisco, September 1981.
  6023. Proceedings published as @emph{APL Quote Quad} 12(1), ACM, September 1981.
  6024. @item [Pitman83]
  6025. @pindex Pitman83
  6026. Kent M. Pitman.
  6027. The revised MacLisp manual (Saturday evening edition).
  6028. MIT Laboratory for Computer Science Technical Report 295, May 1983.
  6029. @item [Rees82]
  6030. @pindex Rees82
  6031. Jonathan A. Rees and Norman I. Adams IV.
  6032. T: A dialect of Lisp or, lambda: The ultimate software tool.
  6033. In @emph{Conference Record of the 1982 ACM Symposium on Lisp and
  6034. Functional Programming}, pages 114--122.
  6035. @item [Rees84]
  6036. @pindex Rees84
  6037. Jonathan A. Rees, Norman I. Adams IV, and James R. Meehan.
  6038. The T manual, fourth edition.
  6039. Yale University Computer Science Department, January 1984.
  6040. @item [R3RS]
  6041. @pindex R3RS
  6042. Jonathan Rees and William Clinger, editors.
  6043. The revised^3 report on the algorithmic language Scheme.
  6044. In @emph{ACM SIGPLAN Notices} 21(12), pages 37--79, December 1986.
  6045. @item [Reynolds72]
  6046. @pindex Reynolds72
  6047. John Reynolds.
  6048. Definitional interpreters for higher order programming languages.
  6049. In @emph{ACM Conference Proceedings}, pages 717--740.
  6050. ACM,
  6051. @ignore todo
  6052. month?
  6053. @end ignore
  6054. 1972.
  6055. @item [Scheme78]
  6056. @pindex Scheme78
  6057. Guy Lewis Steele Jr. and Gerald Jay Sussman.
  6058. The revised report on Scheme, a dialect of Lisp.
  6059. MIT Artificial Intelligence Memo 452, January 1978.
  6060. @item [Rabbit]
  6061. @pindex Rabbit
  6062. Guy Lewis Steele Jr.
  6063. Rabbit: a compiler for Scheme.
  6064. MIT Artificial Intelligence Laboratory Technical Report 474, May 1978.
  6065. @item [CLtL]
  6066. @pindex CLtL
  6067. Guy Lewis Steele Jr.
  6068. @emph{Common Lisp: The Language, second edition.}
  6069. Digital Press, Burlington MA, 1990.
  6070. @item [Scheme75]
  6071. @pindex Scheme75
  6072. Gerald Jay Sussman and Guy Lewis Steele Jr.
  6073. Scheme: an interpreter for extended lambda calculus.
  6074. MIT Artificial Intelligence Memo 349, December 1975.
  6075. @item [Stoy77]
  6076. @pindex Stoy77
  6077. Joseph E. Stoy.
  6078. @emph{Denotational Semantics: The Scott-Strachey Approach to
  6079. Programming Language Theory.}
  6080. MIT Press, Cambridge, 1977.
  6081. @item [TImanual85]
  6082. @pindex TImanual85
  6083. Texas Instruments, Inc.
  6084. TI Scheme Language Reference Manual.
  6085. Preliminary version 1.0, November 1985.
  6086. @end itemize
  6087. @page
  6088. @c Adjustment to avoid having the last index entry on a page by itself.
  6089. @c \addtolength{\baselineskip}{-0.1pt}
  6090. @node Index, , Bibliography, top
  6091. @unnumbered Alphabetic index of definitions of concepts, keywords, and procedures
  6092. The principal entry for each term, procedure, or keyword is listed
  6093. first, separated from the other entries by a semicolon.
  6094. @sp 6
  6095. @unnumberedsec Concepts
  6096. @printindex cp
  6097. @page
  6098. @unnumberedsec Procedures
  6099. @printindex fn
  6100. @ifinfo
  6101. @unnumberedsec References
  6102. @printindex pg
  6103. @end ifinfo
  6104. @contents
  6105. @bye