r5rs.texi 251 KB

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