srfi-modules.texi 362 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000-2004, 2006, 2007-2014, 2017, 2018, 2019, 2020
  4. @c Free Software Foundation, Inc.
  5. @c Copyright (C) 2015-2016 Taylan Ulrich Bayırlı/Kammer
  6. @c Copyright (C) 2015-2016, 2018 John Cowan
  7. @c See the file guile.texi for copying conditions.
  8. @node SRFI Support
  9. @section SRFI Support Modules
  10. @cindex SRFI
  11. SRFI is an acronym for Scheme Request For Implementation. The SRFI
  12. documents define a lot of syntactic and procedure extensions to standard
  13. Scheme as defined in R5RS.
  14. Guile has support for a number of SRFIs. This chapter gives an overview
  15. over the available SRFIs and some usage hints. For complete
  16. documentation, design rationales and further examples, we advise you to
  17. get the relevant SRFI documents from the SRFI home page
  18. @url{http://srfi.schemers.org/}.
  19. @menu
  20. * About SRFI Usage:: What to know about Guile's SRFI support.
  21. * SRFI-0:: cond-expand
  22. * SRFI-1:: List library.
  23. * SRFI-2:: and-let*.
  24. * SRFI-4:: Homogeneous numeric vector datatypes.
  25. * SRFI-6:: Basic String Ports.
  26. * SRFI-8:: receive.
  27. * SRFI-9:: define-record-type.
  28. * SRFI-10:: Hash-Comma Reader Extension.
  29. * SRFI-11:: let-values and let*-values.
  30. * SRFI-13:: String library.
  31. * SRFI-14:: Character-set library.
  32. * SRFI-16:: case-lambda
  33. * SRFI-17:: Generalized set!
  34. * SRFI-18:: Multithreading support
  35. * SRFI-19:: Time/Date library.
  36. * SRFI-23:: Error reporting
  37. * SRFI-26:: Specializing parameters
  38. * SRFI-27:: Sources of Random Bits
  39. * SRFI-28:: Basic format strings.
  40. * SRFI-30:: Nested multi-line block comments
  41. * SRFI-31:: A special form `rec' for recursive evaluation
  42. * SRFI-34:: Exception handling.
  43. * SRFI-35:: Conditions.
  44. * SRFI-37:: args-fold program argument processor
  45. * SRFI-38:: External Representation for Data With Shared Structure
  46. * SRFI-39:: Parameter objects
  47. * SRFI-41:: Streams.
  48. * SRFI-42:: Eager comprehensions
  49. * SRFI-43:: Vector Library.
  50. * SRFI-45:: Primitives for expressing iterative lazy algorithms
  51. * SRFI-46:: Basic syntax-rules Extensions.
  52. * SRFI-55:: Requiring Features.
  53. * SRFI-60:: Integers as bits.
  54. * SRFI-61:: A more general `cond' clause
  55. * SRFI-62:: S-expression comments.
  56. * SRFI-64:: A Scheme API for test suites.
  57. * SRFI-67:: Compare procedures
  58. * SRFI-69:: Basic hash tables.
  59. * SRFI-71:: Extended let-syntax for multiple values.
  60. * SRFI-87:: => in case clauses.
  61. * SRFI-88:: Keyword objects.
  62. * SRFI-98:: Accessing environment variables.
  63. * SRFI-105:: Curly-infix expressions.
  64. * SRFI-111:: Boxes.
  65. * SRFI 125:: Mutators.
  66. * SRFI 126:: R6RS-based hash tables.
  67. * SRFI 128:: Comparators.
  68. * SRFI 151:: Bitwise Operations.
  69. * SRFI 160:: Homogeneous numeric vectors.
  70. * SRFI-171:: Transducers.
  71. * SRFI 178:: Bitvectors.
  72. @end menu
  73. @node About SRFI Usage
  74. @subsection About SRFI Usage
  75. @c FIXME::martin: Review me!
  76. SRFI support in Guile is currently implemented partly in the core
  77. library, and partly as add-on modules. That means that some SRFIs are
  78. automatically available when the interpreter is started, whereas the
  79. other SRFIs require you to use the appropriate support module
  80. explicitly.
  81. There are several reasons for this inconsistency. First, the feature
  82. checking syntactic form @code{cond-expand} (@pxref{SRFI-0}) must be
  83. available immediately, because it must be there when the user wants to
  84. check for the Scheme implementation, that is, before she can know that
  85. it is safe to use @code{use-modules} to load SRFI support modules. The
  86. second reason is that some features defined in SRFIs had been
  87. implemented in Guile before the developers started to add SRFI
  88. implementations as modules (for example SRFI-13 (@pxref{SRFI-13})). In
  89. the future, it is possible that SRFIs in the core library might be
  90. factored out into separate modules, requiring explicit module loading
  91. when they are needed. So you should be prepared to have to use
  92. @code{use-modules} someday in the future to access SRFI-13 bindings. If
  93. you want, you can do that already. We have included the module
  94. @code{(srfi srfi-13)} in the distribution, which currently does nothing,
  95. but ensures that you can write future-safe code.
  96. Generally, support for a specific SRFI is made available by using
  97. modules named @code{(srfi srfi-@var{number})}, where @var{number} is the
  98. number of the SRFI needed. Another possibility is to use the command
  99. line option @code{--use-srfi}, which will load the necessary modules
  100. automatically (@pxref{Invoking Guile}).
  101. @node SRFI-0
  102. @subsection SRFI-0 - cond-expand
  103. @cindex SRFI-0
  104. This SRFI lets a portable Scheme program test for the presence of
  105. certain features, and adapt itself by using different blocks of code,
  106. or fail if the necessary features are not available. There's no
  107. module to load, this is in the Guile core.
  108. A program designed only for Guile will generally not need this
  109. mechanism, such a program can of course directly use the various
  110. documented parts of Guile.
  111. @deffn syntax cond-expand (feature body@dots{}) @dots{}
  112. Expand to the @var{body} of the first clause whose @var{feature}
  113. specification is satisfied. It is an error if no @var{feature} is
  114. satisfied.
  115. Features are symbols such as @code{srfi-1}, and a feature
  116. specification can use @code{and}, @code{or} and @code{not} forms to
  117. test combinations. The last clause can be an @code{else}, to be used
  118. if no other passes.
  119. For example, define a private version of @code{alist-cons} if SRFI-1
  120. is not available.
  121. @example
  122. (cond-expand (srfi-1
  123. )
  124. (else
  125. (define (alist-cons key val alist)
  126. (cons (cons key val) alist))))
  127. @end example
  128. Or demand a certain set of SRFIs (list operations, string ports,
  129. @code{receive} and string operations), failing if they're not
  130. available.
  131. @example
  132. (cond-expand ((and srfi-1 srfi-6 srfi-8 srfi-13)
  133. ))
  134. @end example
  135. @end deffn
  136. @noindent
  137. The Guile core has the following features,
  138. @example
  139. guile
  140. guile-2 ;; starting from Guile 2.x
  141. guile-2.2 ;; starting from Guile 2.2
  142. guile-3 ;; starting from Guile 3.x
  143. guile-3.0 ;; starting from Guile 3.0
  144. r5rs
  145. r6rs
  146. r7rs
  147. exact-closed ieee-float full-unicode ratios ;; R7RS features
  148. srfi-0
  149. srfi-4
  150. srfi-6
  151. srfi-13
  152. srfi-14
  153. srfi-16
  154. srfi-23
  155. srfi-30
  156. srfi-39
  157. srfi-46
  158. srfi-55
  159. srfi-61
  160. srfi-62
  161. srfi-87
  162. srfi-105
  163. @end example
  164. Other SRFI feature symbols are defined once their code has been loaded
  165. with @code{use-modules}, since only then are their bindings available.
  166. The @samp{--use-srfi} command line option (@pxref{Invoking Guile}) is
  167. a good way to load SRFIs to satisfy @code{cond-expand} when running a
  168. portable program.
  169. Testing the @code{guile} feature allows a program to adapt itself to
  170. the Guile module system, but still run on other Scheme systems. For
  171. example the following demands SRFI-8 (@code{receive}), but also knows
  172. how to load it with the Guile mechanism.
  173. @example
  174. (cond-expand (srfi-8
  175. )
  176. (guile
  177. (use-modules (srfi srfi-8))))
  178. @end example
  179. @cindex @code{guile-2} SRFI-0 feature
  180. @cindex portability between 2.0 and older versions
  181. Likewise, testing the @code{guile-2} feature allows code to be portable
  182. between Guile 2.@var{x} and previous versions of Guile. For instance, it
  183. makes it possible to write code that accounts for Guile 2.@var{x}'s compiler,
  184. yet be correctly interpreted on 1.8 and earlier versions:
  185. @example
  186. (cond-expand (guile-2 (eval-when (compile)
  187. ;; This must be evaluated at compile time.
  188. (fluid-set! current-reader my-reader)))
  189. (guile
  190. ;; Earlier versions of Guile do not have a
  191. ;; separate compilation phase.
  192. (fluid-set! current-reader my-reader)))
  193. @end example
  194. It should be noted that @code{cond-expand} is separate from the
  195. @code{*features*} mechanism (@pxref{Feature Tracking}), feature
  196. symbols in one are unrelated to those in the other.
  197. @node SRFI-1
  198. @subsection SRFI-1 - List library
  199. @cindex SRFI-1
  200. @cindex list
  201. @c FIXME::martin: Review me!
  202. The list library defined in SRFI-1 contains a lot of useful list
  203. processing procedures for construction, examining, destructuring and
  204. manipulating lists and pairs.
  205. Since SRFI-1 also defines some procedures which are already contained
  206. in R5RS and thus are supported by the Guile core library, some list
  207. and pair procedures which appear in the SRFI-1 document may not appear
  208. in this section. So when looking for a particular list/pair
  209. processing procedure, you should also have a look at the sections
  210. @ref{Lists} and @ref{Pairs}.
  211. @menu
  212. * SRFI-1 Constructors:: Constructing new lists.
  213. * SRFI-1 Predicates:: Testing list for specific properties.
  214. * SRFI-1 Selectors:: Selecting elements from lists.
  215. * SRFI-1 Length Append etc:: Length calculation and list appending.
  216. * SRFI-1 Fold and Map:: Higher-order list processing.
  217. * SRFI-1 Filtering and Partitioning:: Filter lists based on predicates.
  218. * SRFI-1 Searching:: Search for elements.
  219. * SRFI-1 Deleting:: Delete elements from lists.
  220. * SRFI-1 Association Lists:: Handle association lists.
  221. * SRFI-1 Set Operations:: Use lists for representing sets.
  222. @end menu
  223. @node SRFI-1 Constructors
  224. @subsubsection Constructors
  225. @cindex list constructor
  226. @c FIXME::martin: Review me!
  227. New lists can be constructed by calling one of the following
  228. procedures.
  229. @deffn {Scheme Procedure} xcons d a
  230. Like @code{cons}, but with interchanged arguments. Useful mostly when
  231. passed to higher-order procedures.
  232. @end deffn
  233. @deffn {Scheme Procedure} list-tabulate n init-proc
  234. Return an @var{n}-element list, where each list element is produced by
  235. applying the procedure @var{init-proc} to the corresponding list
  236. index. The order in which @var{init-proc} is applied to the indices
  237. is not specified.
  238. @end deffn
  239. @deffn {Scheme Procedure} list-copy lst
  240. Return a new list containing the elements of the list @var{lst}.
  241. This function differs from the core @code{list-copy} (@pxref{List
  242. Constructors}) in accepting improper lists too. And if @var{lst} is
  243. not a pair at all then it's treated as the final tail of an improper
  244. list and simply returned.
  245. @end deffn
  246. @deffn {Scheme Procedure} circular-list elt1 elt2 @dots{}
  247. Return a circular list containing the given arguments @var{elt1}
  248. @var{elt2} @dots{}.
  249. @end deffn
  250. @deffn {Scheme Procedure} iota count [start step]
  251. Return a list containing @var{count} numbers, starting from
  252. @var{start} and adding @var{step} each time. The default @var{start}
  253. is 0, the default @var{step} is 1. For example,
  254. @example
  255. (iota 6) @result{} (0 1 2 3 4 5)
  256. (iota 4 2.5 -2) @result{} (2.5 0.5 -1.5 -3.5)
  257. @end example
  258. This function takes its name from the corresponding primitive in the
  259. APL language.
  260. @end deffn
  261. @node SRFI-1 Predicates
  262. @subsubsection Predicates
  263. @cindex list predicate
  264. @c FIXME::martin: Review me!
  265. The procedures in this section test specific properties of lists.
  266. @deffn {Scheme Procedure} proper-list? obj
  267. Return @code{#t} if @var{obj} is a proper list, or @code{#f}
  268. otherwise. This is the same as the core @code{list?} (@pxref{List
  269. Predicates}).
  270. A proper list is a list which ends with the empty list @code{()} in
  271. the usual way. The empty list @code{()} itself is a proper list too.
  272. @example
  273. (proper-list? '(1 2 3)) @result{} #t
  274. (proper-list? '()) @result{} #t
  275. @end example
  276. @end deffn
  277. @deffn {Scheme Procedure} circular-list? obj
  278. Return @code{#t} if @var{obj} is a circular list, or @code{#f}
  279. otherwise.
  280. A circular list is a list where at some point the @code{cdr} refers
  281. back to a previous pair in the list (either the start or some later
  282. point), so that following the @code{cdr}s takes you around in a
  283. circle, with no end.
  284. @example
  285. (define x (list 1 2 3 4))
  286. (set-cdr! (last-pair x) (cddr x))
  287. x @result{} (1 2 3 4 3 4 3 4 ...)
  288. (circular-list? x) @result{} #t
  289. @end example
  290. @end deffn
  291. @deffn {Scheme Procedure} dotted-list? obj
  292. Return @code{#t} if @var{obj} is a dotted list, or @code{#f}
  293. otherwise.
  294. A dotted list is a list where the @code{cdr} of the last pair is not
  295. the empty list @code{()}. Any non-pair @var{obj} is also considered a
  296. dotted list, with length zero.
  297. @example
  298. (dotted-list? '(1 2 . 3)) @result{} #t
  299. (dotted-list? 99) @result{} #t
  300. @end example
  301. @end deffn
  302. It will be noted that any Scheme object passes exactly one of the
  303. above three tests @code{proper-list?}, @code{circular-list?} and
  304. @code{dotted-list?}. Non-lists are @code{dotted-list?}, finite lists
  305. are either @code{proper-list?} or @code{dotted-list?}, and infinite
  306. lists are @code{circular-list?}.
  307. @sp 1
  308. @deffn {Scheme Procedure} null-list? lst
  309. Return @code{#t} if @var{lst} is the empty list @code{()}, @code{#f}
  310. otherwise. If something else than a proper or circular list is passed
  311. as @var{lst}, an error is signaled. This procedure is recommended
  312. for checking for the end of a list in contexts where dotted lists are
  313. not allowed.
  314. @end deffn
  315. @deffn {Scheme Procedure} not-pair? obj
  316. Return @code{#t} is @var{obj} is not a pair, @code{#f} otherwise.
  317. This is shorthand notation @code{(not (pair? @var{obj}))} and is
  318. supposed to be used for end-of-list checking in contexts where dotted
  319. lists are allowed.
  320. @end deffn
  321. @deffn {Scheme Procedure} list= elt= list1 @dots{}
  322. Return @code{#t} if all argument lists are equal, @code{#f} otherwise.
  323. List equality is determined by testing whether all lists have the same
  324. length and the corresponding elements are equal in the sense of the
  325. equality predicate @var{elt=}. If no or only one list is given,
  326. @code{#t} is returned.
  327. @end deffn
  328. @node SRFI-1 Selectors
  329. @subsubsection Selectors
  330. @cindex list selector
  331. @c FIXME::martin: Review me!
  332. @deffn {Scheme Procedure} first pair
  333. @deffnx {Scheme Procedure} second pair
  334. @deffnx {Scheme Procedure} third pair
  335. @deffnx {Scheme Procedure} fourth pair
  336. @deffnx {Scheme Procedure} fifth pair
  337. @deffnx {Scheme Procedure} sixth pair
  338. @deffnx {Scheme Procedure} seventh pair
  339. @deffnx {Scheme Procedure} eighth pair
  340. @deffnx {Scheme Procedure} ninth pair
  341. @deffnx {Scheme Procedure} tenth pair
  342. These are synonyms for @code{car}, @code{cadr}, @code{caddr}, @dots{}.
  343. @end deffn
  344. @deffn {Scheme Procedure} car+cdr pair
  345. Return two values, the @sc{car} and the @sc{cdr} of @var{pair}.
  346. @end deffn
  347. @lisp
  348. (car+cdr '(0 1 2 3))
  349. @result{}
  350. 0
  351. (1 2 3)
  352. @end lisp
  353. @deffn {Scheme Procedure} take lst i
  354. @deffnx {Scheme Procedure} take! lst i
  355. Return a list containing the first @var{i} elements of @var{lst}.
  356. @code{take!} may modify the structure of the argument list @var{lst}
  357. in order to produce the result.
  358. @end deffn
  359. @deffn {Scheme Procedure} drop lst i
  360. Return a list containing all but the first @var{i} elements of
  361. @var{lst}.
  362. @end deffn
  363. @deffn {Scheme Procedure} take-right lst i
  364. Return a list containing the @var{i} last elements of @var{lst}.
  365. The return shares a common tail with @var{lst}.
  366. @end deffn
  367. @deffn {Scheme Procedure} drop-right lst i
  368. @deffnx {Scheme Procedure} drop-right! lst i
  369. Return a list containing all but the @var{i} last elements of
  370. @var{lst}.
  371. @code{drop-right} always returns a new list, even when @var{i} is
  372. zero. @code{drop-right!} may modify the structure of the argument
  373. list @var{lst} in order to produce the result.
  374. @end deffn
  375. @deffn {Scheme Procedure} split-at lst i
  376. @deffnx {Scheme Procedure} split-at! lst i
  377. Return two values, a list containing the first @var{i} elements of the
  378. list @var{lst} and a list containing the remaining elements.
  379. @code{split-at!} may modify the structure of the argument list
  380. @var{lst} in order to produce the result.
  381. @end deffn
  382. @deffn {Scheme Procedure} last lst
  383. Return the last element of the non-empty, finite list @var{lst}.
  384. @end deffn
  385. @node SRFI-1 Length Append etc
  386. @subsubsection Length, Append, Concatenate, etc.
  387. @c FIXME::martin: Review me!
  388. @deffn {Scheme Procedure} length+ lst
  389. Return the length of the argument list @var{lst}. When @var{lst} is a
  390. circular list, @code{#f} is returned.
  391. @end deffn
  392. @deffn {Scheme Procedure} concatenate list-of-lists
  393. @deffnx {Scheme Procedure} concatenate! list-of-lists
  394. Construct a list by appending all lists in @var{list-of-lists}.
  395. @code{concatenate!} may modify the structure of the given lists in
  396. order to produce the result.
  397. @code{concatenate} is the same as @code{(apply append
  398. @var{list-of-lists})}. It exists because some Scheme implementations
  399. have a limit on the number of arguments a function takes, which the
  400. @code{apply} might exceed. In Guile there is no such limit.
  401. @end deffn
  402. @deffn {Scheme Procedure} append-reverse rev-head tail
  403. @deffnx {Scheme Procedure} append-reverse! rev-head tail
  404. Reverse @var{rev-head}, append @var{tail} to it, and return the
  405. result. This is equivalent to @code{(append (reverse @var{rev-head})
  406. @var{tail})}, but its implementation is more efficient.
  407. @example
  408. (append-reverse '(1 2 3) '(4 5 6)) @result{} (3 2 1 4 5 6)
  409. @end example
  410. @code{append-reverse!} may modify @var{rev-head} in order to produce
  411. the result.
  412. @end deffn
  413. @deffn {Scheme Procedure} zip lst1 lst2 @dots{}
  414. Return a list as long as the shortest of the argument lists, where
  415. each element is a list. The first list contains the first elements of
  416. the argument lists, the second list contains the second elements, and
  417. so on.
  418. @end deffn
  419. @deffn {Scheme Procedure} unzip1 lst
  420. @deffnx {Scheme Procedure} unzip2 lst
  421. @deffnx {Scheme Procedure} unzip3 lst
  422. @deffnx {Scheme Procedure} unzip4 lst
  423. @deffnx {Scheme Procedure} unzip5 lst
  424. @code{unzip1} takes a list of lists, and returns a list containing the
  425. first elements of each list, @code{unzip2} returns two lists, the
  426. first containing the first elements of each lists and the second
  427. containing the second elements of each lists, and so on.
  428. @end deffn
  429. @deffn {Scheme Procedure} count pred lst1 lst2 @dots{}
  430. Return a count of the number of times @var{pred} returns true when
  431. called on elements from the given lists.
  432. @var{pred} is called with @var{N} parameters @code{(@var{pred}
  433. @var{elem1} @dots{} @var{elemN} )}, each element being from the
  434. corresponding list. The first call is with the first element of each
  435. list, the second with the second element from each, and so on.
  436. Counting stops when the end of the shortest list is reached. At least
  437. one list must be non-circular.
  438. @end deffn
  439. @node SRFI-1 Fold and Map
  440. @subsubsection Fold, Unfold & Map
  441. @cindex list fold
  442. @cindex list map
  443. @c FIXME::martin: Review me!
  444. @deffn {Scheme Procedure} fold proc init lst1 lst2 @dots{}
  445. @deffnx {Scheme Procedure} fold-right proc init lst1 lst2 @dots{}
  446. Apply @var{proc} to the elements of @var{lst1} @var{lst2} @dots{} to
  447. build a result, and return that result.
  448. Each @var{proc} call is @code{(@var{proc} @var{elem1} @var{elem2}
  449. @dots{} @var{previous})}, where @var{elem1} is from @var{lst1},
  450. @var{elem2} is from @var{lst2}, and so on. @var{previous} is the return
  451. from the previous call to @var{proc}, or the given @var{init} for the
  452. first call. If any list is empty, just @var{init} is returned.
  453. @code{fold} works through the list elements from first to last. The
  454. following shows a list reversal and the calls it makes,
  455. @example
  456. (fold cons '() '(1 2 3))
  457. (cons 1 '())
  458. (cons 2 '(1))
  459. (cons 3 '(2 1)
  460. @result{} (3 2 1)
  461. @end example
  462. @code{fold-right} works through the list elements from last to first,
  463. ie.@: from the right. So for example the following finds the longest
  464. string, and the last among equal longest,
  465. @example
  466. (fold-right (lambda (str prev)
  467. (if (> (string-length str) (string-length prev))
  468. str
  469. prev))
  470. ""
  471. '("x" "abc" "xyz" "jk"))
  472. @result{} "xyz"
  473. @end example
  474. If @var{lst1} @var{lst2} @dots{} have different lengths, @code{fold}
  475. stops when the end of the shortest is reached; @code{fold-right}
  476. commences at the last element of the shortest. Ie.@: elements past the
  477. length of the shortest are ignored in the other @var{lst}s. At least
  478. one @var{lst} must be non-circular.
  479. @code{fold} should be preferred over @code{fold-right} if the order of
  480. processing doesn't matter, or can be arranged either way, since
  481. @code{fold} is a little more efficient.
  482. The way @code{fold} builds a result from iterating is quite general,
  483. it can do more than other iterations like say @code{map} or
  484. @code{filter}. The following for example removes adjacent duplicate
  485. elements from a list,
  486. @example
  487. (define (delete-adjacent-duplicates lst)
  488. (fold-right (lambda (elem ret)
  489. (if (equal? elem (first ret))
  490. ret
  491. (cons elem ret)))
  492. (list (last lst))
  493. lst))
  494. (delete-adjacent-duplicates '(1 2 3 3 4 4 4 5))
  495. @result{} (1 2 3 4 5)
  496. @end example
  497. Clearly the same sort of thing can be done with a @code{for-each} and
  498. a variable in which to build the result, but a self-contained
  499. @var{proc} can be re-used in multiple contexts, where a
  500. @code{for-each} would have to be written out each time.
  501. @end deffn
  502. @deffn {Scheme Procedure} pair-fold proc init lst1 lst2 @dots{}
  503. @deffnx {Scheme Procedure} pair-fold-right proc init lst1 lst2 @dots{}
  504. The same as @code{fold} and @code{fold-right}, but apply @var{proc} to
  505. the pairs of the lists instead of the list elements.
  506. @end deffn
  507. @deffn {Scheme Procedure} reduce proc default lst
  508. @deffnx {Scheme Procedure} reduce-right proc default lst
  509. @code{reduce} is a variant of @code{fold}, where the first call to
  510. @var{proc} is on two elements from @var{lst}, rather than one element
  511. and a given initial value.
  512. If @var{lst} is empty, @code{reduce} returns @var{default} (this is
  513. the only use for @var{default}). If @var{lst} has just one element
  514. then that's the return value. Otherwise @var{proc} is called on the
  515. elements of @var{lst}.
  516. Each @var{proc} call is @code{(@var{proc} @var{elem} @var{previous})},
  517. where @var{elem} is from @var{lst} (the second and subsequent elements
  518. of @var{lst}), and @var{previous} is the return from the previous call
  519. to @var{proc}. The first element of @var{lst} is the @var{previous}
  520. for the first call to @var{proc}.
  521. For example, the following adds a list of numbers, the calls made to
  522. @code{+} are shown. (Of course @code{+} accepts multiple arguments
  523. and can add a list directly, with @code{apply}.)
  524. @example
  525. (reduce + 0 '(5 6 7)) @result{} 18
  526. (+ 6 5) @result{} 11
  527. (+ 7 11) @result{} 18
  528. @end example
  529. @code{reduce} can be used instead of @code{fold} where the @var{init}
  530. value is an ``identity'', meaning a value which under @var{proc}
  531. doesn't change the result, in this case 0 is an identity since
  532. @code{(+ 5 0)} is just 5. @code{reduce} avoids that unnecessary call.
  533. @code{reduce-right} is a similar variation on @code{fold-right},
  534. working from the end (ie.@: the right) of @var{lst}. The last element
  535. of @var{lst} is the @var{previous} for the first call to @var{proc},
  536. and the @var{elem} values go from the second last.
  537. @code{reduce} should be preferred over @code{reduce-right} if the
  538. order of processing doesn't matter, or can be arranged either way,
  539. since @code{reduce} is a little more efficient.
  540. @end deffn
  541. @deffn {Scheme Procedure} unfold p f g seed [tail-gen]
  542. @code{unfold} is defined as follows:
  543. @lisp
  544. (unfold p f g seed) =
  545. (if (p seed) (tail-gen seed)
  546. (cons (f seed)
  547. (unfold p f g (g seed))))
  548. @end lisp
  549. @table @var
  550. @item p
  551. Determines when to stop unfolding.
  552. @item f
  553. Maps each seed value to the corresponding list element.
  554. @item g
  555. Maps each seed value to next seed value.
  556. @item seed
  557. The state value for the unfold.
  558. @item tail-gen
  559. Creates the tail of the list; defaults to @code{(lambda (x) '())}.
  560. @end table
  561. @var{g} produces a series of seed values, which are mapped to list
  562. elements by @var{f}. These elements are put into a list in
  563. left-to-right order, and @var{p} tells when to stop unfolding.
  564. @end deffn
  565. @deffn {Scheme Procedure} unfold-right p f g seed [tail]
  566. Construct a list with the following loop.
  567. @lisp
  568. (let lp ((seed seed) (lis tail))
  569. (if (p seed) lis
  570. (lp (g seed)
  571. (cons (f seed) lis))))
  572. @end lisp
  573. @table @var
  574. @item p
  575. Determines when to stop unfolding.
  576. @item f
  577. Maps each seed value to the corresponding list element.
  578. @item g
  579. Maps each seed value to next seed value.
  580. @item seed
  581. The state value for the unfold.
  582. @item tail
  583. The tail of the list; defaults to @code{'()}.
  584. @end table
  585. @end deffn
  586. @deffn {Scheme Procedure} map f lst1 lst2 @dots{}
  587. Map the procedure over the list(s) @var{lst1}, @var{lst2}, @dots{} and
  588. return a list containing the results of the procedure applications.
  589. This procedure is extended with respect to R5RS, because the argument
  590. lists may have different lengths. The result list will have the same
  591. length as the shortest argument lists. The order in which @var{f}
  592. will be applied to the list element(s) is not specified.
  593. @end deffn
  594. @deffn {Scheme Procedure} for-each f lst1 lst2 @dots{}
  595. Apply the procedure @var{f} to each pair of corresponding elements of
  596. the list(s) @var{lst1}, @var{lst2}, @dots{}. The return value is not
  597. specified. This procedure is extended with respect to R5RS, because
  598. the argument lists may have different lengths. The shortest argument
  599. list determines the number of times @var{f} is called. @var{f} will
  600. be applied to the list elements in left-to-right order.
  601. @end deffn
  602. @deffn {Scheme Procedure} append-map f lst1 lst2 @dots{}
  603. @deffnx {Scheme Procedure} append-map! f lst1 lst2 @dots{}
  604. Equivalent to
  605. @lisp
  606. (apply append (map f clist1 clist2 ...))
  607. @end lisp
  608. and
  609. @lisp
  610. (apply append! (map f clist1 clist2 ...))
  611. @end lisp
  612. Map @var{f} over the elements of the lists, just as in the @code{map}
  613. function. However, the results of the applications are appended
  614. together to make the final result. @code{append-map} uses
  615. @code{append} to append the results together; @code{append-map!} uses
  616. @code{append!}.
  617. The dynamic order in which the various applications of @var{f} are
  618. made is not specified.
  619. @end deffn
  620. @deffn {Scheme Procedure} map! f lst1 lst2 @dots{}
  621. Linear-update variant of @code{map} -- @code{map!} is allowed, but not
  622. required, to alter the cons cells of @var{lst1} to construct the
  623. result list.
  624. The dynamic order in which the various applications of @var{f} are
  625. made is not specified. In the n-ary case, @var{lst2}, @var{lst3},
  626. @dots{} must have at least as many elements as @var{lst1}.
  627. @end deffn
  628. @deffn {Scheme Procedure} pair-for-each f lst1 lst2 @dots{}
  629. Like @code{for-each}, but applies the procedure @var{f} to the pairs
  630. from which the argument lists are constructed, instead of the list
  631. elements. The return value is not specified.
  632. @end deffn
  633. @deffn {Scheme Procedure} filter-map f lst1 lst2 @dots{}
  634. Like @code{map}, but only results from the applications of @var{f}
  635. which are true are saved in the result list.
  636. @end deffn
  637. @node SRFI-1 Filtering and Partitioning
  638. @subsubsection Filtering and Partitioning
  639. @cindex list filter
  640. @cindex list partition
  641. @c FIXME::martin: Review me!
  642. Filtering means to collect all elements from a list which satisfy a
  643. specific condition. Partitioning a list means to make two groups of
  644. list elements, one which contains the elements satisfying a condition,
  645. and the other for the elements which don't.
  646. The @code{filter} and @code{filter!} functions are implemented in the
  647. Guile core, @xref{List Modification}.
  648. @deffn {Scheme Procedure} partition pred lst
  649. @deffnx {Scheme Procedure} partition! pred lst
  650. Split @var{lst} into those elements which do and don't satisfy the
  651. predicate @var{pred}.
  652. The return is two values (@pxref{Multiple Values}), the first being a
  653. list of all elements from @var{lst} which satisfy @var{pred}, the
  654. second a list of those which do not.
  655. The elements in the result lists are in the same order as in @var{lst}
  656. but the order in which the calls @code{(@var{pred} elem)} are made on
  657. the list elements is unspecified.
  658. @code{partition} does not change @var{lst}, but one of the returned
  659. lists may share a tail with it. @code{partition!} may modify
  660. @var{lst} to construct its return.
  661. @end deffn
  662. @deffn {Scheme Procedure} remove pred lst
  663. @deffnx {Scheme Procedure} remove! pred lst
  664. Return a list containing all elements from @var{lst} which do not
  665. satisfy the predicate @var{pred}. The elements in the result list
  666. have the same order as in @var{lst}. The order in which @var{pred} is
  667. applied to the list elements is not specified.
  668. @code{remove!} is allowed, but not required to modify the structure of
  669. the input list.
  670. @end deffn
  671. @node SRFI-1 Searching
  672. @subsubsection Searching
  673. @cindex list search
  674. @c FIXME::martin: Review me!
  675. The procedures for searching elements in lists either accept a
  676. predicate or a comparison object for determining which elements are to
  677. be searched.
  678. @deffn {Scheme Procedure} find pred lst
  679. Return the first element of @var{lst} that satisfies the predicate
  680. @var{pred} and @code{#f} if no such element is found.
  681. @end deffn
  682. @deffn {Scheme Procedure} find-tail pred lst
  683. Return the first pair of @var{lst} whose @sc{car} satisfies the
  684. predicate @var{pred} and @code{#f} if no such element is found.
  685. @end deffn
  686. @deffn {Scheme Procedure} take-while pred lst
  687. @deffnx {Scheme Procedure} take-while! pred lst
  688. Return the longest initial prefix of @var{lst} whose elements all
  689. satisfy the predicate @var{pred}.
  690. @code{take-while!} is allowed, but not required to modify the input
  691. list while producing the result.
  692. @end deffn
  693. @deffn {Scheme Procedure} drop-while pred lst
  694. Drop the longest initial prefix of @var{lst} whose elements all
  695. satisfy the predicate @var{pred}.
  696. @end deffn
  697. @deffn {Scheme Procedure} span pred lst
  698. @deffnx {Scheme Procedure} span! pred lst
  699. @deffnx {Scheme Procedure} break pred lst
  700. @deffnx {Scheme Procedure} break! pred lst
  701. @code{span} splits the list @var{lst} into the longest initial prefix
  702. whose elements all satisfy the predicate @var{pred}, and the remaining
  703. tail. @code{break} inverts the sense of the predicate.
  704. @code{span!} and @code{break!} are allowed, but not required to modify
  705. the structure of the input list @var{lst} in order to produce the
  706. result.
  707. Note that the name @code{break} conflicts with the @code{break}
  708. binding established by @code{while} (@pxref{while do}). Applications
  709. wanting to use @code{break} from within a @code{while} loop will need
  710. to make a new define under a different name.
  711. @end deffn
  712. @deffn {Scheme Procedure} any pred lst1 lst2 @dots{}
  713. Test whether any set of elements from @var{lst1} @var{lst2} @dots{}
  714. satisfies @var{pred}. If so, the return value is the return value from
  715. the successful @var{pred} call, or if not, the return value is
  716. @code{#f}.
  717. If there are n list arguments, then @var{pred} must be a predicate
  718. taking n arguments. Each @var{pred} call is @code{(@var{pred}
  719. @var{elem1} @var{elem2} @dots{} )} taking an element from each
  720. @var{lst}. The calls are made successively for the first, second, etc.
  721. elements of the lists, stopping when @var{pred} returns non-@code{#f},
  722. or when the end of the shortest list is reached.
  723. The @var{pred} call on the last set of elements (i.e., when the end of
  724. the shortest list has been reached), if that point is reached, is a
  725. tail call.
  726. @end deffn
  727. @deffn {Scheme Procedure} every pred lst1 lst2 @dots{}
  728. Test whether every set of elements from @var{lst1} @var{lst2} @dots{}
  729. satisfies @var{pred}. If so, the return value is the return from the
  730. final @var{pred} call, or if not, the return value is @code{#f}.
  731. If there are n list arguments, then @var{pred} must be a predicate
  732. taking n arguments. Each @var{pred} call is @code{(@var{pred}
  733. @var{elem1} @var{elem2 @dots{}})} taking an element from each
  734. @var{lst}. The calls are made successively for the first, second, etc.
  735. elements of the lists, stopping if @var{pred} returns @code{#f}, or when
  736. the end of any of the lists is reached.
  737. The @var{pred} call on the last set of elements (i.e., when the end of
  738. the shortest list has been reached) is a tail call.
  739. If one of @var{lst1} @var{lst2} @dots{}is empty then no calls to
  740. @var{pred} are made, and the return value is @code{#t}.
  741. @end deffn
  742. @deffn {Scheme Procedure} list-index pred lst1 lst2 @dots{}
  743. Return the index of the first set of elements, one from each of
  744. @var{lst1} @var{lst2} @dots{}, which satisfies @var{pred}.
  745. @var{pred} is called as @code{(@var{elem1} @var{elem2 @dots{}})}.
  746. Searching stops when the end of the shortest @var{lst} is reached.
  747. The return index starts from 0 for the first set of elements. If no
  748. set of elements pass, then the return value is @code{#f}.
  749. @example
  750. (list-index odd? '(2 4 6 9)) @result{} 3
  751. (list-index = '(1 2 3) '(3 1 2)) @result{} #f
  752. @end example
  753. @end deffn
  754. @deffn {Scheme Procedure} member x lst [=]
  755. Return the first sublist of @var{lst} whose @sc{car} is equal to
  756. @var{x}. If @var{x} does not appear in @var{lst}, return @code{#f}.
  757. Equality is determined by @code{equal?}, or by the equality predicate
  758. @var{=} if given. @var{=} is called @code{(= @var{x} elem)},
  759. ie.@: with the given @var{x} first, so for example to find the first
  760. element greater than 5,
  761. @example
  762. (member 5 '(3 5 1 7 2 9) <) @result{} (7 2 9)
  763. @end example
  764. This version of @code{member} extends the core @code{member}
  765. (@pxref{List Searching}) by accepting an equality predicate.
  766. @end deffn
  767. @node SRFI-1 Deleting
  768. @subsubsection Deleting
  769. @cindex list delete
  770. @deffn {Scheme Procedure} delete x lst [=]
  771. @deffnx {Scheme Procedure} delete! x lst [=]
  772. Return a list containing the elements of @var{lst} but with those
  773. equal to @var{x} deleted. The returned elements will be in the same
  774. order as they were in @var{lst}.
  775. Equality is determined by the @var{=} predicate, or @code{equal?} if
  776. not given. An equality call is made just once for each element, but
  777. the order in which the calls are made on the elements is unspecified.
  778. The equality calls are always @code{(= x elem)}, ie.@: the given @var{x}
  779. is first. This means for instance elements greater than 5 can be
  780. deleted with @code{(delete 5 lst <)}.
  781. @code{delete} does not modify @var{lst}, but the return might share a
  782. common tail with @var{lst}. @code{delete!} may modify the structure
  783. of @var{lst} to construct its return.
  784. These functions extend the core @code{delete} and @code{delete!}
  785. (@pxref{List Modification}) in accepting an equality predicate. See
  786. also @code{lset-difference} (@pxref{SRFI-1 Set Operations}) for
  787. deleting multiple elements from a list.
  788. @end deffn
  789. @deffn {Scheme Procedure} delete-duplicates lst [=]
  790. @deffnx {Scheme Procedure} delete-duplicates! lst [=]
  791. Return a list containing the elements of @var{lst} but without
  792. duplicates.
  793. When elements are equal, only the first in @var{lst} is retained.
  794. Equal elements can be anywhere in @var{lst}, they don't have to be
  795. adjacent. The returned list will have the retained elements in the
  796. same order as they were in @var{lst}.
  797. Equality is determined by the @var{=} predicate, or @code{equal?} if
  798. not given. Calls @code{(= x y)} are made with element @var{x} being
  799. before @var{y} in @var{lst}. A call is made at most once for each
  800. combination, but the sequence of the calls across the elements is
  801. unspecified.
  802. @code{delete-duplicates} does not modify @var{lst}, but the return
  803. might share a common tail with @var{lst}. @code{delete-duplicates!}
  804. may modify the structure of @var{lst} to construct its return.
  805. In the worst case, this is an @math{O(N^2)} algorithm because it must
  806. check each element against all those preceding it. For long lists it
  807. is more efficient to sort and then compare only adjacent elements.
  808. @end deffn
  809. @node SRFI-1 Association Lists
  810. @subsubsection Association Lists
  811. @cindex association list
  812. @cindex alist
  813. @c FIXME::martin: Review me!
  814. Association lists are described in detail in section @ref{Association
  815. Lists}. The present section only documents the additional procedures
  816. for dealing with association lists defined by SRFI-1.
  817. @deffn {Scheme Procedure} assoc key alist [=]
  818. Return the pair from @var{alist} which matches @var{key}. This
  819. extends the core @code{assoc} (@pxref{Retrieving Alist Entries}) by
  820. taking an optional @var{=} comparison procedure.
  821. The default comparison is @code{equal?}. If an @var{=} parameter is
  822. given it's called @code{(@var{=} @var{key} @var{alistcar})}, i.e.@: the
  823. given target @var{key} is the first argument, and a @code{car} from
  824. @var{alist} is second.
  825. For example a case-insensitive string lookup,
  826. @example
  827. (assoc "yy" '(("XX" . 1) ("YY" . 2)) string-ci=?)
  828. @result{} ("YY" . 2)
  829. @end example
  830. @end deffn
  831. @deffn {Scheme Procedure} alist-cons key datum alist
  832. Cons a new association @var{key} and @var{datum} onto @var{alist} and
  833. return the result. This is equivalent to
  834. @lisp
  835. (cons (cons @var{key} @var{datum}) @var{alist})
  836. @end lisp
  837. @code{acons} (@pxref{Adding or Setting Alist Entries}) in the Guile
  838. core does the same thing.
  839. @end deffn
  840. @deffn {Scheme Procedure} alist-copy alist
  841. Return a newly allocated copy of @var{alist}, that means that the
  842. spine of the list as well as the pairs are copied.
  843. @end deffn
  844. @deffn {Scheme Procedure} alist-delete key alist [=]
  845. @deffnx {Scheme Procedure} alist-delete! key alist [=]
  846. Return a list containing the elements of @var{alist} but with those
  847. elements whose keys are equal to @var{key} deleted. The returned
  848. elements will be in the same order as they were in @var{alist}.
  849. Equality is determined by the @var{=} predicate, or @code{equal?} if
  850. not given. The order in which elements are tested is unspecified, but
  851. each equality call is made @code{(= key alistkey)}, i.e.@: the given
  852. @var{key} parameter is first and the key from @var{alist} second.
  853. This means for instance all associations with a key greater than 5 can
  854. be removed with @code{(alist-delete 5 alist <)}.
  855. @code{alist-delete} does not modify @var{alist}, but the return might
  856. share a common tail with @var{alist}. @code{alist-delete!} may modify
  857. the list structure of @var{alist} to construct its return.
  858. @end deffn
  859. @node SRFI-1 Set Operations
  860. @subsubsection Set Operations on Lists
  861. @cindex list set operation
  862. Lists can be used to represent sets of objects. The procedures in
  863. this section operate on such lists as sets.
  864. Note that lists are not an efficient way to implement large sets. The
  865. procedures here typically take time @math{@var{m}@cross{}@var{n}} when
  866. operating on @var{m} and @var{n} element lists. Other data structures
  867. like trees, bitsets (@pxref{Bit Vectors}) or hash tables (@pxref{Hash
  868. Tables}) are faster.
  869. All these procedures take an equality predicate as the first argument.
  870. This predicate is used for testing the objects in the list sets for
  871. sameness. This predicate must be consistent with @code{eq?}
  872. (@pxref{Equality}) in the sense that if two list elements are
  873. @code{eq?} then they must also be equal under the predicate. This
  874. simply means a given object must be equal to itself.
  875. @deffn {Scheme Procedure} lset<= = list @dots{}
  876. Return @code{#t} if each list is a subset of the one following it.
  877. I.e., @var{list1} is a subset of @var{list2}, @var{list2} is a subset of
  878. @var{list3}, etc., for as many lists as given. If only one list or no
  879. lists are given, the return value is @code{#t}.
  880. A list @var{x} is a subset of @var{y} if each element of @var{x} is
  881. equal to some element in @var{y}. Elements are compared using the
  882. given @var{=} procedure, called as @code{(@var{=} xelem yelem)}.
  883. @example
  884. (lset<= eq?) @result{} #t
  885. (lset<= eqv? '(1 2 3) '(1)) @result{} #f
  886. (lset<= eqv? '(1 3 2) '(4 3 1 2)) @result{} #t
  887. @end example
  888. @end deffn
  889. @deffn {Scheme Procedure} lset= = list @dots{}
  890. Return @code{#t} if all argument lists are set-equal. @var{list1} is
  891. compared to @var{list2}, @var{list2} to @var{list3}, etc., for as many
  892. lists as given. If only one list or no lists are given, the return
  893. value is @code{#t}.
  894. Two lists @var{x} and @var{y} are set-equal if each element of @var{x}
  895. is equal to some element of @var{y} and conversely each element of
  896. @var{y} is equal to some element of @var{x}. The order of the
  897. elements in the lists doesn't matter. Element equality is determined
  898. with the given @var{=} procedure, called as @code{(@var{=} xelem
  899. yelem)}, but exactly which calls are made is unspecified.
  900. @example
  901. (lset= eq?) @result{} #t
  902. (lset= eqv? '(1 2 3) '(3 2 1)) @result{} #t
  903. (lset= string-ci=? '("a" "A" "b") '("B" "b" "a")) @result{} #t
  904. @end example
  905. @end deffn
  906. @deffn {Scheme Procedure} lset-adjoin = list elem @dots{}
  907. Add to @var{list} any of the given @var{elem}s not already in the list.
  908. @var{elem}s are @code{cons}ed onto the start of @var{list} (so the
  909. return value shares a common tail with @var{list}), but the order that
  910. the @var{elem}s are added is unspecified.
  911. The given @var{=} procedure is used for comparing elements, called as
  912. @code{(@var{=} listelem elem)}, i.e., the second argument is one of
  913. the given @var{elem} parameters.
  914. @example
  915. (lset-adjoin eqv? '(1 2 3) 4 1 5) @result{} (5 4 1 2 3)
  916. @end example
  917. @end deffn
  918. @deffn {Scheme Procedure} lset-union = list @dots{}
  919. @deffnx {Scheme Procedure} lset-union! = list @dots{}
  920. Return the union of the argument list sets. The result is built by
  921. taking the union of @var{list1} and @var{list2}, then the union of
  922. that with @var{list3}, etc., for as many lists as given. For one list
  923. argument that list itself is the result, for no list arguments the
  924. result is the empty list.
  925. The union of two lists @var{x} and @var{y} is formed as follows. If
  926. @var{x} is empty then the result is @var{y}. Otherwise start with
  927. @var{x} as the result and consider each @var{y} element (from first to
  928. last). A @var{y} element not equal to something already in the result
  929. is @code{cons}ed onto the result.
  930. The given @var{=} procedure is used for comparing elements, called as
  931. @code{(@var{=} relem yelem)}. The first argument is from the result
  932. accumulated so far, and the second is from the list being union-ed in.
  933. But exactly which calls are made is otherwise unspecified.
  934. Notice that duplicate elements in @var{list1} (or the first non-empty
  935. list) are preserved, but that repeated elements in subsequent lists
  936. are only added once.
  937. @example
  938. (lset-union eqv?) @result{} ()
  939. (lset-union eqv? '(1 2 3)) @result{} (1 2 3)
  940. (lset-union eqv? '(1 2 1 3) '(2 4 5) '(5)) @result{} (5 4 1 2 1 3)
  941. @end example
  942. @code{lset-union} doesn't change the given lists but the result may
  943. share a tail with the first non-empty list. @code{lset-union!} can
  944. modify all of the given lists to form the result.
  945. @end deffn
  946. @deffn {Scheme Procedure} lset-intersection = list1 list2 @dots{}
  947. @deffnx {Scheme Procedure} lset-intersection! = list1 list2 @dots{}
  948. Return the intersection of @var{list1} with the other argument lists,
  949. meaning those elements of @var{list1} which are also in all of
  950. @var{list2} etc. For one list argument, just that list is returned.
  951. The test for an element of @var{list1} to be in the return is simply
  952. that it's equal to some element in each of @var{list2} etc. Notice
  953. this means an element appearing twice in @var{list1} but only once in
  954. each of @var{list2} etc will go into the return twice. The return has
  955. its elements in the same order as they were in @var{list1}.
  956. The given @var{=} procedure is used for comparing elements, called as
  957. @code{(@var{=} elem1 elemN)}. The first argument is from @var{list1}
  958. and the second is from one of the subsequent lists. But exactly which
  959. calls are made and in what order is unspecified.
  960. @example
  961. (lset-intersection eqv? '(x y)) @result{} (x y)
  962. (lset-intersection eqv? '(1 2 3) '(4 3 2)) @result{} (2 3)
  963. (lset-intersection eqv? '(1 1 2 2) '(1 2) '(2 1) '(2)) @result{} (2 2)
  964. @end example
  965. The return from @code{lset-intersection} may share a tail with
  966. @var{list1}. @code{lset-intersection!} may modify @var{list1} to form
  967. its result.
  968. @end deffn
  969. @deffn {Scheme Procedure} lset-difference = list1 list2 @dots{}
  970. @deffnx {Scheme Procedure} lset-difference! = list1 list2 @dots{}
  971. Return @var{list1} with any elements in @var{list2}, @var{list3} etc
  972. removed (ie.@: subtracted). For one list argument, just that list is
  973. returned.
  974. The given @var{=} procedure is used for comparing elements, called as
  975. @code{(@var{=} elem1 elemN)}. The first argument is from @var{list1}
  976. and the second from one of the subsequent lists. But exactly which
  977. calls are made and in what order is unspecified.
  978. @example
  979. (lset-difference eqv? '(x y)) @result{} (x y)
  980. (lset-difference eqv? '(1 2 3) '(3 1)) @result{} (2)
  981. (lset-difference eqv? '(1 2 3) '(3) '(2)) @result{} (1)
  982. @end example
  983. The return from @code{lset-difference} may share a tail with
  984. @var{list1}. @code{lset-difference!} may modify @var{list1} to form
  985. its result.
  986. @end deffn
  987. @deffn {Scheme Procedure} lset-diff+intersection = list1 list2 @dots{}
  988. @deffnx {Scheme Procedure} lset-diff+intersection! = list1 list2 @dots{}
  989. Return two values (@pxref{Multiple Values}), the difference and
  990. intersection of the argument lists as per @code{lset-difference} and
  991. @code{lset-intersection} above.
  992. For two list arguments this partitions @var{list1} into those elements
  993. of @var{list1} which are in @var{list2} and not in @var{list2}. (But
  994. for more than two arguments there can be elements of @var{list1} which
  995. are neither part of the difference nor the intersection.)
  996. One of the return values from @code{lset-diff+intersection} may share
  997. a tail with @var{list1}. @code{lset-diff+intersection!} may modify
  998. @var{list1} to form its results.
  999. @end deffn
  1000. @deffn {Scheme Procedure} lset-xor = list @dots{}
  1001. @deffnx {Scheme Procedure} lset-xor! = list @dots{}
  1002. Return an XOR of the argument lists. For two lists this means those
  1003. elements which are in exactly one of the lists. For more than two
  1004. lists it means those elements which appear in an odd number of the
  1005. lists.
  1006. To be precise, the XOR of two lists @var{x} and @var{y} is formed by
  1007. taking those elements of @var{x} not equal to any element of @var{y},
  1008. plus those elements of @var{y} not equal to any element of @var{x}.
  1009. Equality is determined with the given @var{=} procedure, called as
  1010. @code{(@var{=} e1 e2)}. One argument is from @var{x} and the other
  1011. from @var{y}, but which way around is unspecified. Exactly which
  1012. calls are made is also unspecified, as is the order of the elements in
  1013. the result.
  1014. @example
  1015. (lset-xor eqv? '(x y)) @result{} (x y)
  1016. (lset-xor eqv? '(1 2 3) '(4 3 2)) @result{} (4 1)
  1017. @end example
  1018. The return from @code{lset-xor} may share a tail with one of the list
  1019. arguments. @code{lset-xor!} may modify @var{list1} to form its
  1020. result.
  1021. @end deffn
  1022. @node SRFI-2
  1023. @subsection SRFI-2 - and-let*
  1024. @cindex SRFI-2
  1025. @noindent
  1026. The following syntax can be obtained with
  1027. @lisp
  1028. (use-modules (srfi srfi-2))
  1029. @end lisp
  1030. or alternatively
  1031. @lisp
  1032. (use-modules (ice-9 and-let-star))
  1033. @end lisp
  1034. @deffn {library syntax} and-let* (clause @dots{}) body @dots{}
  1035. A combination of @code{and} and @code{let*}.
  1036. Each @var{clause} is evaluated in turn, and if @code{#f} is obtained
  1037. then evaluation stops and @code{#f} is returned. If all are
  1038. non-@code{#f} then @var{body} is evaluated and the last form gives the
  1039. return value, or if @var{body} is empty then the result is @code{#t}.
  1040. Each @var{clause} should be one of the following,
  1041. @table @code
  1042. @item (symbol expr)
  1043. Evaluate @var{expr}, check for @code{#f}, and bind it to @var{symbol}.
  1044. Like @code{let*}, that binding is available to subsequent clauses.
  1045. @item (expr)
  1046. Evaluate @var{expr} and check for @code{#f}.
  1047. @item symbol
  1048. Get the value bound to @var{symbol} and check for @code{#f}.
  1049. @end table
  1050. Notice that @code{(expr)} has an ``extra'' pair of parentheses, for
  1051. instance @code{((eq? x y))}. One way to remember this is to imagine
  1052. the @code{symbol} in @code{(symbol expr)} is omitted.
  1053. @code{and-let*} is good for calculations where a @code{#f} value means
  1054. termination, but where a non-@code{#f} value is going to be needed in
  1055. subsequent expressions.
  1056. The following illustrates this, it returns text between brackets
  1057. @samp{[...]} in a string, or @code{#f} if there are no such brackets
  1058. (ie.@: either @code{string-index} gives @code{#f}).
  1059. @example
  1060. (define (extract-brackets str)
  1061. (and-let* ((start (string-index str #\[))
  1062. (end (string-index str #\] start)))
  1063. (substring str (1+ start) end)))
  1064. @end example
  1065. The following shows plain variables and expressions tested too.
  1066. @code{diagnostic-levels} is taken to be an alist associating a
  1067. diagnostic type with a level. @code{str} is printed only if the type
  1068. is known and its level is high enough.
  1069. @example
  1070. (define (show-diagnostic type str)
  1071. (and-let* (want-diagnostics
  1072. (level (assq-ref diagnostic-levels type))
  1073. ((>= level current-diagnostic-level)))
  1074. (display str)))
  1075. @end example
  1076. The advantage of @code{and-let*} is that an extended sequence of
  1077. expressions and tests doesn't require lots of nesting as would arise
  1078. from separate @code{and} and @code{let*}, or from @code{cond} with
  1079. @code{=>}.
  1080. @end deffn
  1081. @node SRFI-4
  1082. @subsection SRFI-4 - Homogeneous numeric vector datatypes
  1083. @cindex SRFI-4
  1084. SRFI-4 provides an interface to uniform numeric vectors: vectors whose elements
  1085. are all of a single numeric type. Guile offers uniform numeric vectors for
  1086. signed and unsigned 8-bit, 16-bit, 32-bit, and 64-bit integers, two sizes of
  1087. floating point values, and, as an extension to SRFI-4, complex floating-point
  1088. numbers of these two sizes.
  1089. The standard SRFI-4 procedures and data types may be included via loading the
  1090. appropriate module:
  1091. @example
  1092. (use-modules (srfi srfi-4))
  1093. @end example
  1094. This module is currently a part of the default Guile environment, but it is a
  1095. good practice to explicitly import the module. In the future, using SRFI-4
  1096. procedures without importing the SRFI-4 module will cause a deprecation message
  1097. to be printed. (Of course, one may call the C functions at any time. Would that
  1098. C had modules!)
  1099. @menu
  1100. * SRFI-4 Overview:: The warp and weft of uniform numeric vectors.
  1101. * SRFI-4 API:: Uniform vectors, from Scheme and from C.
  1102. * SRFI-4 and Bytevectors:: SRFI-4 vectors are backed by bytevectors.
  1103. * SRFI-4 Extensions:: Guile-specific extensions to the standard.
  1104. @end menu
  1105. @node SRFI-4 Overview
  1106. @subsubsection SRFI-4 - Overview
  1107. Uniform numeric vectors can be useful since they consume less memory
  1108. than the non-uniform, general vectors. Also, since the types they can
  1109. store correspond directly to C types, it is easier to work with them
  1110. efficiently on a low level. Consider image processing as an example,
  1111. where you want to apply a filter to some image. While you could store
  1112. the pixels of an image in a general vector and write a general
  1113. convolution function, things are much more efficient with uniform
  1114. vectors: the convolution function knows that all pixels are unsigned
  1115. 8-bit values (say), and can use a very tight inner loop.
  1116. This is implemented in Scheme by having the compiler notice calls to the SRFI-4
  1117. accessors, and inline them to appropriate compiled code. From C you have access
  1118. to the raw array; functions for efficiently working with uniform numeric vectors
  1119. from C are listed at the end of this section.
  1120. Uniform numeric vectors are the special case of one dimensional uniform
  1121. numeric arrays.
  1122. There are 12 standard kinds of uniform numeric vectors, and they all have their
  1123. own complement of constructors, accessors, and so on. Procedures that operate on
  1124. a specific kind of uniform numeric vector have a ``tag'' in their name,
  1125. indicating the element type.
  1126. @table @nicode
  1127. @item u8
  1128. unsigned 8-bit integers
  1129. @item s8
  1130. signed 8-bit integers
  1131. @item u16
  1132. unsigned 16-bit integers
  1133. @item s16
  1134. signed 16-bit integers
  1135. @item u32
  1136. unsigned 32-bit integers
  1137. @item s32
  1138. signed 32-bit integers
  1139. @item u64
  1140. unsigned 64-bit integers
  1141. @item s64
  1142. signed 64-bit integers
  1143. @item f32
  1144. the C type @code{float}
  1145. @item f64
  1146. the C type @code{double}
  1147. @end table
  1148. In addition, Guile supports uniform arrays of complex numbers, with the
  1149. nonstandard tags:
  1150. @table @nicode
  1151. @item c32
  1152. complex numbers in rectangular form with the real and imaginary part
  1153. being a @code{float}
  1154. @item c64
  1155. complex numbers in rectangular form with the real and imaginary part
  1156. being a @code{double}
  1157. @end table
  1158. The external representation (ie.@: read syntax) for these vectors is
  1159. similar to normal Scheme vectors, but with an additional tag from the
  1160. tables above indicating the vector's type. For example,
  1161. @lisp
  1162. #u16(1 2 3)
  1163. #f64(3.1415 2.71)
  1164. @end lisp
  1165. Note that the read syntax for floating-point here conflicts with
  1166. @code{#f} for false. In Standard Scheme one can write @code{(1 #f3)}
  1167. for a three element list @code{(1 #f 3)}, but for Guile @code{(1 #f3)}
  1168. is invalid. @code{(1 #f 3)} is almost certainly what one should write
  1169. anyway to make the intention clear, so this is rarely a problem.
  1170. @node SRFI-4 API
  1171. @subsubsection SRFI-4 - API
  1172. Note that the @nicode{c32} and @nicode{c64} functions are only available from
  1173. @nicode{(srfi srfi-4 gnu)}.
  1174. @deffn {Scheme Procedure} u8vector? obj
  1175. @deffnx {Scheme Procedure} s8vector? obj
  1176. @deffnx {Scheme Procedure} u16vector? obj
  1177. @deffnx {Scheme Procedure} s16vector? obj
  1178. @deffnx {Scheme Procedure} u32vector? obj
  1179. @deffnx {Scheme Procedure} s32vector? obj
  1180. @deffnx {Scheme Procedure} u64vector? obj
  1181. @deffnx {Scheme Procedure} s64vector? obj
  1182. @deffnx {Scheme Procedure} f32vector? obj
  1183. @deffnx {Scheme Procedure} f64vector? obj
  1184. @deffnx {Scheme Procedure} c32vector? obj
  1185. @deffnx {Scheme Procedure} c64vector? obj
  1186. @deffnx {C Function} scm_u8vector_p (obj)
  1187. @deffnx {C Function} scm_s8vector_p (obj)
  1188. @deffnx {C Function} scm_u16vector_p (obj)
  1189. @deffnx {C Function} scm_s16vector_p (obj)
  1190. @deffnx {C Function} scm_u32vector_p (obj)
  1191. @deffnx {C Function} scm_s32vector_p (obj)
  1192. @deffnx {C Function} scm_u64vector_p (obj)
  1193. @deffnx {C Function} scm_s64vector_p (obj)
  1194. @deffnx {C Function} scm_f32vector_p (obj)
  1195. @deffnx {C Function} scm_f64vector_p (obj)
  1196. @deffnx {C Function} scm_c32vector_p (obj)
  1197. @deffnx {C Function} scm_c64vector_p (obj)
  1198. Return @code{#t} if @var{obj} is a homogeneous numeric vector of the
  1199. indicated type.
  1200. @end deffn
  1201. @deffn {Scheme Procedure} make-u8vector n [value]
  1202. @deffnx {Scheme Procedure} make-s8vector n [value]
  1203. @deffnx {Scheme Procedure} make-u16vector n [value]
  1204. @deffnx {Scheme Procedure} make-s16vector n [value]
  1205. @deffnx {Scheme Procedure} make-u32vector n [value]
  1206. @deffnx {Scheme Procedure} make-s32vector n [value]
  1207. @deffnx {Scheme Procedure} make-u64vector n [value]
  1208. @deffnx {Scheme Procedure} make-s64vector n [value]
  1209. @deffnx {Scheme Procedure} make-f32vector n [value]
  1210. @deffnx {Scheme Procedure} make-f64vector n [value]
  1211. @deffnx {Scheme Procedure} make-c32vector n [value]
  1212. @deffnx {Scheme Procedure} make-c64vector n [value]
  1213. @deffnx {C Function} scm_make_u8vector (n, value)
  1214. @deffnx {C Function} scm_make_s8vector (n, value)
  1215. @deffnx {C Function} scm_make_u16vector (n, value)
  1216. @deffnx {C Function} scm_make_s16vector (n, value)
  1217. @deffnx {C Function} scm_make_u32vector (n, value)
  1218. @deffnx {C Function} scm_make_s32vector (n, value)
  1219. @deffnx {C Function} scm_make_u64vector (n, value)
  1220. @deffnx {C Function} scm_make_s64vector (n, value)
  1221. @deffnx {C Function} scm_make_f32vector (n, value)
  1222. @deffnx {C Function} scm_make_f64vector (n, value)
  1223. @deffnx {C Function} scm_make_c32vector (n, value)
  1224. @deffnx {C Function} scm_make_c64vector (n, value)
  1225. Return a newly allocated homogeneous numeric vector holding @var{n}
  1226. elements of the indicated type. If @var{value} is given, the vector
  1227. is initialized with that value, otherwise the contents are
  1228. unspecified.
  1229. @end deffn
  1230. @deffn {Scheme Procedure} u8vector value @dots{}
  1231. @deffnx {Scheme Procedure} s8vector value @dots{}
  1232. @deffnx {Scheme Procedure} u16vector value @dots{}
  1233. @deffnx {Scheme Procedure} s16vector value @dots{}
  1234. @deffnx {Scheme Procedure} u32vector value @dots{}
  1235. @deffnx {Scheme Procedure} s32vector value @dots{}
  1236. @deffnx {Scheme Procedure} u64vector value @dots{}
  1237. @deffnx {Scheme Procedure} s64vector value @dots{}
  1238. @deffnx {Scheme Procedure} f32vector value @dots{}
  1239. @deffnx {Scheme Procedure} f64vector value @dots{}
  1240. @deffnx {Scheme Procedure} c32vector value @dots{}
  1241. @deffnx {Scheme Procedure} c64vector value @dots{}
  1242. @deffnx {C Function} scm_u8vector (values)
  1243. @deffnx {C Function} scm_s8vector (values)
  1244. @deffnx {C Function} scm_u16vector (values)
  1245. @deffnx {C Function} scm_s16vector (values)
  1246. @deffnx {C Function} scm_u32vector (values)
  1247. @deffnx {C Function} scm_s32vector (values)
  1248. @deffnx {C Function} scm_u64vector (values)
  1249. @deffnx {C Function} scm_s64vector (values)
  1250. @deffnx {C Function} scm_f32vector (values)
  1251. @deffnx {C Function} scm_f64vector (values)
  1252. @deffnx {C Function} scm_c32vector (values)
  1253. @deffnx {C Function} scm_c64vector (values)
  1254. Return a newly allocated homogeneous numeric vector of the indicated
  1255. type, holding the given parameter @var{value}s. The vector length is
  1256. the number of parameters given.
  1257. @end deffn
  1258. @deffn {Scheme Procedure} u8vector-length vec
  1259. @deffnx {Scheme Procedure} s8vector-length vec
  1260. @deffnx {Scheme Procedure} u16vector-length vec
  1261. @deffnx {Scheme Procedure} s16vector-length vec
  1262. @deffnx {Scheme Procedure} u32vector-length vec
  1263. @deffnx {Scheme Procedure} s32vector-length vec
  1264. @deffnx {Scheme Procedure} u64vector-length vec
  1265. @deffnx {Scheme Procedure} s64vector-length vec
  1266. @deffnx {Scheme Procedure} f32vector-length vec
  1267. @deffnx {Scheme Procedure} f64vector-length vec
  1268. @deffnx {Scheme Procedure} c32vector-length vec
  1269. @deffnx {Scheme Procedure} c64vector-length vec
  1270. @deffnx {C Function} scm_u8vector_length (vec)
  1271. @deffnx {C Function} scm_s8vector_length (vec)
  1272. @deffnx {C Function} scm_u16vector_length (vec)
  1273. @deffnx {C Function} scm_s16vector_length (vec)
  1274. @deffnx {C Function} scm_u32vector_length (vec)
  1275. @deffnx {C Function} scm_s32vector_length (vec)
  1276. @deffnx {C Function} scm_u64vector_length (vec)
  1277. @deffnx {C Function} scm_s64vector_length (vec)
  1278. @deffnx {C Function} scm_f32vector_length (vec)
  1279. @deffnx {C Function} scm_f64vector_length (vec)
  1280. @deffnx {C Function} scm_c32vector_length (vec)
  1281. @deffnx {C Function} scm_c64vector_length (vec)
  1282. Return the number of elements in @var{vec}.
  1283. @end deffn
  1284. @deffn {Scheme Procedure} u8vector-ref vec i
  1285. @deffnx {Scheme Procedure} s8vector-ref vec i
  1286. @deffnx {Scheme Procedure} u16vector-ref vec i
  1287. @deffnx {Scheme Procedure} s16vector-ref vec i
  1288. @deffnx {Scheme Procedure} u32vector-ref vec i
  1289. @deffnx {Scheme Procedure} s32vector-ref vec i
  1290. @deffnx {Scheme Procedure} u64vector-ref vec i
  1291. @deffnx {Scheme Procedure} s64vector-ref vec i
  1292. @deffnx {Scheme Procedure} f32vector-ref vec i
  1293. @deffnx {Scheme Procedure} f64vector-ref vec i
  1294. @deffnx {Scheme Procedure} c32vector-ref vec i
  1295. @deffnx {Scheme Procedure} c64vector-ref vec i
  1296. @deffnx {C Function} scm_u8vector_ref (vec, i)
  1297. @deffnx {C Function} scm_s8vector_ref (vec, i)
  1298. @deffnx {C Function} scm_u16vector_ref (vec, i)
  1299. @deffnx {C Function} scm_s16vector_ref (vec, i)
  1300. @deffnx {C Function} scm_u32vector_ref (vec, i)
  1301. @deffnx {C Function} scm_s32vector_ref (vec, i)
  1302. @deffnx {C Function} scm_u64vector_ref (vec, i)
  1303. @deffnx {C Function} scm_s64vector_ref (vec, i)
  1304. @deffnx {C Function} scm_f32vector_ref (vec, i)
  1305. @deffnx {C Function} scm_f64vector_ref (vec, i)
  1306. @deffnx {C Function} scm_c32vector_ref (vec, i)
  1307. @deffnx {C Function} scm_c64vector_ref (vec, i)
  1308. Return the element at index @var{i} in @var{vec}. The first element
  1309. in @var{vec} is index 0.
  1310. @end deffn
  1311. @deffn {Scheme Procedure} u8vector-set! vec i value
  1312. @deffnx {Scheme Procedure} s8vector-set! vec i value
  1313. @deffnx {Scheme Procedure} u16vector-set! vec i value
  1314. @deffnx {Scheme Procedure} s16vector-set! vec i value
  1315. @deffnx {Scheme Procedure} u32vector-set! vec i value
  1316. @deffnx {Scheme Procedure} s32vector-set! vec i value
  1317. @deffnx {Scheme Procedure} u64vector-set! vec i value
  1318. @deffnx {Scheme Procedure} s64vector-set! vec i value
  1319. @deffnx {Scheme Procedure} f32vector-set! vec i value
  1320. @deffnx {Scheme Procedure} f64vector-set! vec i value
  1321. @deffnx {Scheme Procedure} c32vector-set! vec i value
  1322. @deffnx {Scheme Procedure} c64vector-set! vec i value
  1323. @deffnx {C Function} scm_u8vector_set_x (vec, i, value)
  1324. @deffnx {C Function} scm_s8vector_set_x (vec, i, value)
  1325. @deffnx {C Function} scm_u16vector_set_x (vec, i, value)
  1326. @deffnx {C Function} scm_s16vector_set_x (vec, i, value)
  1327. @deffnx {C Function} scm_u32vector_set_x (vec, i, value)
  1328. @deffnx {C Function} scm_s32vector_set_x (vec, i, value)
  1329. @deffnx {C Function} scm_u64vector_set_x (vec, i, value)
  1330. @deffnx {C Function} scm_s64vector_set_x (vec, i, value)
  1331. @deffnx {C Function} scm_f32vector_set_x (vec, i, value)
  1332. @deffnx {C Function} scm_f64vector_set_x (vec, i, value)
  1333. @deffnx {C Function} scm_c32vector_set_x (vec, i, value)
  1334. @deffnx {C Function} scm_c64vector_set_x (vec, i, value)
  1335. Set the element at index @var{i} in @var{vec} to @var{value}. The
  1336. first element in @var{vec} is index 0. The return value is
  1337. unspecified.
  1338. @end deffn
  1339. @deffn {Scheme Procedure} u8vector->list vec
  1340. @deffnx {Scheme Procedure} s8vector->list vec
  1341. @deffnx {Scheme Procedure} u16vector->list vec
  1342. @deffnx {Scheme Procedure} s16vector->list vec
  1343. @deffnx {Scheme Procedure} u32vector->list vec
  1344. @deffnx {Scheme Procedure} s32vector->list vec
  1345. @deffnx {Scheme Procedure} u64vector->list vec
  1346. @deffnx {Scheme Procedure} s64vector->list vec
  1347. @deffnx {Scheme Procedure} f32vector->list vec
  1348. @deffnx {Scheme Procedure} f64vector->list vec
  1349. @deffnx {Scheme Procedure} c32vector->list vec
  1350. @deffnx {Scheme Procedure} c64vector->list vec
  1351. @deffnx {C Function} scm_u8vector_to_list (vec)
  1352. @deffnx {C Function} scm_s8vector_to_list (vec)
  1353. @deffnx {C Function} scm_u16vector_to_list (vec)
  1354. @deffnx {C Function} scm_s16vector_to_list (vec)
  1355. @deffnx {C Function} scm_u32vector_to_list (vec)
  1356. @deffnx {C Function} scm_s32vector_to_list (vec)
  1357. @deffnx {C Function} scm_u64vector_to_list (vec)
  1358. @deffnx {C Function} scm_s64vector_to_list (vec)
  1359. @deffnx {C Function} scm_f32vector_to_list (vec)
  1360. @deffnx {C Function} scm_f64vector_to_list (vec)
  1361. @deffnx {C Function} scm_c32vector_to_list (vec)
  1362. @deffnx {C Function} scm_c64vector_to_list (vec)
  1363. Return a newly allocated list holding all elements of @var{vec}.
  1364. @end deffn
  1365. @deffn {Scheme Procedure} list->u8vector lst
  1366. @deffnx {Scheme Procedure} list->s8vector lst
  1367. @deffnx {Scheme Procedure} list->u16vector lst
  1368. @deffnx {Scheme Procedure} list->s16vector lst
  1369. @deffnx {Scheme Procedure} list->u32vector lst
  1370. @deffnx {Scheme Procedure} list->s32vector lst
  1371. @deffnx {Scheme Procedure} list->u64vector lst
  1372. @deffnx {Scheme Procedure} list->s64vector lst
  1373. @deffnx {Scheme Procedure} list->f32vector lst
  1374. @deffnx {Scheme Procedure} list->f64vector lst
  1375. @deffnx {Scheme Procedure} list->c32vector lst
  1376. @deffnx {Scheme Procedure} list->c64vector lst
  1377. @deffnx {C Function} scm_list_to_u8vector (lst)
  1378. @deffnx {C Function} scm_list_to_s8vector (lst)
  1379. @deffnx {C Function} scm_list_to_u16vector (lst)
  1380. @deffnx {C Function} scm_list_to_s16vector (lst)
  1381. @deffnx {C Function} scm_list_to_u32vector (lst)
  1382. @deffnx {C Function} scm_list_to_s32vector (lst)
  1383. @deffnx {C Function} scm_list_to_u64vector (lst)
  1384. @deffnx {C Function} scm_list_to_s64vector (lst)
  1385. @deffnx {C Function} scm_list_to_f32vector (lst)
  1386. @deffnx {C Function} scm_list_to_f64vector (lst)
  1387. @deffnx {C Function} scm_list_to_c32vector (lst)
  1388. @deffnx {C Function} scm_list_to_c64vector (lst)
  1389. Return a newly allocated homogeneous numeric vector of the indicated type,
  1390. initialized with the elements of the list @var{lst}.
  1391. @end deffn
  1392. @deftypefn {C Function} SCM scm_take_u8vector (const scm_t_uint8 *data, size_t len)
  1393. @deftypefnx {C Function} SCM scm_take_s8vector (const scm_t_int8 *data, size_t len)
  1394. @deftypefnx {C Function} SCM scm_take_u16vector (const scm_t_uint16 *data, size_t len)
  1395. @deftypefnx {C Function} SCM scm_take_s16vector (const scm_t_int16 *data, size_t len)
  1396. @deftypefnx {C Function} SCM scm_take_u32vector (const scm_t_uint32 *data, size_t len)
  1397. @deftypefnx {C Function} SCM scm_take_s32vector (const scm_t_int32 *data, size_t len)
  1398. @deftypefnx {C Function} SCM scm_take_u64vector (const scm_t_uint64 *data, size_t len)
  1399. @deftypefnx {C Function} SCM scm_take_s64vector (const scm_t_int64 *data, size_t len)
  1400. @deftypefnx {C Function} SCM scm_take_f32vector (const float *data, size_t len)
  1401. @deftypefnx {C Function} SCM scm_take_f64vector (const double *data, size_t len)
  1402. @deftypefnx {C Function} SCM scm_take_c32vector (const float *data, size_t len)
  1403. @deftypefnx {C Function} SCM scm_take_c64vector (const double *data, size_t len)
  1404. Return a new uniform numeric vector of the indicated type and length
  1405. that uses the memory pointed to by @var{data} to store its elements.
  1406. This memory will eventually be freed with @code{free}. The argument
  1407. @var{len} specifies the number of elements in @var{data}, not its size
  1408. in bytes.
  1409. The @code{c32} and @code{c64} variants take a pointer to a C array of
  1410. @code{float}s or @code{double}s. The real parts of the complex numbers
  1411. are at even indices in that array, the corresponding imaginary parts are
  1412. at the following odd index.
  1413. @end deftypefn
  1414. @deftypefn {C Function} {const scm_t_uint8 *} scm_u8vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1415. @deftypefnx {C Function} {const scm_t_int8 *} scm_s8vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1416. @deftypefnx {C Function} {const scm_t_uint16 *} scm_u16vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1417. @deftypefnx {C Function} {const scm_t_int16 *} scm_s16vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1418. @deftypefnx {C Function} {const scm_t_uint32 *} scm_u32vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1419. @deftypefnx {C Function} {const scm_t_int32 *} scm_s32vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1420. @deftypefnx {C Function} {const scm_t_uint64 *} scm_u64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1421. @deftypefnx {C Function} {const scm_t_int64 *} scm_s64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1422. @deftypefnx {C Function} {const float *} scm_f32vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1423. @deftypefnx {C Function} {const double *} scm_f64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1424. @deftypefnx {C Function} {const float *} scm_c32vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1425. @deftypefnx {C Function} {const double *} scm_c64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1426. Like @code{scm_vector_elements} (@pxref{Vector Accessing from C}), but
  1427. returns a pointer to the elements of a uniform numeric vector of the
  1428. indicated kind.
  1429. @end deftypefn
  1430. @deftypefn {C Function} {scm_t_uint8 *} scm_u8vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1431. @deftypefnx {C Function} {scm_t_int8 *} scm_s8vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1432. @deftypefnx {C Function} {scm_t_uint16 *} scm_u16vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1433. @deftypefnx {C Function} {scm_t_int16 *} scm_s16vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1434. @deftypefnx {C Function} {scm_t_uint32 *} scm_u32vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1435. @deftypefnx {C Function} {scm_t_int32 *} scm_s32vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1436. @deftypefnx {C Function} {scm_t_uint64 *} scm_u64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1437. @deftypefnx {C Function} {scm_t_int64 *} scm_s64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1438. @deftypefnx {C Function} {float *} scm_f32vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1439. @deftypefnx {C Function} {double *} scm_f64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1440. @deftypefnx {C Function} {float *} scm_c32vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1441. @deftypefnx {C Function} {double *} scm_c64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1442. Like @code{scm_vector_writable_elements} (@pxref{Vector Accessing from C}),
  1443. but returns a pointer to the elements of a uniform numeric vector of the
  1444. indicated kind.
  1445. @end deftypefn
  1446. @node SRFI-4 and Bytevectors
  1447. @subsubsection SRFI-4 - Relation to bytevectors
  1448. Guile implements SRFI-4 vectors using bytevectors (@pxref{Bytevectors}). Often
  1449. when you have a numeric vector, you end up wanting to write its bytes somewhere,
  1450. or have access to the underlying bytes, or read in bytes from somewhere else.
  1451. Bytevectors are very good at this sort of thing. But the SRFI-4 APIs are nicer
  1452. to use when doing number-crunching, because they are addressed by element and
  1453. not by byte.
  1454. So as a compromise, Guile allows all bytevector functions to operate on numeric
  1455. vectors. They address the underlying bytes in the native endianness, as one
  1456. would expect.
  1457. Following the same reasoning, that it's just bytes underneath, Guile also allows
  1458. uniform vectors of a given type to be accessed as if they were of any type. One
  1459. can fill a @nicode{u32vector}, and access its elements with
  1460. @nicode{u8vector-ref}. One can use @nicode{f64vector-ref} on bytevectors. It's
  1461. all the same to Guile.
  1462. In this way, uniform numeric vectors may be written to and read from
  1463. input/output ports using the procedures that operate on bytevectors.
  1464. @xref{Bytevectors}, for more information.
  1465. @node SRFI-4 Extensions
  1466. @subsubsection SRFI-4 - Guile extensions
  1467. Guile defines some useful extensions to SRFI-4, which are not available in the
  1468. default Guile environment. They may be imported by loading the extensions
  1469. module:
  1470. @example
  1471. (use-modules (srfi srfi-4 gnu))
  1472. @end example
  1473. @deffn {Scheme Procedure} srfi-4-vector-type-size obj
  1474. Return the size, in bytes, of each element of SRFI-4 vector
  1475. @var{obj}. For example, @code{(srfi-4-vector-type-size #u32())} returns
  1476. @code{4}.
  1477. @end deffn
  1478. @deffn {Scheme Procedure} any->u8vector obj
  1479. @deffnx {Scheme Procedure} any->s8vector obj
  1480. @deffnx {Scheme Procedure} any->u16vector obj
  1481. @deffnx {Scheme Procedure} any->s16vector obj
  1482. @deffnx {Scheme Procedure} any->u32vector obj
  1483. @deffnx {Scheme Procedure} any->s32vector obj
  1484. @deffnx {Scheme Procedure} any->u64vector obj
  1485. @deffnx {Scheme Procedure} any->s64vector obj
  1486. @deffnx {Scheme Procedure} any->f32vector obj
  1487. @deffnx {Scheme Procedure} any->f64vector obj
  1488. @deffnx {Scheme Procedure} any->c32vector obj
  1489. @deffnx {Scheme Procedure} any->c64vector obj
  1490. @deffnx {C Function} scm_any_to_u8vector (obj)
  1491. @deffnx {C Function} scm_any_to_s8vector (obj)
  1492. @deffnx {C Function} scm_any_to_u16vector (obj)
  1493. @deffnx {C Function} scm_any_to_s16vector (obj)
  1494. @deffnx {C Function} scm_any_to_u32vector (obj)
  1495. @deffnx {C Function} scm_any_to_s32vector (obj)
  1496. @deffnx {C Function} scm_any_to_u64vector (obj)
  1497. @deffnx {C Function} scm_any_to_s64vector (obj)
  1498. @deffnx {C Function} scm_any_to_f32vector (obj)
  1499. @deffnx {C Function} scm_any_to_f64vector (obj)
  1500. @deffnx {C Function} scm_any_to_c32vector (obj)
  1501. @deffnx {C Function} scm_any_to_c64vector (obj)
  1502. Return a (maybe newly allocated) uniform numeric vector of the indicated
  1503. type, initialized with the elements of @var{obj}, which must be a list,
  1504. a vector, or a uniform vector. When @var{obj} is already a suitable
  1505. uniform numeric vector, it is returned unchanged.
  1506. @end deffn
  1507. @deffn {Scheme Procedure} u8vector-copy! dst at src [start [end]]
  1508. @deffnx {Scheme Procedure} s8vector-copy! dst at src [start [end]]
  1509. @deffnx {Scheme Procedure} u16vector-copy! dst at src [start [end]]
  1510. @deffnx {Scheme Procedure} s16vector-copy! dst at src [start [end]]
  1511. @deffnx {Scheme Procedure} u32vector-copy! dst at src [start [end]]
  1512. @deffnx {Scheme Procedure} s32vector-copy! dst at src [start [end]]
  1513. @deffnx {Scheme Procedure} u64vector-copy! dst at src [start [end]]
  1514. @deffnx {Scheme Procedure} s64vector-copy! dst at src [start [end]]
  1515. @deffnx {Scheme Procedure} f32vector-copy! dst at src [start [end]]
  1516. @deffnx {Scheme Procedure} f64vector-copy! dst at src [start [end]]
  1517. @deffnx {Scheme Procedure} c32vector-copy! dst at src [start [end]]
  1518. @deffnx {Scheme Procedure} c64vector-copy! dst at src [start [end]]
  1519. Copy a block of elements from @var{src} to @var{dst}, both of which must
  1520. be vectors of the indicated type, starting in @var{dst} at @var{at} and
  1521. starting in @var{src} at @var{start} and ending at @var{end}. It is an
  1522. error for @var{dst} to have a length less than @var{at} + (@var{end} -
  1523. @var{start}). @var{at} and @var{start} default to 0 and @var{end}
  1524. defaults to the length of @var{src}.
  1525. If source and destination overlap, copying takes place as if the
  1526. source is first copied into a temporary vector and then into the
  1527. destination.
  1528. See also @ref{x-vector-copy!,@code{vector-copy!}}.
  1529. @end deffn
  1530. @deffn {Scheme Procedure} u8vector-copy src [start [end]]
  1531. @deffnx {Scheme Procedure} s8vector-copy src [start [end]]
  1532. @deffnx {Scheme Procedure} u16vector-copy src [start [end]]
  1533. @deffnx {Scheme Procedure} s16vector-copy src [start [end]]
  1534. @deffnx {Scheme Procedure} u32vector-copy src [start [end]]
  1535. @deffnx {Scheme Procedure} s32vector-copy src [start [end]]
  1536. @deffnx {Scheme Procedure} u64vector-copy src [start [end]]
  1537. @deffnx {Scheme Procedure} s64vector-copy src [start [end]]
  1538. @deffnx {Scheme Procedure} f32vector-copy src [start [end]]
  1539. @deffnx {Scheme Procedure} f64vector-copy src [start [end]]
  1540. @deffnx {Scheme Procedure} c32vector-copy src [start [end]]
  1541. @deffnx {Scheme Procedure} c64vector-copy src [start [end]]
  1542. Returns a freshly allocated vector of the indicated type, which must be
  1543. the same as that of @var{src}, containing the elements of @var{src}
  1544. between @var{start} and @var{end}. @var{start} defaults to 0 and
  1545. @var{end} defaults to the length of @var{src}.
  1546. See also @ref{x-vector-copy,@code{vector-copy}}.
  1547. @end deffn
  1548. @node SRFI-6
  1549. @subsection SRFI-6 - Basic String Ports
  1550. @cindex SRFI-6
  1551. SRFI-6 defines the procedures @code{open-input-string},
  1552. @code{open-output-string} and @code{get-output-string}. These
  1553. procedures are included in the Guile core, so using this module does not
  1554. make any difference at the moment. But it is possible that support for
  1555. SRFI-6 will be factored out of the core library in the future, so using
  1556. this module does not hurt, after all.
  1557. @node SRFI-8
  1558. @subsection SRFI-8 - receive
  1559. @cindex SRFI-8
  1560. @code{receive} is a syntax for making the handling of multiple-value
  1561. procedures easier. It is documented in @xref{Multiple Values}.
  1562. @node SRFI-9
  1563. @subsection SRFI-9 - define-record-type
  1564. This SRFI is a syntax for defining new record types and creating
  1565. predicate, constructor, and field getter and setter functions. It is
  1566. documented in the ``Data Types'' section of the manual (@pxref{SRFI-9
  1567. Records}).
  1568. @node SRFI-10
  1569. @subsection SRFI-10 - Hash-Comma Reader Extension
  1570. @cindex SRFI-10
  1571. @cindex hash-comma
  1572. @cindex #,()
  1573. This SRFI implements a reader extension @code{#,()} called hash-comma.
  1574. It allows the reader to give new kinds of objects, for use both in data
  1575. and as constants or literals in source code. This feature is available
  1576. with
  1577. @example
  1578. (use-modules (srfi srfi-10))
  1579. @end example
  1580. @noindent
  1581. The new read syntax is of the form
  1582. @example
  1583. #,(@var{tag} @var{arg}@dots{})
  1584. @end example
  1585. @noindent
  1586. where @var{tag} is a symbol and the @var{arg}s are objects taken as
  1587. parameters. @var{tag}s are registered with the following procedure.
  1588. @deffn {Scheme Procedure} define-reader-ctor tag proc
  1589. Register @var{proc} as the constructor for a hash-comma read syntax
  1590. starting with symbol @var{tag}, i.e.@: @nicode{#,(@var{tag} arg@dots{})}.
  1591. @var{proc} is called with the given arguments @code{(@var{proc}
  1592. arg@dots{})} and the object it returns is the result of the read.
  1593. @end deffn
  1594. @noindent
  1595. For example, a syntax giving a list of @var{N} copies of an object.
  1596. @example
  1597. (define-reader-ctor 'repeat
  1598. (lambda (obj reps)
  1599. (make-list reps obj)))
  1600. (display '#,(repeat 99 3))
  1601. @print{} (99 99 99)
  1602. @end example
  1603. Notice the quote @nicode{'} when the @nicode{#,( )} is used. The
  1604. @code{repeat} handler returns a list and the program must quote to use
  1605. it literally, the same as any other list. Ie.
  1606. @example
  1607. (display '#,(repeat 99 3))
  1608. @result{}
  1609. (display '(99 99 99))
  1610. @end example
  1611. When a handler returns an object which is self-evaluating, like a
  1612. number or a string, then there's no need for quoting, just as there's
  1613. no need when giving those directly as literals. For example an
  1614. addition,
  1615. @example
  1616. (define-reader-ctor 'sum
  1617. (lambda (x y)
  1618. (+ x y)))
  1619. (display #,(sum 123 456)) @print{} 579
  1620. @end example
  1621. Once @code{(srfi srfi-10)} has loaded, @nicode{#,()} is available
  1622. globally, there's no need to use @code{(srfi srfi-10)} in later
  1623. modules. Similarly the tags registered are global and can be used
  1624. anywhere once registered.
  1625. We do not recommend @nicode{#,()} reader extensions, however, and for
  1626. three reasons.
  1627. First of all, this SRFI is not modular: the tag is matched by name, not
  1628. as an identifier within a scope. Defining a reader extension in one
  1629. part of a program can thus affect unrelated parts of a program because
  1630. the tag is not scoped.
  1631. Secondly, reader extensions can be hard to manage from a time
  1632. perspective: when does the reader extension take effect? @xref{Eval
  1633. When}, for more discussion.
  1634. Finally, reader extensions can easily produce objects that can't be
  1635. reified to an object file by the compiler. For example if you define a
  1636. reader extension that makes a hash table (@pxref{Hash Tables}), then it
  1637. will work fine when run with the interpreter, and you think you have a
  1638. neat hack. But then if you try to compile your program, after wrangling
  1639. with the @code{eval-when} concerns mentioned above, the compiler will
  1640. carp that it doesn't know how to serialize a hash table to disk.
  1641. In the specific case of hash tables, it would be possible for Guile to
  1642. know how to pack hash tables into compiled files, but this doesn't work
  1643. in general. What if the object you produce is an instance of a record
  1644. type? Guile would then have to serialize the record type to disk too,
  1645. and then what happens if the program independently loads the code that
  1646. defines the record type? Does it define the same type or a different
  1647. type? Guile's record types are nominal, not structural, so the answer
  1648. is not clear at all.
  1649. For all of these reasons we recommend macros over reader extensions.
  1650. Macros fulfill many of the same needs while preserving modular
  1651. composition, and their interaction with @code{eval-when} is well-known.
  1652. If you need brevity, instead use @code{read-hash-extend} and make your
  1653. reader extension expand to a macro invocation. In that way we preserve
  1654. scoping as much as possible. @xref{Reader Extensions}.
  1655. @node SRFI-11
  1656. @subsection SRFI-11 - let-values
  1657. @cindex SRFI-11
  1658. @findex let-values
  1659. @findex let*-values
  1660. This module implements the binding forms for multiple values
  1661. @code{let-values} and @code{let*-values}. These forms are similar to
  1662. @code{let} and @code{let*} (@pxref{Local Bindings}), but they support
  1663. binding of the values returned by multiple-valued expressions.
  1664. Write @code{(use-modules (srfi srfi-11))} to make the bindings
  1665. available.
  1666. @lisp
  1667. (let-values (((x y) (values 1 2))
  1668. ((z f) (values 3 4)))
  1669. (+ x y z f))
  1670. @result{}
  1671. 10
  1672. @end lisp
  1673. @code{let-values} performs all bindings simultaneously, which means that
  1674. no expression in the binding clauses may refer to variables bound in the
  1675. same clause list. @code{let*-values}, on the other hand, performs the
  1676. bindings sequentially, just like @code{let*} does for single-valued
  1677. expressions.
  1678. @node SRFI-13
  1679. @subsection SRFI-13 - String Library
  1680. @cindex SRFI-13
  1681. The SRFI-13 procedures are always available, @xref{Strings}.
  1682. @node SRFI-14
  1683. @subsection SRFI-14 - Character-set Library
  1684. @cindex SRFI-14
  1685. The SRFI-14 data type and procedures are always available,
  1686. @xref{Character Sets}.
  1687. @node SRFI-16
  1688. @subsection SRFI-16 - case-lambda
  1689. @cindex SRFI-16
  1690. @cindex variable arity
  1691. @cindex arity, variable
  1692. SRFI-16 defines a variable-arity @code{lambda} form,
  1693. @code{case-lambda}. This form is available in the default Guile
  1694. environment. @xref{Case-lambda}, for more information.
  1695. @node SRFI-17
  1696. @subsection SRFI-17 - Generalized set!
  1697. @cindex SRFI-17
  1698. This SRFI implements a generalized @code{set!}, allowing some
  1699. ``referencing'' functions to be used as the target location of a
  1700. @code{set!}. This feature is available from
  1701. @example
  1702. (use-modules (srfi srfi-17))
  1703. @end example
  1704. @noindent
  1705. For example @code{vector-ref} is extended so that
  1706. @example
  1707. (set! (vector-ref vec idx) new-value)
  1708. @end example
  1709. @noindent
  1710. is equivalent to
  1711. @example
  1712. (vector-set! vec idx new-value)
  1713. @end example
  1714. The idea is that a @code{vector-ref} expression identifies a location,
  1715. which may be either fetched or stored. The same form is used for the
  1716. location in both cases, encouraging visual clarity. This is similar
  1717. to the idea of an ``lvalue'' in C.
  1718. The mechanism for this kind of @code{set!} is in the Guile core
  1719. (@pxref{Procedures with Setters}). This module adds definitions of
  1720. the following functions as procedures with setters, allowing them to
  1721. be targets of a @code{set!},
  1722. @quotation
  1723. @nicode{car}, @nicode{cdr}, @nicode{caar}, @nicode{cadr},
  1724. @nicode{cdar}, @nicode{cddr}, @nicode{caaar}, @nicode{caadr},
  1725. @nicode{cadar}, @nicode{caddr}, @nicode{cdaar}, @nicode{cdadr},
  1726. @nicode{cddar}, @nicode{cdddr}, @nicode{caaaar}, @nicode{caaadr},
  1727. @nicode{caadar}, @nicode{caaddr}, @nicode{cadaar}, @nicode{cadadr},
  1728. @nicode{caddar}, @nicode{cadddr}, @nicode{cdaaar}, @nicode{cdaadr},
  1729. @nicode{cdadar}, @nicode{cdaddr}, @nicode{cddaar}, @nicode{cddadr},
  1730. @nicode{cdddar}, @nicode{cddddr}
  1731. @nicode{string-ref}, @nicode{vector-ref}
  1732. @end quotation
  1733. The SRFI specifies @code{setter} (@pxref{Procedures with Setters}) as
  1734. a procedure with setter, allowing the setter for a procedure to be
  1735. changed, eg.@: @code{(set! (setter foo) my-new-setter-handler)}.
  1736. Currently Guile does not implement this, a setter can only be
  1737. specified on creation (@code{getter-with-setter} below).
  1738. @defun getter-with-setter
  1739. The same as the Guile core @code{make-procedure-with-setter}
  1740. (@pxref{Procedures with Setters}).
  1741. @end defun
  1742. @node SRFI-18
  1743. @subsection SRFI-18 - Multithreading support
  1744. @cindex SRFI-18
  1745. This is an implementation of the SRFI-18 threading and synchronization
  1746. library. The functions and variables described here are provided by
  1747. @example
  1748. (use-modules (srfi srfi-18))
  1749. @end example
  1750. SRFI-18 defines facilities for threads, mutexes, condition variables,
  1751. time, and exception handling. Because these facilities are at a higher
  1752. level than Guile's primitives, they are implemented as a layer on top of
  1753. what Guile provides. In particular this means that a Guile mutex is not
  1754. a SRFI-18 mutex, and a Guile thread is not a SRFI-18 thread, and so on.
  1755. Guile provides a set of primitives and SRFI-18 is one of the systems built in terms of those primitives.
  1756. @menu
  1757. * SRFI-18 Threads:: Executing code
  1758. * SRFI-18 Mutexes:: Mutual exclusion devices
  1759. * SRFI-18 Condition variables:: Synchronizing of groups of threads
  1760. * SRFI-18 Time:: Representation of times and durations
  1761. * SRFI-18 Exceptions:: Signalling and handling errors
  1762. @end menu
  1763. @node SRFI-18 Threads
  1764. @subsubsection SRFI-18 Threads
  1765. Threads created by SRFI-18 differ in two ways from threads created by
  1766. Guile's built-in thread functions. First, a thread created by SRFI-18
  1767. @code{make-thread} begins in a blocked state and will not start
  1768. execution until @code{thread-start!} is called on it. Second, SRFI-18
  1769. threads are constructed with a top-level exception handler that
  1770. captures any exceptions that are thrown on thread exit.
  1771. SRFI-18 threads are disjoint from Guile's primitive threads.
  1772. @xref{Threads}, for more on Guile's primitive facility.
  1773. @defun current-thread
  1774. Returns the thread that called this function. This is the same
  1775. procedure as the same-named built-in procedure @code{current-thread}
  1776. (@pxref{Threads}).
  1777. @end defun
  1778. @defun thread? obj
  1779. Returns @code{#t} if @var{obj} is a thread, @code{#f} otherwise. This
  1780. is the same procedure as the same-named built-in procedure
  1781. @code{thread?} (@pxref{Threads}).
  1782. @end defun
  1783. @defun make-thread thunk [name]
  1784. Call @code{thunk} in a new thread and with a new dynamic state,
  1785. returning the new thread and optionally assigning it the object name
  1786. @var{name}, which may be any Scheme object.
  1787. Note that the name @code{make-thread} conflicts with the
  1788. @code{(ice-9 threads)} function @code{make-thread}. Applications
  1789. wanting to use both of these functions will need to refer to them by
  1790. different names.
  1791. @end defun
  1792. @defun thread-name thread
  1793. Returns the name assigned to @var{thread} at the time of its creation,
  1794. or @code{#f} if it was not given a name.
  1795. @end defun
  1796. @defun thread-specific thread
  1797. @defunx thread-specific-set! thread obj
  1798. Get or set the ``object-specific'' property of @var{thread}. In
  1799. Guile's implementation of SRFI-18, this value is stored as an object
  1800. property, and will be @code{#f} if not set.
  1801. @end defun
  1802. @defun thread-start! thread
  1803. Unblocks @var{thread} and allows it to begin execution if it has not
  1804. done so already.
  1805. @end defun
  1806. @defun thread-yield!
  1807. If one or more threads are waiting to execute, calling
  1808. @code{thread-yield!} forces an immediate context switch to one of them.
  1809. Otherwise, @code{thread-yield!} has no effect. @code{thread-yield!}
  1810. behaves identically to the Guile built-in function @code{yield}.
  1811. @end defun
  1812. @defun thread-sleep! timeout
  1813. The current thread waits until the point specified by the time object
  1814. @var{timeout} is reached (@pxref{SRFI-18 Time}). This blocks the
  1815. thread only if @var{timeout} represents a point in the future. it is
  1816. an error for @var{timeout} to be @code{#f}.
  1817. @end defun
  1818. @defun thread-terminate! thread
  1819. Causes an abnormal termination of @var{thread}. If @var{thread} is
  1820. not already terminated, all mutexes owned by @var{thread} become
  1821. unlocked/abandoned. If @var{thread} is the current thread,
  1822. @code{thread-terminate!} does not return. Otherwise
  1823. @code{thread-terminate!} returns an unspecified value; the termination
  1824. of @var{thread} will occur before @code{thread-terminate!} returns.
  1825. Subsequent attempts to join on @var{thread} will cause a ``terminated
  1826. thread exception'' to be raised.
  1827. @code{thread-terminate!} is compatible with the thread cancellation
  1828. procedures in the core threads API (@pxref{Threads}) in that if a
  1829. cleanup handler has been installed for the target thread, it will be
  1830. called before the thread exits and its return value (or exception, if
  1831. any) will be stored for later retrieval via a call to
  1832. @code{thread-join!}.
  1833. @end defun
  1834. @defun thread-join! thread [timeout [timeout-val]]
  1835. Wait for @var{thread} to terminate and return its exit value. When a
  1836. time value @var{timeout} is given, it specifies a point in time where
  1837. the waiting should be aborted. When the waiting is aborted,
  1838. @var{timeout-val} is returned if it is specified; otherwise, a
  1839. @code{join-timeout-exception} exception is raised
  1840. (@pxref{SRFI-18 Exceptions}). Exceptions may also be raised if the
  1841. thread was terminated by a call to @code{thread-terminate!}
  1842. (@code{terminated-thread-exception} will be raised) or if the thread
  1843. exited by raising an exception that was handled by the top-level
  1844. exception handler (@code{uncaught-exception} will be raised; the
  1845. original exception can be retrieved using
  1846. @code{uncaught-exception-reason}).
  1847. @end defun
  1848. @node SRFI-18 Mutexes
  1849. @subsubsection SRFI-18 Mutexes
  1850. SRFI-18 mutexes are disjoint from Guile's primitive mutexes.
  1851. @xref{Mutexes and Condition Variables}, for more on Guile's primitive
  1852. facility.
  1853. @defun make-mutex [name]
  1854. Returns a new mutex, optionally assigning it the object name @var{name},
  1855. which may be any Scheme object. The returned mutex will be created with
  1856. the configuration described above.
  1857. @end defun
  1858. @defun mutex-name mutex
  1859. Returns the name assigned to @var{mutex} at the time of its creation, or
  1860. @code{#f} if it was not given a name.
  1861. @end defun
  1862. @defun mutex-specific mutex
  1863. Return the ``object-specific'' property of @var{mutex}, or @code{#f} if
  1864. none is set.
  1865. @end defun
  1866. @defun mutex-specific-set! mutex obj
  1867. Set the ``object-specific'' property of @var{mutex}.
  1868. @end defun
  1869. @defun mutex-state mutex
  1870. Returns information about the state of @var{mutex}. Possible values
  1871. are:
  1872. @itemize @bullet
  1873. @item
  1874. thread @var{t}: the mutex is in the locked/owned state and thread
  1875. @var{t} is the owner of the mutex
  1876. @item
  1877. symbol @code{not-owned}: the mutex is in the locked/not-owned state
  1878. @item
  1879. symbol @code{abandoned}: the mutex is in the unlocked/abandoned state
  1880. @item
  1881. symbol @code{not-abandoned}: the mutex is in the
  1882. unlocked/not-abandoned state
  1883. @end itemize
  1884. @end defun
  1885. @defun mutex-lock! mutex [timeout [thread]]
  1886. Lock @var{mutex}, optionally specifying a time object @var{timeout}
  1887. after which to abort the lock attempt and a thread @var{thread} giving
  1888. a new owner for @var{mutex} different than the current thread.
  1889. @end defun
  1890. @defun mutex-unlock! mutex [condition-variable [timeout]]
  1891. Unlock @var{mutex}, optionally specifying a condition variable
  1892. @var{condition-variable} on which to wait, either indefinitely or,
  1893. optionally, until the time object @var{timeout} has passed, to be
  1894. signaled.
  1895. @end defun
  1896. @node SRFI-18 Condition variables
  1897. @subsubsection SRFI-18 Condition variables
  1898. SRFI-18 does not specify a ``wait'' function for condition variables.
  1899. Waiting on a condition variable can be simulated using the SRFI-18
  1900. @code{mutex-unlock!} function described in the previous section.
  1901. SRFI-18 condition variables are disjoint from Guile's primitive
  1902. condition variables. @xref{Mutexes and Condition Variables}, for more
  1903. on Guile's primitive facility.
  1904. @defun condition-variable? obj
  1905. Returns @code{#t} if @var{obj} is a condition variable, @code{#f}
  1906. otherwise.
  1907. @end defun
  1908. @defun make-condition-variable [name]
  1909. Returns a new condition variable, optionally assigning it the object
  1910. name @var{name}, which may be any Scheme object.
  1911. @end defun
  1912. @defun condition-variable-name condition-variable
  1913. Returns the name assigned to @var{condition-variable} at the time of its
  1914. creation, or @code{#f} if it was not given a name.
  1915. @end defun
  1916. @defun condition-variable-specific condition-variable
  1917. Return the ``object-specific'' property of @var{condition-variable}, or
  1918. @code{#f} if none is set.
  1919. @end defun
  1920. @defun condition-variable-specific-set! condition-variable obj
  1921. Set the ``object-specific'' property of @var{condition-variable}.
  1922. @end defun
  1923. @defun condition-variable-signal! condition-variable
  1924. @defunx condition-variable-broadcast! condition-variable
  1925. Wake up one thread that is waiting for @var{condition-variable}, in
  1926. the case of @code{condition-variable-signal!}, or all threads waiting
  1927. for it, in the case of @code{condition-variable-broadcast!}.
  1928. @end defun
  1929. @node SRFI-18 Time
  1930. @subsubsection SRFI-18 Time
  1931. The SRFI-18 time functions manipulate time in two formats: a
  1932. ``time object'' type that represents an absolute point in time in some
  1933. implementation-specific way; and the number of seconds since some
  1934. unspecified ``epoch''. In Guile's implementation, the epoch is the
  1935. Unix epoch, 00:00:00 UTC, January 1, 1970.
  1936. @defun current-time
  1937. Return the current time as a time object. This procedure replaces
  1938. the procedure of the same name in the core library, which returns the
  1939. current time in seconds since the epoch.
  1940. @end defun
  1941. @defun time? obj
  1942. Returns @code{#t} if @var{obj} is a time object, @code{#f} otherwise.
  1943. @end defun
  1944. @defun time->seconds time
  1945. @defunx seconds->time seconds
  1946. Convert between time objects and numerical values representing the
  1947. number of seconds since the epoch. When converting from a time object
  1948. to seconds, the return value is the number of seconds between
  1949. @var{time} and the epoch. When converting from seconds to a time
  1950. object, the return value is a time object that represents a time
  1951. @var{seconds} seconds after the epoch.
  1952. @end defun
  1953. @node SRFI-18 Exceptions
  1954. @subsubsection SRFI-18 Exceptions
  1955. SRFI-18 exceptions are identical to the exceptions provided by
  1956. Guile's implementation of SRFI-34. The behavior of exception
  1957. handlers invoked to handle exceptions thrown from SRFI-18 functions,
  1958. however, differs from the conventional behavior of SRFI-34 in that
  1959. the continuation of the handler is the same as that of the call to
  1960. the function. Handlers are called in a tail-recursive manner; the
  1961. exceptions do not ``bubble up''.
  1962. @defun current-exception-handler
  1963. Returns the current exception handler.
  1964. @end defun
  1965. @defun with-exception-handler handler thunk
  1966. Installs @var{handler} as the current exception handler and calls the
  1967. procedure @var{thunk} with no arguments, returning its value as the
  1968. value of the exception. @var{handler} must be a procedure that accepts
  1969. a single argument. The current exception handler at the time this
  1970. procedure is called will be restored after the call returns.
  1971. @end defun
  1972. @defun raise obj
  1973. Raise @var{obj} as an exception. This is the same procedure as the
  1974. same-named procedure defined in SRFI 34.
  1975. @end defun
  1976. @defun join-timeout-exception? obj
  1977. Returns @code{#t} if @var{obj} is an exception raised as the result of
  1978. performing a timed join on a thread that does not exit within the
  1979. specified timeout, @code{#f} otherwise.
  1980. @end defun
  1981. @defun abandoned-mutex-exception? obj
  1982. Returns @code{#t} if @var{obj} is an exception raised as the result of
  1983. attempting to lock a mutex that has been abandoned by its owner thread,
  1984. @code{#f} otherwise.
  1985. @end defun
  1986. @defun terminated-thread-exception? obj
  1987. Returns @code{#t} if @var{obj} is an exception raised as the result of
  1988. joining on a thread that exited as the result of a call to
  1989. @code{thread-terminate!}.
  1990. @end defun
  1991. @defun uncaught-exception? obj
  1992. @defunx uncaught-exception-reason exc
  1993. @code{uncaught-exception?} returns @code{#t} if @var{obj} is an
  1994. exception thrown as the result of joining a thread that exited by
  1995. raising an exception that was handled by the top-level exception
  1996. handler installed by @code{make-thread}. When this occurs, the
  1997. original exception is preserved as part of the exception thrown by
  1998. @code{thread-join!} and can be accessed by calling
  1999. @code{uncaught-exception-reason} on that exception. Note that
  2000. because this exception-preservation mechanism is a side-effect of
  2001. @code{make-thread}, joining on threads that exited as described above
  2002. but were created by other means will not raise this
  2003. @code{uncaught-exception} error.
  2004. @end defun
  2005. @node SRFI-19
  2006. @subsection SRFI-19 - Time/Date Library
  2007. @cindex SRFI-19
  2008. @cindex time
  2009. @cindex date
  2010. This is an implementation of the SRFI-19 time/date library. The
  2011. functions and variables described here are provided by
  2012. @example
  2013. (use-modules (srfi srfi-19))
  2014. @end example
  2015. @menu
  2016. * SRFI-19 Introduction::
  2017. * SRFI-19 Time::
  2018. * SRFI-19 Date::
  2019. * SRFI-19 Time/Date conversions::
  2020. * SRFI-19 Date to string::
  2021. * SRFI-19 String to date::
  2022. @end menu
  2023. @node SRFI-19 Introduction
  2024. @subsubsection SRFI-19 Introduction
  2025. @cindex universal time
  2026. @cindex atomic time
  2027. @cindex UTC
  2028. @cindex TAI
  2029. This module implements time and date representations and calculations,
  2030. in various time systems, including Coordinated Universal Time (UTC)
  2031. and International Atomic Time (TAI).
  2032. For those not familiar with these time systems, TAI is based on a
  2033. fixed length second derived from oscillations of certain atoms. UTC
  2034. differs from TAI by an integral number of seconds, which is increased
  2035. or decreased at announced times to keep UTC aligned to a mean solar
  2036. day (the orbit and rotation of the earth are not quite constant).
  2037. @cindex leap second
  2038. So far, only increases in the TAI
  2039. @tex
  2040. $\leftrightarrow$
  2041. @end tex
  2042. @ifnottex
  2043. <->
  2044. @end ifnottex
  2045. UTC difference have been needed. Such an increase is a ``leap
  2046. second'', an extra second of TAI introduced at the end of a UTC day.
  2047. When working entirely within UTC this is never seen, every day simply
  2048. has 86400 seconds. But when converting from TAI to a UTC date, an
  2049. extra 23:59:60 is present, where normally a day would end at 23:59:59.
  2050. Effectively the UTC second from 23:59:59 to 00:00:00 has taken two TAI
  2051. seconds.
  2052. @cindex system clock
  2053. In the current implementation, the system clock is assumed to be UTC,
  2054. and a table of leap seconds in the code converts to TAI. See comments
  2055. in @file{srfi-19.scm} for how to update this table.
  2056. @cindex julian day
  2057. @cindex modified julian day
  2058. Also, for those not familiar with the terminology, a @dfn{Julian Day}
  2059. represents a point in time as a real number of days since
  2060. -4713-11-24T12:00:00Z, i.e.@: midday UT on 24 November 4714 BC in the
  2061. proleptic Gregorian calendar (1 January 4713 BC in the proleptic Julian
  2062. calendar).
  2063. A @dfn{Modified Julian Day} represents a point in time as a real number
  2064. of days since 1858-11-17T00:00:00Z, i.e.@: midnight UT on Wednesday 17
  2065. November AD 1858. That time is julian day 2400000.5.
  2066. @node SRFI-19 Time
  2067. @subsubsection SRFI-19 Time
  2068. @cindex time
  2069. A @dfn{time} object has type, seconds and nanoseconds fields
  2070. representing a point in time starting from some epoch. This is an
  2071. arbitrary point in time, not just a time of day. Although times are
  2072. represented in nanoseconds, the actual resolution may be lower.
  2073. The following variables hold the possible time types. For instance
  2074. @code{(current-time time-process)} would give the current CPU process
  2075. time.
  2076. @defvar time-utc
  2077. Universal Coordinated Time (UTC).
  2078. @cindex UTC
  2079. @end defvar
  2080. @defvar time-tai
  2081. International Atomic Time (TAI).
  2082. @cindex TAI
  2083. @end defvar
  2084. @defvar time-monotonic
  2085. Monotonic time, meaning a monotonically increasing time starting from
  2086. an unspecified epoch.
  2087. Note that in the current implementation @code{time-monotonic} is the
  2088. same as @code{time-tai}, and unfortunately is therefore affected by
  2089. adjustments to the system clock. Perhaps this will change in the
  2090. future.
  2091. @end defvar
  2092. @defvar time-duration
  2093. A duration, meaning simply a difference between two times.
  2094. @end defvar
  2095. @defvar time-process
  2096. CPU time spent in the current process, starting from when the process
  2097. began.
  2098. @cindex process time
  2099. @end defvar
  2100. @defvar time-thread
  2101. CPU time spent in the current thread. Not currently implemented.
  2102. @cindex thread time
  2103. @end defvar
  2104. @sp 1
  2105. @defun time? obj
  2106. Return @code{#t} if @var{obj} is a time object, or @code{#f} if not.
  2107. @end defun
  2108. @defun make-time type nanoseconds seconds
  2109. Create a time object with the given @var{type}, @var{seconds} and
  2110. @var{nanoseconds}.
  2111. @end defun
  2112. @defun time-type time
  2113. @defunx time-nanosecond time
  2114. @defunx time-second time
  2115. @defunx set-time-type! time type
  2116. @defunx set-time-nanosecond! time nsec
  2117. @defunx set-time-second! time sec
  2118. Get or set the type, seconds or nanoseconds fields of a time object.
  2119. @code{set-time-type!} merely changes the field, it doesn't convert the
  2120. time value. For conversions, see @ref{SRFI-19 Time/Date conversions}.
  2121. @end defun
  2122. @defun copy-time time
  2123. Return a new time object, which is a copy of the given @var{time}.
  2124. @end defun
  2125. @defun current-time [type]
  2126. Return the current time of the given @var{type}. The default
  2127. @var{type} is @code{time-utc}.
  2128. Note that the name @code{current-time} conflicts with the Guile core
  2129. @code{current-time} function (@pxref{Time}) as well as the SRFI-18
  2130. @code{current-time} function (@pxref{SRFI-18 Time}). Applications
  2131. wanting to use more than one of these functions will need to refer to
  2132. them by different names.
  2133. @end defun
  2134. @defun time-resolution [type]
  2135. Return the resolution, in nanoseconds, of the given time @var{type}.
  2136. The default @var{type} is @code{time-utc}.
  2137. @end defun
  2138. @defun time<=? t1 t2
  2139. @defunx time<? t1 t2
  2140. @defunx time=? t1 t2
  2141. @defunx time>=? t1 t2
  2142. @defunx time>? t1 t2
  2143. Return @code{#t} or @code{#f} according to the respective relation
  2144. between time objects @var{t1} and @var{t2}. @var{t1} and @var{t2}
  2145. must be the same time type.
  2146. @end defun
  2147. @defun time-difference t1 t2
  2148. @defunx time-difference! t1 t2
  2149. Return a time object of type @code{time-duration} representing the
  2150. period between @var{t1} and @var{t2}. @var{t1} and @var{t2} must be
  2151. the same time type.
  2152. @code{time-difference} returns a new time object,
  2153. @code{time-difference!} may modify @var{t1} to form its return.
  2154. @end defun
  2155. @defun add-duration time duration
  2156. @defunx add-duration! time duration
  2157. @defunx subtract-duration time duration
  2158. @defunx subtract-duration! time duration
  2159. Return a time object which is @var{time} with the given @var{duration}
  2160. added or subtracted. @var{duration} must be a time object of type
  2161. @code{time-duration}.
  2162. @code{add-duration} and @code{subtract-duration} return a new time
  2163. object. @code{add-duration!} and @code{subtract-duration!} may modify
  2164. the given @var{time} to form their return.
  2165. @end defun
  2166. @node SRFI-19 Date
  2167. @subsubsection SRFI-19 Date
  2168. @cindex date
  2169. A @dfn{date} object represents a date in the Gregorian calendar and a
  2170. time of day on that date in some timezone.
  2171. The fields are year, month, day, hour, minute, second, nanoseconds and
  2172. timezone. A date object is immutable, its fields can be read but they
  2173. cannot be modified once the object is created.
  2174. Historically, the Gregorian calendar was only used from the latter part
  2175. of the year 1582 onwards, and not until even later in many countries.
  2176. Prior to that most countries used the Julian calendar. SRFI-19 does
  2177. not deal with the Julian calendar at all, and so does not reflect this
  2178. historical calendar reform. Instead it projects the Gregorian calendar
  2179. back proleptically as far as necessary. When dealing with historical
  2180. data, especially prior to the British Empire's adoption of the Gregorian
  2181. calendar in 1752, one should be mindful of which calendar is used in
  2182. each context, and apply non-SRFI-19 facilities to convert where necessary.
  2183. @defun date? obj
  2184. Return @code{#t} if @var{obj} is a date object, or @code{#f} if not.
  2185. @end defun
  2186. @defun make-date nsecs seconds minutes hours date month year zone-offset
  2187. Create a new date object.
  2188. @c
  2189. @c FIXME: What can we say about the ranges of the values. The
  2190. @c current code looks it doesn't normalize, but expects then in their
  2191. @c usual range already.
  2192. @c
  2193. @end defun
  2194. @defun date-nanosecond date
  2195. Nanoseconds, 0 to 999999999.
  2196. @end defun
  2197. @defun date-second date
  2198. Seconds, 0 to 59, or 60 for a leap second. 60 is never seen when working
  2199. entirely within UTC, it's only when converting to or from TAI.
  2200. @end defun
  2201. @defun date-minute date
  2202. Minutes, 0 to 59.
  2203. @end defun
  2204. @defun date-hour date
  2205. Hour, 0 to 23.
  2206. @end defun
  2207. @defun date-day date
  2208. Day of the month, 1 to 31 (or less, according to the month).
  2209. @end defun
  2210. @defun date-month date
  2211. Month, 1 to 12.
  2212. @end defun
  2213. @defun date-year date
  2214. Year, eg.@: 2003. Dates B.C.@: are negative, eg.@: @math{-46} is 46
  2215. B.C. There is no year 0, year @math{-1} is followed by year 1.
  2216. @end defun
  2217. @defun date-zone-offset date
  2218. Time zone, an integer number of seconds east of Greenwich.
  2219. @end defun
  2220. @defun date-year-day date
  2221. Day of the year, starting from 1 for 1st January.
  2222. @end defun
  2223. @defun date-week-day date
  2224. Day of the week, starting from 0 for Sunday.
  2225. @end defun
  2226. @defun date-week-number date dstartw
  2227. Week of the year, ignoring a first partial week. @var{dstartw} is the
  2228. day of the week which is taken to start a week, 0 for Sunday, 1 for
  2229. Monday, etc.
  2230. @c
  2231. @c FIXME: The spec doesn't say whether numbering starts at 0 or 1.
  2232. @c The code looks like it's 0, if that's the correct intention.
  2233. @c
  2234. @end defun
  2235. @c The SRFI text doesn't actually give the default for tz-offset, but
  2236. @c the reference implementation has the local timezone and the
  2237. @c conversions functions all specify that, so it should be ok to
  2238. @c document it here.
  2239. @c
  2240. @defun current-date [tz-offset]
  2241. Return a date object representing the current date/time, in UTC offset
  2242. by @var{tz-offset}. @var{tz-offset} is seconds east of Greenwich and
  2243. defaults to the local timezone.
  2244. @end defun
  2245. @defun current-julian-day
  2246. @cindex julian day
  2247. Return the current Julian Day.
  2248. @end defun
  2249. @defun current-modified-julian-day
  2250. @cindex modified julian day
  2251. Return the current Modified Julian Day.
  2252. @end defun
  2253. @node SRFI-19 Time/Date conversions
  2254. @subsubsection SRFI-19 Time/Date conversions
  2255. @cindex time conversion
  2256. @cindex date conversion
  2257. @defun date->julian-day date
  2258. @defunx date->modified-julian-day date
  2259. @defunx date->time-monotonic date
  2260. @defunx date->time-tai date
  2261. @defunx date->time-utc date
  2262. @end defun
  2263. @defun julian-day->date jdn [tz-offset]
  2264. @defunx julian-day->time-monotonic jdn
  2265. @defunx julian-day->time-tai jdn
  2266. @defunx julian-day->time-utc jdn
  2267. @end defun
  2268. @defun modified-julian-day->date jdn [tz-offset]
  2269. @defunx modified-julian-day->time-monotonic jdn
  2270. @defunx modified-julian-day->time-tai jdn
  2271. @defunx modified-julian-day->time-utc jdn
  2272. @end defun
  2273. @defun time-monotonic->date time [tz-offset]
  2274. @defunx time-monotonic->time-tai time
  2275. @defunx time-monotonic->time-tai! time
  2276. @defunx time-monotonic->time-utc time
  2277. @defunx time-monotonic->time-utc! time
  2278. @end defun
  2279. @defun time-tai->date time [tz-offset]
  2280. @defunx time-tai->julian-day time
  2281. @defunx time-tai->modified-julian-day time
  2282. @defunx time-tai->time-monotonic time
  2283. @defunx time-tai->time-monotonic! time
  2284. @defunx time-tai->time-utc time
  2285. @defunx time-tai->time-utc! time
  2286. @end defun
  2287. @defun time-utc->date time [tz-offset]
  2288. @defunx time-utc->julian-day time
  2289. @defunx time-utc->modified-julian-day time
  2290. @defunx time-utc->time-monotonic time
  2291. @defunx time-utc->time-monotonic! time
  2292. @defunx time-utc->time-tai time
  2293. @defunx time-utc->time-tai! time
  2294. @sp 1
  2295. Convert between dates, times and days of the respective types. For
  2296. instance @code{time-tai->time-utc} accepts a @var{time} object of type
  2297. @code{time-tai} and returns an object of type @code{time-utc}.
  2298. The @code{!} variants may modify their @var{time} argument to form
  2299. their return. The plain functions create a new object.
  2300. For conversions to dates, @var{tz-offset} is seconds east of
  2301. Greenwich. The default is the local timezone, at the given time, as
  2302. provided by the system, using @code{localtime} (@pxref{Time}).
  2303. On 32-bit systems, @code{localtime} is limited to a 32-bit
  2304. @code{time_t}, so a default @var{tz-offset} is only available for
  2305. times between Dec 1901 and Jan 2038. For prior dates an application
  2306. might like to use the value in 1902, though some locations have zone
  2307. changes prior to that. For future dates an application might like to
  2308. assume today's rules extend indefinitely. But for correct daylight
  2309. savings transitions it will be necessary to take an offset for the
  2310. same day and time but a year in range and which has the same starting
  2311. weekday and same leap/non-leap (to support rules like last Sunday in
  2312. October).
  2313. @end defun
  2314. @node SRFI-19 Date to string
  2315. @subsubsection SRFI-19 Date to string
  2316. @cindex date to string
  2317. @cindex string, from date
  2318. @defun date->string date [format]
  2319. Convert a date to a string under the control of a format.
  2320. @var{format} should be a string containing @samp{~} escapes, which
  2321. will be expanded as per the following conversion table. The default
  2322. @var{format} is @samp{~c}, a locale-dependent date and time.
  2323. Many of these conversion characters are the same as POSIX
  2324. @code{strftime} (@pxref{Time}), but there are some extras and some
  2325. variations.
  2326. @multitable {MMMM} {MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM}
  2327. @item @nicode{~~} @tab literal ~
  2328. @item @nicode{~a} @tab locale abbreviated weekday, eg.@: @samp{Sun}
  2329. @item @nicode{~A} @tab locale full weekday, eg.@: @samp{Sunday}
  2330. @item @nicode{~b} @tab locale abbreviated month, eg.@: @samp{Jan}
  2331. @item @nicode{~B} @tab locale full month, eg.@: @samp{January}
  2332. @item @nicode{~c} @tab locale date and time, eg.@: @*
  2333. @samp{Fri Jul 14 20:28:42-0400 2000}
  2334. @item @nicode{~d} @tab day of month, zero padded, @samp{01} to @samp{31}
  2335. @c Spec says d/m/y, reference implementation says m/d/y.
  2336. @c Apparently the reference code was the intention, but would like to
  2337. @c see an errata published for the spec before contradicting it here.
  2338. @c
  2339. @c @item @nicode{~D} @tab date @nicode{~d/~m/~y}
  2340. @item @nicode{~e} @tab day of month, blank padded, @samp{ 1} to @samp{31}
  2341. @item @nicode{~f} @tab seconds and fractional seconds,
  2342. with locale decimal point, eg.@: @samp{5.2}
  2343. @item @nicode{~h} @tab same as @nicode{~b}
  2344. @item @nicode{~H} @tab hour, 24-hour clock, zero padded, @samp{00} to @samp{23}
  2345. @item @nicode{~I} @tab hour, 12-hour clock, zero padded, @samp{01} to @samp{12}
  2346. @item @nicode{~j} @tab day of year, zero padded, @samp{001} to @samp{366}
  2347. @item @nicode{~k} @tab hour, 24-hour clock, blank padded, @samp{ 0} to @samp{23}
  2348. @item @nicode{~l} @tab hour, 12-hour clock, blank padded, @samp{ 1} to @samp{12}
  2349. @item @nicode{~m} @tab month, zero padded, @samp{01} to @samp{12}
  2350. @item @nicode{~M} @tab minute, zero padded, @samp{00} to @samp{59}
  2351. @item @nicode{~n} @tab newline
  2352. @item @nicode{~N} @tab nanosecond, zero padded, @samp{000000000} to @samp{999999999}
  2353. @item @nicode{~p} @tab locale AM or PM
  2354. @item @nicode{~r} @tab time, 12 hour clock, @samp{~I:~M:~S ~p}
  2355. @item @nicode{~s} @tab number of full seconds since ``the epoch'' in UTC
  2356. @item @nicode{~S} @tab second, zero padded @samp{00} to @samp{60} @*
  2357. (usual limit is 59, 60 is a leap second)
  2358. @item @nicode{~t} @tab horizontal tab character
  2359. @item @nicode{~T} @tab time, 24 hour clock, @samp{~H:~M:~S}
  2360. @item @nicode{~U} @tab week of year, Sunday first day of week,
  2361. @samp{00} to @samp{52}
  2362. @item @nicode{~V} @tab week of year, Monday first day of week,
  2363. @samp{01} to @samp{53}
  2364. @item @nicode{~w} @tab day of week, 0 for Sunday, @samp{0} to @samp{6}
  2365. @item @nicode{~W} @tab week of year, Monday first day of week,
  2366. @samp{00} to @samp{52}
  2367. @c The spec has ~x as an apparent duplicate of ~W, and ~X as a locale
  2368. @c date. The reference code has ~x as the locale date and ~X as a
  2369. @c locale time. The rule is apparently that the code should be
  2370. @c believed, but would like to see an errata for the spec before
  2371. @c contradicting it here.
  2372. @c
  2373. @c @item @nicode{~x} @tab week of year, Monday as first day of week,
  2374. @c @samp{00} to @samp{53}
  2375. @c @item @nicode{~X} @tab locale date, eg.@: @samp{07/31/00}
  2376. @item @nicode{~y} @tab year, two digits, @samp{00} to @samp{99}
  2377. @item @nicode{~Y} @tab year, full, eg.@: @samp{2003}
  2378. @item @nicode{~z} @tab time zone, RFC-822 style
  2379. @item @nicode{~Z} @tab time zone symbol (not currently implemented)
  2380. @item @nicode{~1} @tab ISO-8601 date, @samp{~Y-~m-~d}
  2381. @item @nicode{~2} @tab ISO-8601 time+zone, @samp{~H:~M:~S~z}
  2382. @item @nicode{~3} @tab ISO-8601 time, @samp{~H:~M:~S}
  2383. @item @nicode{~4} @tab ISO-8601 date/time+zone, @samp{~Y-~m-~dT~H:~M:~S~z}
  2384. @item @nicode{~5} @tab ISO-8601 date/time, @samp{~Y-~m-~dT~H:~M:~S}
  2385. @end multitable
  2386. @end defun
  2387. Conversions @samp{~D}, @samp{~x} and @samp{~X} are not currently
  2388. described here, since the specification and reference implementation
  2389. differ.
  2390. Conversion is locale-dependent on systems that support it
  2391. (@pxref{Accessing Locale Information}). @xref{Locales,
  2392. @code{setlocale}}, for information on how to change the current
  2393. locale.
  2394. @node SRFI-19 String to date
  2395. @subsubsection SRFI-19 String to date
  2396. @cindex string to date
  2397. @cindex date, from string
  2398. @c FIXME: Can we say what happens when an incomplete date is
  2399. @c converted? I.e. fields left as 0, or what? The spec seems to be
  2400. @c silent on this.
  2401. @defun string->date input template
  2402. Convert an @var{input} string to a date under the control of a
  2403. @var{template} string. Return a newly created date object.
  2404. Literal characters in @var{template} must match characters in
  2405. @var{input} and @samp{~} escapes must match the input forms described
  2406. in the table below. ``Skip to'' means characters up to one of the
  2407. given type are ignored, or ``no skip'' for no skipping. ``Read'' is
  2408. what's then read, and ``Set'' is the field affected in the date
  2409. object.
  2410. For example @samp{~Y} skips input characters until a digit is reached,
  2411. at which point it expects a year and stores that to the year field of
  2412. the date.
  2413. @multitable {MMMM} {@nicode{char-alphabetic?}} {MMMMMMMMMMMMMMMMMMMMMMMMM} {@nicode{date-zone-offset}}
  2414. @item
  2415. @tab Skip to
  2416. @tab Read
  2417. @tab Set
  2418. @item @nicode{~~}
  2419. @tab no skip
  2420. @tab literal ~
  2421. @tab nothing
  2422. @item @nicode{~a}
  2423. @tab @nicode{char-alphabetic?}
  2424. @tab locale abbreviated weekday name
  2425. @tab nothing
  2426. @item @nicode{~A}
  2427. @tab @nicode{char-alphabetic?}
  2428. @tab locale full weekday name
  2429. @tab nothing
  2430. @c Note that the SRFI spec says that ~b and ~B don't set anything,
  2431. @c but that looks like a mistake. The reference implementation sets
  2432. @c the month field, which seems sensible and is what we describe
  2433. @c here.
  2434. @item @nicode{~b}
  2435. @tab @nicode{char-alphabetic?}
  2436. @tab locale abbreviated month name
  2437. @tab @nicode{date-month}
  2438. @item @nicode{~B}
  2439. @tab @nicode{char-alphabetic?}
  2440. @tab locale full month name
  2441. @tab @nicode{date-month}
  2442. @item @nicode{~d}
  2443. @tab @nicode{char-numeric?}
  2444. @tab day of month
  2445. @tab @nicode{date-day}
  2446. @item @nicode{~e}
  2447. @tab no skip
  2448. @tab day of month, blank padded
  2449. @tab @nicode{date-day}
  2450. @item @nicode{~h}
  2451. @tab same as @samp{~b}
  2452. @item @nicode{~H}
  2453. @tab @nicode{char-numeric?}
  2454. @tab hour
  2455. @tab @nicode{date-hour}
  2456. @item @nicode{~k}
  2457. @tab no skip
  2458. @tab hour, blank padded
  2459. @tab @nicode{date-hour}
  2460. @item @nicode{~m}
  2461. @tab @nicode{char-numeric?}
  2462. @tab month
  2463. @tab @nicode{date-month}
  2464. @item @nicode{~M}
  2465. @tab @nicode{char-numeric?}
  2466. @tab minute
  2467. @tab @nicode{date-minute}
  2468. @item @nicode{~N}
  2469. @tab @nicode{char-numeric?}
  2470. @tab nanosecond
  2471. @tab @nicode{date-nanosecond}
  2472. @item @nicode{~S}
  2473. @tab @nicode{char-numeric?}
  2474. @tab second
  2475. @tab @nicode{date-second}
  2476. @item @nicode{~y}
  2477. @tab no skip
  2478. @tab 2-digit year
  2479. @tab @nicode{date-year} within 50 years
  2480. @item @nicode{~Y}
  2481. @tab @nicode{char-numeric?}
  2482. @tab year
  2483. @tab @nicode{date-year}
  2484. @item @nicode{~z}
  2485. @tab no skip
  2486. @tab time zone
  2487. @tab date-zone-offset
  2488. @end multitable
  2489. Notice that the weekday matching forms don't affect the date object
  2490. returned, instead the weekday will be derived from the day, month and
  2491. year.
  2492. Conversion is locale-dependent on systems that support it
  2493. (@pxref{Accessing Locale Information}). @xref{Locales,
  2494. @code{setlocale}}, for information on how to change the current
  2495. locale.
  2496. @end defun
  2497. @node SRFI-23
  2498. @subsection SRFI-23 - Error Reporting
  2499. @cindex SRFI-23
  2500. The SRFI-23 @code{error} procedure is always available.
  2501. @node SRFI-26
  2502. @subsection SRFI-26 - specializing parameters
  2503. @cindex SRFI-26
  2504. @cindex parameter specialize
  2505. @cindex argument specialize
  2506. @cindex specialize parameter
  2507. This SRFI provides a syntax for conveniently specializing selected
  2508. parameters of a function. It can be used with,
  2509. @example
  2510. (use-modules (srfi srfi-26))
  2511. @end example
  2512. @deffn {library syntax} cut slot1 slot2 @dots{}
  2513. @deffnx {library syntax} cute slot1 slot2 @dots{}
  2514. Return a new procedure which will make a call (@var{slot1} @var{slot2}
  2515. @dots{}) but with selected parameters specialized to given expressions.
  2516. An example will illustrate the idea. The following is a
  2517. specialization of @code{write}, sending output to
  2518. @code{my-output-port},
  2519. @example
  2520. (cut write <> my-output-port)
  2521. @result{}
  2522. (lambda (obj) (write obj my-output-port))
  2523. @end example
  2524. The special symbol @code{<>} indicates a slot to be filled by an
  2525. argument to the new procedure. @code{my-output-port} on the other
  2526. hand is an expression to be evaluated and passed, ie.@: it specializes
  2527. the behaviour of @code{write}.
  2528. @table @nicode
  2529. @item <>
  2530. A slot to be filled by an argument from the created procedure.
  2531. Arguments are assigned to @code{<>} slots in the order they appear in
  2532. the @code{cut} form, there's no way to re-arrange arguments.
  2533. The first argument to @code{cut} is usually a procedure (or expression
  2534. giving a procedure), but @code{<>} is allowed there too. For example,
  2535. @example
  2536. (cut <> 1 2 3)
  2537. @result{}
  2538. (lambda (proc) (proc 1 2 3))
  2539. @end example
  2540. @item <...>
  2541. A slot to be filled by all remaining arguments from the new procedure.
  2542. This can only occur at the end of a @code{cut} form.
  2543. For example, a procedure taking a variable number of arguments like
  2544. @code{max} but in addition enforcing a lower bound,
  2545. @example
  2546. (define my-lower-bound 123)
  2547. (cut max my-lower-bound <...>)
  2548. @result{}
  2549. (lambda arglist (apply max my-lower-bound arglist))
  2550. @end example
  2551. @end table
  2552. For @code{cut} the specializing expressions are evaluated each time
  2553. the new procedure is called. For @code{cute} they're evaluated just
  2554. once, when the new procedure is created. The name @code{cute} stands
  2555. for ``@code{cut} with evaluated arguments''. In all cases the
  2556. evaluations take place in an unspecified order.
  2557. The following illustrates the difference between @code{cut} and
  2558. @code{cute},
  2559. @example
  2560. (cut format <> "the time is ~s" (current-time))
  2561. @result{}
  2562. (lambda (port) (format port "the time is ~s" (current-time)))
  2563. (cute format <> "the time is ~s" (current-time))
  2564. @result{}
  2565. (let ((val (current-time)))
  2566. (lambda (port) (format port "the time is ~s" val))
  2567. @end example
  2568. (There's no provision for a mixture of @code{cut} and @code{cute}
  2569. where some expressions would be evaluated every time but others
  2570. evaluated only once.)
  2571. @code{cut} is really just a shorthand for the sort of @code{lambda}
  2572. forms shown in the above examples. But notice @code{cut} avoids the
  2573. need to name unspecialized parameters, and is more compact. Use in
  2574. functional programming style or just with @code{map}, @code{for-each}
  2575. or similar is typical.
  2576. @example
  2577. (map (cut * 2 <>) '(1 2 3 4))
  2578. (for-each (cut write <> my-port) my-list)
  2579. @end example
  2580. @end deffn
  2581. @node SRFI-27
  2582. @subsection SRFI-27 - Sources of Random Bits
  2583. @cindex SRFI-27
  2584. This subsection is based on the
  2585. @uref{http://srfi.schemers.org/srfi-27/srfi-27.html, specification of
  2586. SRFI-27} written by Sebastian Egner.
  2587. @c The copyright notice and license text of the SRFI-27 specification is
  2588. @c reproduced below:
  2589. @c Copyright (C) Sebastian Egner (2002). All Rights Reserved.
  2590. @c Permission is hereby granted, free of charge, to any person obtaining a
  2591. @c copy of this software and associated documentation files (the
  2592. @c "Software"), to deal in the Software without restriction, including
  2593. @c without limitation the rights to use, copy, modify, merge, publish,
  2594. @c distribute, sublicense, and/or sell copies of the Software, and to
  2595. @c permit persons to whom the Software is furnished to do so, subject to
  2596. @c the following conditions:
  2597. @c The above copyright notice and this permission notice shall be included
  2598. @c in all copies or substantial portions of the Software.
  2599. @c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  2600. @c OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2601. @c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  2602. @c NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  2603. @c LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  2604. @c OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  2605. @c WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  2606. This SRFI provides access to a (pseudo) random number generator; for
  2607. Guile's built-in random number facilities, which SRFI-27 is implemented
  2608. upon, @xref{Random}. With SRFI-27, random numbers are obtained from a
  2609. @emph{random source}, which encapsulates a random number generation
  2610. algorithm and its state.
  2611. @menu
  2612. * SRFI-27 Default Random Source:: Obtaining random numbers
  2613. * SRFI-27 Random Sources:: Creating and manipulating random sources
  2614. * SRFI-27 Random Number Generators:: Obtaining random number generators
  2615. @end menu
  2616. @node SRFI-27 Default Random Source
  2617. @subsubsection The Default Random Source
  2618. @cindex SRFI-27
  2619. @defun random-integer n
  2620. Return a random number between zero (inclusive) and @var{n} (exclusive),
  2621. using the default random source. The numbers returned have a uniform
  2622. distribution.
  2623. @end defun
  2624. @defun random-real
  2625. Return a random number in (0,1), using the default random source. The
  2626. numbers returned have a uniform distribution.
  2627. @end defun
  2628. @defun default-random-source
  2629. A random source from which @code{random-integer} and @code{random-real}
  2630. have been derived using @code{random-source-make-integers} and
  2631. @code{random-source-make-reals} (@pxref{SRFI-27 Random Number Generators}
  2632. for those procedures). Note that an assignment to
  2633. @code{default-random-source} does not change @code{random-integer} or
  2634. @code{random-real}; it is also strongly recommended not to assign a new
  2635. value.
  2636. @end defun
  2637. @node SRFI-27 Random Sources
  2638. @subsubsection Random Sources
  2639. @cindex SRFI-27
  2640. @defun make-random-source
  2641. Create a new random source. The stream of random numbers obtained from
  2642. each random source created by this procedure will be identical, unless
  2643. its state is changed by one of the procedures below.
  2644. @end defun
  2645. @defun random-source? object
  2646. Tests whether @var{object} is a random source. Random sources are a
  2647. disjoint type.
  2648. @end defun
  2649. @defun random-source-randomize! source
  2650. Attempt to set the state of the random source to a truly random value.
  2651. The current implementation uses a seed based on the current system time.
  2652. @end defun
  2653. @defun random-source-pseudo-randomize! source i j
  2654. Changes the state of the random source s into the initial state of the
  2655. (@var{i}, @var{j})-th independent random source, where @var{i} and
  2656. @var{j} are non-negative integers. This procedure provides a mechanism
  2657. to obtain a large number of independent random sources (usually all
  2658. derived from the same backbone generator), indexed by two integers. In
  2659. contrast to @code{random-source-randomize!}, this procedure is entirely
  2660. deterministic.
  2661. @end defun
  2662. The state associated with a random state can be obtained an reinstated
  2663. with the following procedures:
  2664. @defun random-source-state-ref source
  2665. @defunx random-source-state-set! source state
  2666. Get and set the state of a random source. No assumptions should be made
  2667. about the nature of the state object, besides it having an external
  2668. representation (i.e.@: it can be passed to @code{write} and subsequently
  2669. @code{read} back).
  2670. @end defun
  2671. @node SRFI-27 Random Number Generators
  2672. @subsubsection Obtaining random number generator procedures
  2673. @cindex SRFI-27
  2674. @defun random-source-make-integers source
  2675. Obtains a procedure to generate random integers using the random source
  2676. @var{source}. The returned procedure takes a single argument @var{n},
  2677. which must be a positive integer, and returns the next uniformly
  2678. distributed random integer from the interval @{0, ..., @var{n}-1@} by
  2679. advancing the state of @var{source}.
  2680. If an application obtains and uses several generators for the same
  2681. random source @var{source}, a call to any of these generators advances
  2682. the state of @var{source}. Hence, the generators do not produce the
  2683. same sequence of random integers each but rather share a state. This
  2684. also holds for all other types of generators derived from a fixed random
  2685. sources.
  2686. While the SRFI text specifies that ``Implementations that support
  2687. concurrency make sure that the state of a generator is properly
  2688. advanced'', this is currently not the case in Guile's implementation of
  2689. SRFI-27, as it would cause a severe performance penalty. So in
  2690. multi-threaded programs, you either must perform locking on random
  2691. sources shared between threads yourself, or use different random sources
  2692. for multiple threads.
  2693. @end defun
  2694. @defun random-source-make-reals source
  2695. @defunx random-source-make-reals source unit
  2696. Obtains a procedure to generate random real numbers @math{0 < x < 1}
  2697. using the random source @var{source}. The procedure rand is called
  2698. without arguments.
  2699. The optional parameter @var{unit} determines the type of numbers being
  2700. produced by the returned procedure and the quantization of the output.
  2701. @var{unit} must be a number such that @math{0 < @var{unit} < 1}. The
  2702. numbers created by the returned procedure are of the same numerical type
  2703. as @var{unit} and the potential output values are spaced by at most
  2704. @var{unit}. One can imagine rand to create numbers as @var{x} *
  2705. @var{unit} where @var{x} is a random integer in @{1, ...,
  2706. floor(1/unit)-1@}. Note, however, that this need not be the way the
  2707. values are actually created and that the actual resolution of rand can
  2708. be much higher than unit. In case @var{unit} is absent it defaults to a
  2709. reasonably small value (related to the width of the mantissa of an
  2710. efficient number format).
  2711. @end defun
  2712. @node SRFI-28
  2713. @subsection SRFI-28 - Basic Format Strings
  2714. @cindex SRFI-28
  2715. SRFI-28 provides a basic @code{format} procedure that provides only
  2716. the @code{~a}, @code{~s}, @code{~%}, and @code{~~} format specifiers.
  2717. You can import this procedure by using:
  2718. @lisp
  2719. (use-modules (srfi srfi-28))
  2720. @end lisp
  2721. @deffn {Scheme Procedure} format message arg @dots{}
  2722. Returns a formatted message, using @var{message} as the format string,
  2723. which can contain the following format specifiers:
  2724. @table @code
  2725. @item ~a
  2726. Insert the textual representation of the next @var{arg}, as if printed
  2727. by @code{display}.
  2728. @item ~s
  2729. Insert the textual representation of the next @var{arg}, as if printed
  2730. by @code{write}.
  2731. @item ~%
  2732. Insert a newline.
  2733. @item ~~
  2734. Insert a tilde.
  2735. @end table
  2736. This procedure is the same as calling @code{simple-format}
  2737. (@pxref{Simple Output}) with @code{#f} as the destination.
  2738. @end deffn
  2739. @node SRFI-30
  2740. @subsection SRFI-30 - Nested Multi-line Comments
  2741. @cindex SRFI-30
  2742. Starting from version 2.0, Guile's @code{read} supports SRFI-30/R6RS
  2743. nested multi-line comments by default, @ref{Block Comments}.
  2744. @node SRFI-31
  2745. @subsection SRFI-31 - A special form `rec' for recursive evaluation
  2746. @cindex SRFI-31
  2747. @cindex recursive expression
  2748. @findex rec
  2749. SRFI-31 defines a special form that can be used to create
  2750. self-referential expressions more conveniently. The syntax is as
  2751. follows:
  2752. @example
  2753. @group
  2754. <rec expression> --> (rec <variable> <expression>)
  2755. <rec expression> --> (rec (<variable>+) <body>)
  2756. @end group
  2757. @end example
  2758. The first syntax can be used to create self-referential expressions,
  2759. for example:
  2760. @lisp
  2761. guile> (define tmp (rec ones (cons 1 (delay ones))))
  2762. @end lisp
  2763. The second syntax can be used to create anonymous recursive functions:
  2764. @lisp
  2765. guile> (define tmp (rec (display-n item n)
  2766. (if (positive? n)
  2767. (begin (display n) (display-n (- n 1))))))
  2768. guile> (tmp 42 3)
  2769. 424242
  2770. guile>
  2771. @end lisp
  2772. @node SRFI-34
  2773. @subsection SRFI-34 - Exception handling for programs
  2774. @cindex SRFI-34
  2775. Guile provides an implementation of
  2776. @uref{http://srfi.schemers.org/srfi-34/srfi-34.html, SRFI-34's exception
  2777. handling mechanisms} as an alternative to its own built-in mechanisms
  2778. (@pxref{Exceptions}). It can be made available as follows:
  2779. @lisp
  2780. (use-modules (srfi srfi-34))
  2781. @end lisp
  2782. @xref{Raising and Handling Exceptions}, for more on
  2783. @code{with-exception-handler} and @code{raise} (known as
  2784. @code{raise-exception} in core Guile).
  2785. SRFI-34's @code{guard} form is syntactic sugar over
  2786. @code{with-exception-handler}:
  2787. @deffn {Syntax} guard (var clause @dots{}) body @dots{}
  2788. Evaluate @var{body} with an exception handler that binds the raised
  2789. object to @var{var} and within the scope of that binding evaluates
  2790. @var{clause}@dots{} as if they were the clauses of a cond expression.
  2791. That implicit cond expression is evaluated with the continuation and
  2792. dynamic environment of the guard expression.
  2793. If every @var{clause}'s test evaluates to false and there is no
  2794. @code{else} clause, then @code{raise} is re-invoked on the raised object
  2795. within the dynamic environment of the original call to @code{raise}
  2796. except that the current exception handler is that of the @code{guard}
  2797. expression.
  2798. @end deffn
  2799. @node SRFI-35
  2800. @subsection SRFI-35 - Conditions
  2801. @cindex SRFI-35
  2802. @cindex conditions
  2803. @cindex exceptions
  2804. @uref{http://srfi.schemers.org/srfi-35/srfi-35.html, SRFI-35} defines
  2805. @dfn{conditions}, a data structure akin to records designed to convey
  2806. information about exceptional conditions between parts of a program. It
  2807. is normally used in conjunction with SRFI-34's @code{raise}:
  2808. @lisp
  2809. (raise (condition (&message
  2810. (message "An error occurred"))))
  2811. @end lisp
  2812. Users can define @dfn{condition types} containing arbitrary information.
  2813. Condition types may inherit from one another. This allows the part of
  2814. the program that handles (or ``catches'') conditions to get accurate
  2815. information about the exceptional condition that arose.
  2816. SRFI-35 conditions are made available using:
  2817. @lisp
  2818. (use-modules (srfi srfi-35))
  2819. @end lisp
  2820. The procedures available to manipulate condition types are the
  2821. following:
  2822. @deffn {Scheme Procedure} make-condition-type id parent field-names
  2823. Return a new condition type named @var{id}, inheriting from
  2824. @var{parent}, and with the fields whose names are listed in
  2825. @var{field-names}. @var{field-names} must be a list of symbols and must
  2826. not contain names already used by @var{parent} or one of its supertypes.
  2827. @end deffn
  2828. @deffn {Scheme Procedure} condition-type? obj
  2829. Return true if @var{obj} is a condition type.
  2830. @end deffn
  2831. Conditions can be created and accessed with the following procedures:
  2832. @deffn {Scheme Procedure} make-condition type . field+value
  2833. Return a new condition of type @var{type} with fields initialized as
  2834. specified by @var{field+value}, a sequence of field names (symbols) and
  2835. values as in the following example:
  2836. @lisp
  2837. (let ((&ct (make-condition-type 'foo &condition '(a b c))))
  2838. (make-condition &ct 'a 1 'b 2 'c 3))
  2839. @end lisp
  2840. Note that all fields of @var{type} and its supertypes must be specified.
  2841. @end deffn
  2842. @deffn {Scheme Procedure} make-compound-condition condition1 condition2 @dots{}
  2843. Return a new compound condition composed of @var{condition1}
  2844. @var{condition2} @enddots{}. The returned condition has the type of
  2845. each condition of condition1 condition2 @dots{} (per
  2846. @code{condition-has-type?}).
  2847. @end deffn
  2848. @deffn {Scheme Procedure} condition-has-type? c type
  2849. Return true if condition @var{c} has type @var{type}.
  2850. @end deffn
  2851. @deffn {Scheme Procedure} condition-ref c field-name
  2852. Return the value of the field named @var{field-name} from condition @var{c}.
  2853. If @var{c} is a compound condition and several underlying condition
  2854. types contain a field named @var{field-name}, then the value of the
  2855. first such field is returned, using the order in which conditions were
  2856. passed to @code{make-compound-condition}.
  2857. @end deffn
  2858. @deffn {Scheme Procedure} extract-condition c type
  2859. Return a condition of condition type @var{type} with the field values
  2860. specified by @var{c}.
  2861. If @var{c} is a compound condition, extract the field values from the
  2862. subcondition belonging to @var{type} that appeared first in the call to
  2863. @code{make-compound-condition} that created the condition.
  2864. @end deffn
  2865. Convenience macros are also available to create condition types and
  2866. conditions.
  2867. @deffn {library syntax} define-condition-type type supertype predicate field-spec...
  2868. Define a new condition type named @var{type} that inherits from
  2869. @var{supertype}. In addition, bind @var{predicate} to a type predicate
  2870. that returns true when passed a condition of type @var{type} or any of
  2871. its subtypes. @var{field-spec} must have the form @code{(field
  2872. accessor)} where @var{field} is the name of field of @var{type} and
  2873. @var{accessor} is the name of a procedure to access field @var{field} in
  2874. conditions of type @var{type}.
  2875. The example below defines condition type @code{&foo}, inheriting from
  2876. @code{&condition} with fields @code{a}, @code{b} and @code{c}:
  2877. @lisp
  2878. (define-condition-type &foo &condition
  2879. foo-condition?
  2880. (a foo-a)
  2881. (b foo-b)
  2882. (c foo-c))
  2883. @end lisp
  2884. @end deffn
  2885. @deffn {library syntax} condition type-field-binding1 type-field-binding2 @dots{}
  2886. Return a new condition or compound condition, initialized according to
  2887. @var{type-field-binding1} @var{type-field-binding2} @enddots{}. Each
  2888. @var{type-field-binding} must have the form @code{(type
  2889. field-specs...)}, where @var{type} is the name of a variable bound to a
  2890. condition type; each @var{field-spec} must have the form
  2891. @code{(field-name value)} where @var{field-name} is a symbol denoting
  2892. the field being initialized to @var{value}. As for
  2893. @code{make-condition}, all fields must be specified.
  2894. The following example returns a simple condition:
  2895. @lisp
  2896. (condition (&message (message "An error occurred")))
  2897. @end lisp
  2898. The one below returns a compound condition:
  2899. @lisp
  2900. (condition (&message (message "An error occurred"))
  2901. (&serious))
  2902. @end lisp
  2903. @end deffn
  2904. Finally, SRFI-35 defines a several standard condition types.
  2905. @defvar &condition
  2906. This condition type is the root of all condition types. It has no
  2907. fields.
  2908. @end defvar
  2909. @defvar &message
  2910. A condition type that carries a message describing the nature of the
  2911. condition to humans.
  2912. @end defvar
  2913. @deffn {Scheme Procedure} message-condition? c
  2914. Return true if @var{c} is of type @code{&message} or one of its
  2915. subtypes.
  2916. @end deffn
  2917. @deffn {Scheme Procedure} condition-message c
  2918. Return the message associated with message condition @var{c}.
  2919. @end deffn
  2920. @defvar &serious
  2921. This type describes conditions serious enough that they cannot safely be
  2922. ignored. It has no fields.
  2923. @end defvar
  2924. @deffn {Scheme Procedure} serious-condition? c
  2925. Return true if @var{c} is of type @code{&serious} or one of its
  2926. subtypes.
  2927. @end deffn
  2928. @defvar &error
  2929. This condition describes errors, typically caused by something that has
  2930. gone wrong in the interaction of the program with the external world or
  2931. the user.
  2932. @end defvar
  2933. @deffn {Scheme Procedure} error? c
  2934. Return true if @var{c} is of type @code{&error} or one of its subtypes.
  2935. @end deffn
  2936. As an implementation note, condition objects in Guile are the same as
  2937. ``exception objects''. @xref{Exception Objects}. The
  2938. @code{&condition}, @code{&serious}, and @code{&error} condition types
  2939. are known in core Guile as @code{&exception}, @code{&error}, and
  2940. @code{&external-error}, respectively.
  2941. @node SRFI-37
  2942. @subsection SRFI-37 - args-fold
  2943. @cindex SRFI-37
  2944. This is a processor for GNU @code{getopt_long}-style program
  2945. arguments. It provides an alternative, less declarative interface
  2946. than @code{getopt-long} in @code{(ice-9 getopt-long)}
  2947. (@pxref{getopt-long,,The (ice-9 getopt-long) Module}). Unlike
  2948. @code{getopt-long}, it supports repeated options and any number of
  2949. short and long names per option. Access it with:
  2950. @lisp
  2951. (use-modules (srfi srfi-37))
  2952. @end lisp
  2953. @acronym{SRFI}-37 principally provides an @code{option} type and the
  2954. @code{args-fold} function. To use the library, create a set of
  2955. options with @code{option} and use it as a specification for invoking
  2956. @code{args-fold}.
  2957. Here is an example of a simple argument processor for the typical
  2958. @samp{--version} and @samp{--help} options, which returns a backwards
  2959. list of files given on the command line:
  2960. @lisp
  2961. (args-fold (cdr (program-arguments))
  2962. (let ((display-and-exit-proc
  2963. (lambda (msg)
  2964. (lambda (opt name arg loads)
  2965. (display msg) (quit)))))
  2966. (list (option '(#\v "version") #f #f
  2967. (display-and-exit-proc "Foo version 42.0\n"))
  2968. (option '(#\h "help") #f #f
  2969. (display-and-exit-proc
  2970. "Usage: foo scheme-file ..."))))
  2971. (lambda (opt name arg loads)
  2972. (error "Unrecognized option `~A'" name))
  2973. (lambda (op loads) (cons op loads))
  2974. '())
  2975. @end lisp
  2976. @deffn {Scheme Procedure} option names required-arg? optional-arg? processor
  2977. Return an object that specifies a single kind of program option.
  2978. @var{names} is a list of command-line option names, and should consist of
  2979. characters for traditional @code{getopt} short options and strings for
  2980. @code{getopt_long}-style long options.
  2981. @var{required-arg?} and @var{optional-arg?} are mutually exclusive;
  2982. one or both must be @code{#f}. If @var{required-arg?}, the option
  2983. must be followed by an argument on the command line, such as
  2984. @samp{--opt=value} for long options, or an error will be signaled.
  2985. If @var{optional-arg?}, an argument will be taken if available.
  2986. @var{processor} is a procedure that takes at least 3 arguments, called
  2987. when @code{args-fold} encounters the option: the containing option
  2988. object, the name used on the command line, and the argument given for
  2989. the option (or @code{#f} if none). The rest of the arguments are
  2990. @code{args-fold} ``seeds'', and the @var{processor} should return
  2991. seeds as well.
  2992. @end deffn
  2993. @deffn {Scheme Procedure} option-names opt
  2994. @deffnx {Scheme Procedure} option-required-arg? opt
  2995. @deffnx {Scheme Procedure} option-optional-arg? opt
  2996. @deffnx {Scheme Procedure} option-processor opt
  2997. Return the specified field of @var{opt}, an option object, as
  2998. described above for @code{option}.
  2999. @end deffn
  3000. @deffn {Scheme Procedure} args-fold args options unrecognized-option-proc operand-proc seed @dots{}
  3001. Process @var{args}, a list of program arguments such as that returned by
  3002. @code{(cdr (program-arguments))}, in order against @var{options}, a list
  3003. of option objects as described above. All functions called take the
  3004. ``seeds'', or the last multiple-values as multiple arguments, starting
  3005. with @var{seed} @dots{}, and must return the new seeds. Return the
  3006. final seeds.
  3007. Call @code{unrecognized-option-proc}, which is like an option object's
  3008. processor, for any options not found in @var{options}.
  3009. Call @code{operand-proc} with any items on the command line that are
  3010. not named options. This includes arguments after @samp{--}. It is
  3011. called with the argument in question, as well as the seeds.
  3012. @end deffn
  3013. @node SRFI-38
  3014. @subsection SRFI-38 - External Representation for Data With Shared Structure
  3015. @cindex SRFI-38
  3016. This subsection is based on
  3017. @uref{http://srfi.schemers.org/srfi-38/srfi-38.html, the specification
  3018. of SRFI-38} written by Ray Dillinger.
  3019. @c Copyright (C) Ray Dillinger 2003. All Rights Reserved.
  3020. @c Permission is hereby granted, free of charge, to any person obtaining a
  3021. @c copy of this software and associated documentation files (the
  3022. @c "Software"), to deal in the Software without restriction, including
  3023. @c without limitation the rights to use, copy, modify, merge, publish,
  3024. @c distribute, sublicense, and/or sell copies of the Software, and to
  3025. @c permit persons to whom the Software is furnished to do so, subject to
  3026. @c the following conditions:
  3027. @c The above copyright notice and this permission notice shall be included
  3028. @c in all copies or substantial portions of the Software.
  3029. @c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  3030. @c OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  3031. @c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  3032. @c NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  3033. @c LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  3034. @c OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  3035. @c WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  3036. This SRFI creates an alternative external representation for data
  3037. written and read using @code{write-with-shared-structure} and
  3038. @code{read-with-shared-structure}. It is identical to the grammar for
  3039. external representation for data written and read with @code{write} and
  3040. @code{read} given in section 7 of R5RS, except that the single
  3041. production
  3042. @example
  3043. <datum> --> <simple datum> | <compound datum>
  3044. @end example
  3045. is replaced by the following five productions:
  3046. @example
  3047. <datum> --> <defining datum> | <nondefining datum> | <defined datum>
  3048. <defining datum> --> #<indexnum>=<nondefining datum>
  3049. <defined datum> --> #<indexnum>#
  3050. <nondefining datum> --> <simple datum> | <compound datum>
  3051. <indexnum> --> <digit 10>+
  3052. @end example
  3053. @deffn {Scheme procedure} write-with-shared-structure obj
  3054. @deffnx {Scheme procedure} write-with-shared-structure obj port
  3055. @deffnx {Scheme procedure} write-with-shared-structure obj port optarg
  3056. Writes an external representation of @var{obj} to the given port.
  3057. Strings that appear in the written representation are enclosed in
  3058. doublequotes, and within those strings backslash and doublequote
  3059. characters are escaped by backslashes. Character objects are written
  3060. using the @code{#\} notation.
  3061. Objects which denote locations rather than values (cons cells, vectors,
  3062. and non-zero-length strings in R5RS scheme; also Guile's structs,
  3063. bytevectors and ports and hash-tables), if they appear at more than one
  3064. point in the data being written, are preceded by @samp{#@var{N}=} the
  3065. first time they are written and replaced by @samp{#@var{N}#} all
  3066. subsequent times they are written, where @var{N} is a natural number
  3067. used to identify that particular object. If objects which denote
  3068. locations occur only once in the structure, then
  3069. @code{write-with-shared-structure} must produce the same external
  3070. representation for those objects as @code{write}.
  3071. @code{write-with-shared-structure} terminates in finite time and
  3072. produces a finite representation when writing finite data.
  3073. @code{write-with-shared-structure} returns an unspecified value. The
  3074. @var{port} argument may be omitted, in which case it defaults to the
  3075. value returned by @code{(current-output-port)}. The @var{optarg}
  3076. argument may also be omitted. If present, its effects on the output and
  3077. return value are unspecified but @code{write-with-shared-structure} must
  3078. still write a representation that can be read by
  3079. @code{read-with-shared-structure}. Some implementations may wish to use
  3080. @var{optarg} to specify formatting conventions, numeric radixes, or
  3081. return values. Guile's implementation ignores @var{optarg}.
  3082. For example, the code
  3083. @lisp
  3084. (begin (define a (cons 'val1 'val2))
  3085. (set-cdr! a a)
  3086. (write-with-shared-structure a))
  3087. @end lisp
  3088. should produce the output @code{#1=(val1 . #1#)}. This shows a cons
  3089. cell whose @code{cdr} contains itself.
  3090. @end deffn
  3091. @deffn {Scheme procedure} read-with-shared-structure
  3092. @deffnx {Scheme procedure} read-with-shared-structure port
  3093. @code{read-with-shared-structure} converts the external representations
  3094. of Scheme objects produced by @code{write-with-shared-structure} into
  3095. Scheme objects. That is, it is a parser for the nonterminal
  3096. @samp{<datum>} in the augmented external representation grammar defined
  3097. above. @code{read-with-shared-structure} returns the next object
  3098. parsable from the given input port, updating @var{port} to point to the
  3099. first character past the end of the external representation of the
  3100. object.
  3101. If an end-of-file is encountered in the input before any characters are
  3102. found that can begin an object, then an end-of-file object is returned.
  3103. The port remains open, and further attempts to read it (by
  3104. @code{read-with-shared-structure} or @code{read} will also return an
  3105. end-of-file object. If an end of file is encountered after the
  3106. beginning of an object's external representation, but the external
  3107. representation is incomplete and therefore not parsable, an error is
  3108. signaled.
  3109. The @var{port} argument may be omitted, in which case it defaults to the
  3110. value returned by @code{(current-input-port)}. It is an error to read
  3111. from a closed port.
  3112. @end deffn
  3113. @node SRFI-39
  3114. @subsection SRFI-39 - Parameters
  3115. @cindex SRFI-39
  3116. This SRFI adds support for dynamically-scoped parameters. SRFI 39 is
  3117. implemented in the Guile core; there's no module needed to get SRFI-39
  3118. itself. Parameters are documented in @ref{Parameters}.
  3119. This module does export one extra function: @code{with-parameters*}.
  3120. This is a Guile-specific addition to the SRFI, similar to the core
  3121. @code{with-fluids*} (@pxref{Fluids and Dynamic States}).
  3122. @defun with-parameters* param-list value-list thunk
  3123. Establish a new dynamic scope, as per @code{parameterize} above,
  3124. taking parameters from @var{param-list} and corresponding values from
  3125. @var{value-list}. A call @code{(@var{thunk})} is made in the new
  3126. scope and the result from that @var{thunk} is the return from
  3127. @code{with-parameters*}.
  3128. @end defun
  3129. @node SRFI-41
  3130. @subsection SRFI-41 - Streams
  3131. @cindex SRFI-41
  3132. This subsection is based on the
  3133. @uref{http://srfi.schemers.org/srfi-41/srfi-41.html, specification of
  3134. SRFI-41} by Philip L.@: Bewig.
  3135. @c The copyright notice and license text of the SRFI-41 specification is
  3136. @c reproduced below:
  3137. @c Copyright (C) Philip L. Bewig (2007). All Rights Reserved.
  3138. @c Permission is hereby granted, free of charge, to any person obtaining a
  3139. @c copy of this software and associated documentation files (the
  3140. @c "Software"), to deal in the Software without restriction, including
  3141. @c without limitation the rights to use, copy, modify, merge, publish,
  3142. @c distribute, sublicense, and/or sell copies of the Software, and to
  3143. @c permit persons to whom the Software is furnished to do so, subject to
  3144. @c the following conditions:
  3145. @c The above copyright notice and this permission notice shall be included
  3146. @c in all copies or substantial portions of the Software.
  3147. @c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  3148. @c OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  3149. @c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  3150. @c NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  3151. @c LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  3152. @c OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  3153. @c WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  3154. @noindent
  3155. This SRFI implements streams, sometimes called lazy lists, a sequential
  3156. data structure containing elements computed only on demand. A stream is
  3157. either null or is a pair with a stream in its cdr. Since elements of a
  3158. stream are computed only when accessed, streams can be infinite. Once
  3159. computed, the value of a stream element is cached in case it is needed
  3160. again. SRFI-41 can be made available with:
  3161. @example
  3162. (use-modules (srfi srfi-41))
  3163. @end example
  3164. @menu
  3165. * SRFI-41 Stream Fundamentals::
  3166. * SRFI-41 Stream Primitives::
  3167. * SRFI-41 Stream Library::
  3168. @end menu
  3169. @node SRFI-41 Stream Fundamentals
  3170. @subsubsection SRFI-41 Stream Fundamentals
  3171. SRFI-41 Streams are based on two mutually-recursive abstract data types:
  3172. An object of the @code{stream} abstract data type is a promise that,
  3173. when forced, is either @code{stream-null} or is an object of type
  3174. @code{stream-pair}. An object of the @code{stream-pair} abstract data
  3175. type contains a @code{stream-car} and a @code{stream-cdr}, which must be
  3176. a @code{stream}. The essential feature of streams is the systematic
  3177. suspensions of the recursive promises between the two data types.
  3178. The object stored in the @code{stream-car} of a @code{stream-pair} is a
  3179. promise that is forced the first time the @code{stream-car} is accessed;
  3180. its value is cached in case it is needed again. The object may have any
  3181. type, and different stream elements may have different types. If the
  3182. @code{stream-car} is never accessed, the object stored there is never
  3183. evaluated. Likewise, the @code{stream-cdr} is a promise to return a
  3184. stream, and is only forced on demand.
  3185. @node SRFI-41 Stream Primitives
  3186. @subsubsection SRFI-41 Stream Primitives
  3187. This library provides eight operators: constructors for
  3188. @code{stream-null} and @code{stream-pair}s, type predicates for streams
  3189. and the two kinds of streams, accessors for both fields of a
  3190. @code{stream-pair}, and a lambda that creates procedures that return
  3191. streams.
  3192. @defvr {Scheme Variable} stream-null
  3193. A promise that, when forced, is a single object, distinguishable from
  3194. all other objects, that represents the null stream. @code{stream-null}
  3195. is immutable and unique.
  3196. @end defvr
  3197. @deffn {Scheme Syntax} stream-cons object-expr stream-expr
  3198. Creates a newly-allocated stream containing a promise that, when forced,
  3199. is a @code{stream-pair} with @var{object-expr} in its @code{stream-car}
  3200. and @var{stream-expr} in its @code{stream-cdr}. Neither
  3201. @var{object-expr} nor @var{stream-expr} is evaluated when
  3202. @code{stream-cons} is called.
  3203. Once created, a @code{stream-pair} is immutable; there is no
  3204. @code{stream-set-car!} or @code{stream-set-cdr!} that modifies an
  3205. existing stream-pair. There is no dotted-pair or improper stream as
  3206. with lists.
  3207. @end deffn
  3208. @deffn {Scheme Procedure} stream? object
  3209. Returns true if @var{object} is a stream, otherwise returns false. If
  3210. @var{object} is a stream, its promise will not be forced. If
  3211. @code{(stream? obj)} returns true, then one of @code{(stream-null? obj)}
  3212. or @code{(stream-pair? obj)} will return true and the other will return
  3213. false.
  3214. @end deffn
  3215. @deffn {Scheme Procedure} stream-null? object
  3216. Returns true if @var{object} is the distinguished null stream, otherwise
  3217. returns false. If @var{object} is a stream, its promise will be forced.
  3218. @end deffn
  3219. @deffn {Scheme Procedure} stream-pair? object
  3220. Returns true if @var{object} is a @code{stream-pair} constructed by
  3221. @code{stream-cons}, otherwise returns false. If @var{object} is a
  3222. stream, its promise will be forced.
  3223. @end deffn
  3224. @deffn {Scheme Procedure} stream-car stream
  3225. Returns the object stored in the @code{stream-car} of @var{stream}. An
  3226. error is signaled if the argument is not a @code{stream-pair}. This
  3227. causes the @var{object-expr} passed to @code{stream-cons} to be
  3228. evaluated if it had not yet been; the value is cached in case it is
  3229. needed again.
  3230. @end deffn
  3231. @deffn {Scheme Procedure} stream-cdr stream
  3232. Returns the stream stored in the @code{stream-cdr} of @var{stream}. An
  3233. error is signaled if the argument is not a @code{stream-pair}.
  3234. @end deffn
  3235. @deffn {Scheme Syntax} stream-lambda formals body @dots{}
  3236. Creates a procedure that returns a promise to evaluate the @var{body} of
  3237. the procedure. The last @var{body} expression to be evaluated must
  3238. yield a stream. As with normal @code{lambda}, @var{formals} may be a
  3239. single variable name, in which case all the formal arguments are
  3240. collected into a single list, or a list of variable names, which may be
  3241. null if there are no arguments, proper if there are an exact number of
  3242. arguments, or dotted if a fixed number of arguments is to be followed by
  3243. zero or more arguments collected into a list. @var{Body} must contain
  3244. at least one expression, and may contain internal definitions preceding
  3245. any expressions to be evaluated.
  3246. @end deffn
  3247. @example
  3248. (define strm123
  3249. (stream-cons 1
  3250. (stream-cons 2
  3251. (stream-cons 3
  3252. stream-null))))
  3253. (stream-car strm123) @result{} 1
  3254. (stream-car (stream-cdr strm123) @result{} 2
  3255. (stream-pair?
  3256. (stream-cdr
  3257. (stream-cons (/ 1 0) stream-null))) @result{} #f
  3258. (stream? (list 1 2 3)) @result{} #f
  3259. (define iter
  3260. (stream-lambda (f x)
  3261. (stream-cons x (iter f (f x)))))
  3262. (define nats (iter (lambda (x) (+ x 1)) 0))
  3263. (stream-car (stream-cdr nats)) @result{} 1
  3264. (define stream-add
  3265. (stream-lambda (s1 s2)
  3266. (stream-cons
  3267. (+ (stream-car s1) (stream-car s2))
  3268. (stream-add (stream-cdr s1)
  3269. (stream-cdr s2)))))
  3270. (define evens (stream-add nats nats))
  3271. (stream-car evens) @result{} 0
  3272. (stream-car (stream-cdr evens)) @result{} 2
  3273. (stream-car (stream-cdr (stream-cdr evens))) @result{} 4
  3274. @end example
  3275. @node SRFI-41 Stream Library
  3276. @subsubsection SRFI-41 Stream Library
  3277. @deffn {Scheme Syntax} define-stream (name args @dots{}) body @dots{}
  3278. Creates a procedure that returns a stream, and may appear anywhere a
  3279. normal @code{define} may appear, including as an internal definition.
  3280. It may contain internal definitions of its own. The defined procedure
  3281. takes arguments in the same way as @code{stream-lambda}.
  3282. @code{define-stream} is syntactic sugar on @code{stream-lambda}; see
  3283. also @code{stream-let}, which is also a sugaring of
  3284. @code{stream-lambda}.
  3285. A simple version of @code{stream-map} that takes only a single input
  3286. stream calls itself recursively:
  3287. @example
  3288. (define-stream (stream-map proc strm)
  3289. (if (stream-null? strm)
  3290. stream-null
  3291. (stream-cons
  3292. (proc (stream-car strm))
  3293. (stream-map proc (stream-cdr strm))))))
  3294. @end example
  3295. @end deffn
  3296. @deffn {Scheme Procedure} list->stream list
  3297. Returns a newly-allocated stream containing the elements from
  3298. @var{list}.
  3299. @end deffn
  3300. @deffn {Scheme Procedure} port->stream [port]
  3301. Returns a newly-allocated stream containing in its elements the
  3302. characters on the port. If @var{port} is not given it defaults to the
  3303. current input port. The returned stream has finite length and is
  3304. terminated by @code{stream-null}.
  3305. It looks like one use of @code{port->stream} would be this:
  3306. @example
  3307. (define s ;wrong!
  3308. (with-input-from-file filename
  3309. (lambda () (port->stream))))
  3310. @end example
  3311. But that fails, because @code{with-input-from-file} is eager, and closes
  3312. the input port prematurely, before the first character is read. To read
  3313. a file into a stream, say:
  3314. @example
  3315. (define-stream (file->stream filename)
  3316. (let ((p (open-input-file filename)))
  3317. (stream-let loop ((c (read-char p)))
  3318. (if (eof-object? c)
  3319. (begin (close-input-port p)
  3320. stream-null)
  3321. (stream-cons c
  3322. (loop (read-char p)))))))
  3323. @end example
  3324. @end deffn
  3325. @deffn {Scheme Syntax} stream object-expr @dots{}
  3326. Creates a newly-allocated stream containing in its elements the objects,
  3327. in order. The @var{object-expr}s are evaluated when they are accessed,
  3328. not when the stream is created. If no objects are given, as in
  3329. (stream), the null stream is returned. See also @code{list->stream}.
  3330. @example
  3331. (define strm123 (stream 1 2 3))
  3332. ; (/ 1 0) not evaluated when stream is created
  3333. (define s (stream 1 (/ 1 0) -1))
  3334. @end example
  3335. @end deffn
  3336. @deffn {Scheme Procedure} stream->list [n] stream
  3337. Returns a newly-allocated list containing in its elements the first
  3338. @var{n} items in @var{stream}. If @var{stream} has less than @var{n}
  3339. items, all the items in the stream will be included in the returned
  3340. list. If @var{n} is not given it defaults to infinity, which means that
  3341. unless @var{stream} is finite @code{stream->list} will never return.
  3342. @example
  3343. (stream->list 10
  3344. (stream-map (lambda (x) (* x x))
  3345. (stream-from 0)))
  3346. @result{} (0 1 4 9 16 25 36 49 64 81)
  3347. @end example
  3348. @end deffn
  3349. @deffn {Scheme Procedure} stream-append stream @dots{}
  3350. Returns a newly-allocated stream containing in its elements those
  3351. elements contained in its input @var{stream}s, in order of input. If
  3352. any of the input streams is infinite, no elements of any of the
  3353. succeeding input streams will appear in the output stream. See also
  3354. @code{stream-concat}.
  3355. @end deffn
  3356. @deffn {Scheme Procedure} stream-concat stream
  3357. Takes a @var{stream} consisting of one or more streams and returns a
  3358. newly-allocated stream containing all the elements of the input streams.
  3359. If any of the streams in the input @var{stream} is infinite, any
  3360. remaining streams in the input stream will never appear in the output
  3361. stream. See also @code{stream-append}.
  3362. @end deffn
  3363. @deffn {Scheme Procedure} stream-constant object @dots{}
  3364. Returns a newly-allocated stream containing in its elements the
  3365. @var{object}s, repeating in succession forever.
  3366. @example
  3367. (stream-constant 1) @result{} 1 1 1 @dots{}
  3368. (stream-constant #t #f) @result{} #t #f #t #f #t #f @dots{}
  3369. @end example
  3370. @end deffn
  3371. @deffn {Scheme Procedure} stream-drop n stream
  3372. Returns the suffix of the input @var{stream} that starts at the next
  3373. element after the first @var{n} elements. The output stream shares
  3374. structure with the input @var{stream}; thus, promises forced in one
  3375. instance of the stream are also forced in the other instance of the
  3376. stream. If the input @var{stream} has less than @var{n} elements,
  3377. @code{stream-drop} returns the null stream. See also
  3378. @code{stream-take}.
  3379. @end deffn
  3380. @deffn {Scheme Procedure} stream-drop-while pred stream
  3381. Returns the suffix of the input @var{stream} that starts at the first
  3382. element @var{x} for which @code{(pred x)} returns false. The output
  3383. stream shares structure with the input @var{stream}. See also
  3384. @code{stream-take-while}.
  3385. @end deffn
  3386. @deffn {Scheme Procedure} stream-filter pred stream
  3387. Returns a newly-allocated stream that contains only those elements
  3388. @var{x} of the input @var{stream} which satisfy the predicate
  3389. @code{pred}.
  3390. @example
  3391. (stream-filter odd? (stream-from 0))
  3392. @result{} 1 3 5 7 9 @dots{}
  3393. @end example
  3394. @end deffn
  3395. @deffn {Scheme Procedure} stream-fold proc base stream
  3396. Applies a binary procedure @var{proc} to @var{base} and the first
  3397. element of @var{stream} to compute a new @var{base}, then applies the
  3398. procedure to the new @var{base} and the next element of @var{stream} to
  3399. compute a succeeding @var{base}, and so on, accumulating a value that is
  3400. finally returned as the value of @code{stream-fold} when the end of the
  3401. stream is reached. @var{stream} must be finite, or @code{stream-fold}
  3402. will enter an infinite loop. See also @code{stream-scan}, which is
  3403. similar to @code{stream-fold}, but useful for infinite streams. For
  3404. readers familiar with other functional languages, this is a left-fold;
  3405. there is no corresponding right-fold, since right-fold relies on finite
  3406. streams that are fully-evaluated, in which case they may as well be
  3407. converted to a list.
  3408. @end deffn
  3409. @deffn {Scheme Procedure} stream-for-each proc stream @dots{}
  3410. Applies @var{proc} element-wise to corresponding elements of the input
  3411. @var{stream}s for side-effects; it returns nothing.
  3412. @code{stream-for-each} stops as soon as any of its input streams is
  3413. exhausted.
  3414. @end deffn
  3415. @deffn {Scheme Procedure} stream-from first [step]
  3416. Creates a newly-allocated stream that contains @var{first} as its first
  3417. element and increments each succeeding element by @var{step}. If
  3418. @var{step} is not given it defaults to 1. @var{first} and @var{step}
  3419. may be of any numeric type. @code{stream-from} is frequently useful as
  3420. a generator in @code{stream-of} expressions. See also
  3421. @code{stream-range} for a similar procedure that creates finite streams.
  3422. @end deffn
  3423. @deffn {Scheme Procedure} stream-iterate proc base
  3424. Creates a newly-allocated stream containing @var{base} in its first
  3425. element and applies @var{proc} to each element in turn to determine the
  3426. succeeding element. See also @code{stream-unfold} and
  3427. @code{stream-unfolds}.
  3428. @end deffn
  3429. @deffn {Scheme Procedure} stream-length stream
  3430. Returns the number of elements in the @var{stream}; it does not evaluate
  3431. its elements. @code{stream-length} may only be used on finite streams;
  3432. it enters an infinite loop with infinite streams.
  3433. @end deffn
  3434. @deffn {Scheme Syntax} stream-let tag ((var expr) @dots{}) body @dots{}
  3435. Creates a local scope that binds each variable to the value of its
  3436. corresponding expression. It additionally binds @var{tag} to a
  3437. procedure which takes the bound variables as arguments and @var{body} as
  3438. its defining expressions, binding the @var{tag} with
  3439. @code{stream-lambda}. @var{tag} is in scope within body, and may be
  3440. called recursively. When the expanded expression defined by the
  3441. @code{stream-let} is evaluated, @code{stream-let} evaluates the
  3442. expressions in its @var{body} in an environment containing the
  3443. newly-bound variables, returning the value of the last expression
  3444. evaluated, which must yield a stream.
  3445. @code{stream-let} provides syntactic sugar on @code{stream-lambda}, in
  3446. the same manner as normal @code{let} provides syntactic sugar on normal
  3447. @code{lambda}. However, unlike normal @code{let}, the @var{tag} is
  3448. required, not optional, because unnamed @code{stream-let} is
  3449. meaningless.
  3450. For example, @code{stream-member} returns the first @code{stream-pair}
  3451. of the input @var{strm} with a @code{stream-car} @var{x} that satisfies
  3452. @code{(eql? obj x)}, or the null stream if @var{x} is not present in
  3453. @var{strm}.
  3454. @example
  3455. (define-stream (stream-member eql? obj strm)
  3456. (stream-let loop ((strm strm))
  3457. (cond ((stream-null? strm) strm)
  3458. ((eql? obj (stream-car strm)) strm)
  3459. (else (loop (stream-cdr strm))))))
  3460. @end example
  3461. @end deffn
  3462. @deffn {Scheme Procedure} stream-map proc stream @dots{}
  3463. Applies @var{proc} element-wise to corresponding elements of the input
  3464. @var{stream}s, returning a newly-allocated stream containing elements
  3465. that are the results of those procedure applications. The output stream
  3466. has as many elements as the minimum-length input stream, and may be
  3467. infinite.
  3468. @end deffn
  3469. @deffn {Scheme Syntax} stream-match stream clause @dots{}
  3470. Provides pattern-matching for streams. The input @var{stream} is an
  3471. expression that evaluates to a stream. Clauses are of the form
  3472. @code{(pattern [fender] expression)}, consisting of a @var{pattern} that
  3473. matches a stream of a particular shape, an optional @var{fender} that
  3474. must succeed if the pattern is to match, and an @var{expression} that is
  3475. evaluated if the pattern matches. There are four types of patterns:
  3476. @itemize @bullet
  3477. @item
  3478. () matches the null stream.
  3479. @item
  3480. (@var{pat0} @var{pat1} @dots{}) matches a finite stream with length
  3481. exactly equal to the number of pattern elements.
  3482. @item
  3483. (@var{pat0} @var{pat1} @dots{} @code{.} @var{pat-rest}) matches an
  3484. infinite stream, or a finite stream with length at least as great as the
  3485. number of pattern elements before the literal dot.
  3486. @item
  3487. @var{pat} matches an entire stream. Should always appear last in the
  3488. list of clauses; it's not an error to appear elsewhere, but subsequent
  3489. clauses could never match.
  3490. @end itemize
  3491. Each pattern element may be either:
  3492. @itemize @bullet
  3493. @item
  3494. An identifier, which matches any stream element. Additionally, the
  3495. value of the stream element is bound to the variable named by the
  3496. identifier, which is in scope in the @var{fender} and @var{expression}
  3497. of the corresponding @var{clause}. Each identifier in a single pattern
  3498. must be unique.
  3499. @item
  3500. A literal underscore (@code{_}), which matches any stream element but
  3501. creates no bindings.
  3502. @end itemize
  3503. The @var{pattern}s are tested in order, left-to-right, until a matching
  3504. pattern is found; if @var{fender} is present, it must evaluate to a true
  3505. value for the match to be successful. Pattern variables are bound in
  3506. the corresponding @var{fender} and @var{expression}. Once the matching
  3507. @var{pattern} is found, the corresponding @var{expression} is evaluated
  3508. and returned as the result of the match. An error is signaled if no
  3509. pattern matches the input @var{stream}.
  3510. @code{stream-match} is often used to distinguish null streams from
  3511. non-null streams, binding @var{head} and @var{tail}:
  3512. @example
  3513. (define (len strm)
  3514. (stream-match strm
  3515. (() 0)
  3516. ((head . tail) (+ 1 (len tail)))))
  3517. @end example
  3518. Fenders can test the common case where two stream elements must be
  3519. identical; the @code{else} pattern is an identifier bound to the entire
  3520. stream, not a keyword as in @code{cond}.
  3521. @example
  3522. (stream-match strm
  3523. ((x y . _) (equal? x y) 'ok)
  3524. (else 'error))
  3525. @end example
  3526. A more complex example uses two nested matchers to match two different
  3527. stream arguments; @code{(stream-merge lt? . strms)} stably merges two or
  3528. more streams ordered by the @code{lt?} predicate:
  3529. @example
  3530. (define-stream (stream-merge lt? . strms)
  3531. (define-stream (merge xx yy)
  3532. (stream-match xx (() yy) ((x . xs)
  3533. (stream-match yy (() xx) ((y . ys)
  3534. (if (lt? y x)
  3535. (stream-cons y (merge xx ys))
  3536. (stream-cons x (merge xs yy))))))))
  3537. (stream-let loop ((strms strms))
  3538. (cond ((null? strms) stream-null)
  3539. ((null? (cdr strms)) (car strms))
  3540. (else (merge (car strms)
  3541. (apply stream-merge lt?
  3542. (cdr strms)))))))
  3543. @end example
  3544. @end deffn
  3545. @deffn {Scheme Syntax} stream-of expr clause @dots{}
  3546. Provides the syntax of stream comprehensions, which generate streams by
  3547. means of looping expressions. The result is a stream of objects of the
  3548. type returned by @var{expr}. There are four types of clauses:
  3549. @itemize @bullet
  3550. @item
  3551. (@var{var} @code{in} @var{stream-expr}) loops over the elements of
  3552. @var{stream-expr}, in order from the start of the stream, binding each
  3553. element of the stream in turn to @var{var}. @code{stream-from} and
  3554. @code{stream-range} are frequently useful as generators for
  3555. @var{stream-expr}.
  3556. @item
  3557. (@var{var} @code{is} @var{expr}) binds @var{var} to the value obtained
  3558. by evaluating @var{expr}.
  3559. @item
  3560. (@var{pred} @var{expr}) includes in the output stream only those
  3561. elements @var{x} which satisfy the predicate @var{pred}.
  3562. @end itemize
  3563. The scope of variables bound in the stream comprehension is the clauses
  3564. to the right of the binding clause (but not the binding clause itself)
  3565. plus the result expression.
  3566. When two or more generators are present, the loops are processed as if
  3567. they are nested from left to right; that is, the rightmost generator
  3568. varies fastest. A consequence of this is that only the first generator
  3569. may be infinite and all subsequent generators must be finite. If no
  3570. generators are present, the result of a stream comprehension is a stream
  3571. containing the result expression; thus, @samp{(stream-of 1)} produces a
  3572. finite stream containing only the element 1.
  3573. @example
  3574. (stream-of (* x x)
  3575. (x in (stream-range 0 10))
  3576. (even? x))
  3577. @result{} 0 4 16 36 64
  3578. (stream-of (list a b)
  3579. (a in (stream-range 1 4))
  3580. (b in (stream-range 1 3)))
  3581. @result{} (1 1) (1 2) (2 1) (2 2) (3 1) (3 2)
  3582. (stream-of (list i j)
  3583. (i in (stream-range 1 5))
  3584. (j in (stream-range (+ i 1) 5)))
  3585. @result{} (1 2) (1 3) (1 4) (2 3) (2 4) (3 4)
  3586. @end example
  3587. @end deffn
  3588. @deffn {Scheme Procedure} stream-range first past [step]
  3589. Creates a newly-allocated stream that contains @var{first} as its first
  3590. element and increments each succeeding element by @var{step}. The
  3591. stream is finite and ends before @var{past}, which is not an element of
  3592. the stream. If @var{step} is not given it defaults to 1 if @var{first}
  3593. is less than past and -1 otherwise. @var{first}, @var{past} and
  3594. @var{step} may be of any real numeric type. @code{stream-range} is
  3595. frequently useful as a generator in @code{stream-of} expressions. See
  3596. also @code{stream-from} for a similar procedure that creates infinite
  3597. streams.
  3598. @example
  3599. (stream-range 0 10) @result{} 0 1 2 3 4 5 6 7 8 9
  3600. (stream-range 0 10 2) @result{} 0 2 4 6 8
  3601. @end example
  3602. Successive elements of the stream are calculated by adding @var{step} to
  3603. @var{first}, so if any of @var{first}, @var{past} or @var{step} are
  3604. inexact, the length of the output stream may differ from
  3605. @code{(ceiling (- (/ (- past first) step) 1)}.
  3606. @end deffn
  3607. @deffn {Scheme Procedure} stream-ref stream n
  3608. Returns the @var{n}th element of stream, counting from zero. An error
  3609. is signaled if @var{n} is greater than or equal to the length of stream.
  3610. @example
  3611. (define (fact n)
  3612. (stream-ref
  3613. (stream-scan * 1 (stream-from 1))
  3614. n))
  3615. @end example
  3616. @end deffn
  3617. @deffn {Scheme Procedure} stream-reverse stream
  3618. Returns a newly-allocated stream containing the elements of the input
  3619. @var{stream} but in reverse order. @code{stream-reverse} may only be
  3620. used with finite streams; it enters an infinite loop with infinite
  3621. streams. @code{stream-reverse} does not force evaluation of the
  3622. elements of the stream.
  3623. @end deffn
  3624. @deffn {Scheme Procedure} stream-scan proc base stream
  3625. Accumulates the partial folds of an input @var{stream} into a
  3626. newly-allocated output stream. The output stream is the @var{base}
  3627. followed by @code{(stream-fold proc base (stream-take i stream))} for
  3628. each of the first @var{i} elements of @var{stream}.
  3629. @example
  3630. (stream-scan + 0 (stream-from 1))
  3631. @result{} (stream 0 1 3 6 10 15 @dots{})
  3632. (stream-scan * 1 (stream-from 1))
  3633. @result{} (stream 1 1 2 6 24 120 @dots{})
  3634. @end example
  3635. @end deffn
  3636. @deffn {Scheme Procedure} stream-take n stream
  3637. Returns a newly-allocated stream containing the first @var{n} elements
  3638. of the input @var{stream}. If the input @var{stream} has less than
  3639. @var{n} elements, so does the output stream. See also
  3640. @code{stream-drop}.
  3641. @end deffn
  3642. @deffn {Scheme Procedure} stream-take-while pred stream
  3643. Takes a predicate and a @code{stream} and returns a newly-allocated
  3644. stream containing those elements @code{x} that form the maximal prefix
  3645. of the input stream which satisfy @var{pred}. See also
  3646. @code{stream-drop-while}.
  3647. @end deffn
  3648. @deffn {Scheme Procedure} stream-unfold map pred gen base
  3649. The fundamental recursive stream constructor. It constructs a stream by
  3650. repeatedly applying @var{gen} to successive values of @var{base}, in the
  3651. manner of @code{stream-iterate}, then applying @var{map} to each of the
  3652. values so generated, appending each of the mapped values to the output
  3653. stream as long as @code{(pred? base)} returns a true value. See also
  3654. @code{stream-iterate} and @code{stream-unfolds}.
  3655. The expression below creates the finite stream @samp{0 1 4 9 16 25 36 49
  3656. 64 81}. Initially the @var{base} is 0, which is less than 10, so
  3657. @var{map} squares the @var{base} and the mapped value becomes the first
  3658. element of the output stream. Then @var{gen} increments the @var{base}
  3659. by 1, so it becomes 1; this is less than 10, so @var{map} squares the
  3660. new @var{base} and 1 becomes the second element of the output stream.
  3661. And so on, until the base becomes 10, when @var{pred} stops the
  3662. recursion and stream-null ends the output stream.
  3663. @example
  3664. (stream-unfold
  3665. (lambda (x) (expt x 2)) ; map
  3666. (lambda (x) (< x 10)) ; pred?
  3667. (lambda (x) (+ x 1)) ; gen
  3668. 0) ; base
  3669. @end example
  3670. @end deffn
  3671. @deffn {Scheme Procedure} stream-unfolds proc seed
  3672. Returns @var{n} newly-allocated streams containing those elements
  3673. produced by successive calls to the generator @var{proc}, which takes
  3674. the current @var{seed} as its argument and returns @var{n}+1 values
  3675. (@var{proc} @var{seed}) @result{} @var{seed} @var{result_0} @dots{} @var{result_n-1}
  3676. where the returned @var{seed} is the input @var{seed} to the next call
  3677. to the generator and @var{result_i} indicates how to produce the next
  3678. element of the @var{i}th result stream:
  3679. @itemize @bullet
  3680. @item
  3681. (@var{value}): @var{value} is the next car of the result stream.
  3682. @item
  3683. @code{#f}: no value produced by this iteration of the generator
  3684. @var{proc} for the result stream.
  3685. @item
  3686. (): the end of the result stream.
  3687. @end itemize
  3688. It may require multiple calls of @var{proc} to produce the next element
  3689. of any particular result stream. See also @code{stream-iterate} and
  3690. @code{stream-unfold}.
  3691. @example
  3692. (define (stream-partition pred? strm)
  3693. (stream-unfolds
  3694. (lambda (s)
  3695. (if (stream-null? s)
  3696. (values s '() '())
  3697. (let ((a (stream-car s))
  3698. (d (stream-cdr s)))
  3699. (if (pred? a)
  3700. (values d (list a) #f)
  3701. (values d #f (list a))))))
  3702. strm))
  3703. (call-with-values
  3704. (lambda ()
  3705. (stream-partition odd?
  3706. (stream-range 1 6)))
  3707. (lambda (odds evens)
  3708. (list (stream->list odds)
  3709. (stream->list evens))))
  3710. @result{} ((1 3 5) (2 4))
  3711. @end example
  3712. @end deffn
  3713. @deffn {Scheme Procedure} stream-zip stream @dots{}
  3714. Returns a newly-allocated stream in which each element is a list (not a
  3715. stream) of the corresponding elements of the input @var{stream}s. The
  3716. output stream is as long as the shortest input @var{stream}, if any of
  3717. the input @var{stream}s is finite, or is infinite if all the input
  3718. @var{stream}s are infinite.
  3719. @end deffn
  3720. @node SRFI-42
  3721. @subsection SRFI-42 - Eager Comprehensions
  3722. @cindex SRFI-42
  3723. See @uref{http://srfi.schemers.org/srfi-42/srfi-42.html, the
  3724. specification of SRFI-42}.
  3725. @node SRFI-43
  3726. @subsection SRFI-43 - Vector Library
  3727. @cindex SRFI-43
  3728. This subsection is based on the
  3729. @uref{http://srfi.schemers.org/srfi-43/srfi-43.html, specification of
  3730. SRFI-43} by Taylor Campbell.
  3731. @c The copyright notice and license text of the SRFI-43 specification is
  3732. @c reproduced below:
  3733. @c Copyright (C) Taylor Campbell (2003). All Rights Reserved.
  3734. @c Permission is hereby granted, free of charge, to any person obtaining a
  3735. @c copy of this software and associated documentation files (the
  3736. @c "Software"), to deal in the Software without restriction, including
  3737. @c without limitation the rights to use, copy, modify, merge, publish,
  3738. @c distribute, sublicense, and/or sell copies of the Software, and to
  3739. @c permit persons to whom the Software is furnished to do so, subject to
  3740. @c the following conditions:
  3741. @c The above copyright notice and this permission notice shall be included
  3742. @c in all copies or substantial portions of the Software.
  3743. @c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  3744. @c OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  3745. @c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  3746. @c NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  3747. @c LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  3748. @c OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  3749. @c WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  3750. @noindent
  3751. SRFI-43 implements a comprehensive library of vector operations. It can
  3752. be made available with:
  3753. @example
  3754. (use-modules (srfi srfi-43))
  3755. @end example
  3756. @menu
  3757. * SRFI-43 Constructors::
  3758. * SRFI-43 Predicates::
  3759. * SRFI-43 Selectors::
  3760. * SRFI-43 Iteration::
  3761. * SRFI-43 Searching::
  3762. * SRFI-43 Mutators::
  3763. * SRFI-43 Conversion::
  3764. @end menu
  3765. @node SRFI-43 Constructors
  3766. @subsubsection SRFI-43 Constructors
  3767. @deffn {Scheme Procedure} make-vector size [fill]
  3768. Create and return a vector of size @var{size}, optionally filling it
  3769. with @var{fill}. The default value of @var{fill} is unspecified.
  3770. @example
  3771. (make-vector 5 3) @result{} #(3 3 3 3 3)
  3772. @end example
  3773. @end deffn
  3774. @deffn {Scheme Procedure} vector x @dots{}
  3775. Create and return a vector whose elements are @var{x} @enddots{}.
  3776. @example
  3777. (vector 0 1 2 3 4) @result{} #(0 1 2 3 4)
  3778. @end example
  3779. @end deffn
  3780. @deffn {Scheme Procedure} vector-unfold f length initial-seed @dots{}
  3781. The fundamental vector constructor. Create a vector whose length
  3782. is @var{length} and iterates across each index k from 0 up to
  3783. @var{length} - 1, applying @var{f} at each iteration to the current
  3784. index and current seeds, in that order, to receive n + 1 values: the
  3785. element to put in the kth slot of the new vector, and n new seeds for
  3786. the next iteration. It is an error for the number of seeds to vary
  3787. between iterations.
  3788. @example
  3789. (vector-unfold (lambda (i x) (values x (- x 1)))
  3790. 10 0)
  3791. @result{} #(0 -1 -2 -3 -4 -5 -6 -7 -8 -9)
  3792. (vector-unfold values 10)
  3793. @result{} #(0 1 2 3 4 5 6 7 8 9)
  3794. @end example
  3795. @end deffn
  3796. @deffn {Scheme Procedure} vector-unfold-right f length initial-seed @dots{}
  3797. Like @code{vector-unfold}, but it uses @var{f} to generate elements from
  3798. right-to-left, rather than left-to-right.
  3799. @example
  3800. (vector-unfold-right (lambda (i x) (values x (+ x 1)))
  3801. 10 0)
  3802. @result{} #(9 8 7 6 5 4 3 2 1 0)
  3803. @end example
  3804. @end deffn
  3805. @deffn {Scheme Procedure} vector-copy vec [start [end [fill]]]
  3806. Allocate a new vector whose length is @var{end} - @var{start} and fills
  3807. it with elements from @var{vec}, taking elements from @var{vec} starting
  3808. at index @var{start} and stopping at index @var{end}. @var{start}
  3809. defaults to 0 and @var{end} defaults to the value of
  3810. @code{(vector-length vec)}. If @var{end} extends beyond the length of
  3811. @var{vec}, the slots in the new vector that obviously cannot be filled
  3812. by elements from @var{vec} are filled with @var{fill}, whose default
  3813. value is unspecified.
  3814. @example
  3815. (vector-copy '#(a b c d e f g h i))
  3816. @result{} #(a b c d e f g h i)
  3817. (vector-copy '#(a b c d e f g h i) 6)
  3818. @result{} #(g h i)
  3819. (vector-copy '#(a b c d e f g h i) 3 6)
  3820. @result{} #(d e f)
  3821. (vector-copy '#(a b c d e f g h i) 6 12 'x)
  3822. @result{} #(g h i x x x)
  3823. @end example
  3824. @end deffn
  3825. @deffn {Scheme Procedure} vector-reverse-copy vec [start [end]]
  3826. Like @code{vector-copy}, but it copies the elements in the reverse order
  3827. from @var{vec}.
  3828. @example
  3829. (vector-reverse-copy '#(5 4 3 2 1 0) 1 5)
  3830. @result{} #(1 2 3 4)
  3831. @end example
  3832. @end deffn
  3833. @deffn {Scheme Procedure} vector-append vec @dots{}
  3834. Return a newly allocated vector that contains all elements in order from
  3835. the subsequent locations in @var{vec} @enddots{}.
  3836. @example
  3837. (vector-append '#(a) '#(b c d))
  3838. @result{} #(a b c d)
  3839. @end example
  3840. @end deffn
  3841. @deffn {Scheme Procedure} vector-concatenate list-of-vectors
  3842. Append each vector in @var{list-of-vectors}. Equivalent to
  3843. @code{(apply vector-append list-of-vectors)}.
  3844. @example
  3845. (vector-concatenate '(#(a b) #(c d)))
  3846. @result{} #(a b c d)
  3847. @end example
  3848. @end deffn
  3849. @node SRFI-43 Predicates
  3850. @subsubsection SRFI-43 Predicates
  3851. @deffn {Scheme Procedure} vector? obj
  3852. Return true if @var{obj} is a vector, else return false.
  3853. @end deffn
  3854. @deffn {Scheme Procedure} vector-empty? vec
  3855. Return true if @var{vec} is empty, i.e. its length is 0, else return
  3856. false.
  3857. @end deffn
  3858. @deffn {Scheme Procedure} vector= elt=? vec @dots{}
  3859. Return true if the vectors @var{vec} @dots{} have equal lengths and
  3860. equal elements according to @var{elt=?}. @var{elt=?} is always applied
  3861. to two arguments. Element comparison must be consistent with @code{eq?}
  3862. in the following sense: if @code{(eq? a b)} returns true, then
  3863. @code{(elt=? a b)} must also return true. The order in which
  3864. comparisons are performed is unspecified.
  3865. @end deffn
  3866. @node SRFI-43 Selectors
  3867. @subsubsection SRFI-43 Selectors
  3868. @deffn {Scheme Procedure} vector-ref vec i
  3869. Return the element at index @var{i} in @var{vec}. Indexing is based on
  3870. zero.
  3871. @end deffn
  3872. @deffn {Scheme Procedure} vector-length vec
  3873. Return the length of @var{vec}.
  3874. @end deffn
  3875. @node SRFI-43 Iteration
  3876. @subsubsection SRFI-43 Iteration
  3877. @deffn {Scheme Procedure} vector-fold kons knil vec1 vec2 @dots{}
  3878. The fundamental vector iterator. @var{kons} is iterated over each index
  3879. in all of the vectors, stopping at the end of the shortest; @var{kons}
  3880. is applied as
  3881. @smalllisp
  3882. (kons i state (vector-ref vec1 i) (vector-ref vec2 i) ...)
  3883. @end smalllisp
  3884. where @var{state} is the current state value, and @var{i} is the current
  3885. index. The current state value begins with @var{knil}, and becomes
  3886. whatever @var{kons} returned at the respective iteration. The iteration
  3887. is strictly left-to-right.
  3888. @end deffn
  3889. @deffn {Scheme Procedure} vector-fold-right kons knil vec1 vec2 @dots{}
  3890. Similar to @code{vector-fold}, but it iterates right-to-left instead of
  3891. left-to-right.
  3892. @end deffn
  3893. @deffn {Scheme Procedure} vector-map f vec1 vec2 @dots{}
  3894. Return a new vector of the shortest size of the vector arguments. Each
  3895. element at index i of the new vector is mapped from the old vectors by
  3896. @smalllisp
  3897. (f i (vector-ref vec1 i) (vector-ref vec2 i) ...)
  3898. @end smalllisp
  3899. The dynamic order of application of @var{f} is unspecified.
  3900. @end deffn
  3901. @deffn {Scheme Procedure} vector-map! f vec1 vec2 @dots{}
  3902. Similar to @code{vector-map}, but rather than mapping the new elements
  3903. into a new vector, the new mapped elements are destructively inserted
  3904. into @var{vec1}. The dynamic order of application of @var{f} is
  3905. unspecified.
  3906. @end deffn
  3907. @deffn {Scheme Procedure} vector-for-each f vec1 vec2 @dots{}
  3908. Call @code{(f i (vector-ref vec1 i) (vector-ref vec2 i) ...)} for each
  3909. index i less than the length of the shortest vector passed. The
  3910. iteration is strictly left-to-right.
  3911. @end deffn
  3912. @deffn {Scheme Procedure} vector-count pred? vec1 vec2 @dots{}
  3913. Count the number of parallel elements in the vectors that satisfy
  3914. @var{pred?}, which is applied, for each index i less than the length of
  3915. the smallest vector, to i and each parallel element in the vectors at
  3916. that index, in order.
  3917. @example
  3918. (vector-count (lambda (i elt) (even? elt))
  3919. '#(3 1 4 1 5 9 2 5 6))
  3920. @result{} 3
  3921. (vector-count (lambda (i x y) (< x y))
  3922. '#(1 3 6 9) '#(2 4 6 8 10 12))
  3923. @result{} 2
  3924. @end example
  3925. @end deffn
  3926. @node SRFI-43 Searching
  3927. @subsubsection SRFI-43 Searching
  3928. @deffn {Scheme Procedure} vector-index pred? vec1 vec2 @dots{}
  3929. Find and return the index of the first elements in @var{vec1} @var{vec2}
  3930. @dots{} that satisfy @var{pred?}. If no matching element is found by
  3931. the end of the shortest vector, return @code{#f}.
  3932. @example
  3933. (vector-index even? '#(3 1 4 1 5 9))
  3934. @result{} 2
  3935. (vector-index < '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))
  3936. @result{} 1
  3937. (vector-index = '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))
  3938. @result{} #f
  3939. @end example
  3940. @end deffn
  3941. @deffn {Scheme Procedure} vector-index-right pred? vec1 vec2 @dots{}
  3942. Like @code{vector-index}, but it searches right-to-left, rather than
  3943. left-to-right. Note that the SRFI 43 specification requires that all
  3944. the vectors must have the same length, but both the SRFI 43 reference
  3945. implementation and Guile's implementation allow vectors with unequal
  3946. lengths, and start searching from the last index of the shortest vector.
  3947. @end deffn
  3948. @deffn {Scheme Procedure} vector-skip pred? vec1 vec2 @dots{}
  3949. Find and return the index of the first elements in @var{vec1} @var{vec2}
  3950. @dots{} that do not satisfy @var{pred?}. If no matching element is
  3951. found by the end of the shortest vector, return @code{#f}. Equivalent
  3952. to @code{vector-index} but with the predicate inverted.
  3953. @example
  3954. (vector-skip number? '#(1 2 a b 3 4 c d)) @result{} 2
  3955. @end example
  3956. @end deffn
  3957. @deffn {Scheme Procedure} vector-skip-right pred? vec1 vec2 @dots{}
  3958. Like @code{vector-skip}, but it searches for a non-matching element
  3959. right-to-left, rather than left-to-right. Note that the SRFI 43
  3960. specification requires that all the vectors must have the same length,
  3961. but both the SRFI 43 reference implementation and Guile's implementation
  3962. allow vectors with unequal lengths, and start searching from the last
  3963. index of the shortest vector.
  3964. @end deffn
  3965. @deffn {Scheme Procedure} vector-binary-search vec value cmp [start [end]]
  3966. Find and return an index of @var{vec} between @var{start} and @var{end}
  3967. whose value is @var{value} using a binary search. If no matching
  3968. element is found, return @code{#f}. The default @var{start} is 0 and
  3969. the default @var{end} is the length of @var{vec}.
  3970. @var{cmp} must be a procedure of two arguments such that @code{(cmp a
  3971. b)} returns a negative integer if @math{a < b}, a positive integer if
  3972. @math{a > b}, or zero if @math{a = b}. The elements of @var{vec} must
  3973. be sorted in non-decreasing order according to @var{cmp}.
  3974. Note that SRFI 43 does not document the @var{start} and @var{end}
  3975. arguments, but both its reference implementation and Guile's
  3976. implementation support them.
  3977. @example
  3978. (define (char-cmp c1 c2)
  3979. (cond ((char<? c1 c2) -1)
  3980. ((char>? c1 c2) 1)
  3981. (else 0)))
  3982. (vector-binary-search '#(#\a #\b #\c #\d #\e #\f #\g #\h)
  3983. #\g
  3984. char-cmp)
  3985. @result{} 6
  3986. @end example
  3987. @end deffn
  3988. @deffn {Scheme Procedure} vector-any pred? vec1 vec2 @dots{}
  3989. Find the first parallel set of elements from @var{vec1} @var{vec2}
  3990. @dots{} for which @var{pred?} returns a true value. If such a parallel
  3991. set of elements exists, @code{vector-any} returns the value that
  3992. @var{pred?} returned for that set of elements. The iteration is
  3993. strictly left-to-right.
  3994. @end deffn
  3995. @deffn {Scheme Procedure} vector-every pred? vec1 vec2 @dots{}
  3996. If, for every index i between 0 and the length of the shortest vector
  3997. argument, the set of elements @code{(vector-ref vec1 i)}
  3998. @code{(vector-ref vec2 i)} @dots{} satisfies @var{pred?},
  3999. @code{vector-every} returns the value that @var{pred?} returned for the
  4000. last set of elements, at the last index of the shortest vector.
  4001. Otherwise it returns @code{#f}. The iteration is strictly
  4002. left-to-right.
  4003. @end deffn
  4004. @node SRFI-43 Mutators
  4005. @subsubsection SRFI-43 Mutators
  4006. @deffn {Scheme Procedure} vector-set! vec i value
  4007. Assign the contents of the location at @var{i} in @var{vec} to
  4008. @var{value}.
  4009. @end deffn
  4010. @deffn {Scheme Procedure} vector-swap! vec i j
  4011. Swap the values of the locations in @var{vec} at @var{i} and @var{j}.
  4012. @end deffn
  4013. @deffn {Scheme Procedure} vector-fill! vec fill [start [end]]
  4014. Assign the value of every location in @var{vec} between @var{start} and
  4015. @var{end} to @var{fill}. @var{start} defaults to 0 and @var{end}
  4016. defaults to the length of @var{vec}.
  4017. @end deffn
  4018. @deffn {Scheme Procedure} vector-reverse! vec [start [end]]
  4019. Destructively reverse the contents of @var{vec} between @var{start} and
  4020. @var{end}. @var{start} defaults to 0 and @var{end} defaults to the
  4021. length of @var{vec}.
  4022. @end deffn
  4023. @deffn {Scheme Procedure} vector-copy! target tstart source [sstart [send]]
  4024. Copy a block of elements from @var{source} to @var{target}, both of
  4025. which must be vectors, starting in @var{target} at @var{tstart} and
  4026. starting in @var{source} at @var{sstart}, ending when (@var{send} -
  4027. @var{sstart}) elements have been copied. It is an error for
  4028. @var{target} to have a length less than (@var{tstart} + @var{send} -
  4029. @var{sstart}). @var{sstart} defaults to 0 and @var{send} defaults to
  4030. the length of @var{source}.
  4031. @end deffn
  4032. @deffn {Scheme Procedure} vector-reverse-copy! target tstart source [sstart [send]]
  4033. Like @code{vector-copy!}, but this copies the elements in the reverse
  4034. order. It is an error if @var{target} and @var{source} are identical
  4035. vectors and the @var{target} and @var{source} ranges overlap; however,
  4036. if @var{tstart} = @var{sstart}, @code{vector-reverse-copy!} behaves as
  4037. @code{(vector-reverse! target tstart send)} would.
  4038. @end deffn
  4039. @node SRFI-43 Conversion
  4040. @subsubsection SRFI-43 Conversion
  4041. @deffn {Scheme Procedure} vector->list vec [start [end]]
  4042. Return a newly allocated list containing the elements in @var{vec}
  4043. between @var{start} and @var{end}. @var{start} defaults to 0 and
  4044. @var{end} defaults to the length of @var{vec}.
  4045. @end deffn
  4046. @deffn {Scheme Procedure} reverse-vector->list vec [start [end]]
  4047. Like @code{vector->list}, but the resulting list contains the specified
  4048. range of elements of @var{vec} in reverse order.
  4049. @end deffn
  4050. @deffn {Scheme Procedure} list->vector proper-list [start [end]]
  4051. Return a newly allocated vector of the elements from @var{proper-list}
  4052. with indices between @var{start} and @var{end}. @var{start} defaults to
  4053. 0 and @var{end} defaults to the length of @var{proper-list}. Note that
  4054. SRFI 43 does not document the @var{start} and @var{end} arguments, but
  4055. both its reference implementation and Guile's implementation support
  4056. them.
  4057. @end deffn
  4058. @deffn {Scheme Procedure} reverse-list->vector proper-list [start [end]]
  4059. Like @code{list->vector}, but the resulting vector contains the specified
  4060. range of elements of @var{proper-list} in reverse order. Note that SRFI
  4061. 43 does not document the @var{start} and @var{end} arguments, but both
  4062. its reference implementation and Guile's implementation support them.
  4063. @end deffn
  4064. @node SRFI-45
  4065. @subsection SRFI-45 - Primitives for Expressing Iterative Lazy Algorithms
  4066. @cindex SRFI-45
  4067. This subsection is based on @uref{http://srfi.schemers.org/srfi-45/srfi-45.html, the
  4068. specification of SRFI-45} written by Andr@'e van Tonder.
  4069. @c Copyright (C) André van Tonder (2003). All Rights Reserved.
  4070. @c Permission is hereby granted, free of charge, to any person obtaining a
  4071. @c copy of this software and associated documentation files (the
  4072. @c "Software"), to deal in the Software without restriction, including
  4073. @c without limitation the rights to use, copy, modify, merge, publish,
  4074. @c distribute, sublicense, and/or sell copies of the Software, and to
  4075. @c permit persons to whom the Software is furnished to do so, subject to
  4076. @c the following conditions:
  4077. @c The above copyright notice and this permission notice shall be included
  4078. @c in all copies or substantial portions of the Software.
  4079. @c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  4080. @c OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  4081. @c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  4082. @c NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  4083. @c LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  4084. @c OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  4085. @c WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  4086. Lazy evaluation is traditionally simulated in Scheme using @code{delay}
  4087. and @code{force}. However, these primitives are not powerful enough to
  4088. express a large class of lazy algorithms that are iterative. Indeed, it
  4089. is folklore in the Scheme community that typical iterative lazy
  4090. algorithms written using delay and force will often require unbounded
  4091. memory.
  4092. This SRFI provides set of three operations: @{@code{lazy}, @code{delay},
  4093. @code{force}@}, which allow the programmer to succinctly express lazy
  4094. algorithms while retaining bounded space behavior in cases that are
  4095. properly tail-recursive. A general recipe for using these primitives is
  4096. provided. An additional procedure @code{eager} is provided for the
  4097. construction of eager promises in cases where efficiency is a concern.
  4098. Although this SRFI redefines @code{delay} and @code{force}, the
  4099. extension is conservative in the sense that the semantics of the subset
  4100. @{@code{delay}, @code{force}@} in isolation (i.e., as long as the
  4101. program does not use @code{lazy}) agrees with that in R5RS. In other
  4102. words, no program that uses the R5RS definitions of delay and force will
  4103. break if those definition are replaced by the SRFI-45 definitions of
  4104. delay and force.
  4105. Guile also adds @code{promise?} to the list of exports, which is not
  4106. part of the official SRFI-45.
  4107. @deffn {Scheme Procedure} promise? obj
  4108. Return true if @var{obj} is an SRFI-45 promise, otherwise return false.
  4109. @end deffn
  4110. @deffn {Scheme Syntax} delay expression
  4111. Takes an expression of arbitrary type @var{a} and returns a promise of
  4112. type @code{(Promise @var{a})} which at some point in the future may be
  4113. asked (by the @code{force} procedure) to evaluate the expression and
  4114. deliver the resulting value.
  4115. @end deffn
  4116. @deffn {Scheme Syntax} lazy expression
  4117. Takes an expression of type @code{(Promise @var{a})} and returns a
  4118. promise of type @code{(Promise @var{a})} which at some point in the
  4119. future may be asked (by the @code{force} procedure) to evaluate the
  4120. expression and deliver the resulting promise.
  4121. @end deffn
  4122. @deffn {Scheme Procedure} force expression
  4123. Takes an argument of type @code{(Promise @var{a})} and returns a value
  4124. of type @var{a} as follows: If a value of type @var{a} has been computed
  4125. for the promise, this value is returned. Otherwise, the promise is
  4126. first evaluated, then overwritten by the obtained promise or value, and
  4127. then force is again applied (iteratively) to the promise.
  4128. @end deffn
  4129. @deffn {Scheme Procedure} eager expression
  4130. Takes an argument of type @var{a} and returns a value of type
  4131. @code{(Promise @var{a})}. As opposed to @code{delay}, the argument is
  4132. evaluated eagerly. Semantically, writing @code{(eager expression)} is
  4133. equivalent to writing
  4134. @lisp
  4135. (let ((value expression)) (delay value)).
  4136. @end lisp
  4137. However, the former is more efficient since it does not require
  4138. unnecessary creation and evaluation of thunks. We also have the
  4139. equivalence
  4140. @lisp
  4141. (delay expression) = (lazy (eager expression))
  4142. @end lisp
  4143. @end deffn
  4144. The following reduction rules may be helpful for reasoning about these
  4145. primitives. However, they do not express the memoization and memory
  4146. usage semantics specified above:
  4147. @lisp
  4148. (force (delay expression)) -> expression
  4149. (force (lazy expression)) -> (force expression)
  4150. (force (eager value)) -> value
  4151. @end lisp
  4152. @subsubheading Correct usage
  4153. We now provide a general recipe for using the primitives @{@code{lazy},
  4154. @code{delay}, @code{force}@} to express lazy algorithms in Scheme. The
  4155. transformation is best described by way of an example: Consider the
  4156. stream-filter algorithm, expressed in a hypothetical lazy language as
  4157. @lisp
  4158. (define (stream-filter p? s)
  4159. (if (null? s) '()
  4160. (let ((h (car s))
  4161. (t (cdr s)))
  4162. (if (p? h)
  4163. (cons h (stream-filter p? t))
  4164. (stream-filter p? t)))))
  4165. @end lisp
  4166. This algorithm can be expressed as follows in Scheme:
  4167. @lisp
  4168. (define (stream-filter p? s)
  4169. (lazy
  4170. (if (null? (force s)) (delay '())
  4171. (let ((h (car (force s)))
  4172. (t (cdr (force s))))
  4173. (if (p? h)
  4174. (delay (cons h (stream-filter p? t)))
  4175. (stream-filter p? t))))))
  4176. @end lisp
  4177. In other words, we
  4178. @itemize @bullet
  4179. @item
  4180. wrap all constructors (e.g., @code{'()}, @code{cons}) with @code{delay},
  4181. @item
  4182. apply @code{force} to arguments of deconstructors (e.g., @code{car},
  4183. @code{cdr} and @code{null?}),
  4184. @item
  4185. wrap procedure bodies with @code{(lazy ...)}.
  4186. @end itemize
  4187. @node SRFI-46
  4188. @subsection SRFI-46 Basic syntax-rules Extensions
  4189. @cindex SRFI-46
  4190. Guile's core @code{syntax-rules} supports the extensions specified by
  4191. SRFI-46/R7RS. Tail patterns have been supported since at least Guile
  4192. 2.0, and custom ellipsis identifiers have been supported since Guile
  4193. 2.0.10. @xref{Syntax Rules}.
  4194. @node SRFI-55
  4195. @subsection SRFI-55 - Requiring Features
  4196. @cindex SRFI-55
  4197. SRFI-55 provides @code{require-extension} which is a portable
  4198. mechanism to load selected SRFI modules. This is implemented in the
  4199. Guile core, there's no module needed to get SRFI-55 itself.
  4200. @deffn {library syntax} require-extension clause1 clause2 @dots{}
  4201. Require the features of @var{clause1} @var{clause2} @dots{} , throwing
  4202. an error if any are unavailable.
  4203. A @var{clause} is of the form @code{(@var{identifier} arg...)}. The
  4204. only @var{identifier} currently supported is @code{srfi} and the
  4205. arguments are SRFI numbers. For example to get SRFI-1 and SRFI-6,
  4206. @example
  4207. (require-extension (srfi 1 6))
  4208. @end example
  4209. @code{require-extension} can only be used at the top-level.
  4210. A Guile-specific program can simply @code{use-modules} to load SRFIs
  4211. not already in the core, @code{require-extension} is for programs
  4212. designed to be portable to other Scheme implementations.
  4213. @end deffn
  4214. @node SRFI-60
  4215. @subsection SRFI-60 - Integers as Bits
  4216. @cindex SRFI-60
  4217. @cindex integers as bits
  4218. @cindex bitwise logical
  4219. This SRFI provides various functions for treating integers as bits and
  4220. for bitwise manipulations. These functions can be obtained with,
  4221. @example
  4222. (use-modules (srfi srfi-60))
  4223. @end example
  4224. Integers are treated as infinite precision twos-complement, the same
  4225. as in the core logical functions (@pxref{Bitwise Operations}). And
  4226. likewise bit indexes start from 0 for the least significant bit. The
  4227. following functions in this SRFI are already in the Guile core,
  4228. @quotation
  4229. @code{logand},
  4230. @code{logior},
  4231. @code{logxor},
  4232. @code{lognot},
  4233. @code{logtest},
  4234. @code{logcount},
  4235. @code{integer-length},
  4236. @code{logbit?},
  4237. @code{ash}
  4238. @end quotation
  4239. @sp 1
  4240. @defun bitwise-and n1 ...
  4241. @defunx bitwise-ior n1 ...
  4242. @defunx bitwise-xor n1 ...
  4243. @defunx bitwise-not n
  4244. @defunx any-bits-set? j k
  4245. @defunx bit-set? index n
  4246. @defunx arithmetic-shift n count
  4247. @defunx bit-field n start end
  4248. @defunx bit-count n
  4249. Aliases for @code{logand}, @code{logior}, @code{logxor},
  4250. @code{lognot}, @code{logtest}, @code{logbit?}, @code{ash},
  4251. @code{bit-extract} and @code{logcount} respectively.
  4252. Note that the name @code{bit-count} conflicts with @code{bit-count} in
  4253. the core (@pxref{Bit Vectors}).
  4254. @end defun
  4255. @defun bitwise-if mask n1 n0
  4256. @defunx bitwise-merge mask n1 n0
  4257. Return an integer with bits selected from @var{n1} and @var{n0}
  4258. according to @var{mask}. Those bits where @var{mask} has 1s are taken
  4259. from @var{n1}, and those where @var{mask} has 0s are taken from
  4260. @var{n0}.
  4261. @example
  4262. (bitwise-if 3 #b0101 #b1010) @result{} 9
  4263. @end example
  4264. @end defun
  4265. @defun log2-binary-factors n
  4266. @defunx first-set-bit n
  4267. Return a count of how many factors of 2 are present in @var{n}. This
  4268. is also the bit index of the lowest 1 bit in @var{n}. If @var{n} is
  4269. 0, the return is @math{-1}.
  4270. @example
  4271. (log2-binary-factors 6) @result{} 1
  4272. (log2-binary-factors -8) @result{} 3
  4273. @end example
  4274. @end defun
  4275. @defun copy-bit index n newbit
  4276. Return @var{n} with the bit at @var{index} set according to
  4277. @var{newbit}. @var{newbit} should be @code{#t} to set the bit to 1,
  4278. or @code{#f} to set it to 0. Bits other than at @var{index} are
  4279. unchanged in the return.
  4280. @example
  4281. (copy-bit 1 #b0101 #t) @result{} 7
  4282. @end example
  4283. @end defun
  4284. @defun copy-bit-field n newbits start end
  4285. Return @var{n} with the bits from @var{start} (inclusive) to @var{end}
  4286. (exclusive) changed to the value @var{newbits}.
  4287. The least significant bit in @var{newbits} goes to @var{start}, the
  4288. next to @math{@var{start}+1}, etc. Anything in @var{newbits} past the
  4289. @var{end} given is ignored.
  4290. @example
  4291. (copy-bit-field #b10000 #b11 1 3) @result{} #b10110
  4292. @end example
  4293. @end defun
  4294. @defun rotate-bit-field n count start end
  4295. Return @var{n} with the bit field from @var{start} (inclusive) to
  4296. @var{end} (exclusive) rotated upwards by @var{count} bits.
  4297. @var{count} can be positive or negative, and it can be more than the
  4298. field width (it'll be reduced modulo the width).
  4299. @example
  4300. (rotate-bit-field #b0110 2 1 4) @result{} #b1010
  4301. @end example
  4302. @end defun
  4303. @defun reverse-bit-field n start end
  4304. Return @var{n} with the bits from @var{start} (inclusive) to @var{end}
  4305. (exclusive) reversed.
  4306. @example
  4307. (reverse-bit-field #b101001 2 4) @result{} #b100101
  4308. @end example
  4309. @end defun
  4310. @defun integer->list n [len]
  4311. Return bits from @var{n} in the form of a list of @code{#t} for 1 and
  4312. @code{#f} for 0. The least significant @var{len} bits are returned,
  4313. and the first list element is the most significant of those bits. If
  4314. @var{len} is not given, the default is @code{(integer-length @var{n})}
  4315. (@pxref{Bitwise Operations}).
  4316. @example
  4317. (integer->list 6) @result{} (#t #t #f)
  4318. (integer->list 1 4) @result{} (#f #f #f #t)
  4319. @end example
  4320. @end defun
  4321. @defun list->integer lst
  4322. @defunx booleans->integer bool@dots{}
  4323. Return an integer formed bitwise from the given @var{lst} list of
  4324. booleans, or for @code{booleans->integer} from the @var{bool}
  4325. arguments.
  4326. Each boolean is @code{#t} for a 1 and @code{#f} for a 0. The first
  4327. element becomes the most significant bit in the return.
  4328. @example
  4329. (list->integer '(#t #f #t #f)) @result{} 10
  4330. @end example
  4331. @end defun
  4332. @node SRFI-61
  4333. @subsection SRFI-61 - A more general @code{cond} clause
  4334. This SRFI extends RnRS @code{cond} to support test expressions that
  4335. return multiple values, as well as arbitrary definitions of test
  4336. success. SRFI 61 is implemented in the Guile core; there's no module
  4337. needed to get SRFI-61 itself. Extended @code{cond} is documented in
  4338. @ref{Conditionals,, Simple Conditional Evaluation}.
  4339. @node SRFI-62
  4340. @subsection SRFI-62 - S-expression comments.
  4341. @cindex SRFI-62
  4342. Starting from version 2.0, Guile's @code{read} supports SRFI-62/R7RS
  4343. S-expression comments by default.
  4344. @node SRFI-64
  4345. @subsection SRFI-64 - A Scheme API for test suites.
  4346. @cindex SRFI-64
  4347. See @uref{http://srfi.schemers.org/srfi-64/srfi-64.html, the
  4348. specification of SRFI-64}.
  4349. @node SRFI-67
  4350. @subsection SRFI-67 - Compare procedures
  4351. @cindex SRFI-67
  4352. See @uref{http://srfi.schemers.org/srfi-67/srfi-67.html, the
  4353. specification of SRFI-67}.
  4354. @node SRFI-69
  4355. @subsection SRFI-69 - Basic hash tables
  4356. @cindex SRFI-69
  4357. This is a portable wrapper around Guile's built-in hash table and weak
  4358. table support. @xref{Hash Tables}, for information on that built-in
  4359. support. Above that, this hash-table interface provides association
  4360. of equality and hash functions with tables at creation time, so
  4361. variants of each function are not required, as well as a procedure
  4362. that takes care of most uses for Guile hash table handles, which this
  4363. SRFI does not provide as such.
  4364. Access it with:
  4365. @lisp
  4366. (use-modules (srfi srfi-69))
  4367. @end lisp
  4368. @menu
  4369. * SRFI-69 Creating hash tables::
  4370. * SRFI-69 Accessing table items::
  4371. * SRFI-69 Table properties::
  4372. * SRFI-69 Hash table algorithms::
  4373. @end menu
  4374. @node SRFI-69 Creating hash tables
  4375. @subsubsection Creating hash tables
  4376. @deffn {Scheme Procedure} make-hash-table [equal-proc hash-proc #:weak weakness start-size]
  4377. Create and answer a new hash table with @var{equal-proc} as the
  4378. equality function and @var{hash-proc} as the hashing function.
  4379. By default, @var{equal-proc} is @code{equal?}. It can be any
  4380. two-argument procedure, and should answer whether two keys are the
  4381. same for this table's purposes.
  4382. By default @var{hash-proc} assumes that @code{equal-proc} is no
  4383. coarser than @code{equal?} unless it is literally @code{string-ci=?}.
  4384. If provided, @var{hash-proc} should be a two-argument procedure that
  4385. takes a key and the current table size, and answers a reasonably good
  4386. hash integer between 0 (inclusive) and the size (exclusive).
  4387. @var{weakness} should be @code{#f} or a symbol indicating how ``weak''
  4388. the hash table is:
  4389. @table @code
  4390. @item #f
  4391. An ordinary non-weak hash table. This is the default.
  4392. @item key
  4393. When the key has no more non-weak references at GC, remove that entry.
  4394. @item value
  4395. When the value has no more non-weak references at GC, remove that
  4396. entry.
  4397. @item key-or-value
  4398. When either has no more non-weak references at GC, remove the
  4399. association.
  4400. @end table
  4401. As a legacy of the time when Guile couldn't grow hash tables,
  4402. @var{start-size} is an optional integer argument that specifies the
  4403. approximate starting size for the hash table, which will be rounded to
  4404. an algorithmically-sounder number.
  4405. @end deffn
  4406. By @dfn{coarser} than @code{equal?}, we mean that for all @var{x} and
  4407. @var{y} values where @code{(@var{equal-proc} @var{x} @var{y})},
  4408. @code{(equal? @var{x} @var{y})} as well. If that does not hold for
  4409. your @var{equal-proc}, you must provide a @var{hash-proc}.
  4410. In the case of weak tables, remember that @dfn{references} above
  4411. always refers to @code{eq?}-wise references. Just because you have a
  4412. reference to some string @code{"foo"} doesn't mean that an association
  4413. with key @code{"foo"} in a weak-key table @emph{won't} be collected;
  4414. it only counts as a reference if the two @code{"foo"}s are @code{eq?},
  4415. regardless of @var{equal-proc}. As such, it is usually only sensible
  4416. to use @code{eq?} and @code{hashq} as the equivalence and hash
  4417. functions for a weak table. @xref{Weak References}, for more
  4418. information on Guile's built-in weak table support.
  4419. @deffn {Scheme Procedure} alist->hash-table alist [equal-proc hash-proc #:weak weakness start-size]
  4420. As with @code{make-hash-table}, but initialize it with the
  4421. associations in @var{alist}. Where keys are repeated in @var{alist},
  4422. the leftmost association takes precedence.
  4423. @end deffn
  4424. @node SRFI-69 Accessing table items
  4425. @subsubsection Accessing table items
  4426. @deffn {Scheme Procedure} hash-table-ref table key [default-thunk]
  4427. @deffnx {Scheme Procedure} hash-table-ref/default table key default
  4428. Answer the value associated with @var{key} in @var{table}. If
  4429. @var{key} is not present, answer the result of invoking the thunk
  4430. @var{default-thunk}, which signals an error instead by default.
  4431. @code{hash-table-ref/default} is a variant that requires a third
  4432. argument, @var{default}, and answers @var{default} itself instead of
  4433. invoking it.
  4434. @end deffn
  4435. @deffn {Scheme Procedure} hash-table-set! table key new-value
  4436. Set @var{key} to @var{new-value} in @var{table}.
  4437. @end deffn
  4438. @deffn {Scheme Procedure} hash-table-delete! table key
  4439. Remove the association of @var{key} in @var{table}, if present. If
  4440. absent, do nothing.
  4441. @end deffn
  4442. @deffn {Scheme Procedure} hash-table-exists? table key
  4443. Answer whether @var{key} has an association in @var{table}.
  4444. @end deffn
  4445. @deffn {Scheme Procedure} hash-table-update! table key modifier [default-thunk]
  4446. @deffnx {Scheme Procedure} hash-table-update!/default table key modifier default
  4447. Replace @var{key}'s associated value in @var{table} by invoking
  4448. @var{modifier} with one argument, the old value.
  4449. If @var{key} is not present, and @var{default-thunk} is provided,
  4450. invoke it with no arguments to get the ``old value'' to be passed to
  4451. @var{modifier} as above. If @var{default-thunk} is not provided in
  4452. such a case, signal an error.
  4453. @code{hash-table-update!/default} is a variant that requires the
  4454. fourth argument, which is used directly as the ``old value'' rather
  4455. than as a thunk to be invoked to retrieve the ``old value''.
  4456. @end deffn
  4457. @node SRFI-69 Table properties
  4458. @subsubsection Table properties
  4459. @deffn {Scheme Procedure} hash-table-size table
  4460. Answer the number of associations in @var{table}. This is guaranteed
  4461. to run in constant time for non-weak tables.
  4462. @end deffn
  4463. @deffn {Scheme Procedure} hash-table-keys table
  4464. Answer an unordered list of the keys in @var{table}.
  4465. @end deffn
  4466. @deffn {Scheme Procedure} hash-table-values table
  4467. Answer an unordered list of the values in @var{table}.
  4468. @end deffn
  4469. @deffn {Scheme Procedure} hash-table-walk table proc
  4470. Invoke @var{proc} once for each association in @var{table}, passing
  4471. the key and value as arguments.
  4472. @end deffn
  4473. @deffn {Scheme Procedure} hash-table-fold table proc init
  4474. Invoke @code{(@var{proc} @var{key} @var{value} @var{previous})} for
  4475. each @var{key} and @var{value} in @var{table}, where @var{previous} is
  4476. the result of the previous invocation, using @var{init} as the first
  4477. @var{previous} value. Answer the final @var{proc} result.
  4478. @end deffn
  4479. @deffn {Scheme Procedure} hash-table->alist table
  4480. Answer an alist where each association in @var{table} is an
  4481. association in the result.
  4482. @end deffn
  4483. @node SRFI-69 Hash table algorithms
  4484. @subsubsection Hash table algorithms
  4485. Each hash table carries an @dfn{equivalence function} and a @dfn{hash
  4486. function}, used to implement key lookups. Beginning users should
  4487. follow the rules for consistency of the default @var{hash-proc}
  4488. specified above. Advanced users can use these to implement their own
  4489. equivalence and hash functions for specialized lookup semantics.
  4490. @deffn {Scheme Procedure} hash-table-equivalence-function hash-table
  4491. @deffnx {Scheme Procedure} hash-table-hash-function hash-table
  4492. Answer the equivalence and hash function of @var{hash-table}, respectively.
  4493. @end deffn
  4494. @deffn {Scheme Procedure} hash obj [size]
  4495. @deffnx {Scheme Procedure} string-hash obj [size]
  4496. @deffnx {Scheme Procedure} string-ci-hash obj [size]
  4497. @deffnx {Scheme Procedure} hash-by-identity obj [size]
  4498. Answer a hash value appropriate for equality predicate @code{equal?},
  4499. @code{string=?}, @code{string-ci=?}, and @code{eq?}, respectively.
  4500. @end deffn
  4501. @code{hash} is a backwards-compatible replacement for Guile's built-in
  4502. @code{hash}.
  4503. @node SRFI-71
  4504. @subsection SRFI-71 - Extended let-syntax for multiple values
  4505. @cindex SRFI-71
  4506. This SRFI shadows the forms for @code{let}, @code{let*}, and @code{letrec}
  4507. so that they may accept multiple values. For example:
  4508. @example
  4509. (use-modules (srfi srfi-71))
  4510. (let* ((x y (values 1 2))
  4511. (z (+ x y)))
  4512. (* z 2))
  4513. @result{} 6
  4514. @end example
  4515. See @uref{http://srfi.schemers.org/srfi-71/srfi-71.html, the
  4516. specification of SRFI-71}.
  4517. @node SRFI-87
  4518. @subsection SRFI-87 => in case clauses
  4519. @cindex SRFI-87
  4520. Starting from version 2.0.6, Guile's core @code{case} syntax supports
  4521. @code{=>} in clauses, as specified by SRFI-87/R7RS.
  4522. @xref{Conditionals}.
  4523. @node SRFI-88
  4524. @subsection SRFI-88 Keyword Objects
  4525. @cindex SRFI-88
  4526. @cindex keyword objects
  4527. @uref{http://srfi.schemers.org/srfi-88/srfi-88.html, SRFI-88} provides
  4528. @dfn{keyword objects}, which are equivalent to Guile's keywords
  4529. (@pxref{Keywords}). SRFI-88 keywords can be entered using the
  4530. @dfn{postfix keyword syntax}, which consists of an identifier followed
  4531. by @code{:} (@pxref{Scheme Read, @code{postfix} keyword syntax}).
  4532. SRFI-88 can be made available with:
  4533. @example
  4534. (use-modules (srfi srfi-88))
  4535. @end example
  4536. Doing so installs the right reader option for keyword syntax, using
  4537. @code{(read-set! keywords 'postfix)}. It also provides the procedures
  4538. described below.
  4539. @deffn {Scheme Procedure} keyword? obj
  4540. Return @code{#t} if @var{obj} is a keyword. This is the same procedure
  4541. as the same-named built-in procedure (@pxref{Keyword Procedures,
  4542. @code{keyword?}}).
  4543. @example
  4544. (keyword? foo:) @result{} #t
  4545. (keyword? 'foo:) @result{} #t
  4546. (keyword? "foo") @result{} #f
  4547. @end example
  4548. @end deffn
  4549. @deffn {Scheme Procedure} keyword->string kw
  4550. Return the name of @var{kw} as a string, i.e., without the trailing
  4551. colon. The returned string may not be modified, e.g., with
  4552. @code{string-set!}.
  4553. @example
  4554. (keyword->string foo:) @result{} "foo"
  4555. @end example
  4556. @end deffn
  4557. @deffn {Scheme Procedure} string->keyword str
  4558. Return the keyword object whose name is @var{str}.
  4559. @example
  4560. (keyword->string (string->keyword "a b c")) @result{} "a b c"
  4561. @end example
  4562. @end deffn
  4563. @node SRFI-98
  4564. @subsection SRFI-98 Accessing environment variables.
  4565. @cindex SRFI-98
  4566. @cindex environment variables
  4567. This is a portable wrapper around Guile's built-in support for
  4568. interacting with the current environment, @xref{Runtime Environment}.
  4569. @deffn {Scheme Procedure} get-environment-variable name
  4570. Returns a string containing the value of the environment variable
  4571. given by the string @code{name}, or @code{#f} if the named
  4572. environment variable is not found. This is equivalent to
  4573. @code{(getenv name)}.
  4574. @end deffn
  4575. @deffn {Scheme Procedure} get-environment-variables
  4576. Returns the names and values of all the environment variables as an
  4577. association list in which both the keys and the values are strings.
  4578. @end deffn
  4579. @node SRFI-105
  4580. @subsection SRFI-105 Curly-infix expressions.
  4581. @cindex SRFI-105
  4582. @cindex curly-infix
  4583. @cindex curly-infix-and-bracket-lists
  4584. Guile's built-in reader includes support for SRFI-105 curly-infix
  4585. expressions. See @uref{http://srfi.schemers.org/srfi-105/srfi-105.html,
  4586. the specification of SRFI-105}. Some examples:
  4587. @example
  4588. @{n <= 5@} @result{} (<= n 5)
  4589. @{a + b + c@} @result{} (+ a b c)
  4590. @{a * @{b + c@}@} @result{} (* a (+ b c))
  4591. @{(- a) / b@} @result{} (/ (- a) b)
  4592. @{-(a) / b@} @result{} (/ (- a) b) as well
  4593. @{(f a b) + (g h)@} @result{} (+ (f a b) (g h))
  4594. @{f(a b) + g(h)@} @result{} (+ (f a b) (g h)) as well
  4595. @{f[a b] + g(h)@} @result{} (+ ($bracket-apply$ f a b) (g h))
  4596. '@{a + f(b) + x@} @result{} '(+ a (f b) x)
  4597. @{length(x) >= 6@} @result{} (>= (length x) 6)
  4598. @{n-1 + n-2@} @result{} (+ n-1 n-2)
  4599. @{n * factorial@{n - 1@}@} @result{} (* n (factorial (- n 1)))
  4600. @{@{a > 0@} and @{b >= 1@}@} @result{} (and (> a 0) (>= b 1))
  4601. @{f@{n - 1@}(x)@} @result{} ((f (- n 1)) x)
  4602. @{a . z@} @result{} ($nfx$ a . z)
  4603. @{a + b - c@} @result{} ($nfx$ a + b - c)
  4604. @end example
  4605. To enable curly-infix expressions within a file, place the reader
  4606. directive @code{#!curly-infix} before the first use of curly-infix
  4607. notation. To globally enable curly-infix expressions in Guile's reader,
  4608. set the @code{curly-infix} read option.
  4609. Guile also implements the following non-standard extension to SRFI-105:
  4610. if @code{curly-infix} is enabled and there is no other meaning assigned
  4611. to square brackets (i.e. the @code{square-brackets} read option is
  4612. turned off), then lists within square brackets are read as normal lists
  4613. but with the special symbol @code{$bracket-list$} added to the front.
  4614. To enable this combination of read options within a file, use the reader
  4615. directive @code{#!curly-infix-and-bracket-lists}. For example:
  4616. @example
  4617. [a b] @result{} ($bracket-list$ a b)
  4618. [a . b] @result{} ($bracket-list$ a . b)
  4619. @end example
  4620. For more information on reader options, @xref{Scheme Read}.
  4621. @node SRFI-111
  4622. @subsection SRFI-111 Boxes.
  4623. @cindex SRFI-111
  4624. @uref{http://srfi.schemers.org/srfi-111/srfi-111.html, SRFI-111}
  4625. provides boxes: objects with a single mutable cell.
  4626. @deffn {Scheme Procedure} box value
  4627. Return a newly allocated box whose contents is initialized to
  4628. @var{value}.
  4629. @end deffn
  4630. @deffn {Scheme Procedure} box? obj
  4631. Return true if @var{obj} is a box, otherwise return false.
  4632. @end deffn
  4633. @deffn {Scheme Procedure} unbox box
  4634. Return the current contents of @var{box}.
  4635. @end deffn
  4636. @deffn {Scheme Procedure} set-box! box value
  4637. Set the contents of @var{box} to @var{value}.
  4638. @end deffn
  4639. @node SRFI 125
  4640. @subsection SRFI 125 Intermediate hash tables
  4641. @cindex SRFI 125
  4642. @cindex hash tables
  4643. This SRFI defines an interface to hash tables, which are widely
  4644. recognized as a fundamental data structure for a wide variety of
  4645. applications. A hash table is a data structure that:
  4646. @itemize
  4647. @item
  4648. Is disjoint from all other types.
  4649. @item
  4650. Provides a mapping from objects known as keys to corresponding objects
  4651. known as values.
  4652. @itemize
  4653. @item
  4654. Keys may be any Scheme objects in some kinds of hash tables, but are
  4655. restricted in other kinds.
  4656. @item
  4657. Values may be any Scheme objects.
  4658. @end itemize
  4659. @item
  4660. Has no intrinsic order for the key-value associations it contains.
  4661. @item
  4662. Provides an equality predicate which defines when a proposed key is the
  4663. same as an existing key. No table may contain more than one value for a
  4664. given key.
  4665. @item
  4666. Provides a hash function which maps a candidate key into a non-negative
  4667. exact integer.
  4668. @item
  4669. Supports mutation as the primary means of setting the contents of a
  4670. table.
  4671. @item
  4672. Provides key lookup and destructive update in (expected) amortized
  4673. constant time, provided a satisfactory hash function is available.
  4674. @item
  4675. Does not guarantee that whole-table operations work in the presence of
  4676. concurrent mutation of the whole hash table (values may be safely
  4677. mutated).
  4678. @end itemize
  4679. @menu
  4680. * SRFI 125 Rationale::
  4681. * SRFI 125 Constructors::
  4682. * SRFI 125 Predicates::
  4683. * SRFI 125 Accessors::
  4684. * SRFI 125 Mutators::
  4685. * SRFI 125 The whole hash table::
  4686. * SRFI 125 Mapping and folding::
  4687. * SRFI 125 Copying and conversion::
  4688. * SRFI 125 Hash tables as sets::
  4689. * SRFI 125 Hash functions and reflectivity::
  4690. @end menu
  4691. @node SRFI 125 Rationale
  4692. @subsubsection SRFI 125 Rationale
  4693. Hash tables themselves don't really need defending: almost all
  4694. dynamically typed languages, from awk to JavaScript to Lua to Perl to
  4695. Python to Common Lisp, and including many Scheme implementations,
  4696. provide them in some form as a fundamental data structure. Therefore,
  4697. what needs to be defended is not the data structure but the procedures.
  4698. This SRFI is at an intermediate level. It supports a great many
  4699. convenience procedures on top of the basic hash table interfaces
  4700. provided by SRFI 69 and R6RS. Nothing in it adds power to what those
  4701. interfaces provide, but it does add convenience in the form of
  4702. pre-debugged routines to do various common things, and even some things
  4703. not so commonly done but useful.
  4704. There is no mandated support for thread safety, immutability, or
  4705. weakness, though there are portable hooks for specifying these features.
  4706. While the specification of this SRFI accepts separate equality
  4707. predicates and hash functions for backward compatibility, it strongly
  4708. encourages the use of SRFI 128 comparators, which package a type test,
  4709. an equality predicate, and a hash function into a single bundle.
  4710. @subheading SRFI 69 compatibility
  4711. This SRFI is downward compatible with SRFI 69. Some procedures have
  4712. been given new preferred names for compatibility with other SRFIs, but
  4713. in all cases the SRFI 69 names have been retained as deprecated
  4714. synonyms; in Guile, these deprecated procedures have their name prefixed
  4715. with @code{deprecated:}.
  4716. There is one absolute incompatibility with SRFI 69: the reflective
  4717. procedure @code{hash-table-hash-function} may return @code{#f}, which is
  4718. not permitted by SRFI 69.
  4719. @subheading R6RS compatibility
  4720. The relatively few hash table procedures in R6RS are all available in
  4721. this SRFI under somewhat different names. The only substantive
  4722. difference is that R6RS @code{hashtable-values} and
  4723. @code{hashtable-entries} return vectors, whereas in this SRFI
  4724. @code{hash-table-value} and @code{hash-table-entries} return lists.
  4725. This SRFI adopts SRFI 69's term hash-table rather than R6RS's hashtable,
  4726. because of the universal use of ``hash table'' rather than ``hashtable''
  4727. in other computer languages and in technical prose generally. Besides,
  4728. the English word hashtable obviously means something that can be@dots{}
  4729. hashted.
  4730. In addition, the @code{hashtable-ref} and @code{hashtable-update!} of
  4731. R6RS correspond to the @code{hash-table-ref/default} and
  4732. @code{hash-table-update!/default} of both SRFI 69 and this SRFI.
  4733. @subheading Common Lisp compatibility
  4734. As usual, the Common Lisp names are completely different from the Scheme
  4735. names. Common Lisp provides the following capabilities that are
  4736. @emph{not} in this SRFI:
  4737. @itemize
  4738. @item
  4739. The constructor allows specifying the rehash size and rehash threshold
  4740. of the new hash table. There are also accessors and mutators for these
  4741. and for the current capacity (as opposed to size).
  4742. @item
  4743. There are hash tables based on @code{equalp} (which does not exist in
  4744. Scheme).
  4745. @item
  4746. @code{with-hash-table-iterator} is a hash table external iterator
  4747. implemented as a local macro.
  4748. @item
  4749. @code{sxhash} is an implementation-specific hash function for the equal
  4750. predicate. It has the property that objects in different instantiations
  4751. of the same Lisp implementation that are similar, a concept analogous to
  4752. e@code{qual} but defined across all instantiations, always return the
  4753. same value from @code{sxhash}; for example, the symbol @code{xyz} will
  4754. have the same @code{sxhash} result in all instantiations.
  4755. @end itemize
  4756. @subheading Sources
  4757. The procedures in this SRFI are drawn primarily from SRFI 69 and
  4758. R6RS. In addition, the following sources are acknowledged:
  4759. @itemize
  4760. @item
  4761. The @code{hash-table-mutable?} procedure and the second argument of
  4762. @code{hash-table-copy} (which allows the creation of immutable hash
  4763. tables) are from R6RS, renamed in the style of this SRFI.
  4764. @item
  4765. The @code{hash-table-intern!} procedure is from
  4766. @url{https://docs.racket-lang.org/reference/hashtables.html, Racket},
  4767. renamed in the style of this SRFI.
  4768. @item
  4769. The @code{hash-table-find} procedure is a modified version of
  4770. @code{table-search} in Gambit.
  4771. @item
  4772. The procedures @code{hash-table-unfold} and @code{hash-table-count} were
  4773. suggested by SRFI-1.
  4774. @item
  4775. The procedures @code{hash-table=?} and @code{hash-table-map} were
  4776. suggested by Haskell's @code{Data.Map.Strict} module.
  4777. @item
  4778. The procedure @code{hash-table-map->list} is from Guile.
  4779. @end itemize
  4780. The procedures @code{hash-table-empty?,} @code{hash-table-empty-copy,
  4781. hash-table-pop!,} @code{hash-table-map!,}
  4782. @code{hash-table-intersection!, hash-table-difference!,} and
  4783. @code{hash-table-xor!} were added for convenience and completeness.
  4784. The native hash tables of MIT, SISC, Bigloo, Scheme48, SLIB, RScheme,
  4785. Scheme 7, Scheme 9, Rep, and FemtoLisp were also investigated, but no
  4786. additional procedures were incorporated.
  4787. @subheading Pronunciation
  4788. The slash in the names of some procedures can be pronounced ``with''.
  4789. @node SRFI 125 Constructors
  4790. @subsubsection SRFI 125 Constructors
  4791. @deffn {Scheme Procedure} make-hash-table comparator [ arg @dots{} ]
  4792. @deffnx {Scheme Procedure} make-hash-table equality-predicate [ hash-function ] [ arg @dots{} ])
  4793. Return a newly allocated hash table whose equality predicate and hash
  4794. function are extracted from comparator. Alternatively, for backward
  4795. compatibility with SRFI 69 the equality predicate and hash function can
  4796. be passed as separate arguments; this usage is deprecated.
  4797. These procedures relate to R6RS @code{make-eq-hashtable},
  4798. @code{make-eqv-hashtable} and @code{make-hashtable} ones, and
  4799. @code{make-hash-table} from Common Lisp.
  4800. @end deffn
  4801. @deffn {Scheme Procedure} hash-table comparator [ key value ] @dots{}
  4802. Return a newly allocated hash table, created as if by
  4803. @code{make-hash-table} using @var{comparator}. For each pair of
  4804. arguments, an association is added to the new hash table with @var{key}
  4805. as its key and @var{value} as its value. This procedure returns an
  4806. immutable hash table. If the same key (in the sense of the equality
  4807. predicate) is specified more than once, it is an error.
  4808. @end deffn
  4809. @deffn {Scheme Procedure} hash-table-unfold stop? mapper successor seed comparator arg @dots{}
  4810. Create a new hash table as if by @var{make-hash-table} using
  4811. @var{comparator} and the @var{args}. If the result of applying the
  4812. predicate @code{stop?} to @var{seed} is true, return the hash table.
  4813. Otherwise, apply the procedure @var{mapper} to @var{seed}. @var{mapper}
  4814. returns two values, which are inserted into the hash table as the key
  4815. and the value respectively. Then get a new seed by applying the
  4816. procedure @var{successor} to @var{seed}, and repeat this algorithm.
  4817. @end deffn
  4818. @deffn {Scheme Procedure} alist->hash-table alist comparator arg @dots{}
  4819. @deffnx {Scheme Procedure} alist->hash-table alist equality-predicate [ hash-function ] arg @dots{}
  4820. Return a newly allocated hash table as if by @code{make-hash-table}
  4821. using @var{comparator} and the @var{args}. It is then initialized from
  4822. the associations of @var{alist}. Associations earlier in the list take
  4823. precedence over those that come later. The second form is for
  4824. compatibility with SRFI 69, and is deprecated.
  4825. @end deffn
  4826. @node SRFI 125 Predicates
  4827. @subsubsection SRFI 125 Predicates
  4828. @deffn {Scheme Procedure} hash-table? obj
  4829. Return @code{#t} if @var{obj} is a hash table, and @code{#f} otherwise.
  4830. (R6RS @code{hashtable?}; Common Lisp @code{hash-table-p})
  4831. @end deffn
  4832. @deffn {Scheme Procedure} hash-table-contains? hash-table key
  4833. @deffnx {Scheme Procedure} hash-table-exists? hash-table key
  4834. Return @code{#t} if there is any association to key in @var{hash-table},
  4835. and @code{#f} otherwise. Execute in amortized constant time. The
  4836. @code{hash-table-exists?} procedure is the same as
  4837. @code{hash-table-contains?}; it is provided for backward compatibility
  4838. with SRFI 69, and is deprecated. (R6RS @code{hashtable-contains?})
  4839. @end deffn
  4840. @deffn {Scheme Procedure} hash-table-empty? hash-table
  4841. Return @code{#t} if @var{hash-table} contains no associations, and
  4842. @code{#f} otherwise.
  4843. @end deffn
  4844. @deffn {Scheme Procedure} hash-table=? value-comparator hash-table@sub{1} hash-table@sub{2}
  4845. Return @code{#t} if @var{hash-table@sub{1}} and @var{hash-table@sub{2}}
  4846. have the same keys (in the sense of their common equality predicate) and
  4847. each key has the same value (in the sense of @var{value-comparator)},
  4848. and @code{#f} otherwise.
  4849. @end deffn
  4850. @deffn {Scheme Procedure} hash-table-mutable? hash-table
  4851. Return @code{#t} if @var{hash-table} is mutable. (R6RS
  4852. @code{hashtable-mutable?})
  4853. @end deffn
  4854. @node SRFI 125 Accessors
  4855. @subsubsection SRFI 125 Accessors
  4856. The following procedures, given a key, return the corresponding value.
  4857. @deffn {Scheme Procedure} hash-table-ref hash-table key [ failure [ success ] ]
  4858. Extract the value associated to key in @var{hash-table}, invoke the
  4859. procedure success on it, and return its result; if @var{success} is not
  4860. provided, then the value itself is returned. If @var{key} is not
  4861. contained in @var{hash-table} and @var{failure} is supplied, then
  4862. @var{failure} is called with no arguments and its result is returned.
  4863. Otherwise, it is an error. Execute in expected amortized constant time,
  4864. not counting the time to call the procedures. SRFI 69 does not support
  4865. the @var{success} procedure.
  4866. @end deffn
  4867. @deffn {Scheme Procedure} hash-table-ref/default hash-table key default
  4868. Semantically equivalent to, but may be more efficient than, the
  4869. following code:
  4870. @lisp
  4871. (hash-table-ref @var{hash-table} @var{key} (lambda () @var{default}))
  4872. @end lisp
  4873. (R6RS @code{hashtable-ref}; Common Lisp @code{gethash})
  4874. @end deffn
  4875. @node SRFI 125 Mutators
  4876. @subsubsection SRFI 125 Mutators
  4877. The following procedures alter the associations in a hash table either
  4878. unconditionally, or conditionally on the presence or absence of a
  4879. specified key. It is an error to add an association to a hash table
  4880. whose key does not satisfy the type test predicate of the comparator
  4881. used to create the hash table.
  4882. @deffn {Scheme Procedure} hash-table-set! hash-table arg @dots{}
  4883. Repeatedly mutates @code{hash-table}, creating new associations in it by
  4884. processing the arguments from left to right. The @var{args} alternate
  4885. between keys and values. Whenever there is a previous association for a
  4886. key, it is deleted. It is an error if the type check procedure of the
  4887. comparator of @var{hash-table}, when invoked on a key, does not return
  4888. @code{#t}. Likewise, it is an error if a key is not a valid argument to
  4889. the equality predicate of @var{hash-table}. Return an unspecified
  4890. value. Execute in expected amortized constant time per key.
  4891. SRFI 69, R6RS @code{hashtable-set!} and Common Lisp (@samp{setf
  4892. gethash}) do not handle multiple associations.
  4893. @end deffn
  4894. @deffn {Scheme Procedure} hash-table-delete! hash-table key @dots{}
  4895. Delete any association to each key in @var{hash-table} and returns the
  4896. number of keys that had associations. Execute in expected amortized
  4897. constant time per key. SRFI 69, R6RS @code{hashtable-delete!}, and
  4898. Common Lisp @var{remhash} do not handle multiple associations.
  4899. @end deffn
  4900. @deffn {Scheme Procedure} hash-table-intern! hash-table key failure
  4901. Effectively invoke @code{hash-table-ref} with the given arguments and
  4902. return what it returns. If @var{key} was not found in @var{hash-table},
  4903. its value is set to the result of calling @var{failure}. Execute in
  4904. expected amortized constant time.
  4905. @end deffn
  4906. @deffn {Scheme Procedure} hash-table-update! hash-table key updater [ failure [ success ] ]
  4907. Semantically equivalent to, but may be more efficient than, the
  4908. following code:
  4909. @lisp
  4910. (hash-table-set! @var{hash-table} @var{key}
  4911. (@var{updater} (hash-table-ref @var{hash-table} @var{key} @var{failure} @var{success})))
  4912. @end lisp
  4913. Execute in expected amortized constant time. Return an unspecified
  4914. value. (SRFI 69 and R6RS @code{hashtable-update!} do not support
  4915. the @var{success} procedure)
  4916. @end deffn
  4917. @deffn {Scheme Procedure} hash-table-update!/default hash-table key updater default
  4918. Semantically equivalent to, but may be more efficient than, the
  4919. following code:
  4920. @lisp
  4921. (hash-table-set! @var{hash-table} @var{key}
  4922. (@var{updater} (hash-table-ref/default @var{hash-table} @var{key} @var{default})))
  4923. @end lisp
  4924. Execute in expected amortized constant time. Return an unspecified value.
  4925. @end deffn
  4926. @deffn {Scheme Procedure} hash-table-pop! hash-table
  4927. Choose an arbitrary association from @var{hash-table} and removes it,
  4928. returning the key and value as two values. It is an error if
  4929. @var{hash-table} is empty.
  4930. @end deffn
  4931. @deffn {Scheme Procedure} hash-table-clear! hash-table
  4932. Delete all the associations from @var{hash-table}. (R6RS
  4933. @code{hashtable-clear!}; Common Lisp @code{clrhash}.)
  4934. @end deffn
  4935. @node SRFI 125 The whole hash table
  4936. @subsubsection SRFI 125 The whole hash table
  4937. These procedures process the associations of the hash table in an
  4938. unspecified order.
  4939. @deffn {Scheme Procedure} hash-table-size hash-table
  4940. Return the number of associations in @var{hash-table} as an exact
  4941. integer. Execute in constant time. (R6RS @code{hashtable-size}; Common
  4942. Lisp @code{hash-table-count}.)
  4943. @end deffn
  4944. @deffn {Scheme Procedure} hash-table-keys hash-table
  4945. Return a newly allocated list of all the keys in @var{hash-table}. R6RS
  4946. @code{hashtable-keys} returns a vector.
  4947. @end deffn
  4948. @deffn {Scheme Procedure} hash-table-values hash-table
  4949. Return a newly allocated list of all the keys in @var{hash-table}.
  4950. @end deffn
  4951. @deffn {Scheme Procedure} hash-table-entries hash-table
  4952. Return two values, a newly allocated list of all the keys in
  4953. @var{hash-table} and a newly allocated list of all the values in
  4954. @var{hash-table} in the corresponding order. R6RS
  4955. @code{hash-table-entries} returns vectors.
  4956. @end deffn
  4957. @deffn {Scheme Procedure} hash-table-find proc hash-table failure
  4958. For each association of @var{hash-table}, invoke @var{proc} on its key
  4959. and value. If @var{proc} returns true, then @code{hash-table-find}
  4960. returns what @var{proc} returns. If all the calls to @var{proc} return
  4961. @code{#f}, return the result of invoking the thunk @var{failure}.
  4962. @end deffn
  4963. @deffn {Scheme Procedure} hash-table-count pred hash-table
  4964. For each association of @var{hash-table}, invoke @var{pred} on its key
  4965. and value. Return the number of calls to @var{pred} which returned
  4966. true.
  4967. @end deffn
  4968. @node SRFI 125 Mapping and folding
  4969. @subsubsection SRFI 125 Mapping and folding
  4970. These procedures process the associations of the hash table in an
  4971. unspecified order.
  4972. @deffn {Scheme Procedure} hash-table-map proc comparator hash-table
  4973. Return a newly allocated hash table as if by @samp{(make-hash-table
  4974. comparator)}. Calls @var{proc} for every association in
  4975. @var{hash-table} with the value of the association. The key of the
  4976. association and the result of invoking @var{proc} are entered into the
  4977. new hash table. Note that this is not the result of lifting mapping
  4978. over the domain of hash tables, but it is considered more useful.
  4979. If @var{comparator} recognizes multiple keys in the @var{hash-table} as
  4980. equivalent, any one of such associations is taken.
  4981. @end deffn
  4982. @deffn {Scheme Procedure} hash-table-for-each proc hash-table
  4983. @deffnx {Scheme Procedure} hash-table-walk hash-table proc
  4984. Call @var{proc} for every association in @var{hash-table} with two
  4985. arguments: the key of the association and the value of the association.
  4986. The value returned by @var{proc} is discarded. Return an unspecified
  4987. value. The @code{hash-table-walk} procedure is equivalent to
  4988. @code{hash-table-for-each} with the arguments reversed, is provided for
  4989. backward compatibility with SRFI 69, and is deprecated. (Common
  4990. Lisp @code{maphash})
  4991. @end deffn
  4992. @deffn {Scheme Procedure} hash-table-map! proc hash-table
  4993. Call @var{proc} for every association in @var{hash-table} with two
  4994. arguments: the key of the association and the value of the association.
  4995. The value returned by @var{proc} is used to update the value of the
  4996. association. Return an unspecified value.
  4997. @end deffn
  4998. @deffn {Scheme Procedure} hash-table-map->list proc hash-table
  4999. Call @var{proc} for every association in @var{hash-table} with two
  5000. arguments: the key of the association and the value of the association.
  5001. The values returned by the invocations of @var{proc} are accumulated
  5002. into a list, which is returned.
  5003. @end deffn
  5004. @deffn {Scheme Procedure} hash-table-fold proc seed hash-table
  5005. @deffnx {Scheme Procedure} hash-table-fold hash-table proc seed
  5006. Call @var{proc} for every association in @var{hash-table} with three
  5007. arguments: the key of the association, the value of the association, and
  5008. an accumulated value @var{val}. @var{val} is the seed for the first
  5009. invocation of @var{proc}, and for subsequent invocations of @var{proc},
  5010. the returned value of the previous invocation. The value returned by
  5011. @code{hash-table-fold} is the return value of the last invocation of
  5012. @var{proc}. The order of arguments with @var{hash-table} as the first
  5013. argument is provided for SRFI 69 compatibility, and is deprecated.
  5014. @end deffn
  5015. @deffn {Scheme Procedure} hash-table-prune! proc hash-table
  5016. Call @var{proc} for every association in @var{hash-table} with two
  5017. arguments, the key and the value of the association, and removes all
  5018. associations from @var{hash-table} for which @var{proc} returns true.
  5019. Return an unspecified value.
  5020. @end deffn
  5021. @node SRFI 125 Copying and conversion
  5022. @subsubsection SRFI 125 Copying and conversion
  5023. @deffn {Scheme Procedure} hash-table-copy hash-table [ mutable? ]
  5024. Return a newly allocated hash table with the same properties and
  5025. associations as @var{hash-table}. If the second argument is present and
  5026. is true, the new hash table is mutable. Otherwise it is immutable.
  5027. SRFI 69 @code{hash-table-copy} does not support a second argument.
  5028. (R6RS @code{hashtable-copy})
  5029. @end deffn
  5030. @deffn {Scheme Procedure} hash-table-empty-copy hash-table
  5031. Return a newly allocated mutable hash table with the same properties as
  5032. @var{hash-table}, but with no associations.
  5033. @end deffn
  5034. @deffn {Scheme Procedure} hash-table->alist hash-table
  5035. Return an alist with the same associations as @var{hash-table} in an
  5036. unspecified order.
  5037. @end deffn
  5038. @node SRFI 125 Hash tables as sets
  5039. @subsubsection SRFI 125 Hash tables as sets
  5040. @deffn {Scheme Procedure} hash-table-union! hash-table@sub{1} hash-table@sub{2}
  5041. @deffnx {Scheme Procedure} hash-table-merge! hash-table@sub{1} hash-table@sub{2}
  5042. Add the associations of @var{hash-table@sub{2}} to
  5043. @var{hash-table@sub{1}} and return @var{hash-table@sub{1}}. If a key
  5044. appears in both hash tables, its value is set to the value appearing in
  5045. @var{hash-table@sub{1}}. Return @var{hash-table@sub{1}}. The
  5046. @code{hash-table-merge!} procedure is the same as
  5047. @code{hash-table-union!}, is provided for compatibility with SRFI 69,
  5048. and is deprecated.
  5049. @end deffn
  5050. @deffn {Scheme Procedure} hash-table-intersection! hash-table@sub{1} hash-table@sub{2}
  5051. Delete the associations from @var{hash-table@sub{1}} whose keys don't
  5052. also appear in @var{hash-table@sub{2}} and return
  5053. @var{hash-table@sub{1}}.
  5054. @end deffn
  5055. @deffn {Scheme Procedure} hash-table-difference! hash-table@sub{1} hash-table@sub{2}
  5056. Delete the associations of @var{hash-table@sub{1}} whose keys are also
  5057. present in @var{hash-table@sub{2}} and return @var{hash-table@sub{1}}.
  5058. @end deffn
  5059. @deffn {Scheme Procedure} hash-table-xor! hash-table@sub{1} hash-table@sub{2}
  5060. Delete the associations of @var{hash-table@sub{1}} whose keys are also
  5061. present in @var{hash-table@sub{2}}, and then adds the associations of
  5062. @var{hash-table@sub{2}} whose keys are not present in
  5063. @var{hash-table@sub{1}} to @var{hash-table@sub{1}}. Return
  5064. @var{hash-table@sub{1}}.
  5065. @end deffn
  5066. @node SRFI 125 Hash functions and reflectivity
  5067. @subsubsection SRFI 125 Hash functions and reflectivity
  5068. These functions are made part of this SRFI solely for compatibility with
  5069. SRFI 69, and are deprecated.
  5070. @quotation note
  5071. While the SRFI 125 specifies that these deprecated procedures should be
  5072. exported using their original names, which forces its users to rename
  5073. these procedures to something else to avoid clashing with the SRFI 126
  5074. and SRFI 128 variants that should be preferred instead, Guile exports
  5075. them with the @code{deprecated:} prefix.
  5076. @end quotation
  5077. @deffn {Scheme Procedure} deprecated:hash obj [ arg ]
  5078. The same as SRFI 128's @code{default-hash} procedure, except that it
  5079. must accept (and should ignore) an optional second argument.
  5080. @end deffn
  5081. @deffn {Scheme Procedure} deprecated:string-hash obj [ arg ]
  5082. Similar to SRFI 128's @code{string-hash} procedure, except that it must
  5083. accept (and should ignore) an optional second argument. It is
  5084. incompatible with the procedure of the same name exported by SRFI 128
  5085. and SRFI 126.
  5086. @end deffn
  5087. @deffn {Scheme Procedure} deprecated:hash-by-identity obj [ arg ]
  5088. The same as SRFI 128's @code{default-hash} procedure, except that it
  5089. must accept (and should ignore) an optional second argument.
  5090. @end deffn
  5091. @deffn {Scheme Procedure} deprecated:hash-table-equivalence-function hash-table
  5092. Return the equivalence procedure used to create @var{hash-table}.
  5093. @end deffn
  5094. @deffn {Scheme Procedure} deprecated:hash-table-hash-function hash-table
  5095. Return the hash function used to create @var{hash-table}.
  5096. @end deffn
  5097. @node SRFI 126
  5098. @subsection SRFI 126 R6RS-based hash tables
  5099. @cindex SRFI 126
  5100. @cindex hash tables, r6rs-based
  5101. @uref{http://srfi.schemers.org/srfi-126/srfi-126.html, SRFI 126}
  5102. provides hash tables API that takes the R6RS hash tables API as a basis
  5103. and makes backwards compatible additions such as support for weak hash
  5104. tables, external representation, API support for double hashing
  5105. implementations, and utility procedures. As an alternative to SRFI 125,
  5106. it builds on the R6RS hash tables API instead of SRFI 69, with only
  5107. fully backwards compatible additions such as weak and ephemeral hash
  5108. tables, an external representation, and API support for hashing
  5109. strategies that require a pair of hash functions. This SRFI does not
  5110. attempt to specify thread-safety because typical multi-threaded
  5111. use-cases will most likely involve locking more than just accesses and
  5112. mutations of hash tables.
  5113. @noindent
  5114. The R6RS hash tables API is favored over SRFI 69 because the latter
  5115. contains a crucial flaw: exposing the hash functions for the @code{eq?}
  5116. and @code{eqv?} procedures is a hindrance for Scheme implementations
  5117. with a moving garbage collector. SRFI 125 works around this by allowing
  5118. the user-provided hash function passed to @code{make-hash-table} to be
  5119. ignored by the implementation, and allowing the
  5120. @code{hash-table-hash-function} procedure to return @code{#f} instead of
  5121. the hash function passed to @code{make-hash-table}. R6RS avoids the
  5122. issue by providing dedicated constructors for @code{eq?} and @code{eqv?}
  5123. based hash tables, and returning @code{#f} when their hash function is
  5124. queried.
  5125. While the SRFI is based on the R6RS hash tables API instead of SRFI 69,
  5126. the provided utility procedures nevertheless make it relatively
  5127. straightforward to change code written for SRFI 69 to use the API
  5128. specified herein. The utility procedures provided by this SRFI in
  5129. addition to the R6RS API may be categorized as follows:
  5130. @table @asis
  5131. @item Constructors
  5132. alist->eq-hashtable, alist->eqv-hashtable, alist->hashtable
  5133. @item Access and mutation
  5134. hashtable-lookup, hashtable-intern!
  5135. @item Copying
  5136. hashtable-empty-copy
  5137. @item Key/value collections
  5138. hashtable-values, hashtable-key-list, hashtable-value-list,
  5139. hashtable-entry-lists
  5140. @item Iteration
  5141. hashtable-walk, hashtable-update-all!, hashtable-prune!,
  5142. hashtable-merge!, hashtable-sum, hashtable-map->lset, hashtable-find
  5143. @item Miscellaneous
  5144. hashtable-empty?, hashtable-pop!, hashtable-inc!, hashtable-dec!
  5145. @end table
  5146. Additionally, this specification adheres to the R7RS rule of specifying
  5147. a single return value for procedures which don't have meaningful return
  5148. values.
  5149. @menu
  5150. * SRFI 126 API::
  5151. * SRFI 126 Constructors::
  5152. * SRFI 126 Procedures::
  5153. * SRFI 126 Inspection::
  5154. * SRFI 126 Hash functions::
  5155. @end menu
  5156. @node SRFI 126 API
  5157. @subsubsection SRFI 126 API
  5158. The @code{(srfi srfi-126)} library provides a set of operations on hash
  5159. tables. A hash table is of a disjoint type that associates keys with
  5160. values. Any object can be used as a key, provided a hash function or a
  5161. pair of hash functions, and a suitable equivalence function, are
  5162. available. A hash function is a procedure that maps keys to
  5163. non-negative exact integer objects. It is the programmer's
  5164. responsibility to ensure that the hash functions are compatible with the
  5165. equivalence function, which is a procedure that accepts two keys and
  5166. returns true if they are equivalent and @code{#f} otherwise. Standard
  5167. hash tables for arbitrary objects based on the @code{eq?} and
  5168. @code{eqv?} predicates (see R7RS section on “Equivalence predicates”)
  5169. are provided. Also, hash functions for arbitrary objects, strings, and
  5170. symbols are provided.
  5171. Hash tables can store their key, value, or key and value weakly.
  5172. Storing an object weakly means that the storage location of the object
  5173. does not count towards the total storage locations in the program which
  5174. refer to the object, meaning the object can be reclaimed as soon as no
  5175. non-weak storage locations referring to the object remain. Weakly
  5176. stored objects referring to each other in a cycle will be reclaimed as
  5177. well if none of them are referred to from outside the cycle. When a
  5178. weakly stored object is reclaimed, associations in the hash table which
  5179. have the object as their key or value are deleted.
  5180. Hash tables can also store their key and value in ephemeral storage
  5181. pairs. The objects in an ephemeral storage pair are stored weakly, but
  5182. both protected from reclamation as long as there remain non-weak
  5183. references to the first object from outside the ephemeral storage pair.
  5184. In particular, an @code{ephemeral-key} hash table (where the keys are
  5185. the first objects in the ephemeral storage pairs), with an association
  5186. mapping an element of a vector to the vector itself, may delete said
  5187. association when no non-weak references remain to the vector nor its
  5188. element in the rest of the program. If it were a @code{weak-key} hash
  5189. table, the reference to the key from within the vector would cyclically
  5190. protect the key and value from reclamation, even when no non-weak
  5191. references to the key and value remained from outside the hash table.
  5192. At the absence of such references between the key and value,
  5193. @code{ephemeral-key} and @code{ephemeral-value} hash tables behave
  5194. effectively equivalent to @code{weak-key} and @code{weak-value} hash
  5195. tables.
  5196. @code{ephemeral-key-and-value} hash tables use a pair of ephemeral
  5197. storage pairs for each association: one where the key is the first
  5198. object and one where the value is. This means that the key and value
  5199. are protected from reclamation until no references remain to neither the
  5200. key nor value from outside the hash table. In contrast, a
  5201. @code{weak-key-and-value} hash table will delete an association as soon
  5202. as either the key or value is reclaimed.
  5203. This document uses the @var{hashtable} parameter name for arguments that
  5204. must be hash tables, and the @var{key} parameter name for arguments that
  5205. must be hash table keys.
  5206. @node SRFI 126 Constructors
  5207. @subsubsection SRFI 126 Constructors
  5208. @deffn {Scheme Procedure} make-eq-hashtable
  5209. @deffnx {Scheme Procedure} make-eq-hashtable capacity
  5210. @deffnx {Scheme Procedure} make-eq-hashtable capacity weakness
  5211. Return a newly allocated mutable hash table that accepts arbitrary
  5212. objects as keys, and compares those keys with @code{eq?}. If the
  5213. @var{capacity} argument is provided and not @code{#f}, it must be an
  5214. exact non-negative integer and the initial capacity of the hash table is
  5215. set to approximately @var{capacity} elements. The @var{weakness}
  5216. argument, if provided, must be one of: @code{#f}, @code{weak-key},
  5217. @code{weak-value}, @code{weak-key-and-value}, @code{ephemeral-key},
  5218. @code{ephemeral-value}, and @code{ephemeral-key-and-value}, and
  5219. determines the weakness or ephemeral status for the keys and values in
  5220. the hash table.
  5221. @end deffn
  5222. @deffn {Scheme Procedure} make-eqv-hashtable
  5223. @deffnx {Scheme Procedure} make-eqv-hashtable capacity
  5224. @deffnx {Scheme Procedure} make-eqv-hashtable capacity weakness
  5225. Return a newly allocated mutable hash table that accepts arbitrary
  5226. objects as keys, and compares those keys with @code{eqv?}. The
  5227. semantics of the optional arguments are as in @code{make-eq-hashtable}.
  5228. @end deffn
  5229. @deffn {Scheme Procedure} make-hashtable hash equiv
  5230. @deffnx {Scheme Procedure} make-hashtable hash equiv capacity
  5231. @deffnx {Scheme Procedure} make-hashtable hash equiv capacity weakness
  5232. If @var{hash} is @code{#f} and @var{equiv} is the @code{eq?} procedure,
  5233. the semantics of @code{make-eq-hashtable} apply to the rest of the
  5234. arguments. If @var{hash} is @code{#f} and @var{equiv} is the
  5235. @code{eqv?} procedure, the semantics of @code{make-eqv-hashtable} apply
  5236. to the rest of the arguments.
  5237. Otherwise, @var{hash} must be a pair of hash functions or a hash
  5238. function, and @var{equiv} must be a procedure. @var{equiv} should
  5239. accept two keys as arguments and return a single value. None of the
  5240. procedures should mutate the hash table returned by
  5241. @code{make-hashtable}. The @code{make-hashtable} procedure returns a
  5242. newly allocated mutable hash table using the function(s) specified by
  5243. @var{hash} as its hash function(s), and @var{equiv} as the equivalence
  5244. function used to compare keys. The semantics of the remaining arguments
  5245. are as in @code{make-eq-hashtable} and @code{make-eqv-hashtable}.
  5246. The @var{hash} functions and @var{equiv} should behave like pure
  5247. functions on the domain of keys. For example, the @code{string-hash}
  5248. and @code{string=?} procedures are permissible only if all keys are
  5249. strings and the contents of those strings are never changed so long as
  5250. any of them continues to serve as a key in the hash table. Furthermore,
  5251. any pair of keys for which @var{equiv} returns true should be hashed to
  5252. the same exact integer objects by the given @var{hash} function(s).
  5253. @quotation Note
  5254. Hash tables are allowed to cache the results of calling a hash function
  5255. and equivalence function, so programs cannot rely on a hash function
  5256. being called for every lookup or update. Furthermore any hash table
  5257. operation may call a hash function more than once.
  5258. @end quotation
  5259. @end deffn
  5260. @deffn {Scheme Procedure} alist->eq-hashtable alist
  5261. @deffnx {Scheme Procedure} alist->eq-hashtable capacity alist
  5262. @deffnx {Scheme Procedure} alist->eq-hashtable capacity weakness alist
  5263. The semantics of this procedure can be described as:
  5264. @lisp
  5265. (let ((ht (make-eq-hashtable @var{capacity} @var{weakness})))
  5266. (for-each (lambda (entry)
  5267. (hashtable-set! ht (car entry) (cdr entry)))
  5268. (reverse alist))
  5269. ht)
  5270. @end lisp
  5271. where omission of the @var{capacity} and/or @var{weakness} arguments
  5272. corresponds to their omission in the call to @code{make-eq-hashtable}.
  5273. @end deffn
  5274. @deffn {Scheme Procedure} alist->eqv-hashtable alist
  5275. @deffnx {Scheme Procedure} alist->eqv-hashtable capacity alist
  5276. @deffnx {Scheme Procedure} alist->eqv-hashtable capacity weakness alist
  5277. This procedure is equivalent to @code{alist->eq-hashtable} except that
  5278. @code{make-eqv-hashtable} is used to construct the hash table.
  5279. @end deffn
  5280. @deffn {Scheme Procedure} alist->hashtable hash equiv alist
  5281. @deffnx {Scheme Procedure} alist->hashtable hash equiv capacity alist
  5282. @deffnx {Scheme Procedure} alist->hashtable hash equiv capacity weakness alist
  5283. This procedure is equivalent to @code{alist->eq-hashtable} except that
  5284. @code{make-hashtable} is used to construct the hash table, with the
  5285. given @var{hash} and @var{equiv} arguments.
  5286. @end deffn
  5287. @deffn {Scheme Syntax} weakness weakness-symbol
  5288. The @var{weakness-symbol} must correspond to one of the non-#f values
  5289. accepted for the @var{weakness} argument of the constructor procedures,
  5290. that is, @code{'weak-key}, @code{'weak-value},
  5291. @code{'weak-key-and-value}, @code{'ephemeral-key},
  5292. @code{'ephemeral-value}, or @code{'ephemeral-key-and-value}. Given such
  5293. a symbol, it is returned as a datum. Passing any other argument is an
  5294. error.
  5295. @end deffn
  5296. @node SRFI 126 Procedures
  5297. @subsubsection SRFI 126 Procedures
  5298. @deffn {Scheme Procedure} hashtable? obj
  5299. Return @code{#t} if @var{obj} is a hash table, @code{#f} otherwise.
  5300. @end deffn
  5301. @deffn {Scheme Procedure} hashtable-size hashtable
  5302. Return the number of keys contained in @var{hashtable} as an exact
  5303. integer object.
  5304. @end deffn
  5305. @deffn {Scheme Procedure} hashtable-ref hashtable key
  5306. @deffnx {Scheme Procedure} hashtable-ref hashtable key default
  5307. Return the value in @var{hashtable} associated with @var{key}. If
  5308. @var{hashtable} does not contain an association for key, @var{default}
  5309. is returned. If @var{hashtable} does not contain an association for key
  5310. and the @var{default} argument is not provided, an error is signaled.
  5311. @end deffn
  5312. @deffn {Scheme Procedure} hashtable-set! hashtable key obj
  5313. Change @var{hashtable} to associate @var{key} with @var{obj}, adding a
  5314. new association or replacing any existing association for @var{key}, and
  5315. return an unspecified value.
  5316. @end deffn
  5317. @deffn {Scheme Procedure} hashtable-delete! hashtable key
  5318. Remove any association for @var{key} within @var{hashtable} and return
  5319. an unspecified value.
  5320. @end deffn
  5321. @deffn {Scheme Procedure} hashtable-contains? hashtable key
  5322. Return @code{#t} if @var{hashtable} contains an association for
  5323. @var{key}, @code{#f} otherwise.
  5324. @end deffn
  5325. @deffn {Scheme Procedure} hashtable-lookup hashtable key
  5326. Return two values: the value in @var{hashtable} associated with
  5327. @var{key} or an unspecified value if there is none, and a boolean
  5328. indicating whether an association was found.
  5329. @end deffn
  5330. @deffn {Scheme Procedure} hashtable-update! hashtable key proc
  5331. @deffnx {Scheme Procedure} hashtable-update! hashtable key proc default
  5332. @var{proc} should accept one argument, should return a single value, and
  5333. should not mutate hashtable. The @code{hashtable-update!} procedure
  5334. applies @var{proc} to the value in @var{hashtable} associated with
  5335. @var{key}, or to @var{default} if @var{hashtable} does not contain an
  5336. association for @var{key}. The @var{hashtable} is then changed to
  5337. associate @var{key} with the value returned by @var{proc}. If
  5338. @var{hashtable} does not contain an association for @var{key} and the
  5339. @var{default} argument is not provided, an error should be signaled.
  5340. @var{hashtable-update!} returns the value of the new association for
  5341. @var{key} in @var{hashtable}.
  5342. @end deffn
  5343. @deffn {Scheme Procedure} hashtable-intern! hashtable key default-proc
  5344. @var{default-proc} should accept zero arguments, should return a single
  5345. value, and should not mutate @var{hashtable}. The
  5346. @code{hashtable-intern!} procedure returns the association for key in
  5347. @var{hashtable} if there is one, otherwise it calls @var{default-proc}
  5348. with zero arguments, associates its return value with @var{key} in
  5349. @var{hashtable}, and returns that value.
  5350. @end deffn
  5351. @deffn {Scheme Procedure} hashtable-copy hashtable
  5352. @deffnx {Scheme Procedure} hashtable-copy hashtable mutable
  5353. @deffnx {Scheme Procedure} hashtable-copy hashtable mutable weakness
  5354. Return a copy of @var{hashtable}. If the @var{mutable} argument is
  5355. provided and is true, the returned @var{hashtable} is mutable; otherwise
  5356. it is immutable. If the optional @var{weakness} argument is provided,
  5357. it determines the weakness of the copy, otherwise the weakness attribute
  5358. of @var{hashtable} is used.
  5359. @end deffn
  5360. @deffn {Scheme Procedure} hashtable-clear! hashtable
  5361. @deffnx {Scheme Procedure} hashtable-clear! hashtable capacity
  5362. Remove all associations from @var{hashtable} and return an unspecified
  5363. value. If @var{capacity} is provided and not @code{#f}, it must be an
  5364. exact non-negative integer and the current capacity of the
  5365. @var{hashtable} is reset to approximately @var{capacity} elements.
  5366. @end deffn
  5367. @deffn {Scheme Procedure} hashtable-empty-copy hashtable
  5368. @deffnx {Scheme Procedure} hashtable-empty-copy hashtable capacity
  5369. Return a newly allocated mutable @var{hashtable} that has the same hash
  5370. and equivalence functions and weakness attribute as @var{hashtable}.
  5371. The @var{capacity} argument may be @code{#t} to set the initial capacity
  5372. of the copy to approximately @samp{(hashtable-size @var{hashtable})}
  5373. elements; otherwise the semantics of @code{make-eq-hashtable} apply to
  5374. the @var{capacity} argument.
  5375. @end deffn
  5376. @deffn {Scheme Procedure} hashtable-keys hashtable
  5377. Return a vector of all keys in @var{hashtable}. The order of the vector
  5378. is unspecified.
  5379. @end deffn
  5380. @deffn {Scheme Procedure} hashtable-values hashtable
  5381. Return a vector of all values in @var{hashtable}. The order of the
  5382. vector is unspecified, and is not guaranteed to match the order of keys
  5383. in the result of @code{hashtable-keys}.
  5384. @end deffn
  5385. @deffn {Scheme Procedure} hashtable-entries hashtable
  5386. Return two values, a vector of the keys in @var{hashtable}, and a vector
  5387. of the corresponding values.
  5388. @end deffn
  5389. @deffn {Scheme Procedure} hashtable-key-list hashtable
  5390. Return a list of all keys in @var{hashtable}. The order of the list is
  5391. unspecified.
  5392. @end deffn
  5393. @deffn {Scheme Procedure} hashtable-value-list hashtable
  5394. Return a list of all values in @var{hashtable}. The order of the list
  5395. is unspecified, and is not guaranteed to match the order of keys in the
  5396. result of @code{hashtable-key-list}.
  5397. @end deffn
  5398. @deffn {Scheme Procedure} hashtable-entry-lists hashtable
  5399. Return two values, a list of the keys in @var{hashtable}, and a list of
  5400. the corresponding values.
  5401. @end deffn
  5402. @deffn {Scheme Procedure} hashtable-walk hashtable proc
  5403. @var{proc} should accept two arguments, and should not mutate
  5404. @var{hashtable}. The @code{hashtable-walk} procedure applies @var{proc}
  5405. once for every association in @var{hashtable}, passing it the key and
  5406. value as arguments. The order in which @var{proc} is applied to the
  5407. associations is unspecified. Return values of @var{proc} are ignored.
  5408. @code{hashtable-walk} returns an unspecified value.
  5409. @end deffn
  5410. @deffn {Scheme Procedure} hashtable-update-all! hashtable proc
  5411. @var{proc} should accept two arguments, should return a single value,
  5412. and should not mutate @var{hashtable}. The @code{hashtable-update-all!}
  5413. procedure applies @var{proc} once for every association in
  5414. @var{hashtable}, passing it the key and value as arguments, and changes
  5415. the value of the association to the return value of @var{proc}. The
  5416. order in which @var{proc} is applied to the associations is unspecified.
  5417. @code{hashtable-update-all!} returns an unspecified value.
  5418. @end deffn
  5419. @deffn {Scheme Procedure} hashtable-prune! hashtable proc
  5420. @var{proc} should accept two arguments, should return a single value,
  5421. and should not mutate @var{hashtable}. The @code{hashtable-prune!}
  5422. procedure applies @var{proc} once for every association in
  5423. @var{hashtable}, passing it the key and value as arguments, and deletes
  5424. the association if @var{proc} returns a true value. The order in which
  5425. @var{proc} is applied to the associations is unspecified.
  5426. @code{hashtable-prune!} returns an unspecified value.
  5427. @end deffn
  5428. @deffn {Scheme Procedure} hashtable-merge! hashtable-dest hashtable-source
  5429. Effectively equivalent to:
  5430. @lisp
  5431. (begin
  5432. (hashtable-walk @var{hashtable-source}
  5433. (lambda (key value)
  5434. (hashtable-set! @var{hashtable-dest} key value)))
  5435. hashtable-dest)
  5436. @end lisp
  5437. @end deffn
  5438. @deffn {Scheme Procedure} hashtable-sum hashtable init proc
  5439. @var{proc} should accept three arguments, should return a single value,
  5440. and should not mutate @var{hashtable}. The @code{hashtable-sum}
  5441. procedure accumulates a result by applying @var{proc} once for every
  5442. association in @var{hashtable}, passing it as arguments: the key, the
  5443. value, and the result of the previous application or @var{init} at the
  5444. first application. The order in which @var{proc} is applied to the
  5445. associations is unspecified.
  5446. @end deffn
  5447. @deffn {Scheme Procedure} hashtable-map->lset hashtable proc
  5448. @var{proc} should accept two arguments, should return a single value,
  5449. and should not mutate @var{hashtable}. The @code{hashtable-map->lset}
  5450. procedure applies @var{proc} once for every association in
  5451. @var{hashtable}, passing it the key and value as arguments, and
  5452. accumulates the returned values into a list. The order in which
  5453. @var{proc} is applied to the associations, and the order of the results
  5454. in the returned list, are unspecified.
  5455. @quotation note
  5456. This procedure can trivially imitate @code{hashtable->alist}:
  5457. @samp{(hashtable-map->lset @var{hashtable} cons)}.
  5458. @end quotation
  5459. @quotation warning
  5460. Since the order of the results is unspecified, the returned list should
  5461. be treated as a set or multi-set. Relying on the order of results will
  5462. produce unpredictable programs.
  5463. @end quotation
  5464. @end deffn
  5465. @deffn {Scheme Procedure} hashtable-find hashtable proc
  5466. @var{proc} should accept two arguments, should return a single value,
  5467. and should not mutate @var{hashtable}. The @code{hashtable-find}
  5468. procedure applies @var{proc} to associations in @var{hashtable} in an
  5469. unspecified order until one of the applications returns a true value or
  5470. the associations are exhausted. Three values are returned: the key and
  5471. value of the matching association or two unspecified values if none
  5472. matched, and a boolean indicating whether any association matched.
  5473. @end deffn
  5474. @deffn {Scheme Procedure} hashtable-empty? hashtable
  5475. Effectively equivalent to @samp{(zero? (hashtable-size @var{hashtable}))}.
  5476. @end deffn
  5477. @deffn {Scheme Procedure} hashtable-pop! hashtable
  5478. Effectively equivalent to:
  5479. @lisp
  5480. (let-values (((key value found?)
  5481. (hashtable-find @var{hashtable} (lambda (k v) #t))))
  5482. (when (not found?)
  5483. (error))
  5484. (hashtable-delete! @var{hashtable} key)
  5485. (values key value))
  5486. @end lisp
  5487. @end deffn
  5488. @deffn {Scheme Procedure} hashtable-inc! hashtable key
  5489. @deffnx {Scheme Procedure} hashtable-inc! hashtable key number
  5490. Effectively equivalent to:
  5491. @lisp
  5492. (hashtable-update! @var{hashtable} @var{key} (lambda (v) (+ v @var{number})) 0)
  5493. @end lisp
  5494. where @var{number} is 1 when not provided.
  5495. @end deffn
  5496. @deffn {Scheme Procedure} hashtable-dec! hashtable key
  5497. @deffnx {Scheme Procedure} hashtable-dec! hashtable key number
  5498. Effectively equivalent to:
  5499. @lisp
  5500. (hashtable-update! @var{hashtable} @var{key} (lambda (v) (- v @var{number})) 0)
  5501. @end lisp
  5502. where @var{number} is 1 when not provided.
  5503. @end deffn
  5504. @node SRFI 126 Inspection
  5505. @subsubsection SRFI 126 Inspection
  5506. @deffn {Scheme Procedure} hashtable-equivalence-function hashtable
  5507. Return the equivalence function used by @var{hashtable} to compare
  5508. keys. For hash tables created with @code{make-eq-hashtable} and
  5509. @code{make-eqv-hashtable}, returns @code{eq?} and @code{eqv?}
  5510. respectively.
  5511. @end deffn
  5512. @deffn {Scheme Procedure} hashtable-hash-function hashtable
  5513. Return the hash function(s) used by @var{hashtable}, that is, either a
  5514. procedure, or a pair of procedures. For hash tables created by
  5515. @code{make-eq-hashtable} or @code{make-eqv-hashtable}, @code{#f} is
  5516. returned.
  5517. @end deffn
  5518. @deffn {Scheme Procedure} hashtable-weakness hashtable
  5519. Return the weakness attribute of @var{hashtable}. The same values that
  5520. are accepted as the weakness argument in the constructor procedures are
  5521. returned. This procedure may expose the fact that @code{weak-key} and
  5522. @code{weak-value} hash tables are implemented as @var{ephemeral-key} and
  5523. @var{ephemeral-value} hash tables, returning symbols indicating the
  5524. latter even when the former were used to construct the hash table.
  5525. @end deffn
  5526. @deffn {Scheme Procedure} hashtable-mutable? hashtable
  5527. Return @code{#t} if @var{hashtable} is mutable, otherwise @code{#f}.
  5528. @end deffn
  5529. @node SRFI 126 Hash functions
  5530. @subsubsection SRFI 126 Hash functions
  5531. The @code{equal-hash}, @code{string-hash}, and @code{string-ci-hash}
  5532. procedures of this section are acceptable as the hash functions of a
  5533. hash table only if the keys on which they are called are not mutated
  5534. while they remain in use as keys in the hash table.
  5535. An implementation may initialize its hash functions with a random salt
  5536. value at program startup, meaning they are not guaranteed to return the
  5537. same values for the same inputs across multiple runs of a program. If
  5538. however the environment variable @env{SRFI_126_HASH_SEED} is set to a
  5539. non-empty string before program startup, then the salt value is derived
  5540. from that string in a deterministic manner.
  5541. @deffn {Scheme Syntax} hash-salt
  5542. Expand to a form evaluating to an exact non-negative integer that lies
  5543. within the fixnum range of the implementation. The value the expanded
  5544. form evaluates to remains constant throughout the execution of the
  5545. program. It is random for every run of the program, except when the
  5546. environment variable @env{SRFI_126_HASH_SEED} is set to a non-empty
  5547. string before program startup, in which case it is derived from the
  5548. value of that environment variable in a deterministic manner.
  5549. @end deffn
  5550. @deffn {Scheme Procedure} equal-hash obj
  5551. Return an integer hash value for @var{obj}, based on its structure and
  5552. current contents. This hash function is suitable for use with
  5553. @code{equal?} as an equivalence function.
  5554. @end deffn
  5555. @deffn {Scheme Procedure} string-hash string
  5556. Return an integer hash value for @var{string}, based on its current
  5557. contents. This hash function is suitable for use with @code{string=?}
  5558. as an equivalence function.
  5559. @end deffn
  5560. @deffn {Scheme Procedure} string-ci-hash string
  5561. Return an integer hash value for @var{string} based on its current
  5562. contents, ignoring case. This hash function is suitable for use with
  5563. @code{string-ci=?} as an equivalence function.
  5564. @end deffn
  5565. @deffn {Scheme Procedure} symbol-hash symbol
  5566. Return an integer hash value for @var{symbol}.
  5567. @end deffn
  5568. @node SRFI 128
  5569. @subsection Comparators
  5570. @cindex SRFI 128
  5571. @cindex comparators
  5572. @uref{https://srfi.schemers.org/srfi-128/srfi-128.html, SRFI 128}
  5573. provides comparators, which bundle a @emph{type test predicate}, an
  5574. @emph{equality predicate}, an @emph{ordering predicate}, and a @emph{hash
  5575. function} into a single Scheme object. By packaging these procedures
  5576. together, they can be treated as a single item for use in the
  5577. implementation of data structures.
  5578. @noindent
  5579. The four procedures above have complex dependencies on one another, and
  5580. it is inconvenient to have to pass them individually to other procedures
  5581. that might or might not make use of all of them. For example, a set
  5582. implementation by its nature requires only an equality predicate, but if
  5583. it is implemented using a hash table, an appropriate hash function is
  5584. also required if the implementation does not provide one; alternatively,
  5585. if it is implemented using a tree, procedures specifying a total order
  5586. are required. By passing a comparator rather than a bare equality
  5587. predicate, the set implementation can make use of whatever procedures
  5588. are available and useful to it.
  5589. @subheading Definitions
  5590. A comparator is an object of a disjoint type. It is a bundle of
  5591. procedures that are useful for comparing two objects in a total order.
  5592. It is an error if any of the procedures have side effects. There are
  5593. four procedures in the bundle:
  5594. @enumerate
  5595. @item
  5596. The @emph{type test predicate} returns @code{#t} if its argument has the
  5597. correct type to be passed as an argument to the other three procedures,
  5598. and @code{#f} otherwise.
  5599. @item
  5600. The @emph{equality predicate} returns @code{#t} if the two objects are the
  5601. same in the sense of the comparator, and @code{#f} otherwise. It is the
  5602. programmer's responsibility to ensure that it is reflexive, symmetric,
  5603. transitive, and can handle any arguments that satisfy the type test
  5604. predicate.
  5605. @item
  5606. The @emph{ordering predicate} returns @code{#t} if the first object
  5607. precedes the second in a total order, and @code{#f} otherwise. Note
  5608. that if it is true, the equality predicate must be false. It is the
  5609. programmer's responsibility to ensure that it is irreflexive,
  5610. anti-symmetric, transitive, and can handle any arguments that satisfy
  5611. the type test predicate.
  5612. @item
  5613. The @emph{hash function} takes an object and returns an exact non-negative
  5614. integer. It is the programmer's responsibility to ensure that it can
  5615. handle any argument that satisfies the type test predicate, and that it
  5616. returns the same value on two objects if the equality predicate says
  5617. they are the same (but not necessarily the converse).
  5618. @end enumerate
  5619. It is also the programmer's responsibility to ensure that all four
  5620. procedures provide the same result whenever they are applied to the same
  5621. object(s) (in the sense of @code{eqv?}), unless the object(s) have been
  5622. mutated since the last invocation.
  5623. @subheading Limitations
  5624. The comparator objects defined in SRFI 128 are not applicable to
  5625. circular structures or to NaNs, or to objects containing any of these.
  5626. Attempts to pass any such objects to any procedure defined here, or to
  5627. any procedure that is part of a comparator defined here, is an error
  5628. except as otherwise noted.
  5629. @menu
  5630. * SRFI 128 Predicates::
  5631. * SRFI 128 Constructors::
  5632. * SRFI 128 Standard hash functions::
  5633. * SRFI 128 Bounds and salt::
  5634. * SRFI 128 Default comparators::
  5635. * SRFI 128 Accessors and Invokers::
  5636. * SRFI 128 Comparison predicates::
  5637. * SRFI 128 Syntax::
  5638. @end menu
  5639. @node SRFI 128 Predicates
  5640. @subsubsection SRFI 128 Predicates
  5641. @deffn {Scheme Procedure} comparator? obj
  5642. Return @code{#t} if @var{obj} is a comparator, and @code{#f} otherwise.
  5643. @end deffn
  5644. @deffn {Scheme Procedure} comparator-ordered? comparator
  5645. Return @code{#t} if @var{comparator} has a supplied ordering predicate,
  5646. and @code{#f} otherwise.
  5647. @end deffn
  5648. @deffn {Scheme Procedure} comparator-hashable? comparator
  5649. Return @code{#t} if @var{comparator} has a supplied hash function, and
  5650. @code{#f} otherwise.
  5651. @end deffn
  5652. @node SRFI 128 Constructors
  5653. @subsubsection SRFI 128 Constructors
  5654. The following comparator constructors all supply appropriate type test
  5655. predicates, equality predicates, ordering predicates, and hash functions
  5656. based on the supplied arguments. They are allowed to cache their
  5657. results: they need not return a newly allocated object, since
  5658. comparators are pure and functional. In addition, the procedures in a
  5659. comparator are likewise pure and functional.
  5660. @deffn {Scheme Procedure} make-comparator type-test equality ordering hash
  5661. Return a comparator which bundles the @var{type-test}, @var{equality},
  5662. @var{ordering}, and @var{hash} procedures provided. However, if
  5663. @var{ordering} or @var{hash} is @code{#f}, a procedure is provided that
  5664. signals an error on application. The predicates
  5665. @code{comparator-ordered?} and/or @code{comparator-hashable?},
  5666. respectively, will return @code{#f} in these cases.
  5667. Here are calls on @code{make-comparator} that will return useful
  5668. comparators for standard Scheme types:
  5669. @itemize
  5670. @item
  5671. @samp{(make-comparator boolean? boolean=? (lambda (x y) (and (not x) y))
  5672. boolean-hash)} will return a comparator for booleans, expressing the
  5673. ordering @samp{#f < #t} and the standard hash function for booleans.
  5674. @item
  5675. @samp{(make-comparator real? = < (lambda (x) (exact (abs x))))} will
  5676. return a comparator expressing the natural ordering of real numbers and
  5677. a plausible (but not optimal) hash function.
  5678. @item
  5679. @samp{(make-comparator string? string=? string<? string-hash)} will
  5680. return a comparator expressing the ordering of strings and the standard
  5681. hash function.
  5682. @item
  5683. @samp{(make-comparator string? string-ci=? string-ci<? string-ci-hash)}
  5684. will return a comparator expressing the case-insensitive ordering of
  5685. strings and the standard case-insensitive hash function.
  5686. @end itemize
  5687. @end deffn
  5688. @deffn {Scheme Procedure} make-pair-comparator car-comparator cdr-comparator
  5689. This procedure returns comparators whose functions behave as follows:
  5690. @itemize
  5691. @item
  5692. The type test returns @code{#t} if its argument is a pair, if the car
  5693. satisfies the type test predicate of @var{car-comparator}, and the cdr
  5694. satisfies the type test predicate of @var{cdr-comparator}.
  5695. @item
  5696. The equality function returns @code{#t} if the cars are equal according
  5697. to @var{car-comparator} and the cdrs are equal according to
  5698. @var{cdr-comparator}, and @code{#f} otherwise.
  5699. @item
  5700. The ordering function first compares the cars of its pairs using the
  5701. equality predicate of @var{car-comparator}. If they are not equal, then
  5702. the ordering predicate of @var{car-comparator} is applied to the cars
  5703. and its value is returned. Otherwise, the predicate compares the cdrs
  5704. using the equality predicate of @var{cdr-comparator}. If they are not
  5705. equal, then the ordering predicate of @var{cdr-comparator} is applied to
  5706. the cdrs and its value is returned.
  5707. @item
  5708. The hash function computes the hash values of the car and the cdr using
  5709. the hash functions of @var{car-comparator} and @var{cdr-comparator}
  5710. respectively and then hashes them together.
  5711. @end itemize
  5712. @end deffn
  5713. @deffn {Scheme Procedure} make-list-comparator element-comparator type-test empty? head tail
  5714. This procedure returns comparators whose functions behave as follows:
  5715. @itemize
  5716. @item
  5717. The type test returns @code{#t} if its argument satisfies
  5718. @var{type-test} and the elements satisfy the type test predicate of
  5719. @var{element-comparator}.
  5720. @item
  5721. The total order defined by the equality and ordering functions is as
  5722. follows (known as lexicographic order):
  5723. @itemize
  5724. @item
  5725. The empty sequence, as determined by calling @code{empty?}, compares
  5726. equal to itself.
  5727. @item
  5728. The empty sequence compares less than any non-empty sequence.
  5729. @item
  5730. Two non-empty sequences are compared by calling the @var{head} procedure
  5731. on each. If the heads are not equal when compared using
  5732. @var{element-comparator}, the result is the result of that comparison.
  5733. Otherwise, the results of calling the @var{tail} procedure are compared
  5734. recursively.
  5735. @end itemize
  5736. @item
  5737. The hash function computes the hash values of the elements using the
  5738. hash function of @var{element-comparator} and then hashes them together.
  5739. @end itemize
  5740. @end deffn
  5741. @deffn {Scheme Procedure} make-vector-comparator element-comparator type-test length ref
  5742. This procedure returns comparators whose functions behave as follows:
  5743. @itemize
  5744. @item
  5745. The type test returns @code{#t} if its argument satisfies
  5746. @var{type-test} and the elements satisfy the type test predicate of
  5747. @var{element-comparator}.
  5748. @item
  5749. The equality predicate returns @code{#t} if both of the following tests
  5750. are satisfied in order: the lengths of the vectors are the same in the
  5751. sense of @code{=}, and the elements of the vectors are the same in the
  5752. sense of the equality predicate of @var{element-comparator}.
  5753. @item
  5754. The ordering predicate returns @code{#t} if the results of applying
  5755. @var{length} to the first vector is less than the result of applying
  5756. length to the second vector. If the lengths are equal, then the
  5757. elements are examined pairwise using the ordering predicate of
  5758. @var{element-comparator}. If any pair of elements returns @code{#t},
  5759. then that is the result of the list comparator's ordering predicate;
  5760. otherwise the result is @code{#f}.
  5761. @item
  5762. The hash function computes the hash values of the elements using the
  5763. hash function of @var{element-comparator} and then hashes them together.
  5764. @end itemize
  5765. Here is an example, which returns a comparator for byte vectors:
  5766. @lisp
  5767. (make-vector-comparator
  5768. (make-comparator exact-integer? = < number-hash)
  5769. bytevector?
  5770. bytevector-length
  5771. bytevector-u8-ref)
  5772. @end lisp
  5773. @end deffn
  5774. @deffn {Scheme Procedure} make-eq-comparator
  5775. @deffnx {Scheme Procedure} make-eqv-comparator
  5776. @deffnx {Scheme Procedure} make-equal-comparator
  5777. These procedures return comparators whose functions behave as follows:
  5778. @itemize
  5779. @item
  5780. The type test returns @code{#t} in all cases.
  5781. @item
  5782. The equality functions are @code{eq?}, @code{eqv?}, and @code{equal?},
  5783. respectively.
  5784. @item
  5785. The ordering function is set @code{#f}, and attempting to use it will
  5786. cause an error with the message @code{"ordering is not supported"}.
  5787. @item
  5788. The hash function is @code{default-hash}.
  5789. @end itemize
  5790. @end deffn
  5791. @node SRFI 128 Standard hash functions
  5792. @subsubsection SRFI 128 Standard hash functions
  5793. These are hash functions for some standard Scheme types, suitable for
  5794. passing to @code{make-comparator}. Users may write their own hash
  5795. functions with the same signature. However, if programmers wish their
  5796. hash functions to be backward compatible with the reference
  5797. implementation of @uref{https://srfi.schemers.org/srfi-69/srfi-69.html,
  5798. SRFI 69}, they are advised to write their hash functions to accept a
  5799. second argument and ignore it.
  5800. @deffn {Scheme Procedure} boolean-hash obj
  5801. @deffnx {Scheme Procedure} char-hash obj
  5802. @deffnx {Scheme Procedure} string-hash obj
  5803. @deffnx {Scheme Procedure} string-ci-hash obj
  5804. @deffnx {Scheme Procedure} symbol-hash obj
  5805. @deffnx {Scheme Procedure} number-hash obj
  5806. @end deffn
  5807. These are suitable hash functions for the specified types. The hash
  5808. functions @code{char-ci-hash} and @code{string-ci-hash} treat their
  5809. argument case-insensitively. Note that while @code{symbol-hash} may
  5810. return the hashed value of applying @code{symbol->string} and then
  5811. @code{string-hash} to the symbol, this is not a requirement.
  5812. @node SRFI 128 Bounds and salt
  5813. @subsubsection SRFI 128 Bounds and salt
  5814. The following macros allow the callers of hash functions to affect their
  5815. behavior without interfering with the calling signature of a hash
  5816. function, which accepts a single argument (the object to be hashed) and
  5817. returns its hash value.
  5818. @deffn {Scheme Syntax} hash-bound
  5819. Hash functions should be written so as to return a number between
  5820. @code{0} and the largest reasonable number of elements (such as hash
  5821. buckets) a data structure in the implementation might have. This value
  5822. is defined as @math{2^25-1} or @code{33554432} in the reference
  5823. implementation used by Guile. This value provides the current bound as
  5824. a positive exact integer, typically for use by user-written hash
  5825. functions. However, they are not required to bound their results in
  5826. this way.
  5827. @end deffn
  5828. @deffn {Scheme Syntax} hash-salt
  5829. A salt is random data in the form of a non-negative exact integer used
  5830. as an additional input to a hash function in order to defend against
  5831. dictionary attacks, or (when used in hash tables) against
  5832. denial-of-service attacks that overcrowd certain hash buckets,
  5833. increasing the amortized O(1) lookup time to O(n). Salt can also be
  5834. used to specify which of a family of hash functions should be used for
  5835. purposes such as cuckoo hashing. This macro provides the current value
  5836. of the salt, typically for use by user-written hash functions. However,
  5837. they are not required to make use of the current salt.
  5838. The initial value is implementation-dependent, but must be less than the
  5839. value of @samp{(hash-bound)}, and should be distinct for distinct runs
  5840. of a program unless otherwise specified by the implementation. In the
  5841. reference implementation used by Guile, the initial salt value is
  5842. @code{16064047}.
  5843. @end deffn
  5844. @node SRFI 128 Default comparators
  5845. @subsubsection SRFI 128 Default comparators
  5846. @deffn {Scheme Procedure} make-default-comparator
  5847. Return a comparator known as a @emph{default comparator} that accepts
  5848. Scheme values and orders them in a way that respects the following
  5849. conditions:
  5850. @itemize
  5851. @item
  5852. Given disjoint types @code{a} and @code{b}, one of three conditions must
  5853. hold:
  5854. @itemize
  5855. @item
  5856. All objects of type @code{a} compare less than all objects of type
  5857. @code{b}.
  5858. @item
  5859. All objects of type @code{a} compare greater than all objects of type
  5860. @code{b}.
  5861. @item
  5862. All objects of both type @code{a} and type @code{b} compare equal to
  5863. each other. This is not permitted for any of the Scheme types mentioned
  5864. below.
  5865. @end itemize
  5866. @item
  5867. The empty list must be ordered before all pairs.
  5868. @item
  5869. When comparing booleans, it must use the total order @samp{#f < #t}.
  5870. @item
  5871. When comparing characters, @code{char=?} and @code{char<?} are used.
  5872. @item
  5873. When comparing pairs, it must behave the same as a comparator returned
  5874. by @code{make-pair-comparator} with default comparators as arguments.
  5875. @item
  5876. When comparing symbols, the total order produced with @code{symbol<?}
  5877. and @code{symbol<?} is used.
  5878. @item
  5879. When comparing bytevectors, it must behave the same as a comparator
  5880. created by the expression @samp{(make-vector-comparator (make-comparator
  5881. bytevector? = < number-hash) bytevector? bytevector-length
  5882. bytevector-u8-ref)}.
  5883. @item
  5884. When comparing numbers where either number is complex, since non-real
  5885. numbers cannot be compared with @code{<}, the following least-surprising
  5886. ordering is defined: If the real parts are @code{<} or @code{>}, so are
  5887. the numbers; otherwise, the numbers are ordered by their imaginary
  5888. parts. This can still produce somewhat surprising results if one real
  5889. part is exact and the other is inexact.
  5890. @item
  5891. When comparing real numbers, it must use @code{=} and @code{<}.
  5892. @item
  5893. When comparing strings, it must use @code{string=?} and @code{string<?}.
  5894. @item
  5895. When comparing vectors, it must behave the same as a comparator returned
  5896. by @samp{(make-vector-comparator (make-default-comparator) vector?
  5897. vector-length vector-ref)}.
  5898. @item
  5899. When comparing members of types registered with
  5900. @code{comparator-register-default!}, it must behave in the same way as
  5901. the comparator registered using that function.
  5902. @end itemize
  5903. Default comparators use @code{default-hash} as their hash function.
  5904. @end deffn
  5905. @deffn {Scheme Procedure} default-hash obj
  5906. This is the hash function used by default comparators, which accepts a
  5907. Scheme value and hashes it in a way that respects the following
  5908. conditions:
  5909. @itemize
  5910. @item
  5911. When applied to a pair, it must return the result of hashing together
  5912. the values returned by @code{default-hash} when applied to the car and
  5913. the cdr.
  5914. @item
  5915. When applied to a boolean, character, string, symbol, or number, it must
  5916. return the same result as @code{boolean-hash}, @code{char-hash},
  5917. @code{string-hash}, @code{symbol-hash}, or @code{number-hash}
  5918. respectively.
  5919. @item
  5920. When applied to a list or vector, it must return the result of hashing
  5921. together the values returned by @code{default-hash} when applied to each
  5922. of the elements.
  5923. @end itemize
  5924. @end deffn
  5925. @deffn {Scheme Procedure} comparator-register-default! comparator
  5926. Register @var{comparator} for use by default comparators, such that if
  5927. the objects being compared both satisfy the type test predicate of
  5928. @var{comparator}, it will be employed by default comparators to compare
  5929. them. Return an unspecified value. It is an error if any value
  5930. satisfies both the type test predicate of @var{comparator} and any of
  5931. the following type test predicates: @code{boolean?}, @code{char?},
  5932. @code{null?}, @code{pair?}, @code{symbol?}, @code{bytevector?},
  5933. @code{number?}, @code{string?}, @code{vector?}, or the type test
  5934. predicate of a comparator that has already been registered.
  5935. This procedure is intended only to extend default comparators into
  5936. territory that would otherwise be undefined, not to override their
  5937. existing behavior. In general, the ordering of calls to
  5938. @code{comparator-register-default!} should be irrelevant.
  5939. The comparators available from this library are not registered with the
  5940. @code{comparator-register-default!} procedure, because the default
  5941. comparator is meant to be under the control of the program author rather
  5942. than the library author. It is the program author's responsibility to
  5943. ensure that the registered comparators do not conflict with each other.
  5944. @end deffn
  5945. @node SRFI 128 Accessors and Invokers
  5946. @subsubsection SRFI 128 Accessors and Invokers
  5947. @deffn {Scheme Procedure} comparator-type-test-predicate comparator
  5948. @deffnx {Scheme Procedure} comparator-equality-predicate comparator
  5949. @deffnx {Scheme Procedure} comparator-ordering-predicate comparator
  5950. @deffnx {Scheme Procedure} comparator-hash-function comparator
  5951. @end deffn
  5952. Return the four procedures of @var{comparator}.
  5953. @deffn {Scheme Procedure} comparator-test-type comparator obj
  5954. Invoke the type test predicate of @var{comparator} on @var{obj} and
  5955. return what it returns. More convenient than
  5956. @code{comparator-type-test-predicate}, but less efficient when the
  5957. predicate is called repeatedly.
  5958. @end deffn
  5959. @deffn {Scheme Procedure} comparator-check-type comparator obj
  5960. Invoke the type test predicate of @var{comparator} on @var{obj} and
  5961. return true if it returns true, but signal an error otherwise. More
  5962. convenient than @code{comparator-type-test-predicate}, but less
  5963. efficient when the predicate is called repeatedly.
  5964. @end deffn
  5965. @deffn {Scheme Procedure} comparator-hash comparator obj
  5966. Invoke the hash function of @var{comparator} on @var{obj} and return
  5967. what it returns. More convenient than @code{comparator-hash-function},
  5968. but less efficient when the predicate is called repeatedly.
  5969. @quotation note
  5970. No invokers are required for the equality and ordering predicates,
  5971. because the @code{=?} and @code{<?} predicates described after serve
  5972. this function.
  5973. @end quotation
  5974. @end deffn
  5975. @node SRFI 128 Comparison predicates
  5976. @subsubsection SRFI 128 Comparison predicates
  5977. @deffn {Scheme Procedure} =? comparator object@sub{1} object@sub{2} object@sub{3} @dots{}
  5978. @deffnx {Scheme Procedure} <? comparator object@sub{1} object@sub{2} object@sub{3} @dots{}
  5979. @deffnx {Scheme Procedure} >? comparator object@sub{1} object@sub{2} object@sub{3} @dots{}
  5980. @deffnx {Scheme Procedure} <=? comparator object@sub{1} object@sub{2} object@sub{3} @dots{}
  5981. @deffnx {Scheme Procedure} >=? comparator object@sub{1} object@sub{2} object@sub{3} @dots{}
  5982. @end deffn
  5983. @noindent
  5984. These procedures are analogous to the number, character, and string
  5985. comparison predicates of Scheme. They allow the convenient use of
  5986. comparators to handle variable data types.
  5987. @noindent
  5988. These procedures apply the equality and ordering predicates of
  5989. @var{comparator} to the objects as follows. If the specified relation
  5990. returns @code{#t} for all @var{object@sub{i}} and @var{object@sub{j}}
  5991. where @var{n} is the number of objects and @math{1 <= @var{i} < @var{j}
  5992. <= @var{n}}, then the procedures return @code{#t}, but otherwise
  5993. @code{#f}. Because the relations are transitive, it suffices to compare
  5994. each object with its successor. The order in which the values are
  5995. compared is unspecified.
  5996. @node SRFI 128 Syntax
  5997. @subsubsection SRFI 128 Syntax
  5998. @deffn {Scheme Procedure} comparator-if<=> [comparator] object@sub{1} object@sub{2} less-than equal-to greater-than
  5999. It is an error unless @var{comparator} evaluates to a comparator and
  6000. @var{object@sub{1}} and @var{object@sub{2}} evaluate to objects that the
  6001. comparator can handle. If the ordering predicate returns true when
  6002. applied to the values of @var{object@sub{1}} and @var{object@sub{2}} in
  6003. that order, then @var{less-than} is evaluated and its value returned.
  6004. If the equality predicate returns true when applied in the same way,
  6005. then @var{equal-to} is evaluated and its value returned. If neither
  6006. returns true, @var{greater-than} is evaluated and its value returned.
  6007. If @var{comparator} is omitted, a default comparator is used.
  6008. @end deffn
  6009. @node SRFI 151
  6010. @subsection SRFI 151 Bitwise Operations
  6011. @cindex SRFI 151
  6012. @menu
  6013. * SRFI 151 Abstract::
  6014. * SRFI 151 Rationale::
  6015. * SRFI 151 Procedure index::
  6016. * SRFI 151 Basic operations::
  6017. * SRFI 151 Integer operations::
  6018. * SRFI 151 Single-bit operations::
  6019. * SRFI 151 Bit field operations::
  6020. * SRFI 151 Bits conversion::
  6021. * SRFI 151 Fold/unfold and generate::
  6022. @end menu
  6023. @node SRFI 151 Abstract
  6024. @subsubsection SRFI 151 Abstract
  6025. This SRFI proposes a coherent and comprehensive set of procedures for
  6026. performing bitwise logical operations on integers; it is accompanied by
  6027. a reference implementation of the spec in terms of a set of seven core
  6028. operators.
  6029. The precise semantics of these operators is almost never an issue. A
  6030. consistent, portable set of @emph{names} and @emph{parameter
  6031. conventions}, however, is. Hence this SRFI, which is based mainly on
  6032. @url{https://srfi.schemers.org/srfi-33/srfi-33.html, SRFI 33}, with some
  6033. changes and additions from
  6034. @url{http://srfi.schemers.org/srfi-33/mail-archive/msg00023.html, Olin's
  6035. late revisions to SRFI 33} (which were never consummated).
  6036. @url{https://srfi.schemers.org/srfi-60/srfi-60.html, SRFI 60} (based on
  6037. SLIB) is smaller but has a few procedures of its own; some of its
  6038. procedures have both native (often Common Lisp) and SRFI 33 names. They
  6039. have been incorporated into this SRFI.
  6040. @url{http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-Z-H-12.html#node_sec_11.4,
  6041. R6RS} is a subset of SRFI 60, except that all procedure names begin with
  6042. a @code{bitwise-} prefix. A few procedures have been added from the
  6043. general vector @url{https://srfi.schemers.org/srfi-133/srfi-133.html,
  6044. SRFI 133}.
  6045. Among the applications of bitwise operations are: hashing, Galois-field
  6046. calculations of error-detecting and error-correcting codes, cryptography
  6047. and ciphers, pseudo-random number generation, register-transfer-level
  6048. modeling of digital logic designs, Fast-Fourier transforms, packing and
  6049. unpacking numbers in persistent data structures, space-filling curves
  6050. with applications to dimension reduction and sparse multi-dimensional
  6051. database indexes, and generating approximate seed values for
  6052. root-finders and transcendental function algorithms.
  6053. @noindent
  6054. This SRFI differs from SRFI 142 in only two ways:
  6055. @enumerate
  6056. @item
  6057. The @code{bitwise-if} function has the argument ordering of
  6058. SLIB, SRFI 60, and R6RS rather than the ordering of SRFI 33.
  6059. @item
  6060. The order in which bits are processed by the procedures listed in the
  6061. ``Bits conversion'' section has been clarified and some of the
  6062. procedures' names have been changed. See the ``Bit processing order''
  6063. section for details.
  6064. @end enumerate
  6065. @node SRFI 151 Rationale
  6066. @subsubsection SRFI 151 Rationale
  6067. @subheading General design principles
  6068. @itemize
  6069. @item
  6070. These operations interpret exact integers using two's-complement
  6071. representation.
  6072. @item
  6073. The associative bitwise ops are required to be n-ary. Programmers can
  6074. reliably write @code{bitwise-and} with 3 arguments, for example.
  6075. @item
  6076. The word @code{or} is never used by itself, only with modifiers:
  6077. @code{xor}, @code{ior}, @code{nor}, @code{orc1}, or @code{orc2}. This
  6078. is the same rule as Common Lisp.
  6079. @item
  6080. Extra and redundant functions such as @code{bitwise-count},
  6081. @code{bitwise-nor}and the bit-field ops have been included. Settling on
  6082. a standard choice of names makes it easier to read code that uses these
  6083. sorts of operations. It also means computations can be clearly
  6084. expressed using the more powerful ops rather than synthesized with a
  6085. snarled mess of @code{bitwise-and}s, @code{bitwise-or}s, and
  6086. @code{bitwise-not}s. What we gain is having an agreed-upon set of names
  6087. by which we can refer to these functions. If you believe in "small is
  6088. beautiful," then what is your motivation for including anything beyond
  6089. @code{bitwise-nand}?
  6090. @item
  6091. Programmers don't have to re-implement the redundant functions, and
  6092. stumble over the boundary cases and error checking, but can express
  6093. themselves using a full palette of building blocks.
  6094. @item
  6095. Compilers can directly implement many of these ops for great efficiency
  6096. gains without requiring any tricky analysis.
  6097. @item
  6098. Logical right or left shift operations are excluded because they are not
  6099. well defined on general integers; they are only defined on integers in
  6100. some finite range. Remember that, in this library, integers are
  6101. interpreted as semi-infinite bit strings that have only a finite number
  6102. of ones or a finite number of zeros. Logical shifting operates on bit
  6103. strings of some fixed size. If we shift left, then leftmost bits "fall
  6104. off" the end (and zeros shift in on the right). If we shift right, then
  6105. zeros shift into the string on the left (and rightmost bits fall off the
  6106. end). So to define a logical shift operation, we must specify the size
  6107. of the window.
  6108. @end itemize
  6109. @subheading Common Lisp
  6110. The core of this design mirrors the structure of Common Lisp's pretty
  6111. closely. Here are some differences:
  6112. @itemize
  6113. @item
  6114. "load" and "deposit" are the wrong verbs (e.g., Common Lisp's @code{ldb}
  6115. and @code{dpb} ops), since they have nothing to do with the store.
  6116. @item
  6117. @code{boole} has been removed; it is not one with the Way of Scheme.
  6118. Boolean functions are directly encoded in Scheme as first-class
  6119. functions.
  6120. @item
  6121. The name choices are more in tune with Scheme conventions (hyphenation,
  6122. using @code{?} to mark a predicate, etc.) Common Lisp's name choices
  6123. were more historically motivated, for reasons of backward compatibility
  6124. with Maclisp and Zetalisp.
  6125. @item
  6126. The prefix @code{log} has been changed to @code{bitwise-} (e.g,
  6127. @code{lognot} to @code{bitwise-not}), as the prefix @code{bitwise-} more
  6128. accurately reflects what they do.
  6129. @item
  6130. The six trivial binary boolean ops that return constants, the left or
  6131. right arguments, and the @code{bitwise-not} of the left or right
  6132. arguments, do not appear in this SRFI.
  6133. @end itemize
  6134. @subheading SRFI 33
  6135. This SRFI contains all the procedures of SRFI 33, and retains their
  6136. original names with these exceptions:
  6137. @itemize
  6138. @item
  6139. The name @code{bitwise-merge} is replaced by @code{bitwise-if}, the name
  6140. used in SRFI 60 and R6RS.
  6141. @item
  6142. The name @code{extract-bit-field} (@code{bit-field-extract} in Olin's
  6143. revisions) is replaced by @code{bit-field}, the name used in SRFI 60 and
  6144. R6RS.
  6145. @item
  6146. The names @code{any-bits-set?} and @code{all-bits-set?} are replaced by
  6147. @code{any-bit-set?} and @code{every-bit-set?}, in accordance with Olin's
  6148. revisions.
  6149. @item
  6150. The name @code{test-bit-field?} has been renamed @code{bit-field-any?}
  6151. and supplemented with @code{bit-field-every?}, in accordance with Olin's
  6152. revisions.
  6153. @item
  6154. Because @code{copy-bit-field} means different things in SRFI 33 and SRFI
  6155. 60, SRFI 33's name @code{copy-bit-field} (@code{bit-field-copy} in
  6156. Olin's revisions) has been changed to @code{bit-field-replace-same}
  6157. @end itemize
  6158. @subheading SRFI 60
  6159. SRFI 60 includes six procedures that do not have SRFI 33 equivalents.
  6160. They are incorporated into this SRFI as follows:
  6161. @itemize
  6162. @item
  6163. The names @code{rotate-bit-field} and @code{reverse-bit-field} are
  6164. replaced by @code{bit-field-rotate} and @code{bit-field-reverse}, by
  6165. analogy with Olin's revisions.
  6166. @item
  6167. The procedure @code{copy-bit} is incorporated into this SRFI with the
  6168. same name.
  6169. @item
  6170. The procedures @code{integer->list} and @code{list->integer}are
  6171. incorporated into this SRFI with the slightly different names
  6172. @code{integer->bits}and @code{bits->integer} because they are
  6173. incompatible with SRFI 60.
  6174. @item
  6175. The procedure @code{booleans->integer} is a convenient way to specify a
  6176. bitwise integer: it accepts an arbitrary number of boolean arguments and
  6177. returns a non-negative integer. So in this SRFI it has the short name
  6178. @code{bits}, roughly analogous to @code{list}, @code{string}, and
  6179. @code{vector}
  6180. @end itemize
  6181. @subheading Other sources
  6182. @itemize
  6183. @item
  6184. The following procedures are inspired by
  6185. @url{https://srfi.schemers.org/srfi-133/srfi-133.html, SRFI 133}:
  6186. @code{bit-swap}, @code{bitwise-fold}, @code{bitwise-for-each},
  6187. @code{bitwise-unfold}.
  6188. @item
  6189. The procedure @code{bit-field-set} is the counterpart of
  6190. @code{bit-field-clear}.
  6191. @item
  6192. The procedures @code{bits->vector} and @code{vector->bits} are inspired
  6193. by their list counterparts.
  6194. @item
  6195. The @code{make-bitwise-generator} procedure is a generator constructor
  6196. similar to those provided by
  6197. @url{https://srfi.schemers.org/srfi/srfi-127.html, SRFI 127}.
  6198. @end itemize
  6199. @subheading Argument ordering and semantics
  6200. In general, these procedures place the bitstring arguments to be
  6201. operated on first. Where the operation is not commutative, the
  6202. "destination" argument that provides the background bits to be operated
  6203. on is placed before the "source" argument that provides the bits to be
  6204. transferred to it.
  6205. @itemize
  6206. @item
  6207. In SRFI 33, @code{bitwise-nand} and @code{bitwise-nor} accepted an
  6208. arbitrary number of arguments even though they are not commutative.
  6209. Olin's late revisions made them dyadic, and so does this SRFI.
  6210. @item
  6211. Common Lisp bit-field operations use a @emph{byte spec} to encapsulate
  6212. the position and size of the field. SRFI 33 bit-field operations had
  6213. leading @emph{position} and @emph{size}arguments instead. These have
  6214. been replaced in this SRFI by trailing @emph{start} (inclusive) and
  6215. @emph{end} (exclusive) arguments, the convention used not only in SRFI
  6216. 60 and R6RS but also in most other subsequence operations in Scheme
  6217. standards and SRFIs.
  6218. @item
  6219. In SRFI 60, the @code{bitwise-if} function was defined with a different
  6220. argument ordering from SRFI 33's @code{bitwise-merge}, but was provided
  6221. under both names, using the SLIB ordering. SRFI 142 adopted the SRFI 33
  6222. ordering rather than the SLIB and R6RS ordering. Since SLIB and R6RS
  6223. have seen far more usage than SRFI 33, this SRFI adopts the SRFI 60
  6224. ordering instead.
  6225. @end itemize
  6226. @subheading Bit processing order
  6227. In SLIB and SRFI 60, the the order in which bits were processed by
  6228. @code{integer->list} and @code{list->integer} was not clearly specified.
  6229. When SRFI 142 was written, the specification was clarified to process
  6230. bits from least significant to most significant, so that
  6231. @samp{(integer->list 6) => (#f #t #t)}. However, the SLIB and SRFI 60
  6232. implementation processed them from the most significant bit to the
  6233. least-significant bit, so that @samp{(integer->list 6) => (#t #t #f)}.
  6234. This SRFI retains the little-endian order, but renames the procedures to
  6235. @code{bits->list} and @code{list->bits} to avoid a silent breaking
  6236. change from SLIB and SRFI 60. The same is true of the closely analogous
  6237. @code{integer->vector}, @code{vector->integer}, and @code{bits}
  6238. procedures.
  6239. @node SRFI 151 Procedure index
  6240. @subsubsection SRFI 151 Procedure index
  6241. @lisp
  6242. bitwise-not
  6243. bitwise-and bitwise-ior
  6244. bitwise-xor bitwise-eqv
  6245. bitwise-nand bitwise-nor
  6246. bitwise-andc1 bitwise-andc2
  6247. bitwise-orc1 bitwise-orc2
  6248. arithmetic-shift bit-count
  6249. integer-length bitwise-if
  6250. bit-set? copy-bit bit-swap
  6251. any-bit-set? every-bit-set?
  6252. first-set-bit
  6253. bit-field bit-field-any? bit-field-every?
  6254. bit-field-clear bit-field-set
  6255. bit-field-replace bit-field-replace-same
  6256. bit-field-rotate bit-field-reverse
  6257. bits->list list->bits bits->vector vector->bits
  6258. bits
  6259. bitwise-fold bitwise-for-each bitwise-unfold
  6260. make-bitwise-generator
  6261. @end lisp
  6262. In the following procedure specifications all parameters and return
  6263. values are exact integers unless otherwise indicated (except that
  6264. procedures with names ending in @samp{?} are predicates, as usual). It
  6265. is an error to pass values of other types as arguments to these
  6266. functions.
  6267. Bitstrings are represented by exact integers, using a two's-complement
  6268. encoding of the bitstring. Thus every integer represents a
  6269. semi-infinite bitstring, having either a finite number of zeros
  6270. (negative integers) or a finite number of ones (non-negative integers).
  6271. The bits of a bitstring are numbered from the
  6272. rightmost/least-significant bit: bit #0 is the rightmost or 2@sup{0}
  6273. bit, bit #1 is the next or 2@sup{1} bit, and so forth.
  6274. @node SRFI 151 Basic operations
  6275. @subsubsection SRFI 151 Basic operations
  6276. @deffn {Scheme Procedure} bitwise-not i
  6277. Returns the bitwise complement of @emph{i}; that is, all 1 bits are changed
  6278. to 0 bits and all 0 bits to 1 bits.
  6279. @lisp
  6280. (bitwise-not 10) => -11
  6281. (bitwise-not -37) => 36
  6282. @end lisp
  6283. @end deffn
  6284. The following ten procedures correspond to the useful set of non-trivial
  6285. two-argument boolean functions. For each such function, the
  6286. corresponding bitwise operator maps that function across a pair of
  6287. bitstrings in a bit-wise fashion. The core idea of this group of
  6288. functions is this bitwise ``lifting'' of the set of dyadic boolean
  6289. functions to bitstring parameters.
  6290. @deffn {Scheme Procedure} bitwise-and i @dots{}
  6291. @deffnx {Scheme Procedure} bitwise-ior i @dots{}
  6292. @deffnx {Scheme Procedure} bitwise-xor i @dots{}
  6293. @deffnx {Scheme Procedure} bitwise-eqv i @dots{}
  6294. These operations are associative. When passed no arguments, the procedures
  6295. return the identity values -1, 0, 0, and -1 respectively.
  6296. The @code{bitwise-eqv} procedure produces the complement of the
  6297. @code{bitwise-xor} procedure. When applied to three arguments, it does
  6298. @emph{not} produce a 1 bit everywhere that @var{a}, @var{b} and @var{c}
  6299. all agree. That is, it does @emph{not} produce:
  6300. @lisp
  6301. (bitwise-ior (bitwise-and a b c)
  6302. (bitwise-and (bitwise-not a)
  6303. (bitwise-not b)
  6304. (bitwise-not c)))
  6305. @end lisp
  6306. Rather, it produces @samp{(bitwise-eqv @var{a} (bitwise-eqv @var{b}
  6307. @var{c}))} or the equivalent @samp{(bitwise-eqv (bitwise-eqv @var{a}
  6308. @var{b}) @var{c})}:
  6309. @lisp
  6310. (bitwise-ior 3 10) => 11
  6311. (bitwise-and 11 26) => 10
  6312. (bitwise-xor 3 10) => 9
  6313. (bitwise-eqv 37 12) => -42
  6314. (bitwise-and 37 12) => 4
  6315. @end lisp
  6316. @end deffn
  6317. @deffn {Scheme Procedure} bitwise-nand i j
  6318. @deffnx {Scheme Procedure} bitwise-nor i j
  6319. @deffnx {Scheme Procedure} bitwise-andc1 i j
  6320. @deffnx {Scheme Procedure} bitwise-andc2 i j
  6321. @deffnx {Scheme Procedure} bitwise-orc1 i j
  6322. @deffnx {Scheme Procedure} bitwise-orc2 i j
  6323. These operations are not associative.
  6324. @lisp
  6325. (bitwise-nand 11 26) => -11
  6326. (bitwise-nor 11 26) => -28
  6327. (bitwise-andc1 11 26) => 16
  6328. (bitwise-andc2 11 26) => 1
  6329. (bitwise-orc1 11 26) => -2
  6330. (bitwise-orc2 11 26) => -17
  6331. @end lisp
  6332. @end deffn
  6333. @node SRFI 151 Integer operations
  6334. @subsubsection SRFI 151 Integer operations
  6335. @deffn {Scheme Procedure} arithmetic-shift i count
  6336. Return the arithmetic left shift when @var{count}>0; right shift when
  6337. @var{count}<0.
  6338. @lisp
  6339. (arithmetic-shift 8 2) => 32
  6340. (arithmetic-shift 4 0) => 4
  6341. (arithmetic-shift 8 -1) => 4
  6342. (arithmetic-shift -100000000000000000000000000000000 -100) => -79
  6343. @end lisp
  6344. @end deffn
  6345. @deffn {Scheme Procedure} bit-count i
  6346. Return the population count of 1's (@emph{i} >= 0) or 0's (@emph{i} <
  6347. 0). The result is always non-negative.
  6348. @quotation note
  6349. The R6RS analogue @code{bitwise-bit-count} applies @code{bitwise-not} to
  6350. the population count before returning it if @emph{i} is negative.
  6351. @end quotation
  6352. @lisp
  6353. (bit-count 0) => 0
  6354. (bit-count -1) => 0
  6355. (bit-count 7) => 3
  6356. (bit-count 13) => 3 ;Two's-complement binary: ...0001101
  6357. (bit-count -13) => 2 ;Two's-complement binary: ...1110011
  6358. (bit-count 30) => 4 ;Two's-complement binary: ...0011110
  6359. (bit-count -30) => 4 ;Two's-complement binary: ...1100010
  6360. (bit-count (expt 2 100)) => 1
  6361. (bit-count (- (expt 2 100))) => 100
  6362. (bit-count (- (1+ (expt 2 100)))) => 1
  6363. @end lisp
  6364. @end deffn
  6365. @deffn {Scheme Procedure} integer-length i
  6366. The number of bits needed to represent @var{i}, i.e.
  6367. @lisp
  6368. (ceiling (/ (log (if (negative? integer)
  6369. (- integer)
  6370. (+ 1 integer)))
  6371. (log 2)))
  6372. @end lisp
  6373. The result is always non-negative.
  6374. For non-negative @var{i}, this is the number of bits needed to represent
  6375. @var{i} in an unsigned binary representation. For all @var{i}, @samp{(+
  6376. 1 (integer-length @var{i}))} is the number of bits needed to represent
  6377. @var{i} in a signed twos-complement representation.
  6378. @lisp
  6379. (integer-length 0) => 0
  6380. (integer-length 1) => 1
  6381. (integer-length -1) => 0
  6382. (integer-length 7) => 3
  6383. (integer-length -7) => 3
  6384. (integer-length 8) => 4
  6385. (integer-length -8) => 3
  6386. @end lisp
  6387. @end deffn
  6388. @deffn {Scheme Procedure} bitwise-if mask i j
  6389. Merge the bitstrings @var{i} and @var{j}, with bitstring @var{mask}
  6390. determining from which string to take each bit. That is, if the
  6391. @var{k}th bit of @var{mask} is 1, then the @var{k}th bit of the result
  6392. is the @var{k}th bit of @var{i}, otherwise the @var{k}th bit of @var{j}.
  6393. @lisp
  6394. (bitwise-if 3 1 8) => 9
  6395. (bitwise-if 3 8 1) => 0
  6396. (bitwise-if 1 1 2) => 3
  6397. (bitwise-if #b00111100 #b11110000 #b00001111) => #b00110011
  6398. @end lisp
  6399. @end deffn
  6400. @node SRFI 151 Single-bit operations
  6401. @subsubsection SRFI 151 Single-bit operations
  6402. As always, the rightmost/least-significant bit in @var{i} is bit 0.
  6403. @deffn {Scheme Procedure} bit-set? index i
  6404. Is bit @var{index} set in bitstring @var{i} (where @var{index} is a
  6405. non-negative exact integer)?
  6406. @quotation note
  6407. The R6RS analogue @code{bitwise-bit-set?} accepts its arguments in the
  6408. opposite order.
  6409. @end quotation
  6410. @lisp
  6411. (bit-set? 1 1) => false
  6412. (bit-set? 0 1) => true
  6413. (bit-set? 3 10) => true
  6414. (bit-set? 1000000 -1) => true
  6415. (bit-set? 2 6) => true
  6416. (bit-set? 0 6) => false
  6417. @end lisp
  6418. @end deffn
  6419. @deffn {Scheme Procedure} copy-bit index i boolean
  6420. Return an integer the same as @var{i} except in the @var{index}th bit,
  6421. which is 1 if @var{boolean} is @code{#t} and 0 if @var{boolean} is
  6422. @code{#f}.
  6423. @quotation note
  6424. The R6RS analogue @code{bitwise-copy-bit} as originally documented has a
  6425. completely different interface. @samp{(bitwise-copy-bit @var{dest}
  6426. @var{ndex} @var{source})} replaces the @var{index}'th bit of @var{dest}
  6427. with the @var{index}'th bit of @var{source}. It is equivalent to
  6428. @samp{(bit-field-replace-same @var{dest} @var{source} @var{index} (+
  6429. @var{index} 1))}.
  6430. @end quotation
  6431. @lisp
  6432. (copy-bit 0 0 #t) => #b1
  6433. (copy-bit 2 0 #t) => #b100
  6434. (copy-bit 2 #b1111 #f) => #b1011
  6435. @end lisp
  6436. @end deffn
  6437. @deffn {Scheme Procedure} bit-swap index@sub{1} index@sub{2} i
  6438. Return an integer the same as @var{i} except that the
  6439. @var{index@sub{1}}th bit and the @var{index@sub{2}}th bit have been
  6440. exchanged.
  6441. @lisp
  6442. (bit-swap 0 2 4) => #b1
  6443. @end lisp
  6444. @end deffn
  6445. @deffn {Scheme Procedure} any-bit-set? test-bits i
  6446. @deffnx {Scheme Procedure} every-bit-set? test-bits i
  6447. Determine if any/all of the bits set in bitstring @var{test-bits} are
  6448. set in bitstring @var{i}. I.e., return @samp{(not (zero? (bitwise-and
  6449. @var{test-bits} @var{i})))} and @samp{(= @var{test-bits} (bitwise-and
  6450. @var{test-bits} @var{i}))} respectively.
  6451. @lisp
  6452. (any-bit-set? 3 6) => #t
  6453. (any-bit-set? 3 12) => #f
  6454. (every-bit-set? 4 6) => #t
  6455. (every-bit-set? 7 6) => #f
  6456. @end lisp
  6457. @end deffn
  6458. @deffn {Scheme Procedure} first-set-bit i
  6459. Return the index of the first (smallest index) 1 bit in bitstring
  6460. @var{i}. Return -1 if @var{i} contains no 1 bits (i.e., if @var{i} is
  6461. zero).
  6462. @lisp
  6463. (first-set-bit 1) => 0
  6464. (first-set-bit 2) => 1
  6465. (first-set-bit 0) => -1
  6466. (first-set-bit 40) => 3
  6467. (first-set-bit -28) => 2
  6468. (first-set-bit (expt 2 99)) => 99
  6469. (first-set-bit (expt -2 99)) => 99
  6470. @end lisp
  6471. @end deffn
  6472. @node SRFI 151 Bit field operations
  6473. @subsubsection SRFI 151 Bit field operations
  6474. These functions operate on a contiguous field of bits (a "byte", in
  6475. Common Lisp parlance) in a given bitstring. The @var{start} and
  6476. @var{end} arguments, which are not optional, are non-negative exact
  6477. integers specifying the field: it is the @var{end-start} bits running
  6478. from bit @var{start} to bit @var{end}-1.
  6479. @deffn {Scheme Procedure} bit-field i start end
  6480. Return the field from @var{i}, shifted down to the least-significant
  6481. position in the result.
  6482. @lisp
  6483. (bit-field #b1101101010 0 4) => #b1010
  6484. (bit-field #b1101101010 3 9) => #b101101
  6485. (bit-field #b1101101010 4 9) => #b10110
  6486. (bit-field #b1101101010 4 10) => #b110110
  6487. (bit-field 6 0 1) => 0
  6488. (bit-field 6 1 3) => 3
  6489. (bit-field 6 2 999) => 1
  6490. (bit-field #x100000000000000000000000000000000 128 129) => 1
  6491. @end lisp
  6492. @end deffn
  6493. @deffn {Scheme Procedure} bit-field-any? i start end
  6494. Return true if any of the field's bits are set in bitstring @var{i},
  6495. and false otherwise.
  6496. @lisp
  6497. (bit-field-any? #b1001001 1 6) => #t
  6498. (bit-field-any? #b1000001 1 6) => #f
  6499. @end lisp
  6500. @end deffn
  6501. @deffn {Scheme Procedure} bit-field-every? i start end
  6502. Return false if any of the field's bits are not set in bitstring
  6503. @var{i}, and true otherwise.
  6504. @lisp
  6505. (bit-field-every? #b1011110 1 5) => #t
  6506. (bit-field-every? #b1011010 1 5) => #f
  6507. @end lisp
  6508. @end deffn
  6509. @deffn {Scheme Procedure} bit-field-clear i start end
  6510. @deffnx {Scheme Procedure} bit-field-set i start end
  6511. Return @var{i} with the field's bits set to all 0s/1s.
  6512. @lisp
  6513. (bit-field-clear #b101010 1 4) => #b100000
  6514. (bit-field-set #b101010 1 4) => #b101110
  6515. @end lisp
  6516. @end deffn
  6517. @deffn {Scheme Procedure} bit-field-replace dest source start end
  6518. Return @var{dest} with the field replaced by the least-significant
  6519. @var{end-start} bits in @var{source}.
  6520. @lisp
  6521. (bit-field-replace #b101010 #b010 1 4) => #b100100
  6522. (bit-field-replace #b110 1 0 1) => #b111
  6523. (bit-field-replace #b110 1 1 2) => #b110
  6524. @end lisp
  6525. @end deffn
  6526. @deffn {Scheme Procedure} bit-field-replace-same dest source start end
  6527. Return @var{dest} with its field replaced by the corresponding field in
  6528. @var{source}.
  6529. @lisp
  6530. (bit-field-replace-same #b1111 #b0000 1 3) => #b1001
  6531. @end lisp
  6532. @end deffn
  6533. @deffn {Scheme Procedure} bit-field-rotate i count start end
  6534. Return @var{i} with the field cyclically permuted by @var{count} bits
  6535. towards high-order.
  6536. @quotation note
  6537. The R6RS analogue @code{bitwise-rotate-bit-field} uses the argument
  6538. ordering @var{i} @var{start} @var{end} @var{count}.
  6539. @end quotation
  6540. @lisp
  6541. (bit-field-rotate #b110 0 0 10) => #b110
  6542. (bit-field-rotate #b110 0 0 256) => #b110
  6543. (bit-field-rotate #x100000000000000000000000000000000 1 0 129) => 1
  6544. (bit-field-rotate #b110 1 1 2) => #b110
  6545. (bit-field-rotate #b110 1 2 4) => #b1010
  6546. (bit-field-rotate #b0111 -1 1 4) => #b1011
  6547. @end lisp
  6548. @end deffn
  6549. @deffn {Scheme Procedure} bit-field-reverse i start end
  6550. Return @var{i} with the order of the bits in the field reversed.
  6551. @lisp
  6552. (bit-field-reverse 6 1 3) => 6
  6553. (bit-field-reverse 6 1 4) => 12
  6554. (bit-field-reverse 1 0 32) => #x80000000
  6555. (bit-field-reverse 1 0 31) => #x40000000
  6556. (bit-field-reverse 1 0 30) => #x20000000
  6557. (bit-field-reverse #x140000000000000000000000000000000 0 129) => 5
  6558. @end lisp
  6559. @end deffn
  6560. @node SRFI 151 Bits conversion
  6561. @subsubsection SRFI 151 Bits conversion
  6562. @deffn {Scheme Procedure} bits->list i [len]
  6563. @deffnx {Scheme Procedure} bits->vector i [len]
  6564. Return a list/vector of @var{len} booleans corresponding to each bit of
  6565. the non-negative integer @var{i}, returning bit #0 as the first element,
  6566. bit #1 as the second, and so on. @code{#t} is returned for each 1;
  6567. @code{#f} for 0.
  6568. @lisp
  6569. (bits->list #b1110101) => (#t #f #t #f #t #t #t)
  6570. (bits->list 3 5) => (#t #t #f #f #f)
  6571. (bits->list 6 4) => (#f #t #t #f)
  6572. (bits->vector #b1110101) => #(#t #f #t #f #t #t #t)
  6573. @end lisp
  6574. @end deffn
  6575. @deffn {Scheme Procedure} list->bits list
  6576. @deffnx {Scheme Procedure} vector->bits vector
  6577. Return an integer formed from the booleans in @var{list}/@var{vector},
  6578. using the first element as bit #0, the second element as bit #1, and so
  6579. on. It is an error if @var{list}/@var{vector} contains non-booleans. A
  6580. 1 bit is coded for each @code{#t}; a 0 bit for @code{#f}. Note that the
  6581. result is never a negative integer.
  6582. @lisp
  6583. (list->bits '(#t #f #t #f #t #t #t)) => #b1110101
  6584. (list->bits '(#f #f #t #f #t #f #t #t #t)) => #b111010100
  6585. (list->bits '(#f #t #t)) => 6
  6586. (list->bits '(#f #t #t #f)) => 6
  6587. (list->bits '(#f #f #t #t)) => 12
  6588. (vector->bits '#(#t #f #t #f #t #t #t)) => #b1110101
  6589. (vector->bits '#(#f #f #t #f #t #f #t #t #t)) => #b111010100
  6590. (vector->bits '#(#f #t #t)) => 6
  6591. (vector->bits '#(#f #t #t #f)) => 6
  6592. (vector->bits '#(#f #f #t #t)) => 12
  6593. @end lisp
  6594. For positive integers,
  6595. @code{bits->list} and @code{list->bits} are inverses in the sense of @code{equal?},
  6596. and so are @code{bits->vector} and @code{vector->bits}
  6597. @end deffn
  6598. @deffn {Scheme Procedure} bits bool @dots{}
  6599. Return the integer coded by the @code{bool} arguments. The first
  6600. argument is bit #0, the second argument is bit #1, and so on. Note that
  6601. the result is never a negative integer.
  6602. @lisp
  6603. (bits #t #f #t #f #t #t #t) => #b1110101
  6604. (bits #f #f #t #f #t #f #t #t #t) => #b111010100
  6605. @end lisp
  6606. @end deffn
  6607. @node SRFI 151 Fold/unfold and generate
  6608. @subsubsection SRFI 151 Fold, unfold, and generate
  6609. It is an error if the arguments named @var{proc}, @var{top}?,
  6610. @var{apper} or @var{successor} are not procedures. The arguments named
  6611. @var{seed} may be any Scheme object.
  6612. @deffn {Scheme Procedure} bitwise-fold proc seed i
  6613. For each bit @var{b} of @var{i} from bit #0 (inclusive) to bit
  6614. @samp{(integer-length @var{i})}(exclusive), @var{proc} is called as
  6615. @samp{(@var{proc} @var{b} @var{r})}, where @var{r} is the current
  6616. accumulated result. The initial value of @var{r} is @var{seed}, and
  6617. the value returned by @var{proc} becomes the next accumulated result.
  6618. When the last bit has been processed, the final accumulated result
  6619. becomes the result of @code{bitwise-fold}.
  6620. @lisp
  6621. (bitwise-fold cons '() #b1010111) => (#t #f #t #f #t #t #t)
  6622. @end lisp
  6623. @end deffn
  6624. @deffn {Scheme Procedure} bitwise-for-each proc i
  6625. Repeatedly apply @var{proc} to the bits of @var{i} starting with bit
  6626. #0 (inclusive) and ending with bit @samp{(integer-length @var{i})}
  6627. (exclusive). The values returned by @var{proc} are discarded. Return
  6628. an unspecified value.
  6629. @lisp
  6630. (let ((count 0))
  6631. (bitwise-for-each (lambda (b) (if b (set! count (+ count 1))))
  6632. #b1010111)
  6633. count)
  6634. @end lisp
  6635. @end deffn
  6636. @deffn {Scheme Procedure} bitwise-unfold stop? mapper successor seed
  6637. Generate a non-negative integer bit by bit, starting with bit 0. If the
  6638. result of applying @var{stop?} to the current state (whose initial value
  6639. is @var{seed}) is true, return the currently accumulated bits as an
  6640. integer. Otherwise, apply @var{mapper} to the current state to obtain
  6641. the next bit of the result by interpreting a true value as a 1 bit and a
  6642. false value as a 0 bit. Then get a new state by applying
  6643. @var{successor} to the current state, and repeat this algorithm.
  6644. @lisp
  6645. (bitwise-unfold (lambda (i) (= i 10))
  6646. even?
  6647. (lambda (i) (+ i 1))
  6648. 0) => #b101010101
  6649. @end lisp
  6650. @end deffn
  6651. @deffn {Scheme Procedure} make-bitwise-generator i
  6652. Return a @url{https://srfi.schemers.org/srfi-121/srfi-121.html, SRFI
  6653. 121} generator that generates all the bits of @var{i} starting with bit
  6654. #0. Note that the generator is infinite.
  6655. @lisp
  6656. (let ((g (make-bitwise-generator #b110)))
  6657. (test #f (g))
  6658. (test #t (g))
  6659. (test #t (g))
  6660. (test #f (g)))
  6661. @end lisp
  6662. @end deffn
  6663. @node SRFI 160
  6664. @subsection SRFI 160: Homogeneous numeric vector libraries
  6665. @cindex SRFI 160
  6666. @menu
  6667. * SRFI 160 Abstract::
  6668. * SRFI 160 Rationale::
  6669. * SRFI 160 Datatypes::
  6670. * SRFI 160 Notation::
  6671. * SRFI 160 Packaging::
  6672. * SRFI 160 Procedures::
  6673. * SRFI 160 Optional lexical syntax::
  6674. @end menu
  6675. @node SRFI 160 Abstract
  6676. @subsubsection SRFI 160 Abstract
  6677. This SRFI describes a set of operations on SRFI 4 homogeneous vector
  6678. types (plus a few additional types) that are closely analogous to the
  6679. vector operations library,
  6680. @url{https://srfi.schemers.org/srfi-133/srfi-133.html, SRFI 133}. An
  6681. external representation is specified which may be supported by the
  6682. @code{read} and @code{write} procedures and by the program parser so
  6683. that programs can contain references to literal homogeneous vectors.
  6684. @node SRFI 160 Rationale
  6685. @subsubsection SRFI 160 Rationale
  6686. Like lists, Scheme vectors are a heterogeneous datatype which impose no
  6687. restriction on the type of the elements. This generality is not needed
  6688. for applications where all the elements are of the same type. The use
  6689. of Scheme vectors is not ideal for such applications because, in the
  6690. absence of a compiler with a fancy static analysis, the representation
  6691. will typically use some form of boxing of the elements which means low
  6692. space efficiency and slower access to the elements. Moreover,
  6693. homogeneous vectors are convenient for interfacing with low-level
  6694. libraries (e.g. binary block I/O) and to interface with foreign
  6695. languages which support homogeneous vectors. Finally, the use of
  6696. homogeneous vectors allows certain errors to be caught earlier.
  6697. This SRFI specifies a set of homogeneous vector datatypes which cover
  6698. the most practical cases, that is, where the type of the elements is
  6699. numeric (exact integer or inexact real or complex) and the precision and
  6700. representation is efficiently implemented on the hardware of most
  6701. current computer architectures (8, 16, 32 and 64 bit integers, either
  6702. signed or unsigned, and 32 and 64 bit floating point numbers).
  6703. This SRFI extends @url{https://srfi.schemers.org/srfi-4/srfi-4.html,
  6704. SRFI 4} by providing the additional @code{c64vector} and
  6705. @code{c128vector} types, and by providing analogues for almost all of
  6706. the heterogeneous vector procedures of
  6707. @url{https://srfi.schemers.org/srfi-133/srfi-133.html, SRFI 133}. There
  6708. are some additional procedures, most of which are closely analogous to
  6709. the string procedures of
  6710. @url{https://srfi.schemers.org/srfi-152/srfi-152.html, SRFI 152}
  6711. Note that there are no conversions between homogeneous vectors and
  6712. strings in this SRFI. In addition, there is no support for u1vectors
  6713. (bitvectors) provided, not because they are not useful, but because they
  6714. are different enough in both specification and implementation to be put
  6715. into a future SRFI of their own.
  6716. @node SRFI 160 Datatypes
  6717. @subsubsection SRFI 160 Datatypes
  6718. There are eight datatypes of exact integer homogeneous vectors (which will
  6719. be called integer vectors):
  6720. @deffn {Scheme Datatypes} s8vector
  6721. Signed exact integer in the range -2@sup{7} to 2@sup{7}-1.
  6722. @end deffn
  6723. @deffn {Scheme Datatypes} u8vector
  6724. Unsigned exact integer in the range 0 to 2@sup{8}-1.
  6725. @end deffn
  6726. @deffn {Scheme Datatypes} s16vector
  6727. Signed exact integer in the range -2@sup{15} to 2@sup{15}-1.
  6728. @end deffn
  6729. @deffn {Scheme Datatypes} u16vector
  6730. Unsigned exact integer in the range 0 to 2@sup{16}-1.
  6731. @end deffn
  6732. @deffn {Scheme Datatypes} s32vector
  6733. Signed exact integer in the range -2@sup{31} to 2@sup{31}-1.
  6734. @end deffn
  6735. @deffn {Scheme Datatypes} u32vector
  6736. Unsigned exact integer in the range 0 to 2@sup{32}-1.
  6737. @end deffn
  6738. @deffn {Scheme Datatypes} s64vector
  6739. Signed exact integer in the range -2@sup{63} to 2@sup{63}-1.
  6740. @end deffn
  6741. @deffn {Scheme Datatypes} u64vector
  6742. Unsigned exact integer in the range 0 to 2@sup{64}-1.
  6743. @end deffn
  6744. All are part of SRFI 4.
  6745. There are two datatypes of inexact real homogeneous vectors (which will
  6746. be called float vectors):
  6747. @deffn {Scheme Datatypes} f32vector
  6748. Inexact real, typically 32 bits.
  6749. @end deffn
  6750. @deffn {Scheme Datatypes} f64vector
  6751. Inexact real, typically 64 bits.
  6752. @end deffn
  6753. These are also part of SRFI 4.
  6754. @code{f64vector}s must preserve at least as
  6755. much precision and range as @code{f32vector}s.
  6756. And there are two datatypes of inexact complex homogeneous vectors
  6757. (which will be called complex vectors):
  6758. @deffn {Scheme Datatypes} c64vector
  6759. Inexact complex, typically 64 bits.
  6760. @end deffn
  6761. @deffn {Scheme Datatypes} c128vector
  6762. Inexact complex, typically 128 bits.
  6763. @end deffn
  6764. These are @emph{not} part of SRFI 4.
  6765. @code{c128vector}s must preserve at least as
  6766. much precision and range as @code{c64vector}s.
  6767. Each element of a homogeneous vector must be @i{valid}. That is, for an
  6768. integer vector, it must be an exact integer within the inclusive range
  6769. specified above; for a float vector, it must be an inexact real number;
  6770. and for a complex vector, it must be an inexact complex number. It is
  6771. an error to try to use a constructor or mutator to set an element to an
  6772. invalid value.
  6773. @node SRFI 160 Notation
  6774. @subsubsection SRFI 160 Notation
  6775. So as not to multiply the number of procedures described in this SRFI
  6776. beyond necessity, a special notational convention is used. The
  6777. description of the procedure @code{make-@@vector} is really shorthand
  6778. for the descriptions of the twelve procedures @code{make-s8vector},
  6779. @code{make-u8vector}, @dots{}, @code{make-c128vector}, all of which are
  6780. exactly the same except that they construct different homogeneous vector
  6781. types. Furthermore, except as otherwise noted, the semantics of each
  6782. procedure are those of the corresponding SRFI 133 procedure, except that
  6783. it is an error to attempt to insert an invalid value into a homogeneous
  6784. vector. Consequently, only a brief description of each procedure is
  6785. given, and SRFI 133 (or in some cases SRFI 152) should be consulted for
  6786. the details. It is worth mentioning, however, that all the procedures
  6787. that return one or more vectors (homogeneous or heterogeneous)
  6788. invariably return newly allocated vectors specifically.
  6789. In the section containing specifications of procedures, the following
  6790. notation is used to specify parameters and return values:
  6791. @table @asis
  6792. @item (@var{f} @var{arg@sub{1}} @var{arg@sub{2}} @dots{}) -> @var{something}
  6793. A procedure @var{f} that takes the parameters @var{arg@sub{1}},
  6794. @var{arg@sub{2}}, @dots{} and returns a value of the type
  6795. @var{something}. If two values are returned, two types are specified.
  6796. If @var{something} is @code{unspecified}, then @var{f} returns a single
  6797. implementation-dependent value; this SRFI does not specify what it
  6798. returns, and in order to write portable code, the return value should be
  6799. ignored.
  6800. @item @var{vec}
  6801. Must be a heterogeneous vector, i.e. it must satisfy the predicate
  6802. @code{vector?}
  6803. @item @var{@@vec}, @var{@@to}, @var{@@from}
  6804. Must be a homogeneous vector, i.e. it must satisfy the predicate
  6805. @code{@@vector?} In @code{@@vector-copy!} and
  6806. @code{reverse-@@vector-copy!}, @var{@@to} is the destination and
  6807. @var{@@from} is the source.
  6808. @item @var{i}, @var{j}, @var{start}, @var{at}
  6809. Must be an exact nonnegative integer less than the length of the
  6810. @@vector. In @code{@@vector-copy!} and @code{reverse-@@vector-copy!},
  6811. @var{at} refers to the destination and @var{start} to the source.
  6812. @item @var{end}
  6813. Must be an exact nonnegative integer not less than @var{start} and not
  6814. greater than the length of the vector. This indicates the index
  6815. directly before which traversal will stop --- processing will occur
  6816. until the index of the vector is one less than @var{end}. It is the
  6817. open right side of a range.
  6818. @item @var{f}
  6819. Must be a procedure taking one or more arguments, which returns (except
  6820. as noted otherwise) exactly one value.
  6821. @item @var{pred}
  6822. Must be a procedure taking one or more arguments that returns one value,
  6823. which is treated as a boolean.
  6824. @item @var{=}
  6825. Must be an equivalence procedure.
  6826. @item @var{obj}, @var{seed}, @var{nil}
  6827. Any Scheme object.
  6828. @item @var{fill}, @var{value}
  6829. Any number that is valid with respect to the @var{@@vec}.
  6830. @item @var{[something]}
  6831. An optional argument; it needn't necessarily be applied.
  6832. @var{something} needn't necessarily be one thing; for example, this
  6833. usage of it is perfectly valid:
  6834. @example
  6835. [start [end]]
  6836. @end example
  6837. and is indeed used quite often.
  6838. @item @var{something} @dots{}
  6839. Zero or more @var{something}s are allowed to be arguments.
  6840. @item @var{something@sub{1}} @var{something@sub{2}} @dots{}
  6841. At least one @var{something} must be arguments.
  6842. @end table
  6843. @node SRFI 160 Packaging
  6844. @subsubsection SRFI 160 Packaging
  6845. For each @@vector type, there is a corresponding library named
  6846. @code{(srfi@tie{}srfi-160@tie{}@@)}, and if an implementation provides a
  6847. given type, it must provide that library as well. In addition, the
  6848. library @code{(srfi@tie{}srfi-160@tie{}base)} provides a few basic
  6849. procedures for all @@vector types. If a particular type is not provided
  6850. by an implementation, then it is an error to call the corresponding
  6851. procedures in this library.
  6852. @quotation note
  6853. There is no library named @code{(srfi@tie{}srfi-160)}.
  6854. @end quotation
  6855. @node SRFI 160 Procedures
  6856. @subsubsection SRFI 160 Procedures
  6857. The procedures shared with SRFI 4 are marked with [SRFI@tie{}4]. The
  6858. procedures with the same semantics as SRFI 133 are marked with
  6859. [SRFI@tie{}133] unless they are already marked with [SRFI@tie{}4]. The
  6860. procedures analogous to SRFI 152 string procedures are marked with
  6861. [SRFI@tie{}152].
  6862. @subsubheading Constructors
  6863. @deffn {Scheme Procedure} make-@@vector size [fill] -> @@vector [SRFI@tie{}4]
  6864. Returns a @@vector whose length is @var{size}. If @var{fill} is provided,
  6865. all the elements of the @@vector are initialized to it.
  6866. @end deffn
  6867. @deffn {Scheme Procedure} @@vector value @dots{} -> @@vector [SRFI@tie{}4]
  6868. Returns a @@vector initialized with @var{values}.
  6869. @end deffn
  6870. @deffn {Scheme Procedure} @@vector-unfold f length seed -> @@vector [SRFI@tie{}133]
  6871. Creates a vector whose length is @var{length} and iterates across each
  6872. index @var{k} between 0 and @var{length} - 1, applying @var{f} at each
  6873. iteration to the current index and current state, in that order, to
  6874. receive two values: the element to put in the @var{k}th slot of
  6875. the new vector and a new state for the next iteration. On the first
  6876. call to @var{f}, the state's value is @var{seed}.
  6877. @end deffn
  6878. @deffn {Scheme Procedure} @@vector-unfold-right f length seed -> @@vector [SRFI@tie{}133]
  6879. The same as @code{@@vector-unfold}, but initializes the @@vector from
  6880. right to left.
  6881. @end deffn
  6882. @deffn {Scheme Procedure} @@vector-copy @@vec [start [end]] -> @@vector [SRFI@tie{}133]
  6883. Makes a copy of the portion of @var{@@vec} from @var{start} to @var{end}
  6884. and returns it.
  6885. @end deffn
  6886. @deffn {Scheme Procedure} @@vector-reverse-copy @@vec [start [end]] -> @@vector [SRFI@tie{}133]
  6887. The same as @code{@@vector-copy}, but in reverse order.
  6888. @end deffn
  6889. @deffn {Scheme Procedure} @@vector-append @@vec @dots{} -> @@vector [SRFI@tie{}133]
  6890. Returns a @@vector containing all the elements of the @var{@@vecs} in
  6891. order.
  6892. @end deffn
  6893. @deffn {Scheme Procedure} @@vector-concatenate list-of-@@vectors -> @@vector [SRFI@tie{}133]
  6894. The same as @code{@@vector-append}, but takes a list of @@vectors rather
  6895. than multiple arguments.
  6896. @end deffn
  6897. @deffn {Scheme Procedure} @@vector-append-subvectors [@@vec start end] @dots{} -> @@vector [SRFI@tie{}133]
  6898. Concatenates the result of applying @code{@@vector-copy} to each triplet
  6899. of @var{@@vec}, @var{start}, @var{end} arguments, but may be implemented
  6900. more efficiently.
  6901. @end deffn
  6902. @subsubheading Predicates
  6903. @deffn {Scheme Procedure} @@? obj -> boolean
  6904. Returns @code{#t} if @var{obj} is a valid element of an
  6905. @@vector, and @code{#f} otherwise.
  6906. @end deffn
  6907. @deffn {Scheme Procedure} @@vector? obj -> boolean [SRFI@tie{}4]
  6908. Returns @code{#t} if @var{obj} is a @@vector, and @code{#f} otherwise.
  6909. @end deffn
  6910. @deffn {Scheme Procedure} @@vector-empty? @@vec -> boolean [SRFI@tie{}133]
  6911. Returns @code{#t} if @var{@@vec} has a length of zero, and @code{#f}
  6912. otherwise.
  6913. @end deffn
  6914. @deffn {Scheme Procedure} @@vector= @@vec @dots{} -> boolean [SRFI@tie{}133]
  6915. Compares the @var{@@vecs} for elementwise equality, using @code{=} to do
  6916. the comparisons. Returns @code{#f} unless all @@vectors are the same
  6917. length.
  6918. @end deffn
  6919. @subsubheading Selectors
  6920. @deffn {Scheme Procedure} @@vector-ref @@vec i -> value [SRFI@tie{}4]
  6921. Returns the @var{i}th element of @var{@@vec}.
  6922. @end deffn
  6923. @deffn {Scheme Procedure} @@vector-length @@vec -> exact nonnegative integer [SRFI@tie{}4]
  6924. Returns the length of @i{@@vec}.
  6925. @end deffn
  6926. @subsubheading Iteration
  6927. @deffn {Scheme Procedure} @@vector-take @@vec n -> @@vector [SRFI@tie{}152]
  6928. @deffnx {Scheme Procedure} @@vector-take-right @@vec n -> @@vector [SRFI@tie{}152]
  6929. Returns a @@vector containing the first/last @var{n} elements of
  6930. @var{@@vec}.
  6931. @end deffn
  6932. @deffn {Scheme Procedure} @@vector-drop @@vec n -> @@vector [SRFI@tie{}152]
  6933. @deffnx {Scheme Procedure} @@vector-drop-right @@vec n -> @@vector [SRFI@tie{}152]
  6934. Returns a @@vector containing all except the first/last @var{n} elements
  6935. of @var{@@vec}.
  6936. @end deffn
  6937. @deffn {Scheme Procedure} @@vector-segment @@vec n -> list [SRFI@tie{}152]
  6938. Returns a list of @@vectors, each of which contains @var{n} consecutive
  6939. elements of @var{@@vec}. The last @@vector may be shorter than @var{n}.
  6940. It is an error if @var{n} is not an exact positive integer.
  6941. @end deffn
  6942. @deffn {Scheme Procedure} @@vector-fold kons knil @@vec @@vec2 @dots{} -> object [SRFI@tie{}133]
  6943. @deffnx {Scheme Procedure} @@vector-fold-right kons knil @@vec @@vec2 @dots{} -> object [SRFI@tie{}133]
  6944. When one @@vector argument @var{@@vec} is given, folds @var{kons} over the
  6945. elements of @var{@@vec} in increasing/decreasing order using @var{knil} as
  6946. the initial value. The @var{kons} procedure is called with the state
  6947. first and the element second, as in SRFIs 43 and 133 (heterogeneous
  6948. vectors). This is the opposite order to that used in SRFI 1 (lists) and
  6949. the various string SRFIs.
  6950. When multiple @@vector arguments are given, @var{kons} is called with
  6951. the current state value and each value from all the vectors;
  6952. @code{@@vector-fold} scans elements from left to right, while
  6953. @code{@@vector-fold-right} does from right to left. If the lengths of
  6954. vectors differ, only the portion of each vector up to the length of the
  6955. shortest vector is scanned.
  6956. @end deffn
  6957. @deffn {Scheme Procedure} @@vector-map f @@vec @@vec2 @dots{} -> @@vector [SRFI@tie{}133]
  6958. @deffnx {Scheme Procedure} @@vector-map! f @@vec @@vec2 @dots{} -> unspecified [SRFI@tie{}133]
  6959. @deffnx {Scheme Procedure} @@vector-for-each f @@vec @@vec2 @dots{} -> unspecified [SRFI@tie{}133]
  6960. Iterate over the elements of @var{@@vec} and apply @var{f} to each,
  6961. returning respectively a @@vector of the results, an undefined value
  6962. with the results placed back in @var{@@vec}, and an undefined value with
  6963. no change to @var{@@vec}.
  6964. If more than one vector is passed, @var{f} gets one element from each
  6965. vector as arguments. If the lengths of the vectors differ, iteration
  6966. stops at the end of the shortest vector. For @code{@@vector-map!}, only
  6967. @var{@@vec} is modified even when multiple vectors are passed.
  6968. If @code{@@vector-map} or @code{@@vector-map!} returns more than once
  6969. (i.e. because of a continuation captured by @var{f}), the values
  6970. returned or stored by earlier returns may be mutated.
  6971. @end deffn
  6972. @deffn {Scheme Procedure} @@vector-count pred? @@vec @@vec2 @dots{} -> exact nonnegative integer [SRFI@tie{}133]
  6973. Call @var{pred?} on each element of @var{@@vec} and return the number of
  6974. calls that return true.
  6975. When multiple vectors are given, @var{pred?} must take
  6976. the same number of arguments as the number of vectors, and
  6977. corresponding elements from each vector are given for each iteration,
  6978. which stops at the end of the shortest vector.
  6979. @end deffn
  6980. @deffn {Scheme Procedure} @@vector-cumulate f knil @@vec -> @@vector [SRFI@tie{}133]
  6981. Like @code{@@vector-fold}, but returns a @@vector of partial results
  6982. rather than just the final result.
  6983. @end deffn
  6984. @subsubheading Searching
  6985. @deffn {Scheme Procedure} @@vector-take-while pred? @@vec -> @@vector [SRFI@tie{}152]
  6986. @deffnx {Scheme Procedure} @@vector-take-while-right pred? @@vec -> @@vector [SRFI@tie{}152]
  6987. Return the shortest prefix/suffix of @var{@@vec} all of whose elements
  6988. satisfy @var{pred?}.
  6989. @end deffn
  6990. @deffn {Scheme Procedure} @@vector-drop-while pred? @@vec -> @@vector [SRFI@tie{}152]
  6991. @deffnx {Scheme Procedure} @@vector-drop-while-right pred? @@vec -> @@vector [SRFI@tie{}152]
  6992. Drops the longest initial prefix/suffix of @var{@@vec} such that all its
  6993. elements satisfy @var{pred}.
  6994. @end deffn
  6995. @deffn {Scheme Procedure} @@vector-index pred? @@vec @@vec2 @dots{} -> exact nonnegative integer or #f [SRFI@tie{}133]
  6996. @deffnx {Scheme Procedure} @@vector-index-right pred? @@vec @@vec2 @dots{} -> exact nonnegative integer or #f [SRFI@tie{}133]
  6997. Return the index of the first/last element of @var{@@vec} that satisfies
  6998. @var{pred?}.
  6999. When multiple vectors are passed, @var{pred?} must take the same number of
  7000. arguments as the number of vectors, and corresponding elements from each
  7001. vector are passed for each iteration. If the lengths of vectors differ,
  7002. @code{@@vector-index} stops iteration at the end of the shortest one.
  7003. Lengths of vectors must be the same for @code{@@vector-index-right}
  7004. @end deffn
  7005. @deffn {Scheme Procedure} @@vector-skip pred? @@vec @@vec2 @dots{} -> exact nonnegative integer or #f [SRFI@tie{}133]
  7006. @deffnx {Scheme Procedure} @@vector-skip-right pred? @@vec @@vec2 @dots{} -> exact nonnegative integer or #f [SRFI@tie{}133]
  7007. Returns the index of the first/last element of @var{@@vec} that does not
  7008. satisfy @var{pred?}.
  7009. When multiple vectors are passed, @var{pred?} must take the same number
  7010. of arguments as the number of vectors, and corresponding elements from
  7011. each vector are passed for each iteration. If the lengths of vectors
  7012. differ, @code{@@vector-skip} stops iteration at the end of the shortest
  7013. one. Lengths of vectors must be the same for @code{@@vector-skip-right}
  7014. @end deffn
  7015. @deffn {Scheme Procedure} @@vector-any pred? @@vec @@vec2 @dots{} -> value or boolean [SRFI@tie{}133]
  7016. Returns first non-false result of applying @var{pred?} on a element from
  7017. the @var{@@vec}, or @code{#f} if there is no such element. If
  7018. @var{@@vec} is empty, returns @code{#t}.
  7019. When multiple vectors are passed, @var{pred?} must take the same number
  7020. of arguments as the number of vectors, and corresponding elements from
  7021. each vector are passed for each iteration. If the lengths of vectors
  7022. differ, it stops at the end of the shortest one.
  7023. @end deffn
  7024. @deffn {Scheme Procedure} @@vector-every pred? @@vec @@vec2 @dots{} -> value or boolean [SRFI@tie{}133]
  7025. If all elements from @var{@@vec} satisfy @var{pred?}, return the last
  7026. result of @var{pred?}. If not all do, return @code{#f} If @var{@@vec}
  7027. is empty, return @code{#t}.
  7028. When multiple vectors are passed, @var{pred?} must take the same number
  7029. of arguments as the number of vectors, and corresponding elements from
  7030. each vector is passed for each iteration. If the lengths of vectors
  7031. differ, it stops at the end of the shortest one.
  7032. @end deffn
  7033. @deffn {Scheme Procedure} @@vector-partition pred? @@vec -> @@vector and integer [SRFI@tie{}133]
  7034. Returns a @@vector of the same type as @var{@@vec}, but with all
  7035. elements satisfying @var{pred?} in the leftmost part of the vector and
  7036. the other elements in the remaining part. The order of elements is
  7037. otherwise preserved. Returns two values, the new @@vector and the
  7038. number of elements satisfying @var{pred?}.
  7039. @end deffn
  7040. @deffn {Scheme Procedure} @@vector-filter pred? @@vec -> @@vector [SRFI@tie{}152]
  7041. @deffnx {Scheme Procedure} @@vector-remove pred? @@vec -> @@vector [SRFI@tie{}152]
  7042. Return an @@vector containing the elements of @@vec that satisfy / do
  7043. not satisfy @var{pred?}.
  7044. @end deffn
  7045. @subsubheading Mutators
  7046. @deffn {Scheme Procedure} @@vector-set! @@vec i value -> unspecified [SRFI@tie{}4]
  7047. Sets the @var{i}th element of @var{@@vec} to @var{value}.
  7048. @end deffn
  7049. @deffn {Scheme Procedure} @@vector-swap! @@vec i j -> unspecified [SRFI@tie{}133]
  7050. Interchanges the @var{i}th and @var{j}th elements of @var{@@vec}.
  7051. @end deffn
  7052. @deffn {Scheme Procedure} @@vector-fill! @@vec fill [start [end]] -> unspecified [SRFI@tie{}133]
  7053. Fills the portion of @var{@@vec} from @var{start} to @var{end} with the
  7054. value @var{fill}.
  7055. @end deffn
  7056. @deffn {Scheme Procedure} @@vector-reverse! @@vec [start [end]] -> unspecified [SRFI@tie{}133]
  7057. Reverses the portion of @var{@@vec} from @var{start} to @var{end}.
  7058. @end deffn
  7059. @deffn {Scheme Procedure} @@vector-copy! @@to at @@from [start [end]] -> unspecified [SRFI@tie{}133]
  7060. Copies the portion of @var{@@from} from @var{start} to @var{end} onto
  7061. @var{@@to}, starting at index @var{at}.
  7062. @end deffn
  7063. @deffn {Scheme Procedure} @@vector-reverse-copy! @@to at @@from [start [end]] -> unspecified [SRFI@tie{}133]
  7064. The same as @code{@@vector-copy!}, but copies in reverse.
  7065. @end deffn
  7066. @deffn {Scheme Procedure} @@vector-unfold! f @@vec start end seed -> @@vector [SRFI@tie{}133]
  7067. Like @code{vector-unfold}, but the elements are copied into the vector
  7068. @var{@@vec} starting at element @var{start} rather than into a newly
  7069. allocated vector. Terminates when @var{end} - @var{start} elements have
  7070. been generated.
  7071. @end deffn
  7072. @deffn {Scheme Procedure} @@vector-unfold-right! f @@vec start end seed -> @@vector [SRFI@tie{}133]
  7073. The same as @code{@@vector-unfold!}, but initializes the @@vector from
  7074. right to left.
  7075. @end deffn
  7076. @subsubheading Conversion
  7077. @deffn {Scheme Procedure} @@vector->list @@vec [start [end]] -> proper-list [SRFI@tie{}4 plus start and end]
  7078. @deffnx {Scheme Procedure} reverse-@@vector->list @@vec [start [end]] -> proper-list [SRFI@tie{}133]
  7079. @deffnx {Scheme Procedure} list->@@vector proper-list -> @@vector
  7080. @deffnx {Scheme Procedure} reverse-list->@@vector proper-list -> @@vector [SRFI@tie{}133]
  7081. @deffnx {Scheme Procedure} @@vector->vector @@vec [start [end]] -> vector
  7082. @deffnx {Scheme Procedure} vector->@@vector vec [start [end]] -> @@vector
  7083. Returns a list, @@vector, or heterogeneous vector with the same elements
  7084. as the argument, in reverse order where specified.
  7085. @end deffn
  7086. @subsubheading Generators
  7087. @deffn {Scheme Procedure} make-@@vector-generator @@vector
  7088. Returns a @url{https://srfi.schemers.org/srfi-121/srfi-121.html, SRFI
  7089. 121} generator that generates all the values of @emph{@@vector} in order.
  7090. Note that the generator is finite.
  7091. @end deffn
  7092. @subsubheading Comparators
  7093. @deffn {Scheme Variable} @@vector-comparator
  7094. Variable containing a
  7095. @url{https://srfi.schemers.org/srfi-128/srfi-128.html, SRFI 128}
  7096. comparator whose components provide ordering and hashing of @@vectors.
  7097. @end deffn
  7098. @subsubheading Output
  7099. @deffn {Scheme Procedure} write-@@vector @@vec [port] -> unspecified
  7100. Prints to @var{port} (the current output port by default) a representation of
  7101. @var{@@vec} in the lexical syntax explained below.
  7102. @end deffn
  7103. @node SRFI 160 Optional lexical syntax
  7104. @subsubsection SRFI 160 Optional lexical syntax
  7105. Each homogeneous vector datatype has an external representation which
  7106. may be supported by the @code{read} and @code{write} procedures and by
  7107. the program parser. Conformance to this SRFI does not in itself require
  7108. support for these external representations.
  7109. For each value of @code{@@} in @math{{s8, u8, s16, u16, s32, u32, s64,
  7110. u64, f32, f64, c64, c128}}, if the datatype @code{@@vector} is
  7111. supported, then the external representation of instances of the datatype
  7112. @code{@@vector} is @code{#@@(elements @dots{})}.
  7113. @noindent
  7114. For example, @code{#u8(0 #e1e2 #xff)} is a @code{u8vector} of length 3
  7115. containing 0, 100 and 255; @code{#f64(-1.5)} is an @code{f64vector} of
  7116. length 1 containing -1.5.
  7117. @quotation note
  7118. The syntax for float vectors conflicts with R5RS, which parses
  7119. @code{#f32()} as 3 objects: @code{#f}, @code{32} and @code{()}. For
  7120. this reason, conformance to this SRFI implies this minor non-conformance
  7121. to R5RS.
  7122. @end quotation
  7123. This external representation is also available in program source code.
  7124. For example, @samp{(set! x '#u8(1 2 3))} will set @code{x} to the object
  7125. @code{#u8(1 2 3)}. Literal homogeneous vectors, like heterogeneous
  7126. vectors, are self-evaluating; they do not need to be quoted.
  7127. Homogeneous vectors can appear in quasiquotations but must not contain
  7128. @code{unquote} or @code{unquote-splicing} forms (i.e. @samp{`(,x #u8(1
  7129. 2))} is legal but @samp{`#u8(1 ,x 2)} is not). This restriction is to
  7130. accommodate the many Scheme systems that use the @code{read} procedure
  7131. to parse programs.
  7132. @node SRFI-171
  7133. @subsection Transducers
  7134. @cindex SRFI-171
  7135. @cindex transducers
  7136. Some of the most common operations used in the Scheme language are those
  7137. transforming lists: map, filter, take and so on. They work well, are well
  7138. understood, and are used daily by most Scheme programmers. They are however not
  7139. general because they only work on lists, and they do not compose very well
  7140. since combining N of them builds @code{(- N 1)} intermediate lists.
  7141. Transducers are oblivious to what kind of process they are used in, and
  7142. are composable without building intermediate collections. This means we
  7143. can create a transducer that squares all odd numbers:
  7144. @example
  7145. (compose (tfilter odd?) (tmap (lambda (x) (* x x))))
  7146. @end example
  7147. and reuse it with lists, vectors, or in just about any context where
  7148. data flows in one direction. We could use it as a processing step for
  7149. asynchronous channels, with an event framework as a pre-processing step,
  7150. or even in lazy contexts where you pass a lazy collection and a
  7151. transducer to a function and get a new lazy collection back.
  7152. The traditional Scheme approach of having collection-specific procedures
  7153. is not changed. We instead specify a general form of transformations
  7154. that complement these procedures. The benefits are obvious: a clear,
  7155. well-understood way of describing common transformations in a way that
  7156. is faster than just chaining the collection-specific counterparts. For
  7157. guile in particular this means a lot better GC performance.
  7158. Notice however that @code{(compose @dots{})} composes transducers
  7159. left-to-right, due to how transducers are initiated.
  7160. @menu
  7161. * SRFI-171 General Discussion:: General information about transducers
  7162. * SRFI-171 Applying Transducers:: Documentation of collection-specific forms
  7163. * SRFI-171 Reducers:: Reducers specified by the SRFI
  7164. * SRFI-171 Transducers:: Transducers specified by the SRFI
  7165. * SRFI-171 Helpers:: Utilities for writing your own transducers
  7166. @end menu
  7167. @node SRFI-171 General Discussion
  7168. @subsubsection SRFI-171 General Discussion
  7169. @cindex transducers discussion
  7170. @subheading The concept of reducers
  7171. The central part of transducers are 3-arity reducing procedures.
  7172. @itemize
  7173. @item
  7174. no arguments: Produces the identity of the reducer.
  7175. @item
  7176. (result-so-far): completion. Returns @code{result-so-far} either with or
  7177. without transforming it first.
  7178. @item
  7179. (result-so-far input) combines @code{result-so-far} and @code{input} to produce
  7180. a new @code{result-so-far}.
  7181. @end itemize
  7182. In the case of a summing @code{+} reducer, the reducer would produce, in
  7183. arity order: @code{0}, @code{result-so-far}, @code{(+ result-so-far
  7184. input)}. This happens to be exactly what the regular @code{+} does.
  7185. @subheading The concept of transducers
  7186. A transducer is a one-arity procedure that takes a reducer and produces a
  7187. reducing function that behaves as follows:
  7188. @itemize
  7189. @item
  7190. no arguments: calls reducer with no arguments (producing its identity)
  7191. @item
  7192. (result-so-far): Maybe transform the result-so-far and call reducer with it.
  7193. @item
  7194. (result-so-far input) Maybe do something to input and maybe call the
  7195. reducer with result-so-far and the maybe-transformed input.
  7196. @end itemize
  7197. A simple example is as following:
  7198. @example
  7199. (list-transduce (tfilter odd?) + '(1 2 3 4 5)).
  7200. @end example
  7201. This first returns a transducer filtering all odd
  7202. elements, then it runs @code{+} without arguments to retrieve its
  7203. identity. It then starts the transduction by passing @code{+} to the
  7204. transducer returned by @code{(tfilter odd?)} which returns a reducing
  7205. function. It works not unlike reduce from SRFI 1, but also checks
  7206. whether one of the intermediate transducers returns a "reduced" value
  7207. (implemented as a SRFI 9 record), which means the reduction finished
  7208. early.
  7209. Because transducers compose and the final reduction is only executed in
  7210. the last step, composed transducers will not build any intermediate
  7211. result or collections. Although the normal way of thinking about
  7212. application of composed functions is right to left, due to how the
  7213. transduction is built it is applied left to right. @code{(compose
  7214. (tfilter odd?) (tmap sqrt))} will create a transducer that first filters
  7215. out any odd values and then computes the square root of the rest.
  7216. @subheading State
  7217. Even though transducers appear to be somewhat of a generalisation of
  7218. @code{map} and friends, this is not really true. Since transducers don't
  7219. know in which context they are being used, some transducers must keep
  7220. state where their collection-specific counterparts do not. The
  7221. transducers that keep state do so using hidden mutable state, and as
  7222. such all the caveats of mutation, parallelism, and multi-shot
  7223. continuations apply. Each transducer keeping state is clearly described
  7224. as doing so in the documentation.
  7225. @subheading Naming
  7226. Reducers exported from the transducers module are named as in their
  7227. SRFI-1 counterpart, but prepended with an r. Transducers also follow
  7228. that naming, but are prepended with a t.
  7229. @node SRFI-171 Applying Transducers
  7230. @subsubsection Applying Transducers
  7231. @cindex transducers applying
  7232. @deffn {Scheme Procedure} list-transduce xform f lst
  7233. @deffnx {Scheme Procedure} list-transduce xform f identity lst
  7234. Initialize the transducer @var{xform} by passing the reducer @var{f}
  7235. to it. If no identity is provided, @var{f} runs without arguments to
  7236. return the reducer identity. It then reduces over @var{lst} using the
  7237. identity as the seed.
  7238. If one of the transducers finishes early (such as @code{ttake} or
  7239. @code{tdrop}), it communicates this by returning a reduced value, which
  7240. in the guile implementation is just a value wrapped in a SRFI 9 record
  7241. type named ``reduced''. If such a value is returned by the transducer,
  7242. @code{list-transduce} must stop execution and return an unreduced value
  7243. immediately.
  7244. @end deffn
  7245. @deffn {Scheme Procedure} vector-transduce xform f vec
  7246. @deffnx {Scheme Procedure} vector-transduce xform f identity vec
  7247. @deffnx {Scheme Procedure} string-transduce xform f str
  7248. @deffnx {Scheme Procedure} string-transduce xform f identity str
  7249. @deffnx {Scheme Procedure} bytevector-u8-transduce xform f bv
  7250. @deffnx {Scheme Procedure} bytevector-u8-transduce xform f identity bv
  7251. @deffnx {Scheme Procedure} generator-transduce xform f gen
  7252. @deffnx {Scheme Procedure} generator-transduce xform f identity gen
  7253. Same as @code{list-transduce}, but for vectors, strings, u8-bytevectors
  7254. and SRFI-158-styled generators respectively.
  7255. @end deffn
  7256. @deffn {Scheme Procedure} port-transduce xform f reader
  7257. @deffnx {Scheme Procedure} port-transduce xform f reader port
  7258. @deffnx {Scheme Procedure} port-transduce xform f identity reader port
  7259. Same as @code{list-transduce} but for ports. Called without a port, it
  7260. reduces over the results of applying @var{reader} until the EOF-object
  7261. is returned, presumably to read from @code{current-input-port}. With a
  7262. port @var{reader} is applied to @var{port} instead of without any
  7263. arguments. If @var{identity} is provided, that is used as the initial
  7264. identity in the reduction.
  7265. @end deffn
  7266. @node SRFI-171 Reducers
  7267. @subsubsection Reducers
  7268. @cindex transducers reducers
  7269. @deffn {Scheme Procedure} rcons
  7270. a simple consing reducer. When called without values, it returns its
  7271. identity, @code{'()}. With one value, which will be a list, it reverses
  7272. the list (using @code{reverse!}). When called with two values, it conses
  7273. the second value to the first.
  7274. @example
  7275. (list-transduce (tmap (lambda (x) (+ x 1)) rcons (list 0 1 2 3))
  7276. @result{} (1 2 3 4)
  7277. @end example
  7278. @end deffn
  7279. @deffn {Scheme Procedure} reverse-rcons
  7280. same as rcons, but leaves the values in their reversed order.
  7281. @example
  7282. (list-transduce (tmap (lambda (x) (+ x 1))) reverse-rcons (list 0 1 2 3))
  7283. @result{} (4 3 2 1)
  7284. @end example
  7285. @end deffn
  7286. @deffn {Scheme Procedure} rany pred?
  7287. The reducer version of any. Returns @code{(reduced (pred? value))} if
  7288. any @code{(pred? value)} returns non-#f. The identity is #f.
  7289. @example
  7290. (list-transduce (tmap (lambda (x) (+ x 1))) (rany odd?) (list 1 3 5))
  7291. @result{} #f
  7292. (list-transduce (tmap (lambda (x) (+ x 1))) (rany odd?) (list 1 3 4 5))
  7293. @result{} #t
  7294. @end example
  7295. @end deffn
  7296. @deffn {Scheme Procedure} revery pred?
  7297. The reducer version of every. Stops the transduction and returns
  7298. @code{(reduced #f)} if any @code{(pred? value)} returns #f. If every
  7299. @code{(pred? value)} returns true, it returns the result of the last
  7300. invocation of @code{(pred? value)}. The identity is #t.
  7301. @example
  7302. (list-transduce
  7303. (tmap (lambda (x) (+ x 1)))
  7304. (revery (lambda (v) (if (odd? v) v #f)))
  7305. (list 2 4 6))
  7306. @result{} 7
  7307. (list-transduce (tmap (lambda (x) (+ x 1)) (revery odd?) (list 2 4 5 6))
  7308. @result{} #f
  7309. @end example
  7310. @end deffn
  7311. @deffn {Scheme Procedure} rcount
  7312. A simple counting reducer. Counts the values that pass through the
  7313. transduction.
  7314. @example
  7315. (list-transduce (tfilter odd?) rcount (list 1 2 3 4)) @result{} 2.
  7316. @end example
  7317. @end deffn
  7318. @node SRFI-171 Transducers
  7319. @subsubsection Transducers
  7320. @cindex transducers transducers
  7321. @deffn {Scheme Procedure} tmap proc
  7322. Returns a transducer that applies @var{proc} to all values. Stateless.
  7323. @end deffn
  7324. @deffn {Scheme Procedure} tfilter pred?
  7325. Returns a transducer that removes values for which @var{pred?} returns #f.
  7326. Stateless.
  7327. @end deffn
  7328. @deffn {Scheme Procedure} tremove pred?
  7329. Returns a transducer that removes values for which @var{pred?} returns non-#f.
  7330. Stateless
  7331. @end deffn
  7332. @deffn {Scheme Procedure} tfilter-map proc
  7333. The same as @code{(compose (tmap proc) (tfilter values))}. Stateless.
  7334. @end deffn
  7335. @deffn {Scheme Procedure} treplace mapping
  7336. The argument @var{mapping} is an association list (using @code{equal?}
  7337. to compare keys), a hash-table, a one-argument procedure taking one
  7338. argument and either producing that same argument or a replacement value.
  7339. Returns a transducer which checks for the presence of any value passed
  7340. through it in mapping. If a mapping is found, the value of that mapping
  7341. is returned, otherwise it just returns the original value.
  7342. Does not keep internal state, but modifying the mapping while it's in
  7343. use by treplace is an error.
  7344. @end deffn
  7345. @deffn {Scheme Procedure} tdrop n
  7346. Returns a transducer that discards the first @var{n} values.
  7347. Stateful.
  7348. @end deffn
  7349. @deffn {Scheme Procedure} ttake n
  7350. Returns a transducer that discards all values and stops the transduction
  7351. after the first @var{n} values have been let through. Any subsequent values
  7352. are ignored.
  7353. Stateful.
  7354. @end deffn
  7355. @deffn {Scheme Procedure} tdrop-while pred?
  7356. Returns a transducer that discards the first values for which
  7357. @var{pred?} returns true.
  7358. Stateful.
  7359. @end deffn
  7360. @deffn {Scheme Procedure} ttake-while pred?
  7361. @deffnx {Scheme Procedure} ttake-while pred? retf
  7362. Returns a transducer that stops the transduction after @var{pred?} has
  7363. returned #f. Any subsequent values are ignored and the last successful
  7364. value is returned. @var{retf} is a function that gets called whenever
  7365. @var{pred?} returns false. The arguments passed are the result so far
  7366. and the input for which pred? returns @code{#f}. The default function is
  7367. @code{(lambda (result input) result)}.
  7368. Stateful.
  7369. @end deffn
  7370. @deffn {Scheme Procedure} tconcatenate
  7371. tconcatenate @emph{is} a transducer that concatenates the content of
  7372. each value (that must be a list) into the reduction.
  7373. @example
  7374. (list-transduce tconcatenate rcons '((1 2) (3 4 5) (6 (7 8) 9)))
  7375. @result{} (1 2 3 4 5 6 (7 8) 9)
  7376. @end example
  7377. @end deffn
  7378. @deffn {Scheme Procedure} tappend-map proc
  7379. The same as @code{(compose (tmap proc) tconcatenate)}.
  7380. @end deffn
  7381. @deffn {Scheme Procedure} tflatten
  7382. tflatten @emph{is} a transducer that flattens an input consisting of lists.
  7383. @example
  7384. (list-transduce tflatten rcons '((1 2) 3 (4 (5 6) 7 8) 9)
  7385. @result{} (1 2 3 4 5 6 7 8 9)
  7386. @end example
  7387. @end deffn
  7388. @deffn {Scheme Procedure} tdelete-neighbor-duplicates
  7389. @deffnx {Scheme Procedure} tdelete-neighbor-duplicates equality-predicate
  7390. Returns a transducer that removes any directly following duplicate
  7391. elements. The default @var{equality-predicate} is @code{equal?}.
  7392. Stateful.
  7393. @end deffn
  7394. @deffn {Scheme Procedure} tdelete-duplicates
  7395. @deffnx {Scheme Procedure} tdelete-duplicates equality-predicate
  7396. Returns a transducer that removes any subsequent duplicate elements
  7397. compared using @var{equality-predicate}. The default
  7398. @var{equality-predicate} is @code{equal?}.
  7399. Stateful.
  7400. @end deffn
  7401. @deffn {Scheme Procedure} tsegment n
  7402. Returns a transducer that groups inputs into lists of @var{n} elements.
  7403. When the transduction stops, it flushes any remaining collection, even
  7404. if it contains fewer than @var{n} elements.
  7405. Stateful.
  7406. @end deffn
  7407. @deffn {Scheme Procedure} tpartition pred?
  7408. Returns a transducer that groups inputs in lists by whenever
  7409. @code{(pred? input)} changes value.
  7410. Stateful.
  7411. @end deffn
  7412. @deffn {Scheme Procedure} tadd-between value
  7413. Returns a transducer which interposes @var{value} between each value
  7414. and the next. This does not compose gracefully with transducers like
  7415. @code{ttake}, as you might end up ending the transduction on
  7416. @code{value}.
  7417. Stateful.
  7418. @end deffn
  7419. @deffn {Scheme Procedure} tenumerate
  7420. @deffnx {Scheme Procedure} tenumerate start
  7421. Returns a transducer that indexes values passed through it, starting at
  7422. @var{start}, which defaults to 0. The indexing is done through cons
  7423. pairs like @code{(index . input)}.
  7424. @example
  7425. (list-transduce (tenumerate 1) rcons (list 'first 'second 'third))
  7426. @result{} ((1 . first) (2 . second) (3 . third))
  7427. @end example
  7428. Stateful.
  7429. @end deffn
  7430. @deffn {Scheme Procedure} tlog
  7431. @deffnx {Scheme Procedure} tlog logger
  7432. Returns a transducer that can be used to log or print values and
  7433. results. The result of the @var{logger} procedure is discarded. The
  7434. default @var{logger} is @code{(lambda (result input) (write input)
  7435. (newline))}.
  7436. Stateless.
  7437. @end deffn
  7438. @subheading Guile-specific transducers
  7439. These transducers are available in the @code{(srfi srfi-171 gnu)}
  7440. library, and are provided outside the standard described by the SRFI-171
  7441. document.
  7442. @deffn {Scheme Procedure} tbatch reducer
  7443. @deffnx {Scheme Procedure} tbatch transducer reducer
  7444. A batching transducer that accumulates results using @var{reducer} or
  7445. @code{((transducer) reducer)} until it returns a reduced value. This can
  7446. be used to generalize something like @code{tsegment}:
  7447. @example
  7448. ;; This behaves exactly like (tsegment 4).
  7449. (list-transduce (tbatch (ttake 4) rcons) rcons (iota 10))
  7450. @result{} ((0 1 2 3) (4 5 6 7) (8 9))
  7451. @end example
  7452. @end deffn
  7453. @deffn {Scheme Procedure} tfold reducer
  7454. @deffnx {Scheme Procedure} tfold reducer seed
  7455. A folding transducer that yields the result of @code{(reducer seed
  7456. value)}, saving its result between iterations.
  7457. @example
  7458. (list-transduce (tfold +) rcons (iota 10))
  7459. @result{} (0 1 3 6 10 15 21 28 36 45)
  7460. @end example
  7461. @end deffn
  7462. @node SRFI-171 Helpers
  7463. @subsubsection Helper functions for writing transducers
  7464. @cindex transducers helpers
  7465. These functions are in the @code{(srfi srfi-171 meta)} module and are only
  7466. usable when you want to write your own transducers.
  7467. @deffn {Scheme Procedure} reduced value
  7468. Wraps a value in a @code{<reduced>} container, signalling that the
  7469. reduction should stop.
  7470. @end deffn
  7471. @deffn {Scheme Procedure} reduced? value
  7472. Returns #t if value is a @code{<reduced>} record.
  7473. @end deffn
  7474. @deffn {Scheme Procedure} unreduce reduced-container
  7475. Returns the value in reduced-container.
  7476. @end deffn
  7477. @deffn {Scheme Procedure} ensure-reduced value
  7478. Wraps value in a @code{<reduced>} container if it is not already reduced.
  7479. @end deffn
  7480. @deffn {Scheme Procedure} preserving-reduced reducer
  7481. Wraps @code{reducer} in another reducer that encapsulates any returned
  7482. reduced value in another reduced container. This is useful in places
  7483. where you re-use a reducer with [collection]-reduce. If the reducer
  7484. returns a reduced value, [collection]-reduce unwraps it. Unless handled,
  7485. this leads to the reduction continuing.
  7486. @end deffn
  7487. @deffn {Scheme Procedure} list-reduce f identity lst
  7488. The reducing function used internally by @code{list-transduce}. @var{f}
  7489. is a reducer as returned by a transducer. @var{identity} is the
  7490. identity (sometimes called "seed") of the reduction. @var{lst} is a
  7491. list. If @var{f} returns a reduced value, the reduction stops
  7492. immediately and the unreduced value is returned.
  7493. @end deffn
  7494. @deffn {Scheme Procedure} vector-reduce f identity vec
  7495. The vector version of list-reduce.
  7496. @end deffn
  7497. @deffn {Scheme Procedure} string-reduce f identity str
  7498. The string version of list-reduce.
  7499. @end deffn
  7500. @deffn {Scheme Procedure} bytevector-u8-reduce f identity bv
  7501. The bytevector-u8 version of list-reduce.
  7502. @end deffn
  7503. @deffn {Scheme Procedure} port-reduce f identity reader port
  7504. The port version of list-reduce. It reduces over port using reader
  7505. until reader returns the EOF object.
  7506. @end deffn
  7507. @deffn {Scheme Procedure} generator-reduce f identity gen
  7508. The generator version of list-reduce. It reduces over @code{gen} until
  7509. it returns the EOF object
  7510. @end deffn
  7511. @node SRFI 178
  7512. @subsection SRFI 178: Bitvector library
  7513. @cindex SRFI 178
  7514. @menu
  7515. * SRFI 178 Abstract::
  7516. * SRFI 178 Rationale::
  7517. * SRFI 178 Notation::
  7518. * SRFI 178 Bit conversion::
  7519. * SRFI 178 Constructors::
  7520. * SRFI 178 Predicates::
  7521. * SRFI 178 Selectors::
  7522. * SRFI 178 Iteration::
  7523. * SRFI 178 Prefixes suffixes trimming padding::
  7524. * SRFI 178 Mutators::
  7525. * SRFI 178 Conversion::
  7526. * SRFI 178 Generators::
  7527. * SRFI 178 Basic operations::
  7528. * SRFI 178 Quasi-integer operations::
  7529. * SRFI 178 Bit field operations::
  7530. * SRFI 178 Bitvector literals::
  7531. @end menu
  7532. @node SRFI 178 Abstract
  7533. @subsubsection SRFI 178 Abstract
  7534. This SRFI describes a set of operations on homogeneous bitvectors.
  7535. Operations analogous to those provided on the other homogeneous vector
  7536. types described in
  7537. @url{https://srfi.schemers.org/srfi-160/srfi-160.html, SRFI 160} are
  7538. provided, along with operations analogous to the bitwise operations of
  7539. @url{https://srfi.schemers.org/srfi-151/srfi-151.html, SRFI 151}.
  7540. @node SRFI 178 Rationale
  7541. @subsubsection SRFI 178 Rationale
  7542. Bitvectors were excluded from the final draft of SRFI 160 because they
  7543. are the only type of homogeneous numeric vectors for which bitwise
  7544. operations make sense. In addition, there are two ways to view them: as
  7545. vectors of exact integers limited to the values 0 and 1, and as vectors
  7546. of booleans. This SRFI is designed to allow bitvectors to be viewed in
  7547. either way.
  7548. @node SRFI 178 Notation
  7549. @subsubsection SRFI 178 Notation
  7550. Bitvectors are disjoint from all other Scheme types with the possible
  7551. exception of u1vectors, if the Scheme implementation supports them.
  7552. The procedures of this SRFI that accept single bits or lists of bits can
  7553. be passed any of the values 0, 1, #f, #t. Procedures that return a bit
  7554. or a list of bits come in two flavors: one ending in @samp{/int} that
  7555. returns an exact integer, and one ending in @samp{/bool} that returns a
  7556. boolean.
  7557. Mapping and folding procedures also come in two flavors: those ending in
  7558. @samp{/int} pass exact integers to their procedure arguments, whereas
  7559. those ending in @samp{/bool} pass booleans to theirs.
  7560. Procedures whose names end in @samp{!} are the same as the corresponding
  7561. procedures without @samp{!}, except that the first bitvector argument is
  7562. mutated and an unspecified result is returned. Consequently, only the
  7563. non-@samp{!} version is documented below.
  7564. It is an error unless all bitvector arguments passed to procedures that
  7565. accept more than one are of the same length (except as otherwise noted).
  7566. In the section containing specifications of procedures, the following
  7567. notation is used to specify parameters and return values:
  7568. @table @asis
  7569. @item (@var{f} @var{arg@sub{1}} @var{arg@sub{2}} @dots{}) -> something
  7570. A procedure @var{f} that takes the parameters @var{arg@sub{1}}
  7571. @var{arg@sub{2}} @dots{} and returns a value of the type
  7572. @code{something}. If two values are returned, two types are specified.
  7573. If @code{something} is @code{unspecified}, then @var{f} returns a single
  7574. implementation-dependent value; this SRFI does not specify what it
  7575. returns, and in order to write portable code, the return value should be
  7576. ignored.
  7577. @item @var{vec}
  7578. An heterogeneous vector; that is, it must satisfy the predicate
  7579. @code{vector?}.
  7580. @item @var{bvec}, @var{to}, @var{from}
  7581. A bitvector, i.e., it must satisfy the predicate @code{bitvector?}. In
  7582. @code{bitvector-copy!} and @code{reverse-bitvector-copy!}, @var{to} is the
  7583. destination and @var{from} is the source.
  7584. @item @var{i}, @var{j}, @var{start}, @var{at}
  7585. An exact nonnegative integer less than the length of the bitvector. In
  7586. @code{bitvector-copy!} and @code{reverse-bitvector-copy!}, @var{at} refers
  7587. to the destination and @var{start} to the source.
  7588. @item @var{end}
  7589. An exact nonnegative integer not less than @var{start}. This indicates
  7590. the index directly before which traversal will stop --- processing will
  7591. occur until the index of the vector is one less than @var{end}. It is
  7592. the open right side of a range.
  7593. @item @var{f}
  7594. A procedure taking one or more arguments, which returns (except as noted
  7595. otherwise) exactly one value.
  7596. @item @var{=}
  7597. An equivalence procedure.
  7598. @item @var{obj}, @var{seed}, @var{knil}
  7599. Any Scheme object.
  7600. @item @var{[something]}
  7601. An optional argument; it needn't necessarily be applied.
  7602. @var{something} needn't necessarily be one thing; for example, this
  7603. usage of it is perfectly valid:
  7604. @example
  7605. [@var{start} [@var{end}]]
  7606. @end example
  7607. @noindent
  7608. and is indeed used quite often.
  7609. @item @var{something} @dots{}
  7610. Zero or more @var{something}s are allowed to be arguments.
  7611. @item @var{something@sub{1}} @var{something@sub{2}} @dots{}
  7612. One or more @var{something}s must be arguments.
  7613. @end table
  7614. All procedures that return bitvectors, vectors, or lists newly allocate
  7615. their results, except those that end in @samp{!}. However, a
  7616. zero-length value need not be separately allocated.
  7617. Except as otherwise noted, the semantics of each procedure are those of
  7618. the corresponding SRFI 133 or SRFI 151 procedure.
  7619. @node SRFI 178 Bit conversion
  7620. @subsubsection SRFI 178 Bit conversion
  7621. @deffn {Scheme Procedure} bit->integer bit -> exact integer
  7622. Returns 0 if @var{bit} is 0 or @code{#f} and 1 if @var{bit} is 1 or
  7623. @code{#t}.
  7624. @end deffn
  7625. @deffn {Scheme Procedure} bit->boolean bit -> boolean
  7626. Returns @code{#f} if @var{bit} is 0 or @code{#f} and @code{#t} if
  7627. @var{bit} is 1 or @code{#t}.
  7628. @end deffn
  7629. @node SRFI 178 Constructors
  7630. @subsubsection SRFI 178 Constructors
  7631. @deffn {Scheme Procedure} make-bitvector size [bit] -> bitvector
  7632. Returns a bitvector whose length is @var{size}. If @var{bit} is provided,
  7633. all the elements of the bitvector are initialized to it.
  7634. @end deffn
  7635. @deffn {Scheme Procedure} bitvector value @dots{} -> bitvector
  7636. Returns a bitvector initialized with @var{values}.
  7637. @end deffn
  7638. @deffn {Scheme Procedure} bitvector-unfold f length seed @dots{} -> bitvector
  7639. Creates a vector whose length is @var{length} and iterates across each
  7640. index @var{k} between 0 and @var{length}, applying @var{f} at each
  7641. iteration to the current index and current states, in that order, to
  7642. receive @var{n} + 1 values: the bit to put in the @var{k}th slot of the
  7643. new vector and new states for the next iteration. On the first call to
  7644. @var{f}, the states' values are the @var{seeds}.
  7645. @end deffn
  7646. @deffn {Scheme Procedure} bitvector-unfold-right f length seed -> bitvector
  7647. The same as @code{bitvector-unfold}, but initializes the bitvector from
  7648. right to left.
  7649. @end deffn
  7650. @deffn {Scheme Procedure} bitvector-copy bvec [start [end]] -> bitvector
  7651. Makes a copy of the portion of @var{bvec} from @var{start} to @var{end} and
  7652. returns it.
  7653. @end deffn
  7654. @deffn {Scheme Procedure} bitvector-reverse-copy bvec [start [end]] -> bitvector
  7655. The same as @code{bitvector-copy}, but in reverse order.
  7656. @end deffn
  7657. @deffn {Scheme Procedure} bitvector-append bvec @dots{} -> bitvector
  7658. Returns a bitvector containing all the elements of the @var{bvecs} in
  7659. order.
  7660. @end deffn
  7661. @deffn {Scheme Procedure} bitvector-concatenate list-of-bitvectors -> bitvector
  7662. The same as @code{bitvector-append}, but takes a list of bitvectors
  7663. rather than multiple arguments.
  7664. @end deffn
  7665. @deffn {Scheme Procedure} bitvector-append-subbitvectors [bvec start end] @dots{} -> bitvector
  7666. Concatenates the result of applying @code{bitvector-copy} to each
  7667. triplet of @var{bvec}, @var{start}, @var{end} arguments.
  7668. @end deffn
  7669. @node SRFI 178 Predicates
  7670. @subsubsection SRFI 178 Predicates
  7671. @deffn {Scheme Procedure} bitvector? obj -> boolean
  7672. Returns @code{#t} if @var{obj} is a bitvector, and @code{#f} otherwise.
  7673. @end deffn
  7674. @deffn {Scheme Procedure} bitvector-empty? bvec -> boolean
  7675. Returns @code{#t} if @var{bvec} has a length of zero, and @code{#f}
  7676. otherwise.
  7677. @end deffn
  7678. @deffn {Scheme Procedure} bitvector=? bvec @dots{} -> boolean
  7679. Compares the @var{bvecs} for element-wise equality, using @code{eqv?} to
  7680. do the comparisons, and returns @code{#t} or @code{#f} accordingly.
  7681. @end deffn
  7682. @node SRFI 178 Selectors
  7683. @subsubsection SRFI 178 Selectors
  7684. @deffn {Scheme Procedure} bitvector-ref/int bvec i -> integer
  7685. @deffnx {Scheme Procedure} bitvector-ref/bool bvec i -> boolean
  7686. Returns the @var{i}th element of @var{bvec} as an exact integer or
  7687. boolean, respectively.
  7688. @end deffn
  7689. @deffn {Scheme Procedure} bitvector-length bvec -> exact nonnegative integer
  7690. Returns the length of @var{bvec}.
  7691. @end deffn
  7692. @node SRFI 178 Iteration
  7693. @subsubsection SRFI 178 Iteration
  7694. @deffn {Scheme Procedure} bitvector-take bvec n -> bitvector
  7695. @deffnx {Scheme Procedure} bitvector-take-right bvec n -> bitvector
  7696. Returns a bitvector containing the first/last @var{n} elements of
  7697. @var{bvec}.
  7698. @end deffn
  7699. @deffn {Scheme Procedure} bitvector-drop bvec n -> bitvector
  7700. @deffnx {Scheme Procedure} bitvector-drop-right bvec n -> bitvector
  7701. Returns a bitvector containing all except the first/last @var{n}
  7702. elements of @var{bvec}.
  7703. @end deffn
  7704. @deffn {Scheme Procedure} bitvector-segment bvec n -> list
  7705. Returns a list of bitvectors, each of which contains @var{n} consecutive
  7706. elements of @var{bvec}. The last bitvector may be shorter than @var{n}.
  7707. It is an error if @var{n} is not an exact positive integer.
  7708. @end deffn
  7709. @deffn {Scheme Procedure} bitvector-fold/int kons knil bvec@sub{1} bvec@sub{2} @dots{} -> object
  7710. @deffnx {Scheme Procedure} bitvector-fold/bool kons knil bvec@sub{1} bvec@sub{2} @dots{} -> object
  7711. @deffnx {Scheme Procedure} bitvector-fold-right/int kons knil bvec@sub{1} bvec@sub{2} @dots{} -> object
  7712. @deffnx {Scheme Procedure} bitvector-fold-right/bool kons knil bvec@sub{1} bvec@sub{2} @dots{} -> object
  7713. Folds @var{kons} over the elements of @var{bvec} in
  7714. increasing/decreasing order using @var{knil} as the initial value. The
  7715. kons procedure is called with the states first and the new element last,
  7716. as in SRFIs 43, 133, and 160.
  7717. @end deffn
  7718. @deffn {Scheme Procedure} bitvector-map/int f bvec@sub{1} bvec@sub{2} @dots{} -> bitvector
  7719. @deffnx {Scheme Procedure} bitvector-map/bool f bvec@sub{1} bvec@sub{2} @dots{} -> bitvector
  7720. @deffnx {Scheme Procedure} bitvector-map!/int f bvec@sub{1} bvec@sub{2} @dots{} -> unspecified
  7721. @deffnx {Scheme Procedure} bitvector-map!/bool f bvec@sub{1} bvec@sub{2} @dots{} -> unspecified
  7722. @deffnx {Scheme Procedure} bitvector-map- > list/int f bvec@sub{1} bvec@sub{2} @dots{} -> list
  7723. @deffnx {Scheme Procedure} bitvector-map- > list/bool f bvec@sub{1} bvec@sub{2} @dots{} -> list
  7724. @deffnx {Scheme Procedure} bitvector-for-each/int f bvec@sub{1} bvec@sub{2} @dots{} -> unspecified
  7725. @deffnx {Scheme Procedure} bitvector-for-each/bool f bvec@sub{1} bvec@sub{2} @dots{} -> unspecified
  7726. Iterate over the corresponding elements of the @var{bvecs} and apply
  7727. @var{f} to each, returning respectively: a bitvector of the results, an
  7728. undefined value with the results placed back in @var{bvec1}, a list of
  7729. the results, and an undefined value with no change to @var{bvec1}.
  7730. @end deffn
  7731. @node SRFI 178 Prefixes suffixes trimming padding
  7732. @subsubsection SRFI 178 Prefixes, suffixes, trimming, padding
  7733. @deffn {Scheme Procedure} bitvector-prefix-length bvec1 bvec2 -> index
  7734. @deffnx {Scheme Procedure} bitvector-suffix-length bvec1 bvec2 -> index
  7735. Return the number of elements that are equal in the prefix/suffix
  7736. of the two @i{bvecs}, which are allowed to be of different lengths.
  7737. @end deffn
  7738. @deffn {Scheme Procedure} bitvector-prefix? bvec1 bvec2 -> boolean
  7739. @deffnx {Scheme Procedure} bitvector-suffix? bvec1 bvec2 -> boolean
  7740. Returns @code{#t} if @var{bvec1} is a prefix/suffix of @var{bvec2}, and
  7741. @code{#f} otherwise. The arguments are allowed to be of different
  7742. lengths.
  7743. @end deffn
  7744. @deffn {Scheme Procedure} bitvector-pad bit bvec length -> bvec
  7745. @deffnx {Scheme Procedure} bitvector-pad-right bit bvec length -> bvec
  7746. Returns a copy of @var{bvec} with leading/trailing elements equal to
  7747. @var{bit} added (if necessary) so that the length of the result is
  7748. @var{length}.
  7749. @end deffn
  7750. @deffn {Scheme Procedure} bitvector-trim bit bvec -> bvec
  7751. @deffnx {Scheme Procedure} bitvector-trim-right bit bvec -> bvec
  7752. @deffnx {Scheme Procedure} bitvector-trim-both bit bvec -> bvec
  7753. Returns a copy of @var{bvec} with leading/trailing/both elements equal to
  7754. @var{bit} removed.
  7755. @end deffn
  7756. @node SRFI 178 Mutators
  7757. @subsubsection SRFI 178 Mutators
  7758. @deffn {Scheme Procedure} bitvector-set! bvec i bit -> unspecified
  7759. Sets the @var{i}th element of @var{bvec} to @var{bit}.
  7760. @end deffn
  7761. @deffn {Scheme Procedure} bitvector-swap! bvec i j -> unspecified
  7762. Interchanges the @var{i}th and @var{j}th elements of @var{bvec}.
  7763. @end deffn
  7764. @deffn {Scheme Procedure} bitvector-reverse! bvec [start [end]] -> unspecified
  7765. Reverses the portion of @var{bvec} from @var{start} to @var{end}.
  7766. @end deffn
  7767. @deffn {Scheme Procedure} bitvector-copy! to at from [start [end]] -> unspecified
  7768. Copies the portion of @var{from} from @var{start} to @var{end} onto
  7769. @var{to}, starting at index @var{at}.
  7770. @end deffn
  7771. @deffn {Scheme Procedure} bitvector-reverse-copy! to at from [start [end]] -> unspecified
  7772. The same as @code{bitvector-copy!}, but copies in reverse.
  7773. @end deffn
  7774. @node SRFI 178 Conversion
  7775. @subsubsection SRFI 178 Conversion
  7776. @deffn {Scheme Procedure} bitvector->list/int bvec [start [end]] -> list of integers
  7777. @deffnx {Scheme Procedure} bitvector->list/bool bvec [start [end]] -> list of booleans
  7778. @deffnx {Scheme Procedure} reverse-bitvector->list/int bvec [start [end]] -> list of integers
  7779. @deffnx {Scheme Procedure} reverse-bitvector->list/bool bvec [start [end]] -> list of booleans
  7780. @deffnx {Scheme Procedure} list->bitvector list -> bitvector
  7781. @deffnx {Scheme Procedure} reverse-list->bitvector list -> bitvector
  7782. @deffnx {Scheme Procedure} bitvector->vector/int bvec [start [end]] -> vector of integers
  7783. @deffnx {Scheme Procedure} bitvector->vector/bool bvec [start [end]] -> vector of booleans
  7784. @deffnx {Scheme Procedure} reverse-bitvector->vector/int bvec [start [end]] -> vector of integers
  7785. @deffnx {Scheme Procedure} reverse-bitvector->vector/bool bvec [start [end]] -> vector of booleans
  7786. @deffnx {Scheme Procedure} vector->bitvector vec [start [end]] -> bitvector
  7787. @deffnx {Scheme Procedure} reverse-vector->bitvector vec [start [end]] -> bitvector
  7788. Returns a list, bitvector, or heterogeneous vector with the same
  7789. elements as the argument, in reverse order where specified.
  7790. @end deffn
  7791. @deffn {Scheme Procedure} bitvector->string bvec -> string
  7792. Returns a string beginning with @samp{"#*"} and followed by the values
  7793. of @var{bvec} represented as 0 and 1 characters. This is the Common
  7794. Lisp representation of a bitvector.
  7795. @end deffn
  7796. @deffn {Scheme Procedure} string->bitvector string -> bitvector
  7797. Parses a string in the format generated by @code{bitvector->string} and
  7798. returns the corresponding bitvector, or @code{#f} if the string is not
  7799. in this format.
  7800. @end deffn
  7801. @deffn {Scheme Procedure} bitvector->integer bitvector
  7802. Returns a non-negative exact integer whose bits, starting with the least
  7803. significant bit as bit 0, correspond to the values in @var{bitvector}.
  7804. This ensures compatibility with the integers-as-bits operations of
  7805. @url{https://srfi.schemers.org/srfi-151/srfi-151.html, SRFI 151}.
  7806. @end deffn
  7807. @deffn {Scheme Procedure} integer->bitvector integer [len]
  7808. Returns a bitvector whose length is @var{len} whose values correspond to
  7809. the bits of @var{integer}, a non-negative exact integer, starting with
  7810. the least significant bit as bit 0. This ensures compatibility with the
  7811. integers-as-bits operations of
  7812. @url{https://srfi.schemers.org/srfi-151/srfi-151.html, SRFI 151}
  7813. The default value of @var{len} is @samp{(integer-length @var{integer})}.
  7814. If the value of @var{len} is less than the default, the resulting
  7815. bitvector cannot be converted back by @code{bitvector->integer}
  7816. correctly.
  7817. @end deffn
  7818. @node SRFI 178 Generators
  7819. @subsubsection SRFI 178 Generators
  7820. @deffn {Scheme Procedure} make-bitvector/int-generator bitvector
  7821. @deffnx {Scheme Procedure} make-bitvector/bool-generator bitvector
  7822. Returns a @url{https://srfi.schemers.org/srfi-158/srfi-158.html, SRFI
  7823. 158} generator that generates all the values of @var{bitvector} in
  7824. order. Note that the generator is finite.
  7825. @end deffn
  7826. @deffn {Scheme Procedure} make-bitvector-accumulator
  7827. Returns a @url{https://srfi.schemers.org/srfi-158/srfi-158.html, SRFI
  7828. 158} accumulator that collects all the bits it is invoked on. When
  7829. invoked on an end-of-file object, returns a bitvector containing all the
  7830. bits in order.
  7831. @end deffn
  7832. @node SRFI 178 Basic operations
  7833. @subsubsection SRFI 178 Basic operations
  7834. @deffn {Scheme Procedure} bitvector-not bvec
  7835. @deffnx {Scheme Procedure} bitvector-not! bvec
  7836. Returns the element-wise complement of @var{bvec}; that is, each value
  7837. is changed to the opposite value.
  7838. @end deffn
  7839. The following ten procedures correspond to the useful set of non-trivial
  7840. two-argument boolean functions. For each such function, the
  7841. corresponding bitvector operator maps that function across two or more
  7842. bitvectors in an element-wise fashion. The core idea of this group of
  7843. functions is this element-wise "lifting" of the set of dyadic boolean
  7844. functions to bitvector parameters.
  7845. @deffn {Scheme Procedure} bitvector-and bvec1 bvec2 bvec @dots{}
  7846. @deffnx {Scheme Procedure} bitvector-and! bvec1 bvec2 bvec @dots{}
  7847. @deffnx {Scheme Procedure} bitvector-ior bvec1 bvec2 bvec @dots{}
  7848. @deffnx {Scheme Procedure} bitvector-ior! bvec1 bvec2 bvec @dots{}
  7849. @deffnx {Scheme Procedure} bitvector-xor bvec1 bvec2 bvec @dots{}
  7850. @deffnx {Scheme Procedure} bitvector-xor! bvec1 bvec2 bvec @dots{}
  7851. @deffnx {Scheme Procedure} bitvector-eqv bvec1 bvec2 bvec @dots{}
  7852. @deffnx {Scheme Procedure} bitvector-eqv! bvec1 bvec2 bvec @dots{}
  7853. These operations are associative.
  7854. The @code{bitvector-eqv} procedure produces the complement of the
  7855. @code{bitvector-xor} procedure. When applied to three arguments, it
  7856. does @emph{not} produce a @code{#t} value everywhere that @var{a},
  7857. @var{b} and @var{c} all agree. That is, it does @emph{not} produce:
  7858. @lisp
  7859. (bitvector-ior (bitvector-and a b c)
  7860. (bitvector-and (bitvector-not a)
  7861. (bitvector-not b)
  7862. (bitvector-not c)))
  7863. @end lisp
  7864. Rather, it produces @samp{(bitvector-eqv @var{a} (bitvector-eqv @var{b}
  7865. @var{c}))} or the equivalent @samp{(bitvector-eqv (bitvector-eqv @var{a}
  7866. @var{b}) @var{c})}.
  7867. @end deffn
  7868. @deffn {Scheme Procedure} bitvector-nand bvec1 bvec2
  7869. @deffnx {Scheme Procedure} bitvector-nand! bvec1 bvec2
  7870. @deffnx {Scheme Procedure} bitvector-nor bvec1 bvec2
  7871. @deffnx {Scheme Procedure} bitvector-nor! bvec1 bvec2
  7872. @deffnx {Scheme Procedure} bitvector-andc1 bvec1 bvec2
  7873. @deffnx {Scheme Procedure} bitvector-andc1! bvec1 bvec2
  7874. @deffnx {Scheme Procedure} bitvector-andc2 bvec1 bvec2
  7875. @deffnx {Scheme Procedure} bitvector-andc2! bvec1 bvec2
  7876. @deffnx {Scheme Procedure} bitvector-orc1 bvec1 bvec2
  7877. @deffnx {Scheme Procedure} bitvector-orc1! bvec1 bvec2
  7878. @deffnx {Scheme Procedure} bitvector-orc2 bvec1 bvec2
  7879. @deffnx {Scheme Procedure} bitvector-orc2! bvec1 bvec2
  7880. These operations are not associative.
  7881. @end deffn
  7882. @node SRFI 178 Quasi-integer operations
  7883. @subsubsection SRFI 178 Quasi-integer operations
  7884. @deffn {Scheme Procedure} bitvector-logical-shift bvec count bit
  7885. Returns a bitvector equal in length to @var{bvec} containing the logical
  7886. left shift (toward lower indices) when @var{count}>=0 or the right shift
  7887. (toward upper indices) when @var{count}<0. Newly vacated elements are
  7888. filled with @var{bit}.
  7889. @end deffn
  7890. @deffn {Scheme Procedure} bitvector-count bit bvec
  7891. Returns the number of @var{bit} values in @var{bvec}.
  7892. @end deffn
  7893. @deffn {Scheme Procedure} bitvector-count-run bit bvec i
  7894. Returns the number of consecutive @var{bit} values in @var{bvec},
  7895. starting at index @var{i}.
  7896. @end deffn
  7897. @deffn {Scheme Procedure} bitvector-if if-bitvector then-bitvector else-bitvector
  7898. Returns a bitvector that merges the bitvectors @var{then-bitvector} and
  7899. @var{else-bitvector}, with the bitvector @var{if-bitvector} determining
  7900. from which bitvector to take each value. That is, if the @var{k}th
  7901. value of @var{if-bitvector} is @code{#t} (or 1, depending in how you
  7902. look at it), then the @var{k}th bit of the result is the @var{k}th bit
  7903. of @var{then-bitvector}, otherwise the @var{k}th bit of
  7904. @var{else-bitvector}.
  7905. @end deffn
  7906. @deffn {Scheme Procedure} bitvector-first-bit bit bvec
  7907. Return the index of the first (smallest index) @var{bit} value in
  7908. @var{bvec}. Return @code{-1} if @var{bvec} contains no values equal to
  7909. @var{bit}.
  7910. @end deffn
  7911. @node SRFI 178 Bit field operations
  7912. @subsubsection SRFI 178 Bit field operations
  7913. These procedures operate on a contiguous field of bits (a "byte" in
  7914. Common Lisp parlance) in a given bitvector. The @var{start} and
  7915. @var{end} arguments, which are not optional, are non-negative exact
  7916. integers specifying the field: it is the @var{end} --- @var{start} bits
  7917. running from bit @var{start} to bit @samp{@var{end} - 1}.
  7918. @deffn {Scheme Procedure} bitvector-field-any? bvec start end
  7919. Returns @code{#t} if any of the field's bits are set in @var{bvec}, and
  7920. @code{#f} otherwise.
  7921. @end deffn
  7922. @deffn {Scheme Procedure} bitvector-field-every? bvec start end
  7923. Returns @code{#f} if any of the field's bits are not set in @var{bvec},
  7924. and @code{#t} otherwise.
  7925. @end deffn
  7926. @deffn {Scheme Procedure} bitvector-field-clear bvec start end
  7927. @deffnx {Scheme Procedure} bitvector-field-clear! bvec start end
  7928. @deffnx {Scheme Procedure} bitvector-field-set bvec start end
  7929. @deffnx {Scheme Procedure} bitvector-field-set! bvec start end
  7930. Returns a bitvector containing @var{bvec} with the field's bits set
  7931. appropriately.
  7932. @end deffn
  7933. @deffn {Scheme Procedure} bitvector-field-replace dest source start end
  7934. @deffnx {Scheme Procedure} bitvector-field-replace! dest source start end
  7935. Returns a bitvector containing @var{dest} with the field replaced by the
  7936. first @var{end-start} bits in @var{source}.
  7937. @end deffn
  7938. @deffn {Scheme Procedure} bitvector-field-replace-same dest source start end
  7939. @deffnx {Scheme Procedure} bitvector-field-replace-same! dest source start end
  7940. Returns a bitvector containing @var{dest} with its field replaced by
  7941. the corresponding field in @var{source}.
  7942. @end deffn
  7943. @deffn {Scheme Procedure} bitvector-field-rotate bvec count start end
  7944. Returns @var{bvec} with the field cyclically permuted by @var{count}
  7945. bits towards higher indices when @var{count} is negative, and toward
  7946. lower indices otherwise.
  7947. @end deffn
  7948. @deffn {Scheme Procedure} bitvector-field-flip bvec start end
  7949. @deffnx {Scheme Procedure} bitvector-field-flip! bvec start end
  7950. Returns @var{bvec} with the bits in the field flipped: that is, each
  7951. value is replaced by the opposite value. There is no SRFI 151
  7952. equivalent.
  7953. @end deffn
  7954. @node SRFI 178 Bitvector literals
  7955. @subsubsection SRFI 178 Bitvector literals
  7956. The compact string representation used by @code{bitvector->string} and
  7957. @code{string->bitvector} may be supported by the standard @code{read}
  7958. and @code{write} procedures and by the program parser, so that programs
  7959. can contain references to literal bitvectors. On input, it is an error
  7960. if such a literal is not followed by a <delimiter> or the end of input.
  7961. @c srfi-modules.texi ends here
  7962. @c Local Variables:
  7963. @c TeX-master: "guile.texi"
  7964. @c End: