srfi-modules.texi 222 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156
  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 See the file guile.texi for copying conditions.
  6. @node SRFI Support
  7. @section SRFI Support Modules
  8. @cindex SRFI
  9. SRFI is an acronym for Scheme Request For Implementation. The SRFI
  10. documents define a lot of syntactic and procedure extensions to standard
  11. Scheme as defined in R5RS.
  12. Guile has support for a number of SRFIs. This chapter gives an overview
  13. over the available SRFIs and some usage hints. For complete
  14. documentation, design rationales and further examples, we advise you to
  15. get the relevant SRFI documents from the SRFI home page
  16. @url{http://srfi.schemers.org/}.
  17. @menu
  18. * About SRFI Usage:: What to know about Guile's SRFI support.
  19. * SRFI-0:: cond-expand
  20. * SRFI-1:: List library.
  21. * SRFI-2:: and-let*.
  22. * SRFI-4:: Homogeneous numeric vector datatypes.
  23. * SRFI-6:: Basic String Ports.
  24. * SRFI-8:: receive.
  25. * SRFI-9:: define-record-type.
  26. * SRFI-10:: Hash-Comma Reader Extension.
  27. * SRFI-11:: let-values and let*-values.
  28. * SRFI-13:: String library.
  29. * SRFI-14:: Character-set library.
  30. * SRFI-16:: case-lambda
  31. * SRFI-17:: Generalized set!
  32. * SRFI-18:: Multithreading support
  33. * SRFI-19:: Time/Date library.
  34. * SRFI-23:: Error reporting
  35. * SRFI-26:: Specializing parameters
  36. * SRFI-27:: Sources of Random Bits
  37. * SRFI-28:: Basic format strings.
  38. * SRFI-30:: Nested multi-line block comments
  39. * SRFI-31:: A special form `rec' for recursive evaluation
  40. * SRFI-34:: Exception handling.
  41. * SRFI-35:: Conditions.
  42. * SRFI-37:: args-fold program argument processor
  43. * SRFI-38:: External Representation for Data With Shared Structure
  44. * SRFI-39:: Parameter objects
  45. * SRFI-41:: Streams.
  46. * SRFI-42:: Eager comprehensions
  47. * SRFI-43:: Vector Library.
  48. * SRFI-45:: Primitives for expressing iterative lazy algorithms
  49. * SRFI-46:: Basic syntax-rules Extensions.
  50. * SRFI-55:: Requiring Features.
  51. * SRFI-60:: Integers as bits.
  52. * SRFI-61:: A more general `cond' clause
  53. * SRFI-62:: S-expression comments.
  54. * SRFI-64:: A Scheme API for test suites.
  55. * SRFI-67:: Compare procedures
  56. * SRFI-69:: Basic hash tables.
  57. * SRFI-71:: Extended let-syntax for multiple values.
  58. * SRFI-87:: => in case clauses.
  59. * SRFI-88:: Keyword objects.
  60. * SRFI-98:: Accessing environment variables.
  61. * SRFI-105:: Curly-infix expressions.
  62. * SRFI-111:: Boxes.
  63. * SRFI-171:: Transducers
  64. @end menu
  65. @node About SRFI Usage
  66. @subsection About SRFI Usage
  67. @c FIXME::martin: Review me!
  68. SRFI support in Guile is currently implemented partly in the core
  69. library, and partly as add-on modules. That means that some SRFIs are
  70. automatically available when the interpreter is started, whereas the
  71. other SRFIs require you to use the appropriate support module
  72. explicitly.
  73. There are several reasons for this inconsistency. First, the feature
  74. checking syntactic form @code{cond-expand} (@pxref{SRFI-0}) must be
  75. available immediately, because it must be there when the user wants to
  76. check for the Scheme implementation, that is, before she can know that
  77. it is safe to use @code{use-modules} to load SRFI support modules. The
  78. second reason is that some features defined in SRFIs had been
  79. implemented in Guile before the developers started to add SRFI
  80. implementations as modules (for example SRFI-13 (@pxref{SRFI-13})). In
  81. the future, it is possible that SRFIs in the core library might be
  82. factored out into separate modules, requiring explicit module loading
  83. when they are needed. So you should be prepared to have to use
  84. @code{use-modules} someday in the future to access SRFI-13 bindings. If
  85. you want, you can do that already. We have included the module
  86. @code{(srfi srfi-13)} in the distribution, which currently does nothing,
  87. but ensures that you can write future-safe code.
  88. Generally, support for a specific SRFI is made available by using
  89. modules named @code{(srfi srfi-@var{number})}, where @var{number} is the
  90. number of the SRFI needed. Another possibility is to use the command
  91. line option @code{--use-srfi}, which will load the necessary modules
  92. automatically (@pxref{Invoking Guile}).
  93. @node SRFI-0
  94. @subsection SRFI-0 - cond-expand
  95. @cindex SRFI-0
  96. This SRFI lets a portable Scheme program test for the presence of
  97. certain features, and adapt itself by using different blocks of code,
  98. or fail if the necessary features are not available. There's no
  99. module to load, this is in the Guile core.
  100. A program designed only for Guile will generally not need this
  101. mechanism, such a program can of course directly use the various
  102. documented parts of Guile.
  103. @deffn syntax cond-expand (feature body@dots{}) @dots{}
  104. Expand to the @var{body} of the first clause whose @var{feature}
  105. specification is satisfied. It is an error if no @var{feature} is
  106. satisfied.
  107. Features are symbols such as @code{srfi-1}, and a feature
  108. specification can use @code{and}, @code{or} and @code{not} forms to
  109. test combinations. The last clause can be an @code{else}, to be used
  110. if no other passes.
  111. For example, define a private version of @code{alist-cons} if SRFI-1
  112. is not available.
  113. @example
  114. (cond-expand (srfi-1
  115. )
  116. (else
  117. (define (alist-cons key val alist)
  118. (cons (cons key val) alist))))
  119. @end example
  120. Or demand a certain set of SRFIs (list operations, string ports,
  121. @code{receive} and string operations), failing if they're not
  122. available.
  123. @example
  124. (cond-expand ((and srfi-1 srfi-6 srfi-8 srfi-13)
  125. ))
  126. @end example
  127. @end deffn
  128. @noindent
  129. The Guile core has the following features,
  130. @example
  131. guile
  132. guile-2 ;; starting from Guile 2.x
  133. guile-2.2 ;; starting from Guile 2.2
  134. guile-3 ;; starting from Guile 3.x
  135. guile-3.0 ;; starting from Guile 3.0
  136. r5rs
  137. r6rs
  138. r7rs
  139. exact-closed ieee-float full-unicode ratios ;; R7RS features
  140. srfi-0
  141. srfi-4
  142. srfi-6
  143. srfi-13
  144. srfi-14
  145. srfi-16
  146. srfi-23
  147. srfi-30
  148. srfi-39
  149. srfi-46
  150. srfi-55
  151. srfi-61
  152. srfi-62
  153. srfi-87
  154. srfi-105
  155. @end example
  156. Other SRFI feature symbols are defined once their code has been loaded
  157. with @code{use-modules}, since only then are their bindings available.
  158. The @samp{--use-srfi} command line option (@pxref{Invoking Guile}) is
  159. a good way to load SRFIs to satisfy @code{cond-expand} when running a
  160. portable program.
  161. Testing the @code{guile} feature allows a program to adapt itself to
  162. the Guile module system, but still run on other Scheme systems. For
  163. example the following demands SRFI-8 (@code{receive}), but also knows
  164. how to load it with the Guile mechanism.
  165. @example
  166. (cond-expand (srfi-8
  167. )
  168. (guile
  169. (use-modules (srfi srfi-8))))
  170. @end example
  171. @cindex @code{guile-2} SRFI-0 feature
  172. @cindex portability between 2.0 and older versions
  173. Likewise, testing the @code{guile-2} feature allows code to be portable
  174. between Guile 2.@var{x} and previous versions of Guile. For instance, it
  175. makes it possible to write code that accounts for Guile 2.@var{x}'s compiler,
  176. yet be correctly interpreted on 1.8 and earlier versions:
  177. @example
  178. (cond-expand (guile-2 (eval-when (compile)
  179. ;; This must be evaluated at compile time.
  180. (fluid-set! current-reader my-reader)))
  181. (guile
  182. ;; Earlier versions of Guile do not have a
  183. ;; separate compilation phase.
  184. (fluid-set! current-reader my-reader)))
  185. @end example
  186. It should be noted that @code{cond-expand} is separate from the
  187. @code{*features*} mechanism (@pxref{Feature Tracking}), feature
  188. symbols in one are unrelated to those in the other.
  189. @node SRFI-1
  190. @subsection SRFI-1 - List library
  191. @cindex SRFI-1
  192. @cindex list
  193. @c FIXME::martin: Review me!
  194. The list library defined in SRFI-1 contains a lot of useful list
  195. processing procedures for construction, examining, destructuring and
  196. manipulating lists and pairs.
  197. Since SRFI-1 also defines some procedures which are already contained
  198. in R5RS and thus are supported by the Guile core library, some list
  199. and pair procedures which appear in the SRFI-1 document may not appear
  200. in this section. So when looking for a particular list/pair
  201. processing procedure, you should also have a look at the sections
  202. @ref{Lists} and @ref{Pairs}.
  203. @menu
  204. * SRFI-1 Constructors:: Constructing new lists.
  205. * SRFI-1 Predicates:: Testing list for specific properties.
  206. * SRFI-1 Selectors:: Selecting elements from lists.
  207. * SRFI-1 Length Append etc:: Length calculation and list appending.
  208. * SRFI-1 Fold and Map:: Higher-order list processing.
  209. * SRFI-1 Filtering and Partitioning:: Filter lists based on predicates.
  210. * SRFI-1 Searching:: Search for elements.
  211. * SRFI-1 Deleting:: Delete elements from lists.
  212. * SRFI-1 Association Lists:: Handle association lists.
  213. * SRFI-1 Set Operations:: Use lists for representing sets.
  214. @end menu
  215. @node SRFI-1 Constructors
  216. @subsubsection Constructors
  217. @cindex list constructor
  218. @c FIXME::martin: Review me!
  219. New lists can be constructed by calling one of the following
  220. procedures.
  221. @deffn {Scheme Procedure} xcons d a
  222. Like @code{cons}, but with interchanged arguments. Useful mostly when
  223. passed to higher-order procedures.
  224. @end deffn
  225. @deffn {Scheme Procedure} list-tabulate n init-proc
  226. Return an @var{n}-element list, where each list element is produced by
  227. applying the procedure @var{init-proc} to the corresponding list
  228. index. The order in which @var{init-proc} is applied to the indices
  229. is not specified.
  230. @end deffn
  231. @deffn {Scheme Procedure} list-copy lst
  232. Return a new list containing the elements of the list @var{lst}.
  233. This function differs from the core @code{list-copy} (@pxref{List
  234. Constructors}) in accepting improper lists too. And if @var{lst} is
  235. not a pair at all then it's treated as the final tail of an improper
  236. list and simply returned.
  237. @end deffn
  238. @deffn {Scheme Procedure} circular-list elt1 elt2 @dots{}
  239. Return a circular list containing the given arguments @var{elt1}
  240. @var{elt2} @dots{}.
  241. @end deffn
  242. @deffn {Scheme Procedure} iota count [start step]
  243. Return a list containing @var{count} numbers, starting from
  244. @var{start} and adding @var{step} each time. The default @var{start}
  245. is 0, the default @var{step} is 1. For example,
  246. @example
  247. (iota 6) @result{} (0 1 2 3 4 5)
  248. (iota 4 2.5 -2) @result{} (2.5 0.5 -1.5 -3.5)
  249. @end example
  250. This function takes its name from the corresponding primitive in the
  251. APL language.
  252. @end deffn
  253. @node SRFI-1 Predicates
  254. @subsubsection Predicates
  255. @cindex list predicate
  256. @c FIXME::martin: Review me!
  257. The procedures in this section test specific properties of lists.
  258. @deffn {Scheme Procedure} proper-list? obj
  259. Return @code{#t} if @var{obj} is a proper list, or @code{#f}
  260. otherwise. This is the same as the core @code{list?} (@pxref{List
  261. Predicates}).
  262. A proper list is a list which ends with the empty list @code{()} in
  263. the usual way. The empty list @code{()} itself is a proper list too.
  264. @example
  265. (proper-list? '(1 2 3)) @result{} #t
  266. (proper-list? '()) @result{} #t
  267. @end example
  268. @end deffn
  269. @deffn {Scheme Procedure} circular-list? obj
  270. Return @code{#t} if @var{obj} is a circular list, or @code{#f}
  271. otherwise.
  272. A circular list is a list where at some point the @code{cdr} refers
  273. back to a previous pair in the list (either the start or some later
  274. point), so that following the @code{cdr}s takes you around in a
  275. circle, with no end.
  276. @example
  277. (define x (list 1 2 3 4))
  278. (set-cdr! (last-pair x) (cddr x))
  279. x @result{} (1 2 3 4 3 4 3 4 ...)
  280. (circular-list? x) @result{} #t
  281. @end example
  282. @end deffn
  283. @deffn {Scheme Procedure} dotted-list? obj
  284. Return @code{#t} if @var{obj} is a dotted list, or @code{#f}
  285. otherwise.
  286. A dotted list is a list where the @code{cdr} of the last pair is not
  287. the empty list @code{()}. Any non-pair @var{obj} is also considered a
  288. dotted list, with length zero.
  289. @example
  290. (dotted-list? '(1 2 . 3)) @result{} #t
  291. (dotted-list? 99) @result{} #t
  292. @end example
  293. @end deffn
  294. It will be noted that any Scheme object passes exactly one of the
  295. above three tests @code{proper-list?}, @code{circular-list?} and
  296. @code{dotted-list?}. Non-lists are @code{dotted-list?}, finite lists
  297. are either @code{proper-list?} or @code{dotted-list?}, and infinite
  298. lists are @code{circular-list?}.
  299. @sp 1
  300. @deffn {Scheme Procedure} null-list? lst
  301. Return @code{#t} if @var{lst} is the empty list @code{()}, @code{#f}
  302. otherwise. If something else than a proper or circular list is passed
  303. as @var{lst}, an error is signalled. This procedure is recommended
  304. for checking for the end of a list in contexts where dotted lists are
  305. not allowed.
  306. @end deffn
  307. @deffn {Scheme Procedure} not-pair? obj
  308. Return @code{#t} is @var{obj} is not a pair, @code{#f} otherwise.
  309. This is shorthand notation @code{(not (pair? @var{obj}))} and is
  310. supposed to be used for end-of-list checking in contexts where dotted
  311. lists are allowed.
  312. @end deffn
  313. @deffn {Scheme Procedure} list= elt= list1 @dots{}
  314. Return @code{#t} if all argument lists are equal, @code{#f} otherwise.
  315. List equality is determined by testing whether all lists have the same
  316. length and the corresponding elements are equal in the sense of the
  317. equality predicate @var{elt=}. If no or only one list is given,
  318. @code{#t} is returned.
  319. @end deffn
  320. @node SRFI-1 Selectors
  321. @subsubsection Selectors
  322. @cindex list selector
  323. @c FIXME::martin: Review me!
  324. @deffn {Scheme Procedure} first pair
  325. @deffnx {Scheme Procedure} second pair
  326. @deffnx {Scheme Procedure} third pair
  327. @deffnx {Scheme Procedure} fourth pair
  328. @deffnx {Scheme Procedure} fifth pair
  329. @deffnx {Scheme Procedure} sixth pair
  330. @deffnx {Scheme Procedure} seventh pair
  331. @deffnx {Scheme Procedure} eighth pair
  332. @deffnx {Scheme Procedure} ninth pair
  333. @deffnx {Scheme Procedure} tenth pair
  334. These are synonyms for @code{car}, @code{cadr}, @code{caddr}, @dots{}.
  335. @end deffn
  336. @deffn {Scheme Procedure} car+cdr pair
  337. Return two values, the @sc{car} and the @sc{cdr} of @var{pair}.
  338. @end deffn
  339. @lisp
  340. (car+cdr '(0 1 2 3))
  341. @result{}
  342. 0
  343. (1 2 3)
  344. @end lisp
  345. @deffn {Scheme Procedure} take lst i
  346. @deffnx {Scheme Procedure} take! lst i
  347. Return a list containing the first @var{i} elements of @var{lst}.
  348. @code{take!} may modify the structure of the argument list @var{lst}
  349. in order to produce the result.
  350. @end deffn
  351. @deffn {Scheme Procedure} drop lst i
  352. Return a list containing all but the first @var{i} elements of
  353. @var{lst}.
  354. @end deffn
  355. @deffn {Scheme Procedure} take-right lst i
  356. Return a list containing the @var{i} last elements of @var{lst}.
  357. The return shares a common tail with @var{lst}.
  358. @end deffn
  359. @deffn {Scheme Procedure} drop-right lst i
  360. @deffnx {Scheme Procedure} drop-right! lst i
  361. Return a list containing all but the @var{i} last elements of
  362. @var{lst}.
  363. @code{drop-right} always returns a new list, even when @var{i} is
  364. zero. @code{drop-right!} may modify the structure of the argument
  365. list @var{lst} in order to produce the result.
  366. @end deffn
  367. @deffn {Scheme Procedure} split-at lst i
  368. @deffnx {Scheme Procedure} split-at! lst i
  369. Return two values, a list containing the first @var{i} elements of the
  370. list @var{lst} and a list containing the remaining elements.
  371. @code{split-at!} may modify the structure of the argument list
  372. @var{lst} in order to produce the result.
  373. @end deffn
  374. @deffn {Scheme Procedure} last lst
  375. Return the last element of the non-empty, finite list @var{lst}.
  376. @end deffn
  377. @node SRFI-1 Length Append etc
  378. @subsubsection Length, Append, Concatenate, etc.
  379. @c FIXME::martin: Review me!
  380. @deffn {Scheme Procedure} length+ lst
  381. Return the length of the argument list @var{lst}. When @var{lst} is a
  382. circular list, @code{#f} is returned.
  383. @end deffn
  384. @deffn {Scheme Procedure} concatenate list-of-lists
  385. @deffnx {Scheme Procedure} concatenate! list-of-lists
  386. Construct a list by appending all lists in @var{list-of-lists}.
  387. @code{concatenate!} may modify the structure of the given lists in
  388. order to produce the result.
  389. @code{concatenate} is the same as @code{(apply append
  390. @var{list-of-lists})}. It exists because some Scheme implementations
  391. have a limit on the number of arguments a function takes, which the
  392. @code{apply} might exceed. In Guile there is no such limit.
  393. @end deffn
  394. @deffn {Scheme Procedure} append-reverse rev-head tail
  395. @deffnx {Scheme Procedure} append-reverse! rev-head tail
  396. Reverse @var{rev-head}, append @var{tail} to it, and return the
  397. result. This is equivalent to @code{(append (reverse @var{rev-head})
  398. @var{tail})}, but its implementation is more efficient.
  399. @example
  400. (append-reverse '(1 2 3) '(4 5 6)) @result{} (3 2 1 4 5 6)
  401. @end example
  402. @code{append-reverse!} may modify @var{rev-head} in order to produce
  403. the result.
  404. @end deffn
  405. @deffn {Scheme Procedure} zip lst1 lst2 @dots{}
  406. Return a list as long as the shortest of the argument lists, where
  407. each element is a list. The first list contains the first elements of
  408. the argument lists, the second list contains the second elements, and
  409. so on.
  410. @end deffn
  411. @deffn {Scheme Procedure} unzip1 lst
  412. @deffnx {Scheme Procedure} unzip2 lst
  413. @deffnx {Scheme Procedure} unzip3 lst
  414. @deffnx {Scheme Procedure} unzip4 lst
  415. @deffnx {Scheme Procedure} unzip5 lst
  416. @code{unzip1} takes a list of lists, and returns a list containing the
  417. first elements of each list, @code{unzip2} returns two lists, the
  418. first containing the first elements of each lists and the second
  419. containing the second elements of each lists, and so on.
  420. @end deffn
  421. @deffn {Scheme Procedure} count pred lst1 lst2 @dots{}
  422. Return a count of the number of times @var{pred} returns true when
  423. called on elements from the given lists.
  424. @var{pred} is called with @var{N} parameters @code{(@var{pred}
  425. @var{elem1} @dots{} @var{elemN} )}, each element being from the
  426. corresponding list. The first call is with the first element of each
  427. list, the second with the second element from each, and so on.
  428. Counting stops when the end of the shortest list is reached. At least
  429. one list must be non-circular.
  430. @end deffn
  431. @node SRFI-1 Fold and Map
  432. @subsubsection Fold, Unfold & Map
  433. @cindex list fold
  434. @cindex list map
  435. @c FIXME::martin: Review me!
  436. @deffn {Scheme Procedure} fold proc init lst1 lst2 @dots{}
  437. @deffnx {Scheme Procedure} fold-right proc init lst1 lst2 @dots{}
  438. Apply @var{proc} to the elements of @var{lst1} @var{lst2} @dots{} to
  439. build a result, and return that result.
  440. Each @var{proc} call is @code{(@var{proc} @var{elem1} @var{elem2}
  441. @dots{} @var{previous})}, where @var{elem1} is from @var{lst1},
  442. @var{elem2} is from @var{lst2}, and so on. @var{previous} is the return
  443. from the previous call to @var{proc}, or the given @var{init} for the
  444. first call. If any list is empty, just @var{init} is returned.
  445. @code{fold} works through the list elements from first to last. The
  446. following shows a list reversal and the calls it makes,
  447. @example
  448. (fold cons '() '(1 2 3))
  449. (cons 1 '())
  450. (cons 2 '(1))
  451. (cons 3 '(2 1)
  452. @result{} (3 2 1)
  453. @end example
  454. @code{fold-right} works through the list elements from last to first,
  455. ie.@: from the right. So for example the following finds the longest
  456. string, and the last among equal longest,
  457. @example
  458. (fold-right (lambda (str prev)
  459. (if (> (string-length str) (string-length prev))
  460. str
  461. prev))
  462. ""
  463. '("x" "abc" "xyz" "jk"))
  464. @result{} "xyz"
  465. @end example
  466. If @var{lst1} @var{lst2} @dots{} have different lengths, @code{fold}
  467. stops when the end of the shortest is reached; @code{fold-right}
  468. commences at the last element of the shortest. Ie.@: elements past the
  469. length of the shortest are ignored in the other @var{lst}s. At least
  470. one @var{lst} must be non-circular.
  471. @code{fold} should be preferred over @code{fold-right} if the order of
  472. processing doesn't matter, or can be arranged either way, since
  473. @code{fold} is a little more efficient.
  474. The way @code{fold} builds a result from iterating is quite general,
  475. it can do more than other iterations like say @code{map} or
  476. @code{filter}. The following for example removes adjacent duplicate
  477. elements from a list,
  478. @example
  479. (define (delete-adjacent-duplicates lst)
  480. (fold-right (lambda (elem ret)
  481. (if (equal? elem (first ret))
  482. ret
  483. (cons elem ret)))
  484. (list (last lst))
  485. lst))
  486. (delete-adjacent-duplicates '(1 2 3 3 4 4 4 5))
  487. @result{} (1 2 3 4 5)
  488. @end example
  489. Clearly the same sort of thing can be done with a @code{for-each} and
  490. a variable in which to build the result, but a self-contained
  491. @var{proc} can be re-used in multiple contexts, where a
  492. @code{for-each} would have to be written out each time.
  493. @end deffn
  494. @deffn {Scheme Procedure} pair-fold proc init lst1 lst2 @dots{}
  495. @deffnx {Scheme Procedure} pair-fold-right proc init lst1 lst2 @dots{}
  496. The same as @code{fold} and @code{fold-right}, but apply @var{proc} to
  497. the pairs of the lists instead of the list elements.
  498. @end deffn
  499. @deffn {Scheme Procedure} reduce proc default lst
  500. @deffnx {Scheme Procedure} reduce-right proc default lst
  501. @code{reduce} is a variant of @code{fold}, where the first call to
  502. @var{proc} is on two elements from @var{lst}, rather than one element
  503. and a given initial value.
  504. If @var{lst} is empty, @code{reduce} returns @var{default} (this is
  505. the only use for @var{default}). If @var{lst} has just one element
  506. then that's the return value. Otherwise @var{proc} is called on the
  507. elements of @var{lst}.
  508. Each @var{proc} call is @code{(@var{proc} @var{elem} @var{previous})},
  509. where @var{elem} is from @var{lst} (the second and subsequent elements
  510. of @var{lst}), and @var{previous} is the return from the previous call
  511. to @var{proc}. The first element of @var{lst} is the @var{previous}
  512. for the first call to @var{proc}.
  513. For example, the following adds a list of numbers, the calls made to
  514. @code{+} are shown. (Of course @code{+} accepts multiple arguments
  515. and can add a list directly, with @code{apply}.)
  516. @example
  517. (reduce + 0 '(5 6 7)) @result{} 18
  518. (+ 6 5) @result{} 11
  519. (+ 7 11) @result{} 18
  520. @end example
  521. @code{reduce} can be used instead of @code{fold} where the @var{init}
  522. value is an ``identity'', meaning a value which under @var{proc}
  523. doesn't change the result, in this case 0 is an identity since
  524. @code{(+ 5 0)} is just 5. @code{reduce} avoids that unnecessary call.
  525. @code{reduce-right} is a similar variation on @code{fold-right},
  526. working from the end (ie.@: the right) of @var{lst}. The last element
  527. of @var{lst} is the @var{previous} for the first call to @var{proc},
  528. and the @var{elem} values go from the second last.
  529. @code{reduce} should be preferred over @code{reduce-right} if the
  530. order of processing doesn't matter, or can be arranged either way,
  531. since @code{reduce} is a little more efficient.
  532. @end deffn
  533. @deffn {Scheme Procedure} unfold p f g seed [tail-gen]
  534. @code{unfold} is defined as follows:
  535. @lisp
  536. (unfold p f g seed) =
  537. (if (p seed) (tail-gen seed)
  538. (cons (f seed)
  539. (unfold p f g (g seed))))
  540. @end lisp
  541. @table @var
  542. @item p
  543. Determines when to stop unfolding.
  544. @item f
  545. Maps each seed value to the corresponding list element.
  546. @item g
  547. Maps each seed value to next seed value.
  548. @item seed
  549. The state value for the unfold.
  550. @item tail-gen
  551. Creates the tail of the list; defaults to @code{(lambda (x) '())}.
  552. @end table
  553. @var{g} produces a series of seed values, which are mapped to list
  554. elements by @var{f}. These elements are put into a list in
  555. left-to-right order, and @var{p} tells when to stop unfolding.
  556. @end deffn
  557. @deffn {Scheme Procedure} unfold-right p f g seed [tail]
  558. Construct a list with the following loop.
  559. @lisp
  560. (let lp ((seed seed) (lis tail))
  561. (if (p seed) lis
  562. (lp (g seed)
  563. (cons (f seed) lis))))
  564. @end lisp
  565. @table @var
  566. @item p
  567. Determines when to stop unfolding.
  568. @item f
  569. Maps each seed value to the corresponding list element.
  570. @item g
  571. Maps each seed value to next seed value.
  572. @item seed
  573. The state value for the unfold.
  574. @item tail
  575. The tail of the list; defaults to @code{'()}.
  576. @end table
  577. @end deffn
  578. @deffn {Scheme Procedure} map f lst1 lst2 @dots{}
  579. Map the procedure over the list(s) @var{lst1}, @var{lst2}, @dots{} and
  580. return a list containing the results of the procedure applications.
  581. This procedure is extended with respect to R5RS, because the argument
  582. lists may have different lengths. The result list will have the same
  583. length as the shortest argument lists. The order in which @var{f}
  584. will be applied to the list element(s) is not specified.
  585. @end deffn
  586. @deffn {Scheme Procedure} for-each f lst1 lst2 @dots{}
  587. Apply the procedure @var{f} to each pair of corresponding elements of
  588. the list(s) @var{lst1}, @var{lst2}, @dots{}. The return value is not
  589. specified. This procedure is extended with respect to R5RS, because
  590. the argument lists may have different lengths. The shortest argument
  591. list determines the number of times @var{f} is called. @var{f} will
  592. be applied to the list elements in left-to-right order.
  593. @end deffn
  594. @deffn {Scheme Procedure} append-map f lst1 lst2 @dots{}
  595. @deffnx {Scheme Procedure} append-map! f lst1 lst2 @dots{}
  596. Equivalent to
  597. @lisp
  598. (apply append (map f clist1 clist2 ...))
  599. @end lisp
  600. and
  601. @lisp
  602. (apply append! (map f clist1 clist2 ...))
  603. @end lisp
  604. Map @var{f} over the elements of the lists, just as in the @code{map}
  605. function. However, the results of the applications are appended
  606. together to make the final result. @code{append-map} uses
  607. @code{append} to append the results together; @code{append-map!} uses
  608. @code{append!}.
  609. The dynamic order in which the various applications of @var{f} are
  610. made is not specified.
  611. @end deffn
  612. @deffn {Scheme Procedure} map! f lst1 lst2 @dots{}
  613. Linear-update variant of @code{map} -- @code{map!} is allowed, but not
  614. required, to alter the cons cells of @var{lst1} to construct the
  615. result list.
  616. The dynamic order in which the various applications of @var{f} are
  617. made is not specified. In the n-ary case, @var{lst2}, @var{lst3},
  618. @dots{} must have at least as many elements as @var{lst1}.
  619. @end deffn
  620. @deffn {Scheme Procedure} pair-for-each f lst1 lst2 @dots{}
  621. Like @code{for-each}, but applies the procedure @var{f} to the pairs
  622. from which the argument lists are constructed, instead of the list
  623. elements. The return value is not specified.
  624. @end deffn
  625. @deffn {Scheme Procedure} filter-map f lst1 lst2 @dots{}
  626. Like @code{map}, but only results from the applications of @var{f}
  627. which are true are saved in the result list.
  628. @end deffn
  629. @node SRFI-1 Filtering and Partitioning
  630. @subsubsection Filtering and Partitioning
  631. @cindex list filter
  632. @cindex list partition
  633. @c FIXME::martin: Review me!
  634. Filtering means to collect all elements from a list which satisfy a
  635. specific condition. Partitioning a list means to make two groups of
  636. list elements, one which contains the elements satisfying a condition,
  637. and the other for the elements which don't.
  638. The @code{filter} and @code{filter!} functions are implemented in the
  639. Guile core, @xref{List Modification}.
  640. @deffn {Scheme Procedure} partition pred lst
  641. @deffnx {Scheme Procedure} partition! pred lst
  642. Split @var{lst} into those elements which do and don't satisfy the
  643. predicate @var{pred}.
  644. The return is two values (@pxref{Multiple Values}), the first being a
  645. list of all elements from @var{lst} which satisfy @var{pred}, the
  646. second a list of those which do not.
  647. The elements in the result lists are in the same order as in @var{lst}
  648. but the order in which the calls @code{(@var{pred} elem)} are made on
  649. the list elements is unspecified.
  650. @code{partition} does not change @var{lst}, but one of the returned
  651. lists may share a tail with it. @code{partition!} may modify
  652. @var{lst} to construct its return.
  653. @end deffn
  654. @deffn {Scheme Procedure} remove pred lst
  655. @deffnx {Scheme Procedure} remove! pred lst
  656. Return a list containing all elements from @var{lst} which do not
  657. satisfy the predicate @var{pred}. The elements in the result list
  658. have the same order as in @var{lst}. The order in which @var{pred} is
  659. applied to the list elements is not specified.
  660. @code{remove!} is allowed, but not required to modify the structure of
  661. the input list.
  662. @end deffn
  663. @node SRFI-1 Searching
  664. @subsubsection Searching
  665. @cindex list search
  666. @c FIXME::martin: Review me!
  667. The procedures for searching elements in lists either accept a
  668. predicate or a comparison object for determining which elements are to
  669. be searched.
  670. @deffn {Scheme Procedure} find pred lst
  671. Return the first element of @var{lst} that satisfies the predicate
  672. @var{pred} and @code{#f} if no such element is found.
  673. @end deffn
  674. @deffn {Scheme Procedure} find-tail pred lst
  675. Return the first pair of @var{lst} whose @sc{car} satisfies the
  676. predicate @var{pred} and @code{#f} if no such element is found.
  677. @end deffn
  678. @deffn {Scheme Procedure} take-while pred lst
  679. @deffnx {Scheme Procedure} take-while! pred lst
  680. Return the longest initial prefix of @var{lst} whose elements all
  681. satisfy the predicate @var{pred}.
  682. @code{take-while!} is allowed, but not required to modify the input
  683. list while producing the result.
  684. @end deffn
  685. @deffn {Scheme Procedure} drop-while pred lst
  686. Drop the longest initial prefix of @var{lst} whose elements all
  687. satisfy the predicate @var{pred}.
  688. @end deffn
  689. @deffn {Scheme Procedure} span pred lst
  690. @deffnx {Scheme Procedure} span! pred lst
  691. @deffnx {Scheme Procedure} break pred lst
  692. @deffnx {Scheme Procedure} break! pred lst
  693. @code{span} splits the list @var{lst} into the longest initial prefix
  694. whose elements all satisfy the predicate @var{pred}, and the remaining
  695. tail. @code{break} inverts the sense of the predicate.
  696. @code{span!} and @code{break!} are allowed, but not required to modify
  697. the structure of the input list @var{lst} in order to produce the
  698. result.
  699. Note that the name @code{break} conflicts with the @code{break}
  700. binding established by @code{while} (@pxref{while do}). Applications
  701. wanting to use @code{break} from within a @code{while} loop will need
  702. to make a new define under a different name.
  703. @end deffn
  704. @deffn {Scheme Procedure} any pred lst1 lst2 @dots{}
  705. Test whether any set of elements from @var{lst1} @var{lst2} @dots{}
  706. satisfies @var{pred}. If so, the return value is the return value from
  707. the successful @var{pred} call, or if not, the return value is
  708. @code{#f}.
  709. If there are n list arguments, then @var{pred} must be a predicate
  710. taking n arguments. Each @var{pred} call is @code{(@var{pred}
  711. @var{elem1} @var{elem2} @dots{} )} taking an element from each
  712. @var{lst}. The calls are made successively for the first, second, etc.
  713. elements of the lists, stopping when @var{pred} returns non-@code{#f},
  714. or when the end of the shortest list is reached.
  715. The @var{pred} call on the last set of elements (i.e., when the end of
  716. the shortest list has been reached), if that point is reached, is a
  717. tail call.
  718. @end deffn
  719. @deffn {Scheme Procedure} every pred lst1 lst2 @dots{}
  720. Test whether every set of elements from @var{lst1} @var{lst2} @dots{}
  721. satisfies @var{pred}. If so, the return value is the return from the
  722. final @var{pred} call, or if not, the return value is @code{#f}.
  723. If there are n list arguments, then @var{pred} must be a predicate
  724. taking n arguments. Each @var{pred} call is @code{(@var{pred}
  725. @var{elem1} @var{elem2 @dots{}})} taking an element from each
  726. @var{lst}. The calls are made successively for the first, second, etc.
  727. elements of the lists, stopping if @var{pred} returns @code{#f}, or when
  728. the end of any of the lists is reached.
  729. The @var{pred} call on the last set of elements (i.e., when the end of
  730. the shortest list has been reached) is a tail call.
  731. If one of @var{lst1} @var{lst2} @dots{}is empty then no calls to
  732. @var{pred} are made, and the return value is @code{#t}.
  733. @end deffn
  734. @deffn {Scheme Procedure} list-index pred lst1 lst2 @dots{}
  735. Return the index of the first set of elements, one from each of
  736. @var{lst1} @var{lst2} @dots{}, which satisfies @var{pred}.
  737. @var{pred} is called as @code{(@var{elem1} @var{elem2 @dots{}})}.
  738. Searching stops when the end of the shortest @var{lst} is reached.
  739. The return index starts from 0 for the first set of elements. If no
  740. set of elements pass, then the return value is @code{#f}.
  741. @example
  742. (list-index odd? '(2 4 6 9)) @result{} 3
  743. (list-index = '(1 2 3) '(3 1 2)) @result{} #f
  744. @end example
  745. @end deffn
  746. @deffn {Scheme Procedure} member x lst [=]
  747. Return the first sublist of @var{lst} whose @sc{car} is equal to
  748. @var{x}. If @var{x} does not appear in @var{lst}, return @code{#f}.
  749. Equality is determined by @code{equal?}, or by the equality predicate
  750. @var{=} if given. @var{=} is called @code{(= @var{x} elem)},
  751. ie.@: with the given @var{x} first, so for example to find the first
  752. element greater than 5,
  753. @example
  754. (member 5 '(3 5 1 7 2 9) <) @result{} (7 2 9)
  755. @end example
  756. This version of @code{member} extends the core @code{member}
  757. (@pxref{List Searching}) by accepting an equality predicate.
  758. @end deffn
  759. @node SRFI-1 Deleting
  760. @subsubsection Deleting
  761. @cindex list delete
  762. @deffn {Scheme Procedure} delete x lst [=]
  763. @deffnx {Scheme Procedure} delete! x lst [=]
  764. Return a list containing the elements of @var{lst} but with those
  765. equal to @var{x} deleted. The returned elements will be in the same
  766. order as they were in @var{lst}.
  767. Equality is determined by the @var{=} predicate, or @code{equal?} if
  768. not given. An equality call is made just once for each element, but
  769. the order in which the calls are made on the elements is unspecified.
  770. The equality calls are always @code{(= x elem)}, ie.@: the given @var{x}
  771. is first. This means for instance elements greater than 5 can be
  772. deleted with @code{(delete 5 lst <)}.
  773. @code{delete} does not modify @var{lst}, but the return might share a
  774. common tail with @var{lst}. @code{delete!} may modify the structure
  775. of @var{lst} to construct its return.
  776. These functions extend the core @code{delete} and @code{delete!}
  777. (@pxref{List Modification}) in accepting an equality predicate. See
  778. also @code{lset-difference} (@pxref{SRFI-1 Set Operations}) for
  779. deleting multiple elements from a list.
  780. @end deffn
  781. @deffn {Scheme Procedure} delete-duplicates lst [=]
  782. @deffnx {Scheme Procedure} delete-duplicates! lst [=]
  783. Return a list containing the elements of @var{lst} but without
  784. duplicates.
  785. When elements are equal, only the first in @var{lst} is retained.
  786. Equal elements can be anywhere in @var{lst}, they don't have to be
  787. adjacent. The returned list will have the retained elements in the
  788. same order as they were in @var{lst}.
  789. Equality is determined by the @var{=} predicate, or @code{equal?} if
  790. not given. Calls @code{(= x y)} are made with element @var{x} being
  791. before @var{y} in @var{lst}. A call is made at most once for each
  792. combination, but the sequence of the calls across the elements is
  793. unspecified.
  794. @code{delete-duplicates} does not modify @var{lst}, but the return
  795. might share a common tail with @var{lst}. @code{delete-duplicates!}
  796. may modify the structure of @var{lst} to construct its return.
  797. In the worst case, this is an @math{O(N^2)} algorithm because it must
  798. check each element against all those preceding it. For long lists it
  799. is more efficient to sort and then compare only adjacent elements.
  800. @end deffn
  801. @node SRFI-1 Association Lists
  802. @subsubsection Association Lists
  803. @cindex association list
  804. @cindex alist
  805. @c FIXME::martin: Review me!
  806. Association lists are described in detail in section @ref{Association
  807. Lists}. The present section only documents the additional procedures
  808. for dealing with association lists defined by SRFI-1.
  809. @deffn {Scheme Procedure} assoc key alist [=]
  810. Return the pair from @var{alist} which matches @var{key}. This
  811. extends the core @code{assoc} (@pxref{Retrieving Alist Entries}) by
  812. taking an optional @var{=} comparison procedure.
  813. The default comparison is @code{equal?}. If an @var{=} parameter is
  814. given it's called @code{(@var{=} @var{key} @var{alistcar})}, i.e.@: the
  815. given target @var{key} is the first argument, and a @code{car} from
  816. @var{alist} is second.
  817. For example a case-insensitive string lookup,
  818. @example
  819. (assoc "yy" '(("XX" . 1) ("YY" . 2)) string-ci=?)
  820. @result{} ("YY" . 2)
  821. @end example
  822. @end deffn
  823. @deffn {Scheme Procedure} alist-cons key datum alist
  824. Cons a new association @var{key} and @var{datum} onto @var{alist} and
  825. return the result. This is equivalent to
  826. @lisp
  827. (cons (cons @var{key} @var{datum}) @var{alist})
  828. @end lisp
  829. @code{acons} (@pxref{Adding or Setting Alist Entries}) in the Guile
  830. core does the same thing.
  831. @end deffn
  832. @deffn {Scheme Procedure} alist-copy alist
  833. Return a newly allocated copy of @var{alist}, that means that the
  834. spine of the list as well as the pairs are copied.
  835. @end deffn
  836. @deffn {Scheme Procedure} alist-delete key alist [=]
  837. @deffnx {Scheme Procedure} alist-delete! key alist [=]
  838. Return a list containing the elements of @var{alist} but with those
  839. elements whose keys are equal to @var{key} deleted. The returned
  840. elements will be in the same order as they were in @var{alist}.
  841. Equality is determined by the @var{=} predicate, or @code{equal?} if
  842. not given. The order in which elements are tested is unspecified, but
  843. each equality call is made @code{(= key alistkey)}, i.e.@: the given
  844. @var{key} parameter is first and the key from @var{alist} second.
  845. This means for instance all associations with a key greater than 5 can
  846. be removed with @code{(alist-delete 5 alist <)}.
  847. @code{alist-delete} does not modify @var{alist}, but the return might
  848. share a common tail with @var{alist}. @code{alist-delete!} may modify
  849. the list structure of @var{alist} to construct its return.
  850. @end deffn
  851. @node SRFI-1 Set Operations
  852. @subsubsection Set Operations on Lists
  853. @cindex list set operation
  854. Lists can be used to represent sets of objects. The procedures in
  855. this section operate on such lists as sets.
  856. Note that lists are not an efficient way to implement large sets. The
  857. procedures here typically take time @math{@var{m}@cross{}@var{n}} when
  858. operating on @var{m} and @var{n} element lists. Other data structures
  859. like trees, bitsets (@pxref{Bit Vectors}) or hash tables (@pxref{Hash
  860. Tables}) are faster.
  861. All these procedures take an equality predicate as the first argument.
  862. This predicate is used for testing the objects in the list sets for
  863. sameness. This predicate must be consistent with @code{eq?}
  864. (@pxref{Equality}) in the sense that if two list elements are
  865. @code{eq?} then they must also be equal under the predicate. This
  866. simply means a given object must be equal to itself.
  867. @deffn {Scheme Procedure} lset<= = list @dots{}
  868. Return @code{#t} if each list is a subset of the one following it.
  869. I.e., @var{list1} is a subset of @var{list2}, @var{list2} is a subset of
  870. @var{list3}, etc., for as many lists as given. If only one list or no
  871. lists are given, the return value is @code{#t}.
  872. A list @var{x} is a subset of @var{y} if each element of @var{x} is
  873. equal to some element in @var{y}. Elements are compared using the
  874. given @var{=} procedure, called as @code{(@var{=} xelem yelem)}.
  875. @example
  876. (lset<= eq?) @result{} #t
  877. (lset<= eqv? '(1 2 3) '(1)) @result{} #f
  878. (lset<= eqv? '(1 3 2) '(4 3 1 2)) @result{} #t
  879. @end example
  880. @end deffn
  881. @deffn {Scheme Procedure} lset= = list @dots{}
  882. Return @code{#t} if all argument lists are set-equal. @var{list1} is
  883. compared to @var{list2}, @var{list2} to @var{list3}, etc., for as many
  884. lists as given. If only one list or no lists are given, the return
  885. value is @code{#t}.
  886. Two lists @var{x} and @var{y} are set-equal if each element of @var{x}
  887. is equal to some element of @var{y} and conversely each element of
  888. @var{y} is equal to some element of @var{x}. The order of the
  889. elements in the lists doesn't matter. Element equality is determined
  890. with the given @var{=} procedure, called as @code{(@var{=} xelem
  891. yelem)}, but exactly which calls are made is unspecified.
  892. @example
  893. (lset= eq?) @result{} #t
  894. (lset= eqv? '(1 2 3) '(3 2 1)) @result{} #t
  895. (lset= string-ci=? '("a" "A" "b") '("B" "b" "a")) @result{} #t
  896. @end example
  897. @end deffn
  898. @deffn {Scheme Procedure} lset-adjoin = list elem @dots{}
  899. Add to @var{list} any of the given @var{elem}s not already in the list.
  900. @var{elem}s are @code{cons}ed onto the start of @var{list} (so the
  901. return value shares a common tail with @var{list}), but the order that
  902. the @var{elem}s are added is unspecified.
  903. The given @var{=} procedure is used for comparing elements, called as
  904. @code{(@var{=} listelem elem)}, i.e., the second argument is one of
  905. the given @var{elem} parameters.
  906. @example
  907. (lset-adjoin eqv? '(1 2 3) 4 1 5) @result{} (5 4 1 2 3)
  908. @end example
  909. @end deffn
  910. @deffn {Scheme Procedure} lset-union = list @dots{}
  911. @deffnx {Scheme Procedure} lset-union! = list @dots{}
  912. Return the union of the argument list sets. The result is built by
  913. taking the union of @var{list1} and @var{list2}, then the union of
  914. that with @var{list3}, etc., for as many lists as given. For one list
  915. argument that list itself is the result, for no list arguments the
  916. result is the empty list.
  917. The union of two lists @var{x} and @var{y} is formed as follows. If
  918. @var{x} is empty then the result is @var{y}. Otherwise start with
  919. @var{x} as the result and consider each @var{y} element (from first to
  920. last). A @var{y} element not equal to something already in the result
  921. is @code{cons}ed onto the result.
  922. The given @var{=} procedure is used for comparing elements, called as
  923. @code{(@var{=} relem yelem)}. The first argument is from the result
  924. accumulated so far, and the second is from the list being union-ed in.
  925. But exactly which calls are made is otherwise unspecified.
  926. Notice that duplicate elements in @var{list1} (or the first non-empty
  927. list) are preserved, but that repeated elements in subsequent lists
  928. are only added once.
  929. @example
  930. (lset-union eqv?) @result{} ()
  931. (lset-union eqv? '(1 2 3)) @result{} (1 2 3)
  932. (lset-union eqv? '(1 2 1 3) '(2 4 5) '(5)) @result{} (5 4 1 2 1 3)
  933. @end example
  934. @code{lset-union} doesn't change the given lists but the result may
  935. share a tail with the first non-empty list. @code{lset-union!} can
  936. modify all of the given lists to form the result.
  937. @end deffn
  938. @deffn {Scheme Procedure} lset-intersection = list1 list2 @dots{}
  939. @deffnx {Scheme Procedure} lset-intersection! = list1 list2 @dots{}
  940. Return the intersection of @var{list1} with the other argument lists,
  941. meaning those elements of @var{list1} which are also in all of
  942. @var{list2} etc. For one list argument, just that list is returned.
  943. The test for an element of @var{list1} to be in the return is simply
  944. that it's equal to some element in each of @var{list2} etc. Notice
  945. this means an element appearing twice in @var{list1} but only once in
  946. each of @var{list2} etc will go into the return twice. The return has
  947. its elements in the same order as they were in @var{list1}.
  948. The given @var{=} procedure is used for comparing elements, called as
  949. @code{(@var{=} elem1 elemN)}. The first argument is from @var{list1}
  950. and the second is from one of the subsequent lists. But exactly which
  951. calls are made and in what order is unspecified.
  952. @example
  953. (lset-intersection eqv? '(x y)) @result{} (x y)
  954. (lset-intersection eqv? '(1 2 3) '(4 3 2)) @result{} (2 3)
  955. (lset-intersection eqv? '(1 1 2 2) '(1 2) '(2 1) '(2)) @result{} (2 2)
  956. @end example
  957. The return from @code{lset-intersection} may share a tail with
  958. @var{list1}. @code{lset-intersection!} may modify @var{list1} to form
  959. its result.
  960. @end deffn
  961. @deffn {Scheme Procedure} lset-difference = list1 list2 @dots{}
  962. @deffnx {Scheme Procedure} lset-difference! = list1 list2 @dots{}
  963. Return @var{list1} with any elements in @var{list2}, @var{list3} etc
  964. removed (ie.@: subtracted). For one list argument, just that list is
  965. returned.
  966. The given @var{=} procedure is used for comparing elements, called as
  967. @code{(@var{=} elem1 elemN)}. The first argument is from @var{list1}
  968. and the second from one of the subsequent lists. But exactly which
  969. calls are made and in what order is unspecified.
  970. @example
  971. (lset-difference eqv? '(x y)) @result{} (x y)
  972. (lset-difference eqv? '(1 2 3) '(3 1)) @result{} (2)
  973. (lset-difference eqv? '(1 2 3) '(3) '(2)) @result{} (1)
  974. @end example
  975. The return from @code{lset-difference} may share a tail with
  976. @var{list1}. @code{lset-difference!} may modify @var{list1} to form
  977. its result.
  978. @end deffn
  979. @deffn {Scheme Procedure} lset-diff+intersection = list1 list2 @dots{}
  980. @deffnx {Scheme Procedure} lset-diff+intersection! = list1 list2 @dots{}
  981. Return two values (@pxref{Multiple Values}), the difference and
  982. intersection of the argument lists as per @code{lset-difference} and
  983. @code{lset-intersection} above.
  984. For two list arguments this partitions @var{list1} into those elements
  985. of @var{list1} which are in @var{list2} and not in @var{list2}. (But
  986. for more than two arguments there can be elements of @var{list1} which
  987. are neither part of the difference nor the intersection.)
  988. One of the return values from @code{lset-diff+intersection} may share
  989. a tail with @var{list1}. @code{lset-diff+intersection!} may modify
  990. @var{list1} to form its results.
  991. @end deffn
  992. @deffn {Scheme Procedure} lset-xor = list @dots{}
  993. @deffnx {Scheme Procedure} lset-xor! = list @dots{}
  994. Return an XOR of the argument lists. For two lists this means those
  995. elements which are in exactly one of the lists. For more than two
  996. lists it means those elements which appear in an odd number of the
  997. lists.
  998. To be precise, the XOR of two lists @var{x} and @var{y} is formed by
  999. taking those elements of @var{x} not equal to any element of @var{y},
  1000. plus those elements of @var{y} not equal to any element of @var{x}.
  1001. Equality is determined with the given @var{=} procedure, called as
  1002. @code{(@var{=} e1 e2)}. One argument is from @var{x} and the other
  1003. from @var{y}, but which way around is unspecified. Exactly which
  1004. calls are made is also unspecified, as is the order of the elements in
  1005. the result.
  1006. @example
  1007. (lset-xor eqv? '(x y)) @result{} (x y)
  1008. (lset-xor eqv? '(1 2 3) '(4 3 2)) @result{} (4 1)
  1009. @end example
  1010. The return from @code{lset-xor} may share a tail with one of the list
  1011. arguments. @code{lset-xor!} may modify @var{list1} to form its
  1012. result.
  1013. @end deffn
  1014. @node SRFI-2
  1015. @subsection SRFI-2 - and-let*
  1016. @cindex SRFI-2
  1017. @noindent
  1018. The following syntax can be obtained with
  1019. @lisp
  1020. (use-modules (srfi srfi-2))
  1021. @end lisp
  1022. or alternatively
  1023. @lisp
  1024. (use-modules (ice-9 and-let-star))
  1025. @end lisp
  1026. @deffn {library syntax} and-let* (clause @dots{}) body @dots{}
  1027. A combination of @code{and} and @code{let*}.
  1028. Each @var{clause} is evaluated in turn, and if @code{#f} is obtained
  1029. then evaluation stops and @code{#f} is returned. If all are
  1030. non-@code{#f} then @var{body} is evaluated and the last form gives the
  1031. return value, or if @var{body} is empty then the result is @code{#t}.
  1032. Each @var{clause} should be one of the following,
  1033. @table @code
  1034. @item (symbol expr)
  1035. Evaluate @var{expr}, check for @code{#f}, and bind it to @var{symbol}.
  1036. Like @code{let*}, that binding is available to subsequent clauses.
  1037. @item (expr)
  1038. Evaluate @var{expr} and check for @code{#f}.
  1039. @item symbol
  1040. Get the value bound to @var{symbol} and check for @code{#f}.
  1041. @end table
  1042. Notice that @code{(expr)} has an ``extra'' pair of parentheses, for
  1043. instance @code{((eq? x y))}. One way to remember this is to imagine
  1044. the @code{symbol} in @code{(symbol expr)} is omitted.
  1045. @code{and-let*} is good for calculations where a @code{#f} value means
  1046. termination, but where a non-@code{#f} value is going to be needed in
  1047. subsequent expressions.
  1048. The following illustrates this, it returns text between brackets
  1049. @samp{[...]} in a string, or @code{#f} if there are no such brackets
  1050. (ie.@: either @code{string-index} gives @code{#f}).
  1051. @example
  1052. (define (extract-brackets str)
  1053. (and-let* ((start (string-index str #\[))
  1054. (end (string-index str #\] start)))
  1055. (substring str (1+ start) end)))
  1056. @end example
  1057. The following shows plain variables and expressions tested too.
  1058. @code{diagnostic-levels} is taken to be an alist associating a
  1059. diagnostic type with a level. @code{str} is printed only if the type
  1060. is known and its level is high enough.
  1061. @example
  1062. (define (show-diagnostic type str)
  1063. (and-let* (want-diagnostics
  1064. (level (assq-ref diagnostic-levels type))
  1065. ((>= level current-diagnostic-level)))
  1066. (display str)))
  1067. @end example
  1068. The advantage of @code{and-let*} is that an extended sequence of
  1069. expressions and tests doesn't require lots of nesting as would arise
  1070. from separate @code{and} and @code{let*}, or from @code{cond} with
  1071. @code{=>}.
  1072. @end deffn
  1073. @node SRFI-4
  1074. @subsection SRFI-4 - Homogeneous numeric vector datatypes
  1075. @cindex SRFI-4
  1076. SRFI-4 provides an interface to uniform numeric vectors: vectors whose elements
  1077. are all of a single numeric type. Guile offers uniform numeric vectors for
  1078. signed and unsigned 8-bit, 16-bit, 32-bit, and 64-bit integers, two sizes of
  1079. floating point values, and, as an extension to SRFI-4, complex floating-point
  1080. numbers of these two sizes.
  1081. The standard SRFI-4 procedures and data types may be included via loading the
  1082. appropriate module:
  1083. @example
  1084. (use-modules (srfi srfi-4))
  1085. @end example
  1086. This module is currently a part of the default Guile environment, but it is a
  1087. good practice to explicitly import the module. In the future, using SRFI-4
  1088. procedures without importing the SRFI-4 module will cause a deprecation message
  1089. to be printed. (Of course, one may call the C functions at any time. Would that
  1090. C had modules!)
  1091. @menu
  1092. * SRFI-4 Overview:: The warp and weft of uniform numeric vectors.
  1093. * SRFI-4 API:: Uniform vectors, from Scheme and from C.
  1094. * SRFI-4 and Bytevectors:: SRFI-4 vectors are backed by bytevectors.
  1095. * SRFI-4 Extensions:: Guile-specific extensions to the standard.
  1096. @end menu
  1097. @node SRFI-4 Overview
  1098. @subsubsection SRFI-4 - Overview
  1099. Uniform numeric vectors can be useful since they consume less memory
  1100. than the non-uniform, general vectors. Also, since the types they can
  1101. store correspond directly to C types, it is easier to work with them
  1102. efficiently on a low level. Consider image processing as an example,
  1103. where you want to apply a filter to some image. While you could store
  1104. the pixels of an image in a general vector and write a general
  1105. convolution function, things are much more efficient with uniform
  1106. vectors: the convolution function knows that all pixels are unsigned
  1107. 8-bit values (say), and can use a very tight inner loop.
  1108. This is implemented in Scheme by having the compiler notice calls to the SRFI-4
  1109. accessors, and inline them to appropriate compiled code. From C you have access
  1110. to the raw array; functions for efficiently working with uniform numeric vectors
  1111. from C are listed at the end of this section.
  1112. Uniform numeric vectors are the special case of one dimensional uniform
  1113. numeric arrays.
  1114. There are 12 standard kinds of uniform numeric vectors, and they all have their
  1115. own complement of constructors, accessors, and so on. Procedures that operate on
  1116. a specific kind of uniform numeric vector have a ``tag'' in their name,
  1117. indicating the element type.
  1118. @table @nicode
  1119. @item u8
  1120. unsigned 8-bit integers
  1121. @item s8
  1122. signed 8-bit integers
  1123. @item u16
  1124. unsigned 16-bit integers
  1125. @item s16
  1126. signed 16-bit integers
  1127. @item u32
  1128. unsigned 32-bit integers
  1129. @item s32
  1130. signed 32-bit integers
  1131. @item u64
  1132. unsigned 64-bit integers
  1133. @item s64
  1134. signed 64-bit integers
  1135. @item f32
  1136. the C type @code{float}
  1137. @item f64
  1138. the C type @code{double}
  1139. @end table
  1140. In addition, Guile supports uniform arrays of complex numbers, with the
  1141. nonstandard tags:
  1142. @table @nicode
  1143. @item c32
  1144. complex numbers in rectangular form with the real and imaginary part
  1145. being a @code{float}
  1146. @item c64
  1147. complex numbers in rectangular form with the real and imaginary part
  1148. being a @code{double}
  1149. @end table
  1150. The external representation (ie.@: read syntax) for these vectors is
  1151. similar to normal Scheme vectors, but with an additional tag from the
  1152. tables above indicating the vector's type. For example,
  1153. @lisp
  1154. #u16(1 2 3)
  1155. #f64(3.1415 2.71)
  1156. @end lisp
  1157. Note that the read syntax for floating-point here conflicts with
  1158. @code{#f} for false. In Standard Scheme one can write @code{(1 #f3)}
  1159. for a three element list @code{(1 #f 3)}, but for Guile @code{(1 #f3)}
  1160. is invalid. @code{(1 #f 3)} is almost certainly what one should write
  1161. anyway to make the intention clear, so this is rarely a problem.
  1162. @node SRFI-4 API
  1163. @subsubsection SRFI-4 - API
  1164. Note that the @nicode{c32} and @nicode{c64} functions are only available from
  1165. @nicode{(srfi srfi-4 gnu)}.
  1166. @deffn {Scheme Procedure} u8vector? obj
  1167. @deffnx {Scheme Procedure} s8vector? obj
  1168. @deffnx {Scheme Procedure} u16vector? obj
  1169. @deffnx {Scheme Procedure} s16vector? obj
  1170. @deffnx {Scheme Procedure} u32vector? obj
  1171. @deffnx {Scheme Procedure} s32vector? obj
  1172. @deffnx {Scheme Procedure} u64vector? obj
  1173. @deffnx {Scheme Procedure} s64vector? obj
  1174. @deffnx {Scheme Procedure} f32vector? obj
  1175. @deffnx {Scheme Procedure} f64vector? obj
  1176. @deffnx {Scheme Procedure} c32vector? obj
  1177. @deffnx {Scheme Procedure} c64vector? obj
  1178. @deffnx {C Function} scm_u8vector_p (obj)
  1179. @deffnx {C Function} scm_s8vector_p (obj)
  1180. @deffnx {C Function} scm_u16vector_p (obj)
  1181. @deffnx {C Function} scm_s16vector_p (obj)
  1182. @deffnx {C Function} scm_u32vector_p (obj)
  1183. @deffnx {C Function} scm_s32vector_p (obj)
  1184. @deffnx {C Function} scm_u64vector_p (obj)
  1185. @deffnx {C Function} scm_s64vector_p (obj)
  1186. @deffnx {C Function} scm_f32vector_p (obj)
  1187. @deffnx {C Function} scm_f64vector_p (obj)
  1188. @deffnx {C Function} scm_c32vector_p (obj)
  1189. @deffnx {C Function} scm_c64vector_p (obj)
  1190. Return @code{#t} if @var{obj} is a homogeneous numeric vector of the
  1191. indicated type.
  1192. @end deffn
  1193. @deffn {Scheme Procedure} make-u8vector n [value]
  1194. @deffnx {Scheme Procedure} make-s8vector n [value]
  1195. @deffnx {Scheme Procedure} make-u16vector n [value]
  1196. @deffnx {Scheme Procedure} make-s16vector n [value]
  1197. @deffnx {Scheme Procedure} make-u32vector n [value]
  1198. @deffnx {Scheme Procedure} make-s32vector n [value]
  1199. @deffnx {Scheme Procedure} make-u64vector n [value]
  1200. @deffnx {Scheme Procedure} make-s64vector n [value]
  1201. @deffnx {Scheme Procedure} make-f32vector n [value]
  1202. @deffnx {Scheme Procedure} make-f64vector n [value]
  1203. @deffnx {Scheme Procedure} make-c32vector n [value]
  1204. @deffnx {Scheme Procedure} make-c64vector n [value]
  1205. @deffnx {C Function} scm_make_u8vector (n, value)
  1206. @deffnx {C Function} scm_make_s8vector (n, value)
  1207. @deffnx {C Function} scm_make_u16vector (n, value)
  1208. @deffnx {C Function} scm_make_s16vector (n, value)
  1209. @deffnx {C Function} scm_make_u32vector (n, value)
  1210. @deffnx {C Function} scm_make_s32vector (n, value)
  1211. @deffnx {C Function} scm_make_u64vector (n, value)
  1212. @deffnx {C Function} scm_make_s64vector (n, value)
  1213. @deffnx {C Function} scm_make_f32vector (n, value)
  1214. @deffnx {C Function} scm_make_f64vector (n, value)
  1215. @deffnx {C Function} scm_make_c32vector (n, value)
  1216. @deffnx {C Function} scm_make_c64vector (n, value)
  1217. Return a newly allocated homogeneous numeric vector holding @var{n}
  1218. elements of the indicated type. If @var{value} is given, the vector
  1219. is initialized with that value, otherwise the contents are
  1220. unspecified.
  1221. @end deffn
  1222. @deffn {Scheme Procedure} u8vector value @dots{}
  1223. @deffnx {Scheme Procedure} s8vector value @dots{}
  1224. @deffnx {Scheme Procedure} u16vector value @dots{}
  1225. @deffnx {Scheme Procedure} s16vector value @dots{}
  1226. @deffnx {Scheme Procedure} u32vector value @dots{}
  1227. @deffnx {Scheme Procedure} s32vector value @dots{}
  1228. @deffnx {Scheme Procedure} u64vector value @dots{}
  1229. @deffnx {Scheme Procedure} s64vector value @dots{}
  1230. @deffnx {Scheme Procedure} f32vector value @dots{}
  1231. @deffnx {Scheme Procedure} f64vector value @dots{}
  1232. @deffnx {Scheme Procedure} c32vector value @dots{}
  1233. @deffnx {Scheme Procedure} c64vector value @dots{}
  1234. @deffnx {C Function} scm_u8vector (values)
  1235. @deffnx {C Function} scm_s8vector (values)
  1236. @deffnx {C Function} scm_u16vector (values)
  1237. @deffnx {C Function} scm_s16vector (values)
  1238. @deffnx {C Function} scm_u32vector (values)
  1239. @deffnx {C Function} scm_s32vector (values)
  1240. @deffnx {C Function} scm_u64vector (values)
  1241. @deffnx {C Function} scm_s64vector (values)
  1242. @deffnx {C Function} scm_f32vector (values)
  1243. @deffnx {C Function} scm_f64vector (values)
  1244. @deffnx {C Function} scm_c32vector (values)
  1245. @deffnx {C Function} scm_c64vector (values)
  1246. Return a newly allocated homogeneous numeric vector of the indicated
  1247. type, holding the given parameter @var{value}s. The vector length is
  1248. the number of parameters given.
  1249. @end deffn
  1250. @deffn {Scheme Procedure} u8vector-length vec
  1251. @deffnx {Scheme Procedure} s8vector-length vec
  1252. @deffnx {Scheme Procedure} u16vector-length vec
  1253. @deffnx {Scheme Procedure} s16vector-length vec
  1254. @deffnx {Scheme Procedure} u32vector-length vec
  1255. @deffnx {Scheme Procedure} s32vector-length vec
  1256. @deffnx {Scheme Procedure} u64vector-length vec
  1257. @deffnx {Scheme Procedure} s64vector-length vec
  1258. @deffnx {Scheme Procedure} f32vector-length vec
  1259. @deffnx {Scheme Procedure} f64vector-length vec
  1260. @deffnx {Scheme Procedure} c32vector-length vec
  1261. @deffnx {Scheme Procedure} c64vector-length vec
  1262. @deffnx {C Function} scm_u8vector_length (vec)
  1263. @deffnx {C Function} scm_s8vector_length (vec)
  1264. @deffnx {C Function} scm_u16vector_length (vec)
  1265. @deffnx {C Function} scm_s16vector_length (vec)
  1266. @deffnx {C Function} scm_u32vector_length (vec)
  1267. @deffnx {C Function} scm_s32vector_length (vec)
  1268. @deffnx {C Function} scm_u64vector_length (vec)
  1269. @deffnx {C Function} scm_s64vector_length (vec)
  1270. @deffnx {C Function} scm_f32vector_length (vec)
  1271. @deffnx {C Function} scm_f64vector_length (vec)
  1272. @deffnx {C Function} scm_c32vector_length (vec)
  1273. @deffnx {C Function} scm_c64vector_length (vec)
  1274. Return the number of elements in @var{vec}.
  1275. @end deffn
  1276. @deffn {Scheme Procedure} u8vector-ref vec i
  1277. @deffnx {Scheme Procedure} s8vector-ref vec i
  1278. @deffnx {Scheme Procedure} u16vector-ref vec i
  1279. @deffnx {Scheme Procedure} s16vector-ref vec i
  1280. @deffnx {Scheme Procedure} u32vector-ref vec i
  1281. @deffnx {Scheme Procedure} s32vector-ref vec i
  1282. @deffnx {Scheme Procedure} u64vector-ref vec i
  1283. @deffnx {Scheme Procedure} s64vector-ref vec i
  1284. @deffnx {Scheme Procedure} f32vector-ref vec i
  1285. @deffnx {Scheme Procedure} f64vector-ref vec i
  1286. @deffnx {Scheme Procedure} c32vector-ref vec i
  1287. @deffnx {Scheme Procedure} c64vector-ref vec i
  1288. @deffnx {C Function} scm_u8vector_ref (vec, i)
  1289. @deffnx {C Function} scm_s8vector_ref (vec, i)
  1290. @deffnx {C Function} scm_u16vector_ref (vec, i)
  1291. @deffnx {C Function} scm_s16vector_ref (vec, i)
  1292. @deffnx {C Function} scm_u32vector_ref (vec, i)
  1293. @deffnx {C Function} scm_s32vector_ref (vec, i)
  1294. @deffnx {C Function} scm_u64vector_ref (vec, i)
  1295. @deffnx {C Function} scm_s64vector_ref (vec, i)
  1296. @deffnx {C Function} scm_f32vector_ref (vec, i)
  1297. @deffnx {C Function} scm_f64vector_ref (vec, i)
  1298. @deffnx {C Function} scm_c32vector_ref (vec, i)
  1299. @deffnx {C Function} scm_c64vector_ref (vec, i)
  1300. Return the element at index @var{i} in @var{vec}. The first element
  1301. in @var{vec} is index 0.
  1302. @end deffn
  1303. @deffn {Scheme Procedure} u8vector-set! vec i value
  1304. @deffnx {Scheme Procedure} s8vector-set! vec i value
  1305. @deffnx {Scheme Procedure} u16vector-set! vec i value
  1306. @deffnx {Scheme Procedure} s16vector-set! vec i value
  1307. @deffnx {Scheme Procedure} u32vector-set! vec i value
  1308. @deffnx {Scheme Procedure} s32vector-set! vec i value
  1309. @deffnx {Scheme Procedure} u64vector-set! vec i value
  1310. @deffnx {Scheme Procedure} s64vector-set! vec i value
  1311. @deffnx {Scheme Procedure} f32vector-set! vec i value
  1312. @deffnx {Scheme Procedure} f64vector-set! vec i value
  1313. @deffnx {Scheme Procedure} c32vector-set! vec i value
  1314. @deffnx {Scheme Procedure} c64vector-set! vec i value
  1315. @deffnx {C Function} scm_u8vector_set_x (vec, i, value)
  1316. @deffnx {C Function} scm_s8vector_set_x (vec, i, value)
  1317. @deffnx {C Function} scm_u16vector_set_x (vec, i, value)
  1318. @deffnx {C Function} scm_s16vector_set_x (vec, i, value)
  1319. @deffnx {C Function} scm_u32vector_set_x (vec, i, value)
  1320. @deffnx {C Function} scm_s32vector_set_x (vec, i, value)
  1321. @deffnx {C Function} scm_u64vector_set_x (vec, i, value)
  1322. @deffnx {C Function} scm_s64vector_set_x (vec, i, value)
  1323. @deffnx {C Function} scm_f32vector_set_x (vec, i, value)
  1324. @deffnx {C Function} scm_f64vector_set_x (vec, i, value)
  1325. @deffnx {C Function} scm_c32vector_set_x (vec, i, value)
  1326. @deffnx {C Function} scm_c64vector_set_x (vec, i, value)
  1327. Set the element at index @var{i} in @var{vec} to @var{value}. The
  1328. first element in @var{vec} is index 0. The return value is
  1329. unspecified.
  1330. @end deffn
  1331. @deffn {Scheme Procedure} u8vector->list vec
  1332. @deffnx {Scheme Procedure} s8vector->list vec
  1333. @deffnx {Scheme Procedure} u16vector->list vec
  1334. @deffnx {Scheme Procedure} s16vector->list vec
  1335. @deffnx {Scheme Procedure} u32vector->list vec
  1336. @deffnx {Scheme Procedure} s32vector->list vec
  1337. @deffnx {Scheme Procedure} u64vector->list vec
  1338. @deffnx {Scheme Procedure} s64vector->list vec
  1339. @deffnx {Scheme Procedure} f32vector->list vec
  1340. @deffnx {Scheme Procedure} f64vector->list vec
  1341. @deffnx {Scheme Procedure} c32vector->list vec
  1342. @deffnx {Scheme Procedure} c64vector->list vec
  1343. @deffnx {C Function} scm_u8vector_to_list (vec)
  1344. @deffnx {C Function} scm_s8vector_to_list (vec)
  1345. @deffnx {C Function} scm_u16vector_to_list (vec)
  1346. @deffnx {C Function} scm_s16vector_to_list (vec)
  1347. @deffnx {C Function} scm_u32vector_to_list (vec)
  1348. @deffnx {C Function} scm_s32vector_to_list (vec)
  1349. @deffnx {C Function} scm_u64vector_to_list (vec)
  1350. @deffnx {C Function} scm_s64vector_to_list (vec)
  1351. @deffnx {C Function} scm_f32vector_to_list (vec)
  1352. @deffnx {C Function} scm_f64vector_to_list (vec)
  1353. @deffnx {C Function} scm_c32vector_to_list (vec)
  1354. @deffnx {C Function} scm_c64vector_to_list (vec)
  1355. Return a newly allocated list holding all elements of @var{vec}.
  1356. @end deffn
  1357. @deffn {Scheme Procedure} list->u8vector lst
  1358. @deffnx {Scheme Procedure} list->s8vector lst
  1359. @deffnx {Scheme Procedure} list->u16vector lst
  1360. @deffnx {Scheme Procedure} list->s16vector lst
  1361. @deffnx {Scheme Procedure} list->u32vector lst
  1362. @deffnx {Scheme Procedure} list->s32vector lst
  1363. @deffnx {Scheme Procedure} list->u64vector lst
  1364. @deffnx {Scheme Procedure} list->s64vector lst
  1365. @deffnx {Scheme Procedure} list->f32vector lst
  1366. @deffnx {Scheme Procedure} list->f64vector lst
  1367. @deffnx {Scheme Procedure} list->c32vector lst
  1368. @deffnx {Scheme Procedure} list->c64vector lst
  1369. @deffnx {C Function} scm_list_to_u8vector (lst)
  1370. @deffnx {C Function} scm_list_to_s8vector (lst)
  1371. @deffnx {C Function} scm_list_to_u16vector (lst)
  1372. @deffnx {C Function} scm_list_to_s16vector (lst)
  1373. @deffnx {C Function} scm_list_to_u32vector (lst)
  1374. @deffnx {C Function} scm_list_to_s32vector (lst)
  1375. @deffnx {C Function} scm_list_to_u64vector (lst)
  1376. @deffnx {C Function} scm_list_to_s64vector (lst)
  1377. @deffnx {C Function} scm_list_to_f32vector (lst)
  1378. @deffnx {C Function} scm_list_to_f64vector (lst)
  1379. @deffnx {C Function} scm_list_to_c32vector (lst)
  1380. @deffnx {C Function} scm_list_to_c64vector (lst)
  1381. Return a newly allocated homogeneous numeric vector of the indicated type,
  1382. initialized with the elements of the list @var{lst}.
  1383. @end deffn
  1384. @deftypefn {C Function} SCM scm_take_u8vector (const scm_t_uint8 *data, size_t len)
  1385. @deftypefnx {C Function} SCM scm_take_s8vector (const scm_t_int8 *data, size_t len)
  1386. @deftypefnx {C Function} SCM scm_take_u16vector (const scm_t_uint16 *data, size_t len)
  1387. @deftypefnx {C Function} SCM scm_take_s16vector (const scm_t_int16 *data, size_t len)
  1388. @deftypefnx {C Function} SCM scm_take_u32vector (const scm_t_uint32 *data, size_t len)
  1389. @deftypefnx {C Function} SCM scm_take_s32vector (const scm_t_int32 *data, size_t len)
  1390. @deftypefnx {C Function} SCM scm_take_u64vector (const scm_t_uint64 *data, size_t len)
  1391. @deftypefnx {C Function} SCM scm_take_s64vector (const scm_t_int64 *data, size_t len)
  1392. @deftypefnx {C Function} SCM scm_take_f32vector (const float *data, size_t len)
  1393. @deftypefnx {C Function} SCM scm_take_f64vector (const double *data, size_t len)
  1394. @deftypefnx {C Function} SCM scm_take_c32vector (const float *data, size_t len)
  1395. @deftypefnx {C Function} SCM scm_take_c64vector (const double *data, size_t len)
  1396. Return a new uniform numeric vector of the indicated type and length
  1397. that uses the memory pointed to by @var{data} to store its elements.
  1398. This memory will eventually be freed with @code{free}. The argument
  1399. @var{len} specifies the number of elements in @var{data}, not its size
  1400. in bytes.
  1401. The @code{c32} and @code{c64} variants take a pointer to a C array of
  1402. @code{float}s or @code{double}s. The real parts of the complex numbers
  1403. are at even indices in that array, the corresponding imaginary parts are
  1404. at the following odd index.
  1405. @end deftypefn
  1406. @deftypefn {C Function} {const scm_t_uint8 *} scm_u8vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1407. @deftypefnx {C Function} {const scm_t_int8 *} scm_s8vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1408. @deftypefnx {C Function} {const scm_t_uint16 *} scm_u16vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1409. @deftypefnx {C Function} {const scm_t_int16 *} scm_s16vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1410. @deftypefnx {C Function} {const scm_t_uint32 *} scm_u32vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1411. @deftypefnx {C Function} {const scm_t_int32 *} scm_s32vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1412. @deftypefnx {C Function} {const scm_t_uint64 *} scm_u64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1413. @deftypefnx {C Function} {const scm_t_int64 *} scm_s64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1414. @deftypefnx {C Function} {const float *} scm_f32vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1415. @deftypefnx {C Function} {const double *} scm_f64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1416. @deftypefnx {C Function} {const float *} scm_c32vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1417. @deftypefnx {C Function} {const double *} scm_c64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1418. Like @code{scm_vector_elements} (@pxref{Vector Accessing from C}), but
  1419. returns a pointer to the elements of a uniform numeric vector of the
  1420. indicated kind.
  1421. @end deftypefn
  1422. @deftypefn {C Function} {scm_t_uint8 *} scm_u8vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1423. @deftypefnx {C Function} {scm_t_int8 *} scm_s8vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1424. @deftypefnx {C Function} {scm_t_uint16 *} scm_u16vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1425. @deftypefnx {C Function} {scm_t_int16 *} scm_s16vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1426. @deftypefnx {C Function} {scm_t_uint32 *} scm_u32vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1427. @deftypefnx {C Function} {scm_t_int32 *} scm_s32vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1428. @deftypefnx {C Function} {scm_t_uint64 *} scm_u64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1429. @deftypefnx {C Function} {scm_t_int64 *} scm_s64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1430. @deftypefnx {C Function} {float *} scm_f32vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1431. @deftypefnx {C Function} {double *} scm_f64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1432. @deftypefnx {C Function} {float *} scm_c32vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1433. @deftypefnx {C Function} {double *} scm_c64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1434. Like @code{scm_vector_writable_elements} (@pxref{Vector Accessing from C}),
  1435. but returns a pointer to the elements of a uniform numeric vector of the
  1436. indicated kind.
  1437. @end deftypefn
  1438. @node SRFI-4 and Bytevectors
  1439. @subsubsection SRFI-4 - Relation to bytevectors
  1440. Guile implements SRFI-4 vectors using bytevectors (@pxref{Bytevectors}). Often
  1441. when you have a numeric vector, you end up wanting to write its bytes somewhere,
  1442. or have access to the underlying bytes, or read in bytes from somewhere else.
  1443. Bytevectors are very good at this sort of thing. But the SRFI-4 APIs are nicer
  1444. to use when doing number-crunching, because they are addressed by element and
  1445. not by byte.
  1446. So as a compromise, Guile allows all bytevector functions to operate on numeric
  1447. vectors. They address the underlying bytes in the native endianness, as one
  1448. would expect.
  1449. Following the same reasoning, that it's just bytes underneath, Guile also allows
  1450. uniform vectors of a given type to be accessed as if they were of any type. One
  1451. can fill a @nicode{u32vector}, and access its elements with
  1452. @nicode{u8vector-ref}. One can use @nicode{f64vector-ref} on bytevectors. It's
  1453. all the same to Guile.
  1454. In this way, uniform numeric vectors may be written to and read from
  1455. input/output ports using the procedures that operate on bytevectors.
  1456. @xref{Bytevectors}, for more information.
  1457. @node SRFI-4 Extensions
  1458. @subsubsection SRFI-4 - Guile extensions
  1459. Guile defines some useful extensions to SRFI-4, which are not available in the
  1460. default Guile environment. They may be imported by loading the extensions
  1461. module:
  1462. @example
  1463. (use-modules (srfi srfi-4 gnu))
  1464. @end example
  1465. @deffn {Scheme Procedure} srfi-4-vector-type-size obj
  1466. Return the size, in bytes, of each element of SRFI-4 vector
  1467. @var{obj}. For example, @code{(srfi-4-vector-type-size #u32())} returns
  1468. @code{4}.
  1469. @end deffn
  1470. @deffn {Scheme Procedure} any->u8vector obj
  1471. @deffnx {Scheme Procedure} any->s8vector obj
  1472. @deffnx {Scheme Procedure} any->u16vector obj
  1473. @deffnx {Scheme Procedure} any->s16vector obj
  1474. @deffnx {Scheme Procedure} any->u32vector obj
  1475. @deffnx {Scheme Procedure} any->s32vector obj
  1476. @deffnx {Scheme Procedure} any->u64vector obj
  1477. @deffnx {Scheme Procedure} any->s64vector obj
  1478. @deffnx {Scheme Procedure} any->f32vector obj
  1479. @deffnx {Scheme Procedure} any->f64vector obj
  1480. @deffnx {Scheme Procedure} any->c32vector obj
  1481. @deffnx {Scheme Procedure} any->c64vector obj
  1482. @deffnx {C Function} scm_any_to_u8vector (obj)
  1483. @deffnx {C Function} scm_any_to_s8vector (obj)
  1484. @deffnx {C Function} scm_any_to_u16vector (obj)
  1485. @deffnx {C Function} scm_any_to_s16vector (obj)
  1486. @deffnx {C Function} scm_any_to_u32vector (obj)
  1487. @deffnx {C Function} scm_any_to_s32vector (obj)
  1488. @deffnx {C Function} scm_any_to_u64vector (obj)
  1489. @deffnx {C Function} scm_any_to_s64vector (obj)
  1490. @deffnx {C Function} scm_any_to_f32vector (obj)
  1491. @deffnx {C Function} scm_any_to_f64vector (obj)
  1492. @deffnx {C Function} scm_any_to_c32vector (obj)
  1493. @deffnx {C Function} scm_any_to_c64vector (obj)
  1494. Return a (maybe newly allocated) uniform numeric vector of the indicated
  1495. type, initialized with the elements of @var{obj}, which must be a list,
  1496. a vector, or a uniform vector. When @var{obj} is already a suitable
  1497. uniform numeric vector, it is returned unchanged.
  1498. @end deffn
  1499. @deffn {Scheme Procedure} u8vector-copy! dst at src [start [end]]
  1500. @deffnx {Scheme Procedure} s8vector-copy! dst at src [start [end]]
  1501. @deffnx {Scheme Procedure} u16vector-copy! dst at src [start [end]]
  1502. @deffnx {Scheme Procedure} s16vector-copy! dst at src [start [end]]
  1503. @deffnx {Scheme Procedure} u32vector-copy! dst at src [start [end]]
  1504. @deffnx {Scheme Procedure} s32vector-copy! dst at src [start [end]]
  1505. @deffnx {Scheme Procedure} u64vector-copy! dst at src [start [end]]
  1506. @deffnx {Scheme Procedure} s64vector-copy! dst at src [start [end]]
  1507. @deffnx {Scheme Procedure} f32vector-copy! dst at src [start [end]]
  1508. @deffnx {Scheme Procedure} f64vector-copy! dst at src [start [end]]
  1509. @deffnx {Scheme Procedure} c32vector-copy! dst at src [start [end]]
  1510. @deffnx {Scheme Procedure} c64vector-copy! dst at src [start [end]]
  1511. Copy a block of elements from @var{src} to @var{dst}, both of which must
  1512. be vectors of the indicated type, starting in @var{dst} at @var{at} and
  1513. starting in @var{src} at @var{start} and ending at @var{end}. It is an
  1514. error for @var{dst} to have a length less than @var{at} + (@var{end} -
  1515. @var{start}). @var{at} and @var{start} default to 0 and @var{end}
  1516. defaults to the length of @var{src}.
  1517. If source and destination overlap, copying takes place as if the
  1518. source is first copied into a temporary vector and then into the
  1519. destination.
  1520. See also @ref{x-vector-copy!,@code{vector-copy!}}.
  1521. @end deffn
  1522. @deffn {Scheme Procedure} u8vector-copy src [start [end]]
  1523. @deffnx {Scheme Procedure} s8vector-copy src [start [end]]
  1524. @deffnx {Scheme Procedure} u16vector-copy src [start [end]]
  1525. @deffnx {Scheme Procedure} s16vector-copy src [start [end]]
  1526. @deffnx {Scheme Procedure} u32vector-copy src [start [end]]
  1527. @deffnx {Scheme Procedure} s32vector-copy src [start [end]]
  1528. @deffnx {Scheme Procedure} u64vector-copy src [start [end]]
  1529. @deffnx {Scheme Procedure} s64vector-copy src [start [end]]
  1530. @deffnx {Scheme Procedure} f32vector-copy src [start [end]]
  1531. @deffnx {Scheme Procedure} f64vector-copy src [start [end]]
  1532. @deffnx {Scheme Procedure} c32vector-copy src [start [end]]
  1533. @deffnx {Scheme Procedure} c64vector-copy src [start [end]]
  1534. Returns a freshly allocated vector of the indicated type, which must be
  1535. the same as that of @var{src}, containing the elements of @var{src}
  1536. between @var{start} and @var{end}. @var{start} defaults to 0 and
  1537. @var{end} defaults to the length of @var{src}.
  1538. See also @ref{x-vector-copy,@code{vector-copy}}.
  1539. @end deffn
  1540. @node SRFI-6
  1541. @subsection SRFI-6 - Basic String Ports
  1542. @cindex SRFI-6
  1543. SRFI-6 defines the procedures @code{open-input-string},
  1544. @code{open-output-string} and @code{get-output-string}. These
  1545. procedures are included in the Guile core, so using this module does not
  1546. make any difference at the moment. But it is possible that support for
  1547. SRFI-6 will be factored out of the core library in the future, so using
  1548. this module does not hurt, after all.
  1549. @node SRFI-8
  1550. @subsection SRFI-8 - receive
  1551. @cindex SRFI-8
  1552. @code{receive} is a syntax for making the handling of multiple-value
  1553. procedures easier. It is documented in @xref{Multiple Values}.
  1554. @node SRFI-9
  1555. @subsection SRFI-9 - define-record-type
  1556. This SRFI is a syntax for defining new record types and creating
  1557. predicate, constructor, and field getter and setter functions. It is
  1558. documented in the ``Data Types'' section of the manual (@pxref{SRFI-9
  1559. Records}).
  1560. @node SRFI-10
  1561. @subsection SRFI-10 - Hash-Comma Reader Extension
  1562. @cindex SRFI-10
  1563. @cindex hash-comma
  1564. @cindex #,()
  1565. This SRFI implements a reader extension @code{#,()} called hash-comma.
  1566. It allows the reader to give new kinds of objects, for use both in data
  1567. and as constants or literals in source code. This feature is available
  1568. with
  1569. @example
  1570. (use-modules (srfi srfi-10))
  1571. @end example
  1572. @noindent
  1573. The new read syntax is of the form
  1574. @example
  1575. #,(@var{tag} @var{arg}@dots{})
  1576. @end example
  1577. @noindent
  1578. where @var{tag} is a symbol and the @var{arg}s are objects taken as
  1579. parameters. @var{tag}s are registered with the following procedure.
  1580. @deffn {Scheme Procedure} define-reader-ctor tag proc
  1581. Register @var{proc} as the constructor for a hash-comma read syntax
  1582. starting with symbol @var{tag}, i.e.@: @nicode{#,(@var{tag} arg@dots{})}.
  1583. @var{proc} is called with the given arguments @code{(@var{proc}
  1584. arg@dots{})} and the object it returns is the result of the read.
  1585. @end deffn
  1586. @noindent
  1587. For example, a syntax giving a list of @var{N} copies of an object.
  1588. @example
  1589. (define-reader-ctor 'repeat
  1590. (lambda (obj reps)
  1591. (make-list reps obj)))
  1592. (display '#,(repeat 99 3))
  1593. @print{} (99 99 99)
  1594. @end example
  1595. Notice the quote @nicode{'} when the @nicode{#,( )} is used. The
  1596. @code{repeat} handler returns a list and the program must quote to use
  1597. it literally, the same as any other list. Ie.
  1598. @example
  1599. (display '#,(repeat 99 3))
  1600. @result{}
  1601. (display '(99 99 99))
  1602. @end example
  1603. When a handler returns an object which is self-evaluating, like a
  1604. number or a string, then there's no need for quoting, just as there's
  1605. no need when giving those directly as literals. For example an
  1606. addition,
  1607. @example
  1608. (define-reader-ctor 'sum
  1609. (lambda (x y)
  1610. (+ x y)))
  1611. (display #,(sum 123 456)) @print{} 579
  1612. @end example
  1613. Once @code{(srfi srfi-10)} has loaded, @nicode{#,()} is available
  1614. globally, there's no need to use @code{(srfi srfi-10)} in later
  1615. modules. Similarly the tags registered are global and can be used
  1616. anywhere once registered.
  1617. We do not recommend @nicode{#,()} reader extensions, however, and for
  1618. three reasons.
  1619. First of all, this SRFI is not modular: the tag is matched by name, not
  1620. as an identifier within a scope. Defining a reader extension in one
  1621. part of a program can thus affect unrelated parts of a program because
  1622. the tag is not scoped.
  1623. Secondly, reader extensions can be hard to manage from a time
  1624. perspective: when does the reader extension take effect? @xref{Eval
  1625. When}, for more discussion.
  1626. Finally, reader extensions can easily produce objects that can't be
  1627. reified to an object file by the compiler. For example if you define a
  1628. reader extension that makes a hash table (@pxref{Hash Tables}), then it
  1629. will work fine when run with the interpreter, and you think you have a
  1630. neat hack. But then if you try to compile your program, after wrangling
  1631. with the @code{eval-when} concerns mentioned above, the compiler will
  1632. carp that it doesn't know how to serialize a hash table to disk.
  1633. In the specific case of hash tables, it would be possible for Guile to
  1634. know how to pack hash tables into compiled files, but this doesn't work
  1635. in general. What if the object you produce is an instance of a record
  1636. type? Guile would then have to serialize the record type to disk too,
  1637. and then what happens if the program independently loads the code that
  1638. defines the record type? Does it define the same type or a different
  1639. type? Guile's record types are nominal, not structural, so the answer
  1640. is not clear at all.
  1641. For all of these reasons we recommend macros over reader extensions.
  1642. Macros fulfill many of the same needs while preserving modular
  1643. composition, and their interaction with @code{eval-when} is well-known.
  1644. If you need brevity, instead use @code{read-hash-extend} and make your
  1645. reader extension expand to a macro invocation. In that way we preserve
  1646. scoping as much as possible. @xref{Reader Extensions}.
  1647. @node SRFI-11
  1648. @subsection SRFI-11 - let-values
  1649. @cindex SRFI-11
  1650. @findex let-values
  1651. @findex let*-values
  1652. This module implements the binding forms for multiple values
  1653. @code{let-values} and @code{let*-values}. These forms are similar to
  1654. @code{let} and @code{let*} (@pxref{Local Bindings}), but they support
  1655. binding of the values returned by multiple-valued expressions.
  1656. Write @code{(use-modules (srfi srfi-11))} to make the bindings
  1657. available.
  1658. @lisp
  1659. (let-values (((x y) (values 1 2))
  1660. ((z f) (values 3 4)))
  1661. (+ x y z f))
  1662. @result{}
  1663. 10
  1664. @end lisp
  1665. @code{let-values} performs all bindings simultaneously, which means that
  1666. no expression in the binding clauses may refer to variables bound in the
  1667. same clause list. @code{let*-values}, on the other hand, performs the
  1668. bindings sequentially, just like @code{let*} does for single-valued
  1669. expressions.
  1670. @node SRFI-13
  1671. @subsection SRFI-13 - String Library
  1672. @cindex SRFI-13
  1673. The SRFI-13 procedures are always available, @xref{Strings}.
  1674. @node SRFI-14
  1675. @subsection SRFI-14 - Character-set Library
  1676. @cindex SRFI-14
  1677. The SRFI-14 data type and procedures are always available,
  1678. @xref{Character Sets}.
  1679. @node SRFI-16
  1680. @subsection SRFI-16 - case-lambda
  1681. @cindex SRFI-16
  1682. @cindex variable arity
  1683. @cindex arity, variable
  1684. SRFI-16 defines a variable-arity @code{lambda} form,
  1685. @code{case-lambda}. This form is available in the default Guile
  1686. environment. @xref{Case-lambda}, for more information.
  1687. @node SRFI-17
  1688. @subsection SRFI-17 - Generalized set!
  1689. @cindex SRFI-17
  1690. This SRFI implements a generalized @code{set!}, allowing some
  1691. ``referencing'' functions to be used as the target location of a
  1692. @code{set!}. This feature is available from
  1693. @example
  1694. (use-modules (srfi srfi-17))
  1695. @end example
  1696. @noindent
  1697. For example @code{vector-ref} is extended so that
  1698. @example
  1699. (set! (vector-ref vec idx) new-value)
  1700. @end example
  1701. @noindent
  1702. is equivalent to
  1703. @example
  1704. (vector-set! vec idx new-value)
  1705. @end example
  1706. The idea is that a @code{vector-ref} expression identifies a location,
  1707. which may be either fetched or stored. The same form is used for the
  1708. location in both cases, encouraging visual clarity. This is similar
  1709. to the idea of an ``lvalue'' in C.
  1710. The mechanism for this kind of @code{set!} is in the Guile core
  1711. (@pxref{Procedures with Setters}). This module adds definitions of
  1712. the following functions as procedures with setters, allowing them to
  1713. be targets of a @code{set!},
  1714. @quotation
  1715. @nicode{car}, @nicode{cdr}, @nicode{caar}, @nicode{cadr},
  1716. @nicode{cdar}, @nicode{cddr}, @nicode{caaar}, @nicode{caadr},
  1717. @nicode{cadar}, @nicode{caddr}, @nicode{cdaar}, @nicode{cdadr},
  1718. @nicode{cddar}, @nicode{cdddr}, @nicode{caaaar}, @nicode{caaadr},
  1719. @nicode{caadar}, @nicode{caaddr}, @nicode{cadaar}, @nicode{cadadr},
  1720. @nicode{caddar}, @nicode{cadddr}, @nicode{cdaaar}, @nicode{cdaadr},
  1721. @nicode{cdadar}, @nicode{cdaddr}, @nicode{cddaar}, @nicode{cddadr},
  1722. @nicode{cdddar}, @nicode{cddddr}
  1723. @nicode{string-ref}, @nicode{vector-ref}
  1724. @end quotation
  1725. The SRFI specifies @code{setter} (@pxref{Procedures with Setters}) as
  1726. a procedure with setter, allowing the setter for a procedure to be
  1727. changed, eg.@: @code{(set! (setter foo) my-new-setter-handler)}.
  1728. Currently Guile does not implement this, a setter can only be
  1729. specified on creation (@code{getter-with-setter} below).
  1730. @defun getter-with-setter
  1731. The same as the Guile core @code{make-procedure-with-setter}
  1732. (@pxref{Procedures with Setters}).
  1733. @end defun
  1734. @node SRFI-18
  1735. @subsection SRFI-18 - Multithreading support
  1736. @cindex SRFI-18
  1737. This is an implementation of the SRFI-18 threading and synchronization
  1738. library. The functions and variables described here are provided by
  1739. @example
  1740. (use-modules (srfi srfi-18))
  1741. @end example
  1742. SRFI-18 defines facilities for threads, mutexes, condition variables,
  1743. time, and exception handling. Because these facilities are at a higher
  1744. level than Guile's primitives, they are implemented as a layer on top of
  1745. what Guile provides. In particular this means that a Guile mutex is not
  1746. a SRFI-18 mutex, and a Guile thread is not a SRFI-18 thread, and so on.
  1747. Guile provides a set of primitives and SRFI-18 is one of the systems built in terms of those primitives.
  1748. @menu
  1749. * SRFI-18 Threads:: Executing code
  1750. * SRFI-18 Mutexes:: Mutual exclusion devices
  1751. * SRFI-18 Condition variables:: Synchronizing of groups of threads
  1752. * SRFI-18 Time:: Representation of times and durations
  1753. * SRFI-18 Exceptions:: Signalling and handling errors
  1754. @end menu
  1755. @node SRFI-18 Threads
  1756. @subsubsection SRFI-18 Threads
  1757. Threads created by SRFI-18 differ in two ways from threads created by
  1758. Guile's built-in thread functions. First, a thread created by SRFI-18
  1759. @code{make-thread} begins in a blocked state and will not start
  1760. execution until @code{thread-start!} is called on it. Second, SRFI-18
  1761. threads are constructed with a top-level exception handler that
  1762. captures any exceptions that are thrown on thread exit.
  1763. SRFI-18 threads are disjoint from Guile's primitive threads.
  1764. @xref{Threads}, for more on Guile's primitive facility.
  1765. @defun current-thread
  1766. Returns the thread that called this function. This is the same
  1767. procedure as the same-named built-in procedure @code{current-thread}
  1768. (@pxref{Threads}).
  1769. @end defun
  1770. @defun thread? obj
  1771. Returns @code{#t} if @var{obj} is a thread, @code{#f} otherwise. This
  1772. is the same procedure as the same-named built-in procedure
  1773. @code{thread?} (@pxref{Threads}).
  1774. @end defun
  1775. @defun make-thread thunk [name]
  1776. Call @code{thunk} in a new thread and with a new dynamic state,
  1777. returning the new thread and optionally assigning it the object name
  1778. @var{name}, which may be any Scheme object.
  1779. Note that the name @code{make-thread} conflicts with the
  1780. @code{(ice-9 threads)} function @code{make-thread}. Applications
  1781. wanting to use both of these functions will need to refer to them by
  1782. different names.
  1783. @end defun
  1784. @defun thread-name thread
  1785. Returns the name assigned to @var{thread} at the time of its creation,
  1786. or @code{#f} if it was not given a name.
  1787. @end defun
  1788. @defun thread-specific thread
  1789. @defunx thread-specific-set! thread obj
  1790. Get or set the ``object-specific'' property of @var{thread}. In
  1791. Guile's implementation of SRFI-18, this value is stored as an object
  1792. property, and will be @code{#f} if not set.
  1793. @end defun
  1794. @defun thread-start! thread
  1795. Unblocks @var{thread} and allows it to begin execution if it has not
  1796. done so already.
  1797. @end defun
  1798. @defun thread-yield!
  1799. If one or more threads are waiting to execute, calling
  1800. @code{thread-yield!} forces an immediate context switch to one of them.
  1801. Otherwise, @code{thread-yield!} has no effect. @code{thread-yield!}
  1802. behaves identically to the Guile built-in function @code{yield}.
  1803. @end defun
  1804. @defun thread-sleep! timeout
  1805. The current thread waits until the point specified by the time object
  1806. @var{timeout} is reached (@pxref{SRFI-18 Time}). This blocks the
  1807. thread only if @var{timeout} represents a point in the future. it is
  1808. an error for @var{timeout} to be @code{#f}.
  1809. @end defun
  1810. @defun thread-terminate! thread
  1811. Causes an abnormal termination of @var{thread}. If @var{thread} is
  1812. not already terminated, all mutexes owned by @var{thread} become
  1813. unlocked/abandoned. If @var{thread} is the current thread,
  1814. @code{thread-terminate!} does not return. Otherwise
  1815. @code{thread-terminate!} returns an unspecified value; the termination
  1816. of @var{thread} will occur before @code{thread-terminate!} returns.
  1817. Subsequent attempts to join on @var{thread} will cause a ``terminated
  1818. thread exception'' to be raised.
  1819. @code{thread-terminate!} is compatible with the thread cancellation
  1820. procedures in the core threads API (@pxref{Threads}) in that if a
  1821. cleanup handler has been installed for the target thread, it will be
  1822. called before the thread exits and its return value (or exception, if
  1823. any) will be stored for later retrieval via a call to
  1824. @code{thread-join!}.
  1825. @end defun
  1826. @defun thread-join! thread [timeout [timeout-val]]
  1827. Wait for @var{thread} to terminate and return its exit value. When a
  1828. time value @var{timeout} is given, it specifies a point in time where
  1829. the waiting should be aborted. When the waiting is aborted,
  1830. @var{timeout-val} is returned if it is specified; otherwise, a
  1831. @code{join-timeout-exception} exception is raised
  1832. (@pxref{SRFI-18 Exceptions}). Exceptions may also be raised if the
  1833. thread was terminated by a call to @code{thread-terminate!}
  1834. (@code{terminated-thread-exception} will be raised) or if the thread
  1835. exited by raising an exception that was handled by the top-level
  1836. exception handler (@code{uncaught-exception} will be raised; the
  1837. original exception can be retrieved using
  1838. @code{uncaught-exception-reason}).
  1839. @end defun
  1840. @node SRFI-18 Mutexes
  1841. @subsubsection SRFI-18 Mutexes
  1842. SRFI-18 mutexes are disjoint from Guile's primitive mutexes.
  1843. @xref{Mutexes and Condition Variables}, for more on Guile's primitive
  1844. facility.
  1845. @defun make-mutex [name]
  1846. Returns a new mutex, optionally assigning it the object name @var{name},
  1847. which may be any Scheme object. The returned mutex will be created with
  1848. the configuration described above.
  1849. @end defun
  1850. @defun mutex-name mutex
  1851. Returns the name assigned to @var{mutex} at the time of its creation, or
  1852. @code{#f} if it was not given a name.
  1853. @end defun
  1854. @defun mutex-specific mutex
  1855. Return the ``object-specific'' property of @var{mutex}, or @code{#f} if
  1856. none is set.
  1857. @end defun
  1858. @defun mutex-specific-set! mutex obj
  1859. Set the ``object-specific'' property of @var{mutex}.
  1860. @end defun
  1861. @defun mutex-state mutex
  1862. Returns information about the state of @var{mutex}. Possible values
  1863. are:
  1864. @itemize @bullet
  1865. @item
  1866. thread @var{t}: the mutex is in the locked/owned state and thread
  1867. @var{t} is the owner of the mutex
  1868. @item
  1869. symbol @code{not-owned}: the mutex is in the locked/not-owned state
  1870. @item
  1871. symbol @code{abandoned}: the mutex is in the unlocked/abandoned state
  1872. @item
  1873. symbol @code{not-abandoned}: the mutex is in the
  1874. unlocked/not-abandoned state
  1875. @end itemize
  1876. @end defun
  1877. @defun mutex-lock! mutex [timeout [thread]]
  1878. Lock @var{mutex}, optionally specifying a time object @var{timeout}
  1879. after which to abort the lock attempt and a thread @var{thread} giving
  1880. a new owner for @var{mutex} different than the current thread.
  1881. @end defun
  1882. @defun mutex-unlock! mutex [condition-variable [timeout]]
  1883. Unlock @var{mutex}, optionally specifying a condition variable
  1884. @var{condition-variable} on which to wait, either indefinitely or,
  1885. optionally, until the time object @var{timeout} has passed, to be
  1886. signalled.
  1887. @end defun
  1888. @node SRFI-18 Condition variables
  1889. @subsubsection SRFI-18 Condition variables
  1890. SRFI-18 does not specify a ``wait'' function for condition variables.
  1891. Waiting on a condition variable can be simulated using the SRFI-18
  1892. @code{mutex-unlock!} function described in the previous section.
  1893. SRFI-18 condition variables are disjoint from Guile's primitive
  1894. condition variables. @xref{Mutexes and Condition Variables}, for more
  1895. on Guile's primitive facility.
  1896. @defun condition-variable? obj
  1897. Returns @code{#t} if @var{obj} is a condition variable, @code{#f}
  1898. otherwise.
  1899. @end defun
  1900. @defun make-condition-variable [name]
  1901. Returns a new condition variable, optionally assigning it the object
  1902. name @var{name}, which may be any Scheme object.
  1903. @end defun
  1904. @defun condition-variable-name condition-variable
  1905. Returns the name assigned to @var{condition-variable} at the time of its
  1906. creation, or @code{#f} if it was not given a name.
  1907. @end defun
  1908. @defun condition-variable-specific condition-variable
  1909. Return the ``object-specific'' property of @var{condition-variable}, or
  1910. @code{#f} if none is set.
  1911. @end defun
  1912. @defun condition-variable-specific-set! condition-variable obj
  1913. Set the ``object-specific'' property of @var{condition-variable}.
  1914. @end defun
  1915. @defun condition-variable-signal! condition-variable
  1916. @defunx condition-variable-broadcast! condition-variable
  1917. Wake up one thread that is waiting for @var{condition-variable}, in
  1918. the case of @code{condition-variable-signal!}, or all threads waiting
  1919. for it, in the case of @code{condition-variable-broadcast!}.
  1920. @end defun
  1921. @node SRFI-18 Time
  1922. @subsubsection SRFI-18 Time
  1923. The SRFI-18 time functions manipulate time in two formats: a
  1924. ``time object'' type that represents an absolute point in time in some
  1925. implementation-specific way; and the number of seconds since some
  1926. unspecified ``epoch''. In Guile's implementation, the epoch is the
  1927. Unix epoch, 00:00:00 UTC, January 1, 1970.
  1928. @defun current-time
  1929. Return the current time as a time object. This procedure replaces
  1930. the procedure of the same name in the core library, which returns the
  1931. current time in seconds since the epoch.
  1932. @end defun
  1933. @defun time? obj
  1934. Returns @code{#t} if @var{obj} is a time object, @code{#f} otherwise.
  1935. @end defun
  1936. @defun time->seconds time
  1937. @defunx seconds->time seconds
  1938. Convert between time objects and numerical values representing the
  1939. number of seconds since the epoch. When converting from a time object
  1940. to seconds, the return value is the number of seconds between
  1941. @var{time} and the epoch. When converting from seconds to a time
  1942. object, the return value is a time object that represents a time
  1943. @var{seconds} seconds after the epoch.
  1944. @end defun
  1945. @node SRFI-18 Exceptions
  1946. @subsubsection SRFI-18 Exceptions
  1947. SRFI-18 exceptions are identical to the exceptions provided by
  1948. Guile's implementation of SRFI-34. The behavior of exception
  1949. handlers invoked to handle exceptions thrown from SRFI-18 functions,
  1950. however, differs from the conventional behavior of SRFI-34 in that
  1951. the continuation of the handler is the same as that of the call to
  1952. the function. Handlers are called in a tail-recursive manner; the
  1953. exceptions do not ``bubble up''.
  1954. @defun current-exception-handler
  1955. Returns the current exception handler.
  1956. @end defun
  1957. @defun with-exception-handler handler thunk
  1958. Installs @var{handler} as the current exception handler and calls the
  1959. procedure @var{thunk} with no arguments, returning its value as the
  1960. value of the exception. @var{handler} must be a procedure that accepts
  1961. a single argument. The current exception handler at the time this
  1962. procedure is called will be restored after the call returns.
  1963. @end defun
  1964. @defun raise obj
  1965. Raise @var{obj} as an exception. This is the same procedure as the
  1966. same-named procedure defined in SRFI 34.
  1967. @end defun
  1968. @defun join-timeout-exception? obj
  1969. Returns @code{#t} if @var{obj} is an exception raised as the result of
  1970. performing a timed join on a thread that does not exit within the
  1971. specified timeout, @code{#f} otherwise.
  1972. @end defun
  1973. @defun abandoned-mutex-exception? obj
  1974. Returns @code{#t} if @var{obj} is an exception raised as the result of
  1975. attempting to lock a mutex that has been abandoned by its owner thread,
  1976. @code{#f} otherwise.
  1977. @end defun
  1978. @defun terminated-thread-exception? obj
  1979. Returns @code{#t} if @var{obj} is an exception raised as the result of
  1980. joining on a thread that exited as the result of a call to
  1981. @code{thread-terminate!}.
  1982. @end defun
  1983. @defun uncaught-exception? obj
  1984. @defunx uncaught-exception-reason exc
  1985. @code{uncaught-exception?} returns @code{#t} if @var{obj} is an
  1986. exception thrown as the result of joining a thread that exited by
  1987. raising an exception that was handled by the top-level exception
  1988. handler installed by @code{make-thread}. When this occurs, the
  1989. original exception is preserved as part of the exception thrown by
  1990. @code{thread-join!} and can be accessed by calling
  1991. @code{uncaught-exception-reason} on that exception. Note that
  1992. because this exception-preservation mechanism is a side-effect of
  1993. @code{make-thread}, joining on threads that exited as described above
  1994. but were created by other means will not raise this
  1995. @code{uncaught-exception} error.
  1996. @end defun
  1997. @node SRFI-19
  1998. @subsection SRFI-19 - Time/Date Library
  1999. @cindex SRFI-19
  2000. @cindex time
  2001. @cindex date
  2002. This is an implementation of the SRFI-19 time/date library. The
  2003. functions and variables described here are provided by
  2004. @example
  2005. (use-modules (srfi srfi-19))
  2006. @end example
  2007. @menu
  2008. * SRFI-19 Introduction::
  2009. * SRFI-19 Time::
  2010. * SRFI-19 Date::
  2011. * SRFI-19 Time/Date conversions::
  2012. * SRFI-19 Date to string::
  2013. * SRFI-19 String to date::
  2014. @end menu
  2015. @node SRFI-19 Introduction
  2016. @subsubsection SRFI-19 Introduction
  2017. @cindex universal time
  2018. @cindex atomic time
  2019. @cindex UTC
  2020. @cindex TAI
  2021. This module implements time and date representations and calculations,
  2022. in various time systems, including Coordinated Universal Time (UTC)
  2023. and International Atomic Time (TAI).
  2024. For those not familiar with these time systems, TAI is based on a
  2025. fixed length second derived from oscillations of certain atoms. UTC
  2026. differs from TAI by an integral number of seconds, which is increased
  2027. or decreased at announced times to keep UTC aligned to a mean solar
  2028. day (the orbit and rotation of the earth are not quite constant).
  2029. @cindex leap second
  2030. So far, only increases in the TAI
  2031. @tex
  2032. $\leftrightarrow$
  2033. @end tex
  2034. @ifnottex
  2035. <->
  2036. @end ifnottex
  2037. UTC difference have been needed. Such an increase is a ``leap
  2038. second'', an extra second of TAI introduced at the end of a UTC day.
  2039. When working entirely within UTC this is never seen, every day simply
  2040. has 86400 seconds. But when converting from TAI to a UTC date, an
  2041. extra 23:59:60 is present, where normally a day would end at 23:59:59.
  2042. Effectively the UTC second from 23:59:59 to 00:00:00 has taken two TAI
  2043. seconds.
  2044. @cindex system clock
  2045. In the current implementation, the system clock is assumed to be UTC,
  2046. and a table of leap seconds in the code converts to TAI. See comments
  2047. in @file{srfi-19.scm} for how to update this table.
  2048. @cindex julian day
  2049. @cindex modified julian day
  2050. Also, for those not familiar with the terminology, a @dfn{Julian Day}
  2051. represents a point in time as a real number of days since
  2052. -4713-11-24T12:00:00Z, i.e.@: midday UT on 24 November 4714 BC in the
  2053. proleptic Gregorian calendar (1 January 4713 BC in the proleptic Julian
  2054. calendar).
  2055. A @dfn{Modified Julian Day} represents a point in time as a real number
  2056. of days since 1858-11-17T00:00:00Z, i.e.@: midnight UT on Wednesday 17
  2057. November AD 1858. That time is julian day 2400000.5.
  2058. @node SRFI-19 Time
  2059. @subsubsection SRFI-19 Time
  2060. @cindex time
  2061. A @dfn{time} object has type, seconds and nanoseconds fields
  2062. representing a point in time starting from some epoch. This is an
  2063. arbitrary point in time, not just a time of day. Although times are
  2064. represented in nanoseconds, the actual resolution may be lower.
  2065. The following variables hold the possible time types. For instance
  2066. @code{(current-time time-process)} would give the current CPU process
  2067. time.
  2068. @defvar time-utc
  2069. Universal Coordinated Time (UTC).
  2070. @cindex UTC
  2071. @end defvar
  2072. @defvar time-tai
  2073. International Atomic Time (TAI).
  2074. @cindex TAI
  2075. @end defvar
  2076. @defvar time-monotonic
  2077. Monotonic time, meaning a monotonically increasing time starting from
  2078. an unspecified epoch.
  2079. Note that in the current implementation @code{time-monotonic} is the
  2080. same as @code{time-tai}, and unfortunately is therefore affected by
  2081. adjustments to the system clock. Perhaps this will change in the
  2082. future.
  2083. @end defvar
  2084. @defvar time-duration
  2085. A duration, meaning simply a difference between two times.
  2086. @end defvar
  2087. @defvar time-process
  2088. CPU time spent in the current process, starting from when the process
  2089. began.
  2090. @cindex process time
  2091. @end defvar
  2092. @defvar time-thread
  2093. CPU time spent in the current thread. Not currently implemented.
  2094. @cindex thread time
  2095. @end defvar
  2096. @sp 1
  2097. @defun time? obj
  2098. Return @code{#t} if @var{obj} is a time object, or @code{#f} if not.
  2099. @end defun
  2100. @defun make-time type nanoseconds seconds
  2101. Create a time object with the given @var{type}, @var{seconds} and
  2102. @var{nanoseconds}.
  2103. @end defun
  2104. @defun time-type time
  2105. @defunx time-nanosecond time
  2106. @defunx time-second time
  2107. @defunx set-time-type! time type
  2108. @defunx set-time-nanosecond! time nsec
  2109. @defunx set-time-second! time sec
  2110. Get or set the type, seconds or nanoseconds fields of a time object.
  2111. @code{set-time-type!} merely changes the field, it doesn't convert the
  2112. time value. For conversions, see @ref{SRFI-19 Time/Date conversions}.
  2113. @end defun
  2114. @defun copy-time time
  2115. Return a new time object, which is a copy of the given @var{time}.
  2116. @end defun
  2117. @defun current-time [type]
  2118. Return the current time of the given @var{type}. The default
  2119. @var{type} is @code{time-utc}.
  2120. Note that the name @code{current-time} conflicts with the Guile core
  2121. @code{current-time} function (@pxref{Time}) as well as the SRFI-18
  2122. @code{current-time} function (@pxref{SRFI-18 Time}). Applications
  2123. wanting to use more than one of these functions will need to refer to
  2124. them by different names.
  2125. @end defun
  2126. @defun time-resolution [type]
  2127. Return the resolution, in nanoseconds, of the given time @var{type}.
  2128. The default @var{type} is @code{time-utc}.
  2129. @end defun
  2130. @defun time<=? t1 t2
  2131. @defunx time<? t1 t2
  2132. @defunx time=? t1 t2
  2133. @defunx time>=? t1 t2
  2134. @defunx time>? t1 t2
  2135. Return @code{#t} or @code{#f} according to the respective relation
  2136. between time objects @var{t1} and @var{t2}. @var{t1} and @var{t2}
  2137. must be the same time type.
  2138. @end defun
  2139. @defun time-difference t1 t2
  2140. @defunx time-difference! t1 t2
  2141. Return a time object of type @code{time-duration} representing the
  2142. period between @var{t1} and @var{t2}. @var{t1} and @var{t2} must be
  2143. the same time type.
  2144. @code{time-difference} returns a new time object,
  2145. @code{time-difference!} may modify @var{t1} to form its return.
  2146. @end defun
  2147. @defun add-duration time duration
  2148. @defunx add-duration! time duration
  2149. @defunx subtract-duration time duration
  2150. @defunx subtract-duration! time duration
  2151. Return a time object which is @var{time} with the given @var{duration}
  2152. added or subtracted. @var{duration} must be a time object of type
  2153. @code{time-duration}.
  2154. @code{add-duration} and @code{subtract-duration} return a new time
  2155. object. @code{add-duration!} and @code{subtract-duration!} may modify
  2156. the given @var{time} to form their return.
  2157. @end defun
  2158. @node SRFI-19 Date
  2159. @subsubsection SRFI-19 Date
  2160. @cindex date
  2161. A @dfn{date} object represents a date in the Gregorian calendar and a
  2162. time of day on that date in some timezone.
  2163. The fields are year, month, day, hour, minute, second, nanoseconds and
  2164. timezone. A date object is immutable, its fields can be read but they
  2165. cannot be modified once the object is created.
  2166. Historically, the Gregorian calendar was only used from the latter part
  2167. of the year 1582 onwards, and not until even later in many countries.
  2168. Prior to that most countries used the Julian calendar. SRFI-19 does
  2169. not deal with the Julian calendar at all, and so does not reflect this
  2170. historical calendar reform. Instead it projects the Gregorian calendar
  2171. back proleptically as far as necessary. When dealing with historical
  2172. data, especially prior to the British Empire's adoption of the Gregorian
  2173. calendar in 1752, one should be mindful of which calendar is used in
  2174. each context, and apply non-SRFI-19 facilities to convert where necessary.
  2175. @defun date? obj
  2176. Return @code{#t} if @var{obj} is a date object, or @code{#f} if not.
  2177. @end defun
  2178. @defun make-date nsecs seconds minutes hours date month year zone-offset
  2179. Create a new date object.
  2180. @c
  2181. @c FIXME: What can we say about the ranges of the values. The
  2182. @c current code looks it doesn't normalize, but expects then in their
  2183. @c usual range already.
  2184. @c
  2185. @end defun
  2186. @defun date-nanosecond date
  2187. Nanoseconds, 0 to 999999999.
  2188. @end defun
  2189. @defun date-second date
  2190. Seconds, 0 to 59, or 60 for a leap second. 60 is never seen when working
  2191. entirely within UTC, it's only when converting to or from TAI.
  2192. @end defun
  2193. @defun date-minute date
  2194. Minutes, 0 to 59.
  2195. @end defun
  2196. @defun date-hour date
  2197. Hour, 0 to 23.
  2198. @end defun
  2199. @defun date-day date
  2200. Day of the month, 1 to 31 (or less, according to the month).
  2201. @end defun
  2202. @defun date-month date
  2203. Month, 1 to 12.
  2204. @end defun
  2205. @defun date-year date
  2206. Year, eg.@: 2003. Dates B.C.@: are negative, eg.@: @math{-46} is 46
  2207. B.C. There is no year 0, year @math{-1} is followed by year 1.
  2208. @end defun
  2209. @defun date-zone-offset date
  2210. Time zone, an integer number of seconds east of Greenwich.
  2211. @end defun
  2212. @defun date-year-day date
  2213. Day of the year, starting from 1 for 1st January.
  2214. @end defun
  2215. @defun date-week-day date
  2216. Day of the week, starting from 0 for Sunday.
  2217. @end defun
  2218. @defun date-week-number date dstartw
  2219. Week of the year, ignoring a first partial week. @var{dstartw} is the
  2220. day of the week which is taken to start a week, 0 for Sunday, 1 for
  2221. Monday, etc.
  2222. @c
  2223. @c FIXME: The spec doesn't say whether numbering starts at 0 or 1.
  2224. @c The code looks like it's 0, if that's the correct intention.
  2225. @c
  2226. @end defun
  2227. @c The SRFI text doesn't actually give the default for tz-offset, but
  2228. @c the reference implementation has the local timezone and the
  2229. @c conversions functions all specify that, so it should be ok to
  2230. @c document it here.
  2231. @c
  2232. @defun current-date [tz-offset]
  2233. Return a date object representing the current date/time, in UTC offset
  2234. by @var{tz-offset}. @var{tz-offset} is seconds east of Greenwich and
  2235. defaults to the local timezone.
  2236. @end defun
  2237. @defun current-julian-day
  2238. @cindex julian day
  2239. Return the current Julian Day.
  2240. @end defun
  2241. @defun current-modified-julian-day
  2242. @cindex modified julian day
  2243. Return the current Modified Julian Day.
  2244. @end defun
  2245. @node SRFI-19 Time/Date conversions
  2246. @subsubsection SRFI-19 Time/Date conversions
  2247. @cindex time conversion
  2248. @cindex date conversion
  2249. @defun date->julian-day date
  2250. @defunx date->modified-julian-day date
  2251. @defunx date->time-monotonic date
  2252. @defunx date->time-tai date
  2253. @defunx date->time-utc date
  2254. @end defun
  2255. @defun julian-day->date jdn [tz-offset]
  2256. @defunx julian-day->time-monotonic jdn
  2257. @defunx julian-day->time-tai jdn
  2258. @defunx julian-day->time-utc jdn
  2259. @end defun
  2260. @defun modified-julian-day->date jdn [tz-offset]
  2261. @defunx modified-julian-day->time-monotonic jdn
  2262. @defunx modified-julian-day->time-tai jdn
  2263. @defunx modified-julian-day->time-utc jdn
  2264. @end defun
  2265. @defun time-monotonic->date time [tz-offset]
  2266. @defunx time-monotonic->time-tai time
  2267. @defunx time-monotonic->time-tai! time
  2268. @defunx time-monotonic->time-utc time
  2269. @defunx time-monotonic->time-utc! time
  2270. @end defun
  2271. @defun time-tai->date time [tz-offset]
  2272. @defunx time-tai->julian-day time
  2273. @defunx time-tai->modified-julian-day time
  2274. @defunx time-tai->time-monotonic time
  2275. @defunx time-tai->time-monotonic! time
  2276. @defunx time-tai->time-utc time
  2277. @defunx time-tai->time-utc! time
  2278. @end defun
  2279. @defun time-utc->date time [tz-offset]
  2280. @defunx time-utc->julian-day time
  2281. @defunx time-utc->modified-julian-day time
  2282. @defunx time-utc->time-monotonic time
  2283. @defunx time-utc->time-monotonic! time
  2284. @defunx time-utc->time-tai time
  2285. @defunx time-utc->time-tai! time
  2286. @sp 1
  2287. Convert between dates, times and days of the respective types. For
  2288. instance @code{time-tai->time-utc} accepts a @var{time} object of type
  2289. @code{time-tai} and returns an object of type @code{time-utc}.
  2290. The @code{!} variants may modify their @var{time} argument to form
  2291. their return. The plain functions create a new object.
  2292. For conversions to dates, @var{tz-offset} is seconds east of
  2293. Greenwich. The default is the local timezone, at the given time, as
  2294. provided by the system, using @code{localtime} (@pxref{Time}).
  2295. On 32-bit systems, @code{localtime} is limited to a 32-bit
  2296. @code{time_t}, so a default @var{tz-offset} is only available for
  2297. times between Dec 1901 and Jan 2038. For prior dates an application
  2298. might like to use the value in 1902, though some locations have zone
  2299. changes prior to that. For future dates an application might like to
  2300. assume today's rules extend indefinitely. But for correct daylight
  2301. savings transitions it will be necessary to take an offset for the
  2302. same day and time but a year in range and which has the same starting
  2303. weekday and same leap/non-leap (to support rules like last Sunday in
  2304. October).
  2305. @end defun
  2306. @node SRFI-19 Date to string
  2307. @subsubsection SRFI-19 Date to string
  2308. @cindex date to string
  2309. @cindex string, from date
  2310. @defun date->string date [format]
  2311. Convert a date to a string under the control of a format.
  2312. @var{format} should be a string containing @samp{~} escapes, which
  2313. will be expanded as per the following conversion table. The default
  2314. @var{format} is @samp{~c}, a locale-dependent date and time.
  2315. Many of these conversion characters are the same as POSIX
  2316. @code{strftime} (@pxref{Time}), but there are some extras and some
  2317. variations.
  2318. @multitable {MMMM} {MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM}
  2319. @item @nicode{~~} @tab literal ~
  2320. @item @nicode{~a} @tab locale abbreviated weekday, eg.@: @samp{Sun}
  2321. @item @nicode{~A} @tab locale full weekday, eg.@: @samp{Sunday}
  2322. @item @nicode{~b} @tab locale abbreviated month, eg.@: @samp{Jan}
  2323. @item @nicode{~B} @tab locale full month, eg.@: @samp{January}
  2324. @item @nicode{~c} @tab locale date and time, eg.@: @*
  2325. @samp{Fri Jul 14 20:28:42-0400 2000}
  2326. @item @nicode{~d} @tab day of month, zero padded, @samp{01} to @samp{31}
  2327. @c Spec says d/m/y, reference implementation says m/d/y.
  2328. @c Apparently the reference code was the intention, but would like to
  2329. @c see an errata published for the spec before contradicting it here.
  2330. @c
  2331. @c @item @nicode{~D} @tab date @nicode{~d/~m/~y}
  2332. @item @nicode{~e} @tab day of month, blank padded, @samp{ 1} to @samp{31}
  2333. @item @nicode{~f} @tab seconds and fractional seconds,
  2334. with locale decimal point, eg.@: @samp{5.2}
  2335. @item @nicode{~h} @tab same as @nicode{~b}
  2336. @item @nicode{~H} @tab hour, 24-hour clock, zero padded, @samp{00} to @samp{23}
  2337. @item @nicode{~I} @tab hour, 12-hour clock, zero padded, @samp{01} to @samp{12}
  2338. @item @nicode{~j} @tab day of year, zero padded, @samp{001} to @samp{366}
  2339. @item @nicode{~k} @tab hour, 24-hour clock, blank padded, @samp{ 0} to @samp{23}
  2340. @item @nicode{~l} @tab hour, 12-hour clock, blank padded, @samp{ 1} to @samp{12}
  2341. @item @nicode{~m} @tab month, zero padded, @samp{01} to @samp{12}
  2342. @item @nicode{~M} @tab minute, zero padded, @samp{00} to @samp{59}
  2343. @item @nicode{~n} @tab newline
  2344. @item @nicode{~N} @tab nanosecond, zero padded, @samp{000000000} to @samp{999999999}
  2345. @item @nicode{~p} @tab locale AM or PM
  2346. @item @nicode{~r} @tab time, 12 hour clock, @samp{~I:~M:~S ~p}
  2347. @item @nicode{~s} @tab number of full seconds since ``the epoch'' in UTC
  2348. @item @nicode{~S} @tab second, zero padded @samp{00} to @samp{60} @*
  2349. (usual limit is 59, 60 is a leap second)
  2350. @item @nicode{~t} @tab horizontal tab character
  2351. @item @nicode{~T} @tab time, 24 hour clock, @samp{~H:~M:~S}
  2352. @item @nicode{~U} @tab week of year, Sunday first day of week,
  2353. @samp{00} to @samp{52}
  2354. @item @nicode{~V} @tab week of year, Monday first day of week,
  2355. @samp{01} to @samp{53}
  2356. @item @nicode{~w} @tab day of week, 0 for Sunday, @samp{0} to @samp{6}
  2357. @item @nicode{~W} @tab week of year, Monday first day of week,
  2358. @samp{00} to @samp{52}
  2359. @c The spec has ~x as an apparent duplicate of ~W, and ~X as a locale
  2360. @c date. The reference code has ~x as the locale date and ~X as a
  2361. @c locale time. The rule is apparently that the code should be
  2362. @c believed, but would like to see an errata for the spec before
  2363. @c contradicting it here.
  2364. @c
  2365. @c @item @nicode{~x} @tab week of year, Monday as first day of week,
  2366. @c @samp{00} to @samp{53}
  2367. @c @item @nicode{~X} @tab locale date, eg.@: @samp{07/31/00}
  2368. @item @nicode{~y} @tab year, two digits, @samp{00} to @samp{99}
  2369. @item @nicode{~Y} @tab year, full, eg.@: @samp{2003}
  2370. @item @nicode{~z} @tab time zone, RFC-822 style
  2371. @item @nicode{~Z} @tab time zone symbol (not currently implemented)
  2372. @item @nicode{~1} @tab ISO-8601 date, @samp{~Y-~m-~d}
  2373. @item @nicode{~2} @tab ISO-8601 time+zone, @samp{~H:~M:~S~z}
  2374. @item @nicode{~3} @tab ISO-8601 time, @samp{~H:~M:~S}
  2375. @item @nicode{~4} @tab ISO-8601 date/time+zone, @samp{~Y-~m-~dT~H:~M:~S~z}
  2376. @item @nicode{~5} @tab ISO-8601 date/time, @samp{~Y-~m-~dT~H:~M:~S}
  2377. @end multitable
  2378. @end defun
  2379. Conversions @samp{~D}, @samp{~x} and @samp{~X} are not currently
  2380. described here, since the specification and reference implementation
  2381. differ.
  2382. Conversion is locale-dependent on systems that support it
  2383. (@pxref{Accessing Locale Information}). @xref{Locales,
  2384. @code{setlocale}}, for information on how to change the current
  2385. locale.
  2386. @node SRFI-19 String to date
  2387. @subsubsection SRFI-19 String to date
  2388. @cindex string to date
  2389. @cindex date, from string
  2390. @c FIXME: Can we say what happens when an incomplete date is
  2391. @c converted? I.e. fields left as 0, or what? The spec seems to be
  2392. @c silent on this.
  2393. @defun string->date input template
  2394. Convert an @var{input} string to a date under the control of a
  2395. @var{template} string. Return a newly created date object.
  2396. Literal characters in @var{template} must match characters in
  2397. @var{input} and @samp{~} escapes must match the input forms described
  2398. in the table below. ``Skip to'' means characters up to one of the
  2399. given type are ignored, or ``no skip'' for no skipping. ``Read'' is
  2400. what's then read, and ``Set'' is the field affected in the date
  2401. object.
  2402. For example @samp{~Y} skips input characters until a digit is reached,
  2403. at which point it expects a year and stores that to the year field of
  2404. the date.
  2405. @multitable {MMMM} {@nicode{char-alphabetic?}} {MMMMMMMMMMMMMMMMMMMMMMMMM} {@nicode{date-zone-offset}}
  2406. @item
  2407. @tab Skip to
  2408. @tab Read
  2409. @tab Set
  2410. @item @nicode{~~}
  2411. @tab no skip
  2412. @tab literal ~
  2413. @tab nothing
  2414. @item @nicode{~a}
  2415. @tab @nicode{char-alphabetic?}
  2416. @tab locale abbreviated weekday name
  2417. @tab nothing
  2418. @item @nicode{~A}
  2419. @tab @nicode{char-alphabetic?}
  2420. @tab locale full weekday name
  2421. @tab nothing
  2422. @c Note that the SRFI spec says that ~b and ~B don't set anything,
  2423. @c but that looks like a mistake. The reference implementation sets
  2424. @c the month field, which seems sensible and is what we describe
  2425. @c here.
  2426. @item @nicode{~b}
  2427. @tab @nicode{char-alphabetic?}
  2428. @tab locale abbreviated month name
  2429. @tab @nicode{date-month}
  2430. @item @nicode{~B}
  2431. @tab @nicode{char-alphabetic?}
  2432. @tab locale full month name
  2433. @tab @nicode{date-month}
  2434. @item @nicode{~d}
  2435. @tab @nicode{char-numeric?}
  2436. @tab day of month
  2437. @tab @nicode{date-day}
  2438. @item @nicode{~e}
  2439. @tab no skip
  2440. @tab day of month, blank padded
  2441. @tab @nicode{date-day}
  2442. @item @nicode{~h}
  2443. @tab same as @samp{~b}
  2444. @item @nicode{~H}
  2445. @tab @nicode{char-numeric?}
  2446. @tab hour
  2447. @tab @nicode{date-hour}
  2448. @item @nicode{~k}
  2449. @tab no skip
  2450. @tab hour, blank padded
  2451. @tab @nicode{date-hour}
  2452. @item @nicode{~m}
  2453. @tab @nicode{char-numeric?}
  2454. @tab month
  2455. @tab @nicode{date-month}
  2456. @item @nicode{~M}
  2457. @tab @nicode{char-numeric?}
  2458. @tab minute
  2459. @tab @nicode{date-minute}
  2460. @item @nicode{~N}
  2461. @tab @nicode{char-numeric?}
  2462. @tab nanosecond
  2463. @tab @nicode{date-nanosecond}
  2464. @item @nicode{~S}
  2465. @tab @nicode{char-numeric?}
  2466. @tab second
  2467. @tab @nicode{date-second}
  2468. @item @nicode{~y}
  2469. @tab no skip
  2470. @tab 2-digit year
  2471. @tab @nicode{date-year} within 50 years
  2472. @item @nicode{~Y}
  2473. @tab @nicode{char-numeric?}
  2474. @tab year
  2475. @tab @nicode{date-year}
  2476. @item @nicode{~z}
  2477. @tab no skip
  2478. @tab time zone
  2479. @tab date-zone-offset
  2480. @end multitable
  2481. Notice that the weekday matching forms don't affect the date object
  2482. returned, instead the weekday will be derived from the day, month and
  2483. year.
  2484. Conversion is locale-dependent on systems that support it
  2485. (@pxref{Accessing Locale Information}). @xref{Locales,
  2486. @code{setlocale}}, for information on how to change the current
  2487. locale.
  2488. @end defun
  2489. @node SRFI-23
  2490. @subsection SRFI-23 - Error Reporting
  2491. @cindex SRFI-23
  2492. The SRFI-23 @code{error} procedure is always available.
  2493. @node SRFI-26
  2494. @subsection SRFI-26 - specializing parameters
  2495. @cindex SRFI-26
  2496. @cindex parameter specialize
  2497. @cindex argument specialize
  2498. @cindex specialize parameter
  2499. This SRFI provides a syntax for conveniently specializing selected
  2500. parameters of a function. It can be used with,
  2501. @example
  2502. (use-modules (srfi srfi-26))
  2503. @end example
  2504. @deffn {library syntax} cut slot1 slot2 @dots{}
  2505. @deffnx {library syntax} cute slot1 slot2 @dots{}
  2506. Return a new procedure which will make a call (@var{slot1} @var{slot2}
  2507. @dots{}) but with selected parameters specialized to given expressions.
  2508. An example will illustrate the idea. The following is a
  2509. specialization of @code{write}, sending output to
  2510. @code{my-output-port},
  2511. @example
  2512. (cut write <> my-output-port)
  2513. @result{}
  2514. (lambda (obj) (write obj my-output-port))
  2515. @end example
  2516. The special symbol @code{<>} indicates a slot to be filled by an
  2517. argument to the new procedure. @code{my-output-port} on the other
  2518. hand is an expression to be evaluated and passed, ie.@: it specializes
  2519. the behaviour of @code{write}.
  2520. @table @nicode
  2521. @item <>
  2522. A slot to be filled by an argument from the created procedure.
  2523. Arguments are assigned to @code{<>} slots in the order they appear in
  2524. the @code{cut} form, there's no way to re-arrange arguments.
  2525. The first argument to @code{cut} is usually a procedure (or expression
  2526. giving a procedure), but @code{<>} is allowed there too. For example,
  2527. @example
  2528. (cut <> 1 2 3)
  2529. @result{}
  2530. (lambda (proc) (proc 1 2 3))
  2531. @end example
  2532. @item <...>
  2533. A slot to be filled by all remaining arguments from the new procedure.
  2534. This can only occur at the end of a @code{cut} form.
  2535. For example, a procedure taking a variable number of arguments like
  2536. @code{max} but in addition enforcing a lower bound,
  2537. @example
  2538. (define my-lower-bound 123)
  2539. (cut max my-lower-bound <...>)
  2540. @result{}
  2541. (lambda arglist (apply max my-lower-bound arglist))
  2542. @end example
  2543. @end table
  2544. For @code{cut} the specializing expressions are evaluated each time
  2545. the new procedure is called. For @code{cute} they're evaluated just
  2546. once, when the new procedure is created. The name @code{cute} stands
  2547. for ``@code{cut} with evaluated arguments''. In all cases the
  2548. evaluations take place in an unspecified order.
  2549. The following illustrates the difference between @code{cut} and
  2550. @code{cute},
  2551. @example
  2552. (cut format <> "the time is ~s" (current-time))
  2553. @result{}
  2554. (lambda (port) (format port "the time is ~s" (current-time)))
  2555. (cute format <> "the time is ~s" (current-time))
  2556. @result{}
  2557. (let ((val (current-time)))
  2558. (lambda (port) (format port "the time is ~s" val))
  2559. @end example
  2560. (There's no provision for a mixture of @code{cut} and @code{cute}
  2561. where some expressions would be evaluated every time but others
  2562. evaluated only once.)
  2563. @code{cut} is really just a shorthand for the sort of @code{lambda}
  2564. forms shown in the above examples. But notice @code{cut} avoids the
  2565. need to name unspecialized parameters, and is more compact. Use in
  2566. functional programming style or just with @code{map}, @code{for-each}
  2567. or similar is typical.
  2568. @example
  2569. (map (cut * 2 <>) '(1 2 3 4))
  2570. (for-each (cut write <> my-port) my-list)
  2571. @end example
  2572. @end deffn
  2573. @node SRFI-27
  2574. @subsection SRFI-27 - Sources of Random Bits
  2575. @cindex SRFI-27
  2576. This subsection is based on the
  2577. @uref{http://srfi.schemers.org/srfi-27/srfi-27.html, specification of
  2578. SRFI-27} written by Sebastian Egner.
  2579. @c The copyright notice and license text of the SRFI-27 specification is
  2580. @c reproduced below:
  2581. @c Copyright (C) Sebastian Egner (2002). All Rights Reserved.
  2582. @c Permission is hereby granted, free of charge, to any person obtaining a
  2583. @c copy of this software and associated documentation files (the
  2584. @c "Software"), to deal in the Software without restriction, including
  2585. @c without limitation the rights to use, copy, modify, merge, publish,
  2586. @c distribute, sublicense, and/or sell copies of the Software, and to
  2587. @c permit persons to whom the Software is furnished to do so, subject to
  2588. @c the following conditions:
  2589. @c The above copyright notice and this permission notice shall be included
  2590. @c in all copies or substantial portions of the Software.
  2591. @c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  2592. @c OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2593. @c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  2594. @c NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  2595. @c LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  2596. @c OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  2597. @c WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  2598. This SRFI provides access to a (pseudo) random number generator; for
  2599. Guile's built-in random number facilities, which SRFI-27 is implemented
  2600. upon, @xref{Random}. With SRFI-27, random numbers are obtained from a
  2601. @emph{random source}, which encapsulates a random number generation
  2602. algorithm and its state.
  2603. @menu
  2604. * SRFI-27 Default Random Source:: Obtaining random numbers
  2605. * SRFI-27 Random Sources:: Creating and manipulating random sources
  2606. * SRFI-27 Random Number Generators:: Obtaining random number generators
  2607. @end menu
  2608. @node SRFI-27 Default Random Source
  2609. @subsubsection The Default Random Source
  2610. @cindex SRFI-27
  2611. @defun random-integer n
  2612. Return a random number between zero (inclusive) and @var{n} (exclusive),
  2613. using the default random source. The numbers returned have a uniform
  2614. distribution.
  2615. @end defun
  2616. @defun random-real
  2617. Return a random number in (0,1), using the default random source. The
  2618. numbers returned have a uniform distribution.
  2619. @end defun
  2620. @defun default-random-source
  2621. A random source from which @code{random-integer} and @code{random-real}
  2622. have been derived using @code{random-source-make-integers} and
  2623. @code{random-source-make-reals} (@pxref{SRFI-27 Random Number Generators}
  2624. for those procedures). Note that an assignment to
  2625. @code{default-random-source} does not change @code{random-integer} or
  2626. @code{random-real}; it is also strongly recommended not to assign a new
  2627. value.
  2628. @end defun
  2629. @node SRFI-27 Random Sources
  2630. @subsubsection Random Sources
  2631. @cindex SRFI-27
  2632. @defun make-random-source
  2633. Create a new random source. The stream of random numbers obtained from
  2634. each random source created by this procedure will be identical, unless
  2635. its state is changed by one of the procedures below.
  2636. @end defun
  2637. @defun random-source? object
  2638. Tests whether @var{object} is a random source. Random sources are a
  2639. disjoint type.
  2640. @end defun
  2641. @defun random-source-randomize! source
  2642. Attempt to set the state of the random source to a truly random value.
  2643. The current implementation uses a seed based on the current system time.
  2644. @end defun
  2645. @defun random-source-pseudo-randomize! source i j
  2646. Changes the state of the random source s into the initial state of the
  2647. (@var{i}, @var{j})-th independent random source, where @var{i} and
  2648. @var{j} are non-negative integers. This procedure provides a mechanism
  2649. to obtain a large number of independent random sources (usually all
  2650. derived from the same backbone generator), indexed by two integers. In
  2651. contrast to @code{random-source-randomize!}, this procedure is entirely
  2652. deterministic.
  2653. @end defun
  2654. The state associated with a random state can be obtained an reinstated
  2655. with the following procedures:
  2656. @defun random-source-state-ref source
  2657. @defunx random-source-state-set! source state
  2658. Get and set the state of a random source. No assumptions should be made
  2659. about the nature of the state object, besides it having an external
  2660. representation (i.e.@: it can be passed to @code{write} and subsequently
  2661. @code{read} back).
  2662. @end defun
  2663. @node SRFI-27 Random Number Generators
  2664. @subsubsection Obtaining random number generator procedures
  2665. @cindex SRFI-27
  2666. @defun random-source-make-integers source
  2667. Obtains a procedure to generate random integers using the random source
  2668. @var{source}. The returned procedure takes a single argument @var{n},
  2669. which must be a positive integer, and returns the next uniformly
  2670. distributed random integer from the interval @{0, ..., @var{n}-1@} by
  2671. advancing the state of @var{source}.
  2672. If an application obtains and uses several generators for the same
  2673. random source @var{source}, a call to any of these generators advances
  2674. the state of @var{source}. Hence, the generators do not produce the
  2675. same sequence of random integers each but rather share a state. This
  2676. also holds for all other types of generators derived from a fixed random
  2677. sources.
  2678. While the SRFI text specifies that ``Implementations that support
  2679. concurrency make sure that the state of a generator is properly
  2680. advanced'', this is currently not the case in Guile's implementation of
  2681. SRFI-27, as it would cause a severe performance penalty. So in
  2682. multi-threaded programs, you either must perform locking on random
  2683. sources shared between threads yourself, or use different random sources
  2684. for multiple threads.
  2685. @end defun
  2686. @defun random-source-make-reals source
  2687. @defunx random-source-make-reals source unit
  2688. Obtains a procedure to generate random real numbers @math{0 < x < 1}
  2689. using the random source @var{source}. The procedure rand is called
  2690. without arguments.
  2691. The optional parameter @var{unit} determines the type of numbers being
  2692. produced by the returned procedure and the quantization of the output.
  2693. @var{unit} must be a number such that @math{0 < @var{unit} < 1}. The
  2694. numbers created by the returned procedure are of the same numerical type
  2695. as @var{unit} and the potential output values are spaced by at most
  2696. @var{unit}. One can imagine rand to create numbers as @var{x} *
  2697. @var{unit} where @var{x} is a random integer in @{1, ...,
  2698. floor(1/unit)-1@}. Note, however, that this need not be the way the
  2699. values are actually created and that the actual resolution of rand can
  2700. be much higher than unit. In case @var{unit} is absent it defaults to a
  2701. reasonably small value (related to the width of the mantissa of an
  2702. efficient number format).
  2703. @end defun
  2704. @node SRFI-28
  2705. @subsection SRFI-28 - Basic Format Strings
  2706. @cindex SRFI-28
  2707. SRFI-28 provides a basic @code{format} procedure that provides only
  2708. the @code{~a}, @code{~s}, @code{~%}, and @code{~~} format specifiers.
  2709. You can import this procedure by using:
  2710. @lisp
  2711. (use-modules (srfi srfi-28))
  2712. @end lisp
  2713. @deffn {Scheme Procedure} format message arg @dots{}
  2714. Returns a formatted message, using @var{message} as the format string,
  2715. which can contain the following format specifiers:
  2716. @table @code
  2717. @item ~a
  2718. Insert the textual representation of the next @var{arg}, as if printed
  2719. by @code{display}.
  2720. @item ~s
  2721. Insert the textual representation of the next @var{arg}, as if printed
  2722. by @code{write}.
  2723. @item ~%
  2724. Insert a newline.
  2725. @item ~~
  2726. Insert a tilde.
  2727. @end table
  2728. This procedure is the same as calling @code{simple-format}
  2729. (@pxref{Simple Output}) with @code{#f} as the destination.
  2730. @end deffn
  2731. @node SRFI-30
  2732. @subsection SRFI-30 - Nested Multi-line Comments
  2733. @cindex SRFI-30
  2734. Starting from version 2.0, Guile's @code{read} supports SRFI-30/R6RS
  2735. nested multi-line comments by default, @ref{Block Comments}.
  2736. @node SRFI-31
  2737. @subsection SRFI-31 - A special form `rec' for recursive evaluation
  2738. @cindex SRFI-31
  2739. @cindex recursive expression
  2740. @findex rec
  2741. SRFI-31 defines a special form that can be used to create
  2742. self-referential expressions more conveniently. The syntax is as
  2743. follows:
  2744. @example
  2745. @group
  2746. <rec expression> --> (rec <variable> <expression>)
  2747. <rec expression> --> (rec (<variable>+) <body>)
  2748. @end group
  2749. @end example
  2750. The first syntax can be used to create self-referential expressions,
  2751. for example:
  2752. @lisp
  2753. guile> (define tmp (rec ones (cons 1 (delay ones))))
  2754. @end lisp
  2755. The second syntax can be used to create anonymous recursive functions:
  2756. @lisp
  2757. guile> (define tmp (rec (display-n item n)
  2758. (if (positive? n)
  2759. (begin (display n) (display-n (- n 1))))))
  2760. guile> (tmp 42 3)
  2761. 424242
  2762. guile>
  2763. @end lisp
  2764. @node SRFI-34
  2765. @subsection SRFI-34 - Exception handling for programs
  2766. @cindex SRFI-34
  2767. Guile provides an implementation of
  2768. @uref{http://srfi.schemers.org/srfi-34/srfi-34.html, SRFI-34's exception
  2769. handling mechanisms} as an alternative to its own built-in mechanisms
  2770. (@pxref{Exceptions}). It can be made available as follows:
  2771. @lisp
  2772. (use-modules (srfi srfi-34))
  2773. @end lisp
  2774. @xref{Raising and Handling Exceptions}, for more on
  2775. @code{with-exception-handler} and @code{raise} (known as
  2776. @code{raise-exception} in core Guile).
  2777. SRFI-34's @code{guard} form is syntactic sugar over
  2778. @code{with-exception-handler}:
  2779. @deffn {Syntax} guard (var clause @dots{}) body @dots{}
  2780. Evaluate @var{body} with an exception handler that binds the raised
  2781. object to @var{var} and within the scope of that binding evaluates
  2782. @var{clause}@dots{} as if they were the clauses of a cond expression.
  2783. That implicit cond expression is evaluated with the continuation and
  2784. dynamic environment of the guard expression.
  2785. If every @var{clause}'s test evaluates to false and there is no
  2786. @code{else} clause, then @code{raise} is re-invoked on the raised object
  2787. within the dynamic environment of the original call to @code{raise}
  2788. except that the current exception handler is that of the @code{guard}
  2789. expression.
  2790. @end deffn
  2791. @node SRFI-35
  2792. @subsection SRFI-35 - Conditions
  2793. @cindex SRFI-35
  2794. @cindex conditions
  2795. @cindex exceptions
  2796. @uref{http://srfi.schemers.org/srfi-35/srfi-35.html, SRFI-35} defines
  2797. @dfn{conditions}, a data structure akin to records designed to convey
  2798. information about exceptional conditions between parts of a program. It
  2799. is normally used in conjunction with SRFI-34's @code{raise}:
  2800. @lisp
  2801. (raise (condition (&message
  2802. (message "An error occurred"))))
  2803. @end lisp
  2804. Users can define @dfn{condition types} containing arbitrary information.
  2805. Condition types may inherit from one another. This allows the part of
  2806. the program that handles (or ``catches'') conditions to get accurate
  2807. information about the exceptional condition that arose.
  2808. SRFI-35 conditions are made available using:
  2809. @lisp
  2810. (use-modules (srfi srfi-35))
  2811. @end lisp
  2812. The procedures available to manipulate condition types are the
  2813. following:
  2814. @deffn {Scheme Procedure} make-condition-type id parent field-names
  2815. Return a new condition type named @var{id}, inheriting from
  2816. @var{parent}, and with the fields whose names are listed in
  2817. @var{field-names}. @var{field-names} must be a list of symbols and must
  2818. not contain names already used by @var{parent} or one of its supertypes.
  2819. @end deffn
  2820. @deffn {Scheme Procedure} condition-type? obj
  2821. Return true if @var{obj} is a condition type.
  2822. @end deffn
  2823. Conditions can be created and accessed with the following procedures:
  2824. @deffn {Scheme Procedure} make-condition type . field+value
  2825. Return a new condition of type @var{type} with fields initialized as
  2826. specified by @var{field+value}, a sequence of field names (symbols) and
  2827. values as in the following example:
  2828. @lisp
  2829. (let ((&ct (make-condition-type 'foo &condition '(a b c))))
  2830. (make-condition &ct 'a 1 'b 2 'c 3))
  2831. @end lisp
  2832. Note that all fields of @var{type} and its supertypes must be specified.
  2833. @end deffn
  2834. @deffn {Scheme Procedure} make-compound-condition condition1 condition2 @dots{}
  2835. Return a new compound condition composed of @var{condition1}
  2836. @var{condition2} @enddots{}. The returned condition has the type of
  2837. each condition of condition1 condition2 @dots{} (per
  2838. @code{condition-has-type?}).
  2839. @end deffn
  2840. @deffn {Scheme Procedure} condition-has-type? c type
  2841. Return true if condition @var{c} has type @var{type}.
  2842. @end deffn
  2843. @deffn {Scheme Procedure} condition-ref c field-name
  2844. Return the value of the field named @var{field-name} from condition @var{c}.
  2845. If @var{c} is a compound condition and several underlying condition
  2846. types contain a field named @var{field-name}, then the value of the
  2847. first such field is returned, using the order in which conditions were
  2848. passed to @code{make-compound-condition}.
  2849. @end deffn
  2850. @deffn {Scheme Procedure} extract-condition c type
  2851. Return a condition of condition type @var{type} with the field values
  2852. specified by @var{c}.
  2853. If @var{c} is a compound condition, extract the field values from the
  2854. subcondition belonging to @var{type} that appeared first in the call to
  2855. @code{make-compound-condition} that created the condition.
  2856. @end deffn
  2857. Convenience macros are also available to create condition types and
  2858. conditions.
  2859. @deffn {library syntax} define-condition-type type supertype predicate field-spec...
  2860. Define a new condition type named @var{type} that inherits from
  2861. @var{supertype}. In addition, bind @var{predicate} to a type predicate
  2862. that returns true when passed a condition of type @var{type} or any of
  2863. its subtypes. @var{field-spec} must have the form @code{(field
  2864. accessor)} where @var{field} is the name of field of @var{type} and
  2865. @var{accessor} is the name of a procedure to access field @var{field} in
  2866. conditions of type @var{type}.
  2867. The example below defines condition type @code{&foo}, inheriting from
  2868. @code{&condition} with fields @code{a}, @code{b} and @code{c}:
  2869. @lisp
  2870. (define-condition-type &foo &condition
  2871. foo-condition?
  2872. (a foo-a)
  2873. (b foo-b)
  2874. (c foo-c))
  2875. @end lisp
  2876. @end deffn
  2877. @deffn {library syntax} condition type-field-binding1 type-field-binding2 @dots{}
  2878. Return a new condition or compound condition, initialized according to
  2879. @var{type-field-binding1} @var{type-field-binding2} @enddots{}. Each
  2880. @var{type-field-binding} must have the form @code{(type
  2881. field-specs...)}, where @var{type} is the name of a variable bound to a
  2882. condition type; each @var{field-spec} must have the form
  2883. @code{(field-name value)} where @var{field-name} is a symbol denoting
  2884. the field being initialized to @var{value}. As for
  2885. @code{make-condition}, all fields must be specified.
  2886. The following example returns a simple condition:
  2887. @lisp
  2888. (condition (&message (message "An error occurred")))
  2889. @end lisp
  2890. The one below returns a compound condition:
  2891. @lisp
  2892. (condition (&message (message "An error occurred"))
  2893. (&serious))
  2894. @end lisp
  2895. @end deffn
  2896. Finally, SRFI-35 defines a several standard condition types.
  2897. @defvar &condition
  2898. This condition type is the root of all condition types. It has no
  2899. fields.
  2900. @end defvar
  2901. @defvar &message
  2902. A condition type that carries a message describing the nature of the
  2903. condition to humans.
  2904. @end defvar
  2905. @deffn {Scheme Procedure} message-condition? c
  2906. Return true if @var{c} is of type @code{&message} or one of its
  2907. subtypes.
  2908. @end deffn
  2909. @deffn {Scheme Procedure} condition-message c
  2910. Return the message associated with message condition @var{c}.
  2911. @end deffn
  2912. @defvar &serious
  2913. This type describes conditions serious enough that they cannot safely be
  2914. ignored. It has no fields.
  2915. @end defvar
  2916. @deffn {Scheme Procedure} serious-condition? c
  2917. Return true if @var{c} is of type @code{&serious} or one of its
  2918. subtypes.
  2919. @end deffn
  2920. @defvar &error
  2921. This condition describes errors, typically caused by something that has
  2922. gone wrong in the interaction of the program with the external world or
  2923. the user.
  2924. @end defvar
  2925. @deffn {Scheme Procedure} error? c
  2926. Return true if @var{c} is of type @code{&error} or one of its subtypes.
  2927. @end deffn
  2928. As an implementation note, condition objects in Guile are the same as
  2929. ``exception objects''. @xref{Exception Objects}. The
  2930. @code{&condition}, @code{&serious}, and @code{&error} condition types
  2931. are known in core Guile as @code{&exception}, @code{&error}, and
  2932. @code{&external-error}, respectively.
  2933. @node SRFI-37
  2934. @subsection SRFI-37 - args-fold
  2935. @cindex SRFI-37
  2936. This is a processor for GNU @code{getopt_long}-style program
  2937. arguments. It provides an alternative, less declarative interface
  2938. than @code{getopt-long} in @code{(ice-9 getopt-long)}
  2939. (@pxref{getopt-long,,The (ice-9 getopt-long) Module}). Unlike
  2940. @code{getopt-long}, it supports repeated options and any number of
  2941. short and long names per option. Access it with:
  2942. @lisp
  2943. (use-modules (srfi srfi-37))
  2944. @end lisp
  2945. @acronym{SRFI}-37 principally provides an @code{option} type and the
  2946. @code{args-fold} function. To use the library, create a set of
  2947. options with @code{option} and use it as a specification for invoking
  2948. @code{args-fold}.
  2949. Here is an example of a simple argument processor for the typical
  2950. @samp{--version} and @samp{--help} options, which returns a backwards
  2951. list of files given on the command line:
  2952. @lisp
  2953. (args-fold (cdr (program-arguments))
  2954. (let ((display-and-exit-proc
  2955. (lambda (msg)
  2956. (lambda (opt name arg loads)
  2957. (display msg) (quit)))))
  2958. (list (option '(#\v "version") #f #f
  2959. (display-and-exit-proc "Foo version 42.0\n"))
  2960. (option '(#\h "help") #f #f
  2961. (display-and-exit-proc
  2962. "Usage: foo scheme-file ..."))))
  2963. (lambda (opt name arg loads)
  2964. (error "Unrecognized option `~A'" name))
  2965. (lambda (op loads) (cons op loads))
  2966. '())
  2967. @end lisp
  2968. @deffn {Scheme Procedure} option names required-arg? optional-arg? processor
  2969. Return an object that specifies a single kind of program option.
  2970. @var{names} is a list of command-line option names, and should consist of
  2971. characters for traditional @code{getopt} short options and strings for
  2972. @code{getopt_long}-style long options.
  2973. @var{required-arg?} and @var{optional-arg?} are mutually exclusive;
  2974. one or both must be @code{#f}. If @var{required-arg?}, the option
  2975. must be followed by an argument on the command line, such as
  2976. @samp{--opt=value} for long options, or an error will be signalled.
  2977. If @var{optional-arg?}, an argument will be taken if available.
  2978. @var{processor} is a procedure that takes at least 3 arguments, called
  2979. when @code{args-fold} encounters the option: the containing option
  2980. object, the name used on the command line, and the argument given for
  2981. the option (or @code{#f} if none). The rest of the arguments are
  2982. @code{args-fold} ``seeds'', and the @var{processor} should return
  2983. seeds as well.
  2984. @end deffn
  2985. @deffn {Scheme Procedure} option-names opt
  2986. @deffnx {Scheme Procedure} option-required-arg? opt
  2987. @deffnx {Scheme Procedure} option-optional-arg? opt
  2988. @deffnx {Scheme Procedure} option-processor opt
  2989. Return the specified field of @var{opt}, an option object, as
  2990. described above for @code{option}.
  2991. @end deffn
  2992. @deffn {Scheme Procedure} args-fold args options unrecognized-option-proc operand-proc seed @dots{}
  2993. Process @var{args}, a list of program arguments such as that returned by
  2994. @code{(cdr (program-arguments))}, in order against @var{options}, a list
  2995. of option objects as described above. All functions called take the
  2996. ``seeds'', or the last multiple-values as multiple arguments, starting
  2997. with @var{seed} @dots{}, and must return the new seeds. Return the
  2998. final seeds.
  2999. Call @code{unrecognized-option-proc}, which is like an option object's
  3000. processor, for any options not found in @var{options}.
  3001. Call @code{operand-proc} with any items on the command line that are
  3002. not named options. This includes arguments after @samp{--}. It is
  3003. called with the argument in question, as well as the seeds.
  3004. @end deffn
  3005. @node SRFI-38
  3006. @subsection SRFI-38 - External Representation for Data With Shared Structure
  3007. @cindex SRFI-38
  3008. This subsection is based on
  3009. @uref{http://srfi.schemers.org/srfi-38/srfi-38.html, the specification
  3010. of SRFI-38} written by Ray Dillinger.
  3011. @c Copyright (C) Ray Dillinger 2003. All Rights Reserved.
  3012. @c Permission is hereby granted, free of charge, to any person obtaining a
  3013. @c copy of this software and associated documentation files (the
  3014. @c "Software"), to deal in the Software without restriction, including
  3015. @c without limitation the rights to use, copy, modify, merge, publish,
  3016. @c distribute, sublicense, and/or sell copies of the Software, and to
  3017. @c permit persons to whom the Software is furnished to do so, subject to
  3018. @c the following conditions:
  3019. @c The above copyright notice and this permission notice shall be included
  3020. @c in all copies or substantial portions of the Software.
  3021. @c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  3022. @c OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  3023. @c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  3024. @c NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  3025. @c LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  3026. @c OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  3027. @c WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  3028. This SRFI creates an alternative external representation for data
  3029. written and read using @code{write-with-shared-structure} and
  3030. @code{read-with-shared-structure}. It is identical to the grammar for
  3031. external representation for data written and read with @code{write} and
  3032. @code{read} given in section 7 of R5RS, except that the single
  3033. production
  3034. @example
  3035. <datum> --> <simple datum> | <compound datum>
  3036. @end example
  3037. is replaced by the following five productions:
  3038. @example
  3039. <datum> --> <defining datum> | <nondefining datum> | <defined datum>
  3040. <defining datum> --> #<indexnum>=<nondefining datum>
  3041. <defined datum> --> #<indexnum>#
  3042. <nondefining datum> --> <simple datum> | <compound datum>
  3043. <indexnum> --> <digit 10>+
  3044. @end example
  3045. @deffn {Scheme procedure} write-with-shared-structure obj
  3046. @deffnx {Scheme procedure} write-with-shared-structure obj port
  3047. @deffnx {Scheme procedure} write-with-shared-structure obj port optarg
  3048. Writes an external representation of @var{obj} to the given port.
  3049. Strings that appear in the written representation are enclosed in
  3050. doublequotes, and within those strings backslash and doublequote
  3051. characters are escaped by backslashes. Character objects are written
  3052. using the @code{#\} notation.
  3053. Objects which denote locations rather than values (cons cells, vectors,
  3054. and non-zero-length strings in R5RS scheme; also Guile's structs,
  3055. bytevectors and ports and hash-tables), if they appear at more than one
  3056. point in the data being written, are preceded by @samp{#@var{N}=} the
  3057. first time they are written and replaced by @samp{#@var{N}#} all
  3058. subsequent times they are written, where @var{N} is a natural number
  3059. used to identify that particular object. If objects which denote
  3060. locations occur only once in the structure, then
  3061. @code{write-with-shared-structure} must produce the same external
  3062. representation for those objects as @code{write}.
  3063. @code{write-with-shared-structure} terminates in finite time and
  3064. produces a finite representation when writing finite data.
  3065. @code{write-with-shared-structure} returns an unspecified value. The
  3066. @var{port} argument may be omitted, in which case it defaults to the
  3067. value returned by @code{(current-output-port)}. The @var{optarg}
  3068. argument may also be omitted. If present, its effects on the output and
  3069. return value are unspecified but @code{write-with-shared-structure} must
  3070. still write a representation that can be read by
  3071. @code{read-with-shared-structure}. Some implementations may wish to use
  3072. @var{optarg} to specify formatting conventions, numeric radixes, or
  3073. return values. Guile's implementation ignores @var{optarg}.
  3074. For example, the code
  3075. @lisp
  3076. (begin (define a (cons 'val1 'val2))
  3077. (set-cdr! a a)
  3078. (write-with-shared-structure a))
  3079. @end lisp
  3080. should produce the output @code{#1=(val1 . #1#)}. This shows a cons
  3081. cell whose @code{cdr} contains itself.
  3082. @end deffn
  3083. @deffn {Scheme procedure} read-with-shared-structure
  3084. @deffnx {Scheme procedure} read-with-shared-structure port
  3085. @code{read-with-shared-structure} converts the external representations
  3086. of Scheme objects produced by @code{write-with-shared-structure} into
  3087. Scheme objects. That is, it is a parser for the nonterminal
  3088. @samp{<datum>} in the augmented external representation grammar defined
  3089. above. @code{read-with-shared-structure} returns the next object
  3090. parsable from the given input port, updating @var{port} to point to the
  3091. first character past the end of the external representation of the
  3092. object.
  3093. If an end-of-file is encountered in the input before any characters are
  3094. found that can begin an object, then an end-of-file object is returned.
  3095. The port remains open, and further attempts to read it (by
  3096. @code{read-with-shared-structure} or @code{read} will also return an
  3097. end-of-file object. If an end of file is encountered after the
  3098. beginning of an object's external representation, but the external
  3099. representation is incomplete and therefore not parsable, an error is
  3100. signalled.
  3101. The @var{port} argument may be omitted, in which case it defaults to the
  3102. value returned by @code{(current-input-port)}. It is an error to read
  3103. from a closed port.
  3104. @end deffn
  3105. @node SRFI-39
  3106. @subsection SRFI-39 - Parameters
  3107. @cindex SRFI-39
  3108. This SRFI adds support for dynamically-scoped parameters. SRFI 39 is
  3109. implemented in the Guile core; there's no module needed to get SRFI-39
  3110. itself. Parameters are documented in @ref{Parameters}.
  3111. This module does export one extra function: @code{with-parameters*}.
  3112. This is a Guile-specific addition to the SRFI, similar to the core
  3113. @code{with-fluids*} (@pxref{Fluids and Dynamic States}).
  3114. @defun with-parameters* param-list value-list thunk
  3115. Establish a new dynamic scope, as per @code{parameterize} above,
  3116. taking parameters from @var{param-list} and corresponding values from
  3117. @var{value-list}. A call @code{(@var{thunk})} is made in the new
  3118. scope and the result from that @var{thunk} is the return from
  3119. @code{with-parameters*}.
  3120. @end defun
  3121. @node SRFI-41
  3122. @subsection SRFI-41 - Streams
  3123. @cindex SRFI-41
  3124. This subsection is based on the
  3125. @uref{http://srfi.schemers.org/srfi-41/srfi-41.html, specification of
  3126. SRFI-41} by Philip L.@: Bewig.
  3127. @c The copyright notice and license text of the SRFI-41 specification is
  3128. @c reproduced below:
  3129. @c Copyright (C) Philip L. Bewig (2007). All Rights Reserved.
  3130. @c Permission is hereby granted, free of charge, to any person obtaining a
  3131. @c copy of this software and associated documentation files (the
  3132. @c "Software"), to deal in the Software without restriction, including
  3133. @c without limitation the rights to use, copy, modify, merge, publish,
  3134. @c distribute, sublicense, and/or sell copies of the Software, and to
  3135. @c permit persons to whom the Software is furnished to do so, subject to
  3136. @c the following conditions:
  3137. @c The above copyright notice and this permission notice shall be included
  3138. @c in all copies or substantial portions of the Software.
  3139. @c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  3140. @c OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  3141. @c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  3142. @c NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  3143. @c LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  3144. @c OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  3145. @c WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  3146. @noindent
  3147. This SRFI implements streams, sometimes called lazy lists, a sequential
  3148. data structure containing elements computed only on demand. A stream is
  3149. either null or is a pair with a stream in its cdr. Since elements of a
  3150. stream are computed only when accessed, streams can be infinite. Once
  3151. computed, the value of a stream element is cached in case it is needed
  3152. again. SRFI-41 can be made available with:
  3153. @example
  3154. (use-modules (srfi srfi-41))
  3155. @end example
  3156. @menu
  3157. * SRFI-41 Stream Fundamentals::
  3158. * SRFI-41 Stream Primitives::
  3159. * SRFI-41 Stream Library::
  3160. @end menu
  3161. @node SRFI-41 Stream Fundamentals
  3162. @subsubsection SRFI-41 Stream Fundamentals
  3163. SRFI-41 Streams are based on two mutually-recursive abstract data types:
  3164. An object of the @code{stream} abstract data type is a promise that,
  3165. when forced, is either @code{stream-null} or is an object of type
  3166. @code{stream-pair}. An object of the @code{stream-pair} abstract data
  3167. type contains a @code{stream-car} and a @code{stream-cdr}, which must be
  3168. a @code{stream}. The essential feature of streams is the systematic
  3169. suspensions of the recursive promises between the two data types.
  3170. The object stored in the @code{stream-car} of a @code{stream-pair} is a
  3171. promise that is forced the first time the @code{stream-car} is accessed;
  3172. its value is cached in case it is needed again. The object may have any
  3173. type, and different stream elements may have different types. If the
  3174. @code{stream-car} is never accessed, the object stored there is never
  3175. evaluated. Likewise, the @code{stream-cdr} is a promise to return a
  3176. stream, and is only forced on demand.
  3177. @node SRFI-41 Stream Primitives
  3178. @subsubsection SRFI-41 Stream Primitives
  3179. This library provides eight operators: constructors for
  3180. @code{stream-null} and @code{stream-pair}s, type predicates for streams
  3181. and the two kinds of streams, accessors for both fields of a
  3182. @code{stream-pair}, and a lambda that creates procedures that return
  3183. streams.
  3184. @defvr {Scheme Variable} stream-null
  3185. A promise that, when forced, is a single object, distinguishable from
  3186. all other objects, that represents the null stream. @code{stream-null}
  3187. is immutable and unique.
  3188. @end defvr
  3189. @deffn {Scheme Syntax} stream-cons object-expr stream-expr
  3190. Creates a newly-allocated stream containing a promise that, when forced,
  3191. is a @code{stream-pair} with @var{object-expr} in its @code{stream-car}
  3192. and @var{stream-expr} in its @code{stream-cdr}. Neither
  3193. @var{object-expr} nor @var{stream-expr} is evaluated when
  3194. @code{stream-cons} is called.
  3195. Once created, a @code{stream-pair} is immutable; there is no
  3196. @code{stream-set-car!} or @code{stream-set-cdr!} that modifies an
  3197. existing stream-pair. There is no dotted-pair or improper stream as
  3198. with lists.
  3199. @end deffn
  3200. @deffn {Scheme Procedure} stream? object
  3201. Returns true if @var{object} is a stream, otherwise returns false. If
  3202. @var{object} is a stream, its promise will not be forced. If
  3203. @code{(stream? obj)} returns true, then one of @code{(stream-null? obj)}
  3204. or @code{(stream-pair? obj)} will return true and the other will return
  3205. false.
  3206. @end deffn
  3207. @deffn {Scheme Procedure} stream-null? object
  3208. Returns true if @var{object} is the distinguished null stream, otherwise
  3209. returns false. If @var{object} is a stream, its promise will be forced.
  3210. @end deffn
  3211. @deffn {Scheme Procedure} stream-pair? object
  3212. Returns true if @var{object} is a @code{stream-pair} constructed by
  3213. @code{stream-cons}, otherwise returns false. If @var{object} is a
  3214. stream, its promise will be forced.
  3215. @end deffn
  3216. @deffn {Scheme Procedure} stream-car stream
  3217. Returns the object stored in the @code{stream-car} of @var{stream}. An
  3218. error is signalled if the argument is not a @code{stream-pair}. This
  3219. causes the @var{object-expr} passed to @code{stream-cons} to be
  3220. evaluated if it had not yet been; the value is cached in case it is
  3221. needed again.
  3222. @end deffn
  3223. @deffn {Scheme Procedure} stream-cdr stream
  3224. Returns the stream stored in the @code{stream-cdr} of @var{stream}. An
  3225. error is signalled if the argument is not a @code{stream-pair}.
  3226. @end deffn
  3227. @deffn {Scheme Syntax} stream-lambda formals body @dots{}
  3228. Creates a procedure that returns a promise to evaluate the @var{body} of
  3229. the procedure. The last @var{body} expression to be evaluated must
  3230. yield a stream. As with normal @code{lambda}, @var{formals} may be a
  3231. single variable name, in which case all the formal arguments are
  3232. collected into a single list, or a list of variable names, which may be
  3233. null if there are no arguments, proper if there are an exact number of
  3234. arguments, or dotted if a fixed number of arguments is to be followed by
  3235. zero or more arguments collected into a list. @var{Body} must contain
  3236. at least one expression, and may contain internal definitions preceding
  3237. any expressions to be evaluated.
  3238. @end deffn
  3239. @example
  3240. (define strm123
  3241. (stream-cons 1
  3242. (stream-cons 2
  3243. (stream-cons 3
  3244. stream-null))))
  3245. (stream-car strm123) @result{} 1
  3246. (stream-car (stream-cdr strm123) @result{} 2
  3247. (stream-pair?
  3248. (stream-cdr
  3249. (stream-cons (/ 1 0) stream-null))) @result{} #f
  3250. (stream? (list 1 2 3)) @result{} #f
  3251. (define iter
  3252. (stream-lambda (f x)
  3253. (stream-cons x (iter f (f x)))))
  3254. (define nats (iter (lambda (x) (+ x 1)) 0))
  3255. (stream-car (stream-cdr nats)) @result{} 1
  3256. (define stream-add
  3257. (stream-lambda (s1 s2)
  3258. (stream-cons
  3259. (+ (stream-car s1) (stream-car s2))
  3260. (stream-add (stream-cdr s1)
  3261. (stream-cdr s2)))))
  3262. (define evens (stream-add nats nats))
  3263. (stream-car evens) @result{} 0
  3264. (stream-car (stream-cdr evens)) @result{} 2
  3265. (stream-car (stream-cdr (stream-cdr evens))) @result{} 4
  3266. @end example
  3267. @node SRFI-41 Stream Library
  3268. @subsubsection SRFI-41 Stream Library
  3269. @deffn {Scheme Syntax} define-stream (name args @dots{}) body @dots{}
  3270. Creates a procedure that returns a stream, and may appear anywhere a
  3271. normal @code{define} may appear, including as an internal definition.
  3272. It may contain internal definitions of its own. The defined procedure
  3273. takes arguments in the same way as @code{stream-lambda}.
  3274. @code{define-stream} is syntactic sugar on @code{stream-lambda}; see
  3275. also @code{stream-let}, which is also a sugaring of
  3276. @code{stream-lambda}.
  3277. A simple version of @code{stream-map} that takes only a single input
  3278. stream calls itself recursively:
  3279. @example
  3280. (define-stream (stream-map proc strm)
  3281. (if (stream-null? strm)
  3282. stream-null
  3283. (stream-cons
  3284. (proc (stream-car strm))
  3285. (stream-map proc (stream-cdr strm))))))
  3286. @end example
  3287. @end deffn
  3288. @deffn {Scheme Procedure} list->stream list
  3289. Returns a newly-allocated stream containing the elements from
  3290. @var{list}.
  3291. @end deffn
  3292. @deffn {Scheme Procedure} port->stream [port]
  3293. Returns a newly-allocated stream containing in its elements the
  3294. characters on the port. If @var{port} is not given it defaults to the
  3295. current input port. The returned stream has finite length and is
  3296. terminated by @code{stream-null}.
  3297. It looks like one use of @code{port->stream} would be this:
  3298. @example
  3299. (define s ;wrong!
  3300. (with-input-from-file filename
  3301. (lambda () (port->stream))))
  3302. @end example
  3303. But that fails, because @code{with-input-from-file} is eager, and closes
  3304. the input port prematurely, before the first character is read. To read
  3305. a file into a stream, say:
  3306. @example
  3307. (define-stream (file->stream filename)
  3308. (let ((p (open-input-file filename)))
  3309. (stream-let loop ((c (read-char p)))
  3310. (if (eof-object? c)
  3311. (begin (close-input-port p)
  3312. stream-null)
  3313. (stream-cons c
  3314. (loop (read-char p)))))))
  3315. @end example
  3316. @end deffn
  3317. @deffn {Scheme Syntax} stream object-expr @dots{}
  3318. Creates a newly-allocated stream containing in its elements the objects,
  3319. in order. The @var{object-expr}s are evaluated when they are accessed,
  3320. not when the stream is created. If no objects are given, as in
  3321. (stream), the null stream is returned. See also @code{list->stream}.
  3322. @example
  3323. (define strm123 (stream 1 2 3))
  3324. ; (/ 1 0) not evaluated when stream is created
  3325. (define s (stream 1 (/ 1 0) -1))
  3326. @end example
  3327. @end deffn
  3328. @deffn {Scheme Procedure} stream->list [n] stream
  3329. Returns a newly-allocated list containing in its elements the first
  3330. @var{n} items in @var{stream}. If @var{stream} has less than @var{n}
  3331. items, all the items in the stream will be included in the returned
  3332. list. If @var{n} is not given it defaults to infinity, which means that
  3333. unless @var{stream} is finite @code{stream->list} will never return.
  3334. @example
  3335. (stream->list 10
  3336. (stream-map (lambda (x) (* x x))
  3337. (stream-from 0)))
  3338. @result{} (0 1 4 9 16 25 36 49 64 81)
  3339. @end example
  3340. @end deffn
  3341. @deffn {Scheme Procedure} stream-append stream @dots{}
  3342. Returns a newly-allocated stream containing in its elements those
  3343. elements contained in its input @var{stream}s, in order of input. If
  3344. any of the input streams is infinite, no elements of any of the
  3345. succeeding input streams will appear in the output stream. See also
  3346. @code{stream-concat}.
  3347. @end deffn
  3348. @deffn {Scheme Procedure} stream-concat stream
  3349. Takes a @var{stream} consisting of one or more streams and returns a
  3350. newly-allocated stream containing all the elements of the input streams.
  3351. If any of the streams in the input @var{stream} is infinite, any
  3352. remaining streams in the input stream will never appear in the output
  3353. stream. See also @code{stream-append}.
  3354. @end deffn
  3355. @deffn {Scheme Procedure} stream-constant object @dots{}
  3356. Returns a newly-allocated stream containing in its elements the
  3357. @var{object}s, repeating in succession forever.
  3358. @example
  3359. (stream-constant 1) @result{} 1 1 1 @dots{}
  3360. (stream-constant #t #f) @result{} #t #f #t #f #t #f @dots{}
  3361. @end example
  3362. @end deffn
  3363. @deffn {Scheme Procedure} stream-drop n stream
  3364. Returns the suffix of the input @var{stream} that starts at the next
  3365. element after the first @var{n} elements. The output stream shares
  3366. structure with the input @var{stream}; thus, promises forced in one
  3367. instance of the stream are also forced in the other instance of the
  3368. stream. If the input @var{stream} has less than @var{n} elements,
  3369. @code{stream-drop} returns the null stream. See also
  3370. @code{stream-take}.
  3371. @end deffn
  3372. @deffn {Scheme Procedure} stream-drop-while pred stream
  3373. Returns the suffix of the input @var{stream} that starts at the first
  3374. element @var{x} for which @code{(pred x)} returns false. The output
  3375. stream shares structure with the input @var{stream}. See also
  3376. @code{stream-take-while}.
  3377. @end deffn
  3378. @deffn {Scheme Procedure} stream-filter pred stream
  3379. Returns a newly-allocated stream that contains only those elements
  3380. @var{x} of the input @var{stream} which satisfy the predicate
  3381. @code{pred}.
  3382. @example
  3383. (stream-filter odd? (stream-from 0))
  3384. @result{} 1 3 5 7 9 @dots{}
  3385. @end example
  3386. @end deffn
  3387. @deffn {Scheme Procedure} stream-fold proc base stream
  3388. Applies a binary procedure @var{proc} to @var{base} and the first
  3389. element of @var{stream} to compute a new @var{base}, then applies the
  3390. procedure to the new @var{base} and the next element of @var{stream} to
  3391. compute a succeeding @var{base}, and so on, accumulating a value that is
  3392. finally returned as the value of @code{stream-fold} when the end of the
  3393. stream is reached. @var{stream} must be finite, or @code{stream-fold}
  3394. will enter an infinite loop. See also @code{stream-scan}, which is
  3395. similar to @code{stream-fold}, but useful for infinite streams. For
  3396. readers familiar with other functional languages, this is a left-fold;
  3397. there is no corresponding right-fold, since right-fold relies on finite
  3398. streams that are fully-evaluated, in which case they may as well be
  3399. converted to a list.
  3400. @end deffn
  3401. @deffn {Scheme Procedure} stream-for-each proc stream @dots{}
  3402. Applies @var{proc} element-wise to corresponding elements of the input
  3403. @var{stream}s for side-effects; it returns nothing.
  3404. @code{stream-for-each} stops as soon as any of its input streams is
  3405. exhausted.
  3406. @end deffn
  3407. @deffn {Scheme Procedure} stream-from first [step]
  3408. Creates a newly-allocated stream that contains @var{first} as its first
  3409. element and increments each succeeding element by @var{step}. If
  3410. @var{step} is not given it defaults to 1. @var{first} and @var{step}
  3411. may be of any numeric type. @code{stream-from} is frequently useful as
  3412. a generator in @code{stream-of} expressions. See also
  3413. @code{stream-range} for a similar procedure that creates finite streams.
  3414. @end deffn
  3415. @deffn {Scheme Procedure} stream-iterate proc base
  3416. Creates a newly-allocated stream containing @var{base} in its first
  3417. element and applies @var{proc} to each element in turn to determine the
  3418. succeeding element. See also @code{stream-unfold} and
  3419. @code{stream-unfolds}.
  3420. @end deffn
  3421. @deffn {Scheme Procedure} stream-length stream
  3422. Returns the number of elements in the @var{stream}; it does not evaluate
  3423. its elements. @code{stream-length} may only be used on finite streams;
  3424. it enters an infinite loop with infinite streams.
  3425. @end deffn
  3426. @deffn {Scheme Syntax} stream-let tag ((var expr) @dots{}) body @dots{}
  3427. Creates a local scope that binds each variable to the value of its
  3428. corresponding expression. It additionally binds @var{tag} to a
  3429. procedure which takes the bound variables as arguments and @var{body} as
  3430. its defining expressions, binding the @var{tag} with
  3431. @code{stream-lambda}. @var{tag} is in scope within body, and may be
  3432. called recursively. When the expanded expression defined by the
  3433. @code{stream-let} is evaluated, @code{stream-let} evaluates the
  3434. expressions in its @var{body} in an environment containing the
  3435. newly-bound variables, returning the value of the last expression
  3436. evaluated, which must yield a stream.
  3437. @code{stream-let} provides syntactic sugar on @code{stream-lambda}, in
  3438. the same manner as normal @code{let} provides syntactic sugar on normal
  3439. @code{lambda}. However, unlike normal @code{let}, the @var{tag} is
  3440. required, not optional, because unnamed @code{stream-let} is
  3441. meaningless.
  3442. For example, @code{stream-member} returns the first @code{stream-pair}
  3443. of the input @var{strm} with a @code{stream-car} @var{x} that satisfies
  3444. @code{(eql? obj x)}, or the null stream if @var{x} is not present in
  3445. @var{strm}.
  3446. @example
  3447. (define-stream (stream-member eql? obj strm)
  3448. (stream-let loop ((strm strm))
  3449. (cond ((stream-null? strm) strm)
  3450. ((eql? obj (stream-car strm)) strm)
  3451. (else (loop (stream-cdr strm))))))
  3452. @end example
  3453. @end deffn
  3454. @deffn {Scheme Procedure} stream-map proc stream @dots{}
  3455. Applies @var{proc} element-wise to corresponding elements of the input
  3456. @var{stream}s, returning a newly-allocated stream containing elements
  3457. that are the results of those procedure applications. The output stream
  3458. has as many elements as the minimum-length input stream, and may be
  3459. infinite.
  3460. @end deffn
  3461. @deffn {Scheme Syntax} stream-match stream clause @dots{}
  3462. Provides pattern-matching for streams. The input @var{stream} is an
  3463. expression that evaluates to a stream. Clauses are of the form
  3464. @code{(pattern [fender] expression)}, consisting of a @var{pattern} that
  3465. matches a stream of a particular shape, an optional @var{fender} that
  3466. must succeed if the pattern is to match, and an @var{expression} that is
  3467. evaluated if the pattern matches. There are four types of patterns:
  3468. @itemize @bullet
  3469. @item
  3470. () matches the null stream.
  3471. @item
  3472. (@var{pat0} @var{pat1} @dots{}) matches a finite stream with length
  3473. exactly equal to the number of pattern elements.
  3474. @item
  3475. (@var{pat0} @var{pat1} @dots{} @code{.} @var{pat-rest}) matches an
  3476. infinite stream, or a finite stream with length at least as great as the
  3477. number of pattern elements before the literal dot.
  3478. @item
  3479. @var{pat} matches an entire stream. Should always appear last in the
  3480. list of clauses; it's not an error to appear elsewhere, but subsequent
  3481. clauses could never match.
  3482. @end itemize
  3483. Each pattern element may be either:
  3484. @itemize @bullet
  3485. @item
  3486. An identifier, which matches any stream element. Additionally, the
  3487. value of the stream element is bound to the variable named by the
  3488. identifier, which is in scope in the @var{fender} and @var{expression}
  3489. of the corresponding @var{clause}. Each identifier in a single pattern
  3490. must be unique.
  3491. @item
  3492. A literal underscore (@code{_}), which matches any stream element but
  3493. creates no bindings.
  3494. @end itemize
  3495. The @var{pattern}s are tested in order, left-to-right, until a matching
  3496. pattern is found; if @var{fender} is present, it must evaluate to a true
  3497. value for the match to be successful. Pattern variables are bound in
  3498. the corresponding @var{fender} and @var{expression}. Once the matching
  3499. @var{pattern} is found, the corresponding @var{expression} is evaluated
  3500. and returned as the result of the match. An error is signaled if no
  3501. pattern matches the input @var{stream}.
  3502. @code{stream-match} is often used to distinguish null streams from
  3503. non-null streams, binding @var{head} and @var{tail}:
  3504. @example
  3505. (define (len strm)
  3506. (stream-match strm
  3507. (() 0)
  3508. ((head . tail) (+ 1 (len tail)))))
  3509. @end example
  3510. Fenders can test the common case where two stream elements must be
  3511. identical; the @code{else} pattern is an identifier bound to the entire
  3512. stream, not a keyword as in @code{cond}.
  3513. @example
  3514. (stream-match strm
  3515. ((x y . _) (equal? x y) 'ok)
  3516. (else 'error))
  3517. @end example
  3518. A more complex example uses two nested matchers to match two different
  3519. stream arguments; @code{(stream-merge lt? . strms)} stably merges two or
  3520. more streams ordered by the @code{lt?} predicate:
  3521. @example
  3522. (define-stream (stream-merge lt? . strms)
  3523. (define-stream (merge xx yy)
  3524. (stream-match xx (() yy) ((x . xs)
  3525. (stream-match yy (() xx) ((y . ys)
  3526. (if (lt? y x)
  3527. (stream-cons y (merge xx ys))
  3528. (stream-cons x (merge xs yy))))))))
  3529. (stream-let loop ((strms strms))
  3530. (cond ((null? strms) stream-null)
  3531. ((null? (cdr strms)) (car strms))
  3532. (else (merge (car strms)
  3533. (apply stream-merge lt?
  3534. (cdr strms)))))))
  3535. @end example
  3536. @end deffn
  3537. @deffn {Scheme Syntax} stream-of expr clause @dots{}
  3538. Provides the syntax of stream comprehensions, which generate streams by
  3539. means of looping expressions. The result is a stream of objects of the
  3540. type returned by @var{expr}. There are four types of clauses:
  3541. @itemize @bullet
  3542. @item
  3543. (@var{var} @code{in} @var{stream-expr}) loops over the elements of
  3544. @var{stream-expr}, in order from the start of the stream, binding each
  3545. element of the stream in turn to @var{var}. @code{stream-from} and
  3546. @code{stream-range} are frequently useful as generators for
  3547. @var{stream-expr}.
  3548. @item
  3549. (@var{var} @code{is} @var{expr}) binds @var{var} to the value obtained
  3550. by evaluating @var{expr}.
  3551. @item
  3552. (@var{pred} @var{expr}) includes in the output stream only those
  3553. elements @var{x} which satisfy the predicate @var{pred}.
  3554. @end itemize
  3555. The scope of variables bound in the stream comprehension is the clauses
  3556. to the right of the binding clause (but not the binding clause itself)
  3557. plus the result expression.
  3558. When two or more generators are present, the loops are processed as if
  3559. they are nested from left to right; that is, the rightmost generator
  3560. varies fastest. A consequence of this is that only the first generator
  3561. may be infinite and all subsequent generators must be finite. If no
  3562. generators are present, the result of a stream comprehension is a stream
  3563. containing the result expression; thus, @samp{(stream-of 1)} produces a
  3564. finite stream containing only the element 1.
  3565. @example
  3566. (stream-of (* x x)
  3567. (x in (stream-range 0 10))
  3568. (even? x))
  3569. @result{} 0 4 16 36 64
  3570. (stream-of (list a b)
  3571. (a in (stream-range 1 4))
  3572. (b in (stream-range 1 3)))
  3573. @result{} (1 1) (1 2) (2 1) (2 2) (3 1) (3 2)
  3574. (stream-of (list i j)
  3575. (i in (stream-range 1 5))
  3576. (j in (stream-range (+ i 1) 5)))
  3577. @result{} (1 2) (1 3) (1 4) (2 3) (2 4) (3 4)
  3578. @end example
  3579. @end deffn
  3580. @deffn {Scheme Procedure} stream-range first past [step]
  3581. Creates a newly-allocated stream that contains @var{first} as its first
  3582. element and increments each succeeding element by @var{step}. The
  3583. stream is finite and ends before @var{past}, which is not an element of
  3584. the stream. If @var{step} is not given it defaults to 1 if @var{first}
  3585. is less than past and -1 otherwise. @var{first}, @var{past} and
  3586. @var{step} may be of any real numeric type. @code{stream-range} is
  3587. frequently useful as a generator in @code{stream-of} expressions. See
  3588. also @code{stream-from} for a similar procedure that creates infinite
  3589. streams.
  3590. @example
  3591. (stream-range 0 10) @result{} 0 1 2 3 4 5 6 7 8 9
  3592. (stream-range 0 10 2) @result{} 0 2 4 6 8
  3593. @end example
  3594. Successive elements of the stream are calculated by adding @var{step} to
  3595. @var{first}, so if any of @var{first}, @var{past} or @var{step} are
  3596. inexact, the length of the output stream may differ from
  3597. @code{(ceiling (- (/ (- past first) step) 1)}.
  3598. @end deffn
  3599. @deffn {Scheme Procedure} stream-ref stream n
  3600. Returns the @var{n}th element of stream, counting from zero. An error
  3601. is signaled if @var{n} is greater than or equal to the length of stream.
  3602. @example
  3603. (define (fact n)
  3604. (stream-ref
  3605. (stream-scan * 1 (stream-from 1))
  3606. n))
  3607. @end example
  3608. @end deffn
  3609. @deffn {Scheme Procedure} stream-reverse stream
  3610. Returns a newly-allocated stream containing the elements of the input
  3611. @var{stream} but in reverse order. @code{stream-reverse} may only be
  3612. used with finite streams; it enters an infinite loop with infinite
  3613. streams. @code{stream-reverse} does not force evaluation of the
  3614. elements of the stream.
  3615. @end deffn
  3616. @deffn {Scheme Procedure} stream-scan proc base stream
  3617. Accumulates the partial folds of an input @var{stream} into a
  3618. newly-allocated output stream. The output stream is the @var{base}
  3619. followed by @code{(stream-fold proc base (stream-take i stream))} for
  3620. each of the first @var{i} elements of @var{stream}.
  3621. @example
  3622. (stream-scan + 0 (stream-from 1))
  3623. @result{} (stream 0 1 3 6 10 15 @dots{})
  3624. (stream-scan * 1 (stream-from 1))
  3625. @result{} (stream 1 1 2 6 24 120 @dots{})
  3626. @end example
  3627. @end deffn
  3628. @deffn {Scheme Procedure} stream-take n stream
  3629. Returns a newly-allocated stream containing the first @var{n} elements
  3630. of the input @var{stream}. If the input @var{stream} has less than
  3631. @var{n} elements, so does the output stream. See also
  3632. @code{stream-drop}.
  3633. @end deffn
  3634. @deffn {Scheme Procedure} stream-take-while pred stream
  3635. Takes a predicate and a @code{stream} and returns a newly-allocated
  3636. stream containing those elements @code{x} that form the maximal prefix
  3637. of the input stream which satisfy @var{pred}. See also
  3638. @code{stream-drop-while}.
  3639. @end deffn
  3640. @deffn {Scheme Procedure} stream-unfold map pred gen base
  3641. The fundamental recursive stream constructor. It constructs a stream by
  3642. repeatedly applying @var{gen} to successive values of @var{base}, in the
  3643. manner of @code{stream-iterate}, then applying @var{map} to each of the
  3644. values so generated, appending each of the mapped values to the output
  3645. stream as long as @code{(pred? base)} returns a true value. See also
  3646. @code{stream-iterate} and @code{stream-unfolds}.
  3647. The expression below creates the finite stream @samp{0 1 4 9 16 25 36 49
  3648. 64 81}. Initially the @var{base} is 0, which is less than 10, so
  3649. @var{map} squares the @var{base} and the mapped value becomes the first
  3650. element of the output stream. Then @var{gen} increments the @var{base}
  3651. by 1, so it becomes 1; this is less than 10, so @var{map} squares the
  3652. new @var{base} and 1 becomes the second element of the output stream.
  3653. And so on, until the base becomes 10, when @var{pred} stops the
  3654. recursion and stream-null ends the output stream.
  3655. @example
  3656. (stream-unfold
  3657. (lambda (x) (expt x 2)) ; map
  3658. (lambda (x) (< x 10)) ; pred?
  3659. (lambda (x) (+ x 1)) ; gen
  3660. 0) ; base
  3661. @end example
  3662. @end deffn
  3663. @deffn {Scheme Procedure} stream-unfolds proc seed
  3664. Returns @var{n} newly-allocated streams containing those elements
  3665. produced by successive calls to the generator @var{proc}, which takes
  3666. the current @var{seed} as its argument and returns @var{n}+1 values
  3667. (@var{proc} @var{seed}) @result{} @var{seed} @var{result_0} @dots{} @var{result_n-1}
  3668. where the returned @var{seed} is the input @var{seed} to the next call
  3669. to the generator and @var{result_i} indicates how to produce the next
  3670. element of the @var{i}th result stream:
  3671. @itemize @bullet
  3672. @item
  3673. (@var{value}): @var{value} is the next car of the result stream.
  3674. @item
  3675. @code{#f}: no value produced by this iteration of the generator
  3676. @var{proc} for the result stream.
  3677. @item
  3678. (): the end of the result stream.
  3679. @end itemize
  3680. It may require multiple calls of @var{proc} to produce the next element
  3681. of any particular result stream. See also @code{stream-iterate} and
  3682. @code{stream-unfold}.
  3683. @example
  3684. (define (stream-partition pred? strm)
  3685. (stream-unfolds
  3686. (lambda (s)
  3687. (if (stream-null? s)
  3688. (values s '() '())
  3689. (let ((a (stream-car s))
  3690. (d (stream-cdr s)))
  3691. (if (pred? a)
  3692. (values d (list a) #f)
  3693. (values d #f (list a))))))
  3694. strm))
  3695. (call-with-values
  3696. (lambda ()
  3697. (stream-partition odd?
  3698. (stream-range 1 6)))
  3699. (lambda (odds evens)
  3700. (list (stream->list odds)
  3701. (stream->list evens))))
  3702. @result{} ((1 3 5) (2 4))
  3703. @end example
  3704. @end deffn
  3705. @deffn {Scheme Procedure} stream-zip stream @dots{}
  3706. Returns a newly-allocated stream in which each element is a list (not a
  3707. stream) of the corresponding elements of the input @var{stream}s. The
  3708. output stream is as long as the shortest input @var{stream}, if any of
  3709. the input @var{stream}s is finite, or is infinite if all the input
  3710. @var{stream}s are infinite.
  3711. @end deffn
  3712. @node SRFI-42
  3713. @subsection SRFI-42 - Eager Comprehensions
  3714. @cindex SRFI-42
  3715. See @uref{http://srfi.schemers.org/srfi-42/srfi-42.html, the
  3716. specification of SRFI-42}.
  3717. @node SRFI-43
  3718. @subsection SRFI-43 - Vector Library
  3719. @cindex SRFI-43
  3720. This subsection is based on the
  3721. @uref{http://srfi.schemers.org/srfi-43/srfi-43.html, specification of
  3722. SRFI-43} by Taylor Campbell.
  3723. @c The copyright notice and license text of the SRFI-43 specification is
  3724. @c reproduced below:
  3725. @c Copyright (C) Taylor Campbell (2003). All Rights Reserved.
  3726. @c Permission is hereby granted, free of charge, to any person obtaining a
  3727. @c copy of this software and associated documentation files (the
  3728. @c "Software"), to deal in the Software without restriction, including
  3729. @c without limitation the rights to use, copy, modify, merge, publish,
  3730. @c distribute, sublicense, and/or sell copies of the Software, and to
  3731. @c permit persons to whom the Software is furnished to do so, subject to
  3732. @c the following conditions:
  3733. @c The above copyright notice and this permission notice shall be included
  3734. @c in all copies or substantial portions of the Software.
  3735. @c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  3736. @c OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  3737. @c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  3738. @c NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  3739. @c LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  3740. @c OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  3741. @c WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  3742. @noindent
  3743. SRFI-43 implements a comprehensive library of vector operations. It can
  3744. be made available with:
  3745. @example
  3746. (use-modules (srfi srfi-43))
  3747. @end example
  3748. @menu
  3749. * SRFI-43 Constructors::
  3750. * SRFI-43 Predicates::
  3751. * SRFI-43 Selectors::
  3752. * SRFI-43 Iteration::
  3753. * SRFI-43 Searching::
  3754. * SRFI-43 Mutators::
  3755. * SRFI-43 Conversion::
  3756. @end menu
  3757. @node SRFI-43 Constructors
  3758. @subsubsection SRFI-43 Constructors
  3759. @deffn {Scheme Procedure} make-vector size [fill]
  3760. Create and return a vector of size @var{size}, optionally filling it
  3761. with @var{fill}. The default value of @var{fill} is unspecified.
  3762. @example
  3763. (make-vector 5 3) @result{} #(3 3 3 3 3)
  3764. @end example
  3765. @end deffn
  3766. @deffn {Scheme Procedure} vector x @dots{}
  3767. Create and return a vector whose elements are @var{x} @enddots{}.
  3768. @example
  3769. (vector 0 1 2 3 4) @result{} #(0 1 2 3 4)
  3770. @end example
  3771. @end deffn
  3772. @deffn {Scheme Procedure} vector-unfold f length initial-seed @dots{}
  3773. The fundamental vector constructor. Create a vector whose length
  3774. is @var{length} and iterates across each index k from 0 up to
  3775. @var{length} - 1, applying @var{f} at each iteration to the current
  3776. index and current seeds, in that order, to receive n + 1 values: the
  3777. element to put in the kth slot of the new vector, and n new seeds for
  3778. the next iteration. It is an error for the number of seeds to vary
  3779. between iterations.
  3780. @example
  3781. (vector-unfold (lambda (i x) (values x (- x 1)))
  3782. 10 0)
  3783. @result{} #(0 -1 -2 -3 -4 -5 -6 -7 -8 -9)
  3784. (vector-unfold values 10)
  3785. @result{} #(0 1 2 3 4 5 6 7 8 9)
  3786. @end example
  3787. @end deffn
  3788. @deffn {Scheme Procedure} vector-unfold-right f length initial-seed @dots{}
  3789. Like @code{vector-unfold}, but it uses @var{f} to generate elements from
  3790. right-to-left, rather than left-to-right.
  3791. @example
  3792. (vector-unfold-right (lambda (i x) (values x (+ x 1)))
  3793. 10 0)
  3794. @result{} #(9 8 7 6 5 4 3 2 1 0)
  3795. @end example
  3796. @end deffn
  3797. @deffn {Scheme Procedure} vector-copy vec [start [end [fill]]]
  3798. Allocate a new vector whose length is @var{end} - @var{start} and fills
  3799. it with elements from @var{vec}, taking elements from @var{vec} starting
  3800. at index @var{start} and stopping at index @var{end}. @var{start}
  3801. defaults to 0 and @var{end} defaults to the value of
  3802. @code{(vector-length vec)}. If @var{end} extends beyond the length of
  3803. @var{vec}, the slots in the new vector that obviously cannot be filled
  3804. by elements from @var{vec} are filled with @var{fill}, whose default
  3805. value is unspecified.
  3806. @example
  3807. (vector-copy '#(a b c d e f g h i))
  3808. @result{} #(a b c d e f g h i)
  3809. (vector-copy '#(a b c d e f g h i) 6)
  3810. @result{} #(g h i)
  3811. (vector-copy '#(a b c d e f g h i) 3 6)
  3812. @result{} #(d e f)
  3813. (vector-copy '#(a b c d e f g h i) 6 12 'x)
  3814. @result{} #(g h i x x x)
  3815. @end example
  3816. @end deffn
  3817. @deffn {Scheme Procedure} vector-reverse-copy vec [start [end]]
  3818. Like @code{vector-copy}, but it copies the elements in the reverse order
  3819. from @var{vec}.
  3820. @example
  3821. (vector-reverse-copy '#(5 4 3 2 1 0) 1 5)
  3822. @result{} #(1 2 3 4)
  3823. @end example
  3824. @end deffn
  3825. @deffn {Scheme Procedure} vector-append vec @dots{}
  3826. Return a newly allocated vector that contains all elements in order from
  3827. the subsequent locations in @var{vec} @enddots{}.
  3828. @example
  3829. (vector-append '#(a) '#(b c d))
  3830. @result{} #(a b c d)
  3831. @end example
  3832. @end deffn
  3833. @deffn {Scheme Procedure} vector-concatenate list-of-vectors
  3834. Append each vector in @var{list-of-vectors}. Equivalent to
  3835. @code{(apply vector-append list-of-vectors)}.
  3836. @example
  3837. (vector-concatenate '(#(a b) #(c d)))
  3838. @result{} #(a b c d)
  3839. @end example
  3840. @end deffn
  3841. @node SRFI-43 Predicates
  3842. @subsubsection SRFI-43 Predicates
  3843. @deffn {Scheme Procedure} vector? obj
  3844. Return true if @var{obj} is a vector, else return false.
  3845. @end deffn
  3846. @deffn {Scheme Procedure} vector-empty? vec
  3847. Return true if @var{vec} is empty, i.e. its length is 0, else return
  3848. false.
  3849. @end deffn
  3850. @deffn {Scheme Procedure} vector= elt=? vec @dots{}
  3851. Return true if the vectors @var{vec} @dots{} have equal lengths and
  3852. equal elements according to @var{elt=?}. @var{elt=?} is always applied
  3853. to two arguments. Element comparison must be consistent with @code{eq?}
  3854. in the following sense: if @code{(eq? a b)} returns true, then
  3855. @code{(elt=? a b)} must also return true. The order in which
  3856. comparisons are performed is unspecified.
  3857. @end deffn
  3858. @node SRFI-43 Selectors
  3859. @subsubsection SRFI-43 Selectors
  3860. @deffn {Scheme Procedure} vector-ref vec i
  3861. Return the element at index @var{i} in @var{vec}. Indexing is based on
  3862. zero.
  3863. @end deffn
  3864. @deffn {Scheme Procedure} vector-length vec
  3865. Return the length of @var{vec}.
  3866. @end deffn
  3867. @node SRFI-43 Iteration
  3868. @subsubsection SRFI-43 Iteration
  3869. @deffn {Scheme Procedure} vector-fold kons knil vec1 vec2 @dots{}
  3870. The fundamental vector iterator. @var{kons} is iterated over each index
  3871. in all of the vectors, stopping at the end of the shortest; @var{kons}
  3872. is applied as
  3873. @smalllisp
  3874. (kons i state (vector-ref vec1 i) (vector-ref vec2 i) ...)
  3875. @end smalllisp
  3876. where @var{state} is the current state value, and @var{i} is the current
  3877. index. The current state value begins with @var{knil}, and becomes
  3878. whatever @var{kons} returned at the respective iteration. The iteration
  3879. is strictly left-to-right.
  3880. @end deffn
  3881. @deffn {Scheme Procedure} vector-fold-right kons knil vec1 vec2 @dots{}
  3882. Similar to @code{vector-fold}, but it iterates right-to-left instead of
  3883. left-to-right.
  3884. @end deffn
  3885. @deffn {Scheme Procedure} vector-map f vec1 vec2 @dots{}
  3886. Return a new vector of the shortest size of the vector arguments. Each
  3887. element at index i of the new vector is mapped from the old vectors by
  3888. @smalllisp
  3889. (f i (vector-ref vec1 i) (vector-ref vec2 i) ...)
  3890. @end smalllisp
  3891. The dynamic order of application of @var{f} is unspecified.
  3892. @end deffn
  3893. @deffn {Scheme Procedure} vector-map! f vec1 vec2 @dots{}
  3894. Similar to @code{vector-map}, but rather than mapping the new elements
  3895. into a new vector, the new mapped elements are destructively inserted
  3896. into @var{vec1}. The dynamic order of application of @var{f} is
  3897. unspecified.
  3898. @end deffn
  3899. @deffn {Scheme Procedure} vector-for-each f vec1 vec2 @dots{}
  3900. Call @code{(f i (vector-ref vec1 i) (vector-ref vec2 i) ...)} for each
  3901. index i less than the length of the shortest vector passed. The
  3902. iteration is strictly left-to-right.
  3903. @end deffn
  3904. @deffn {Scheme Procedure} vector-count pred? vec1 vec2 @dots{}
  3905. Count the number of parallel elements in the vectors that satisfy
  3906. @var{pred?}, which is applied, for each index i less than the length of
  3907. the smallest vector, to i and each parallel element in the vectors at
  3908. that index, in order.
  3909. @example
  3910. (vector-count (lambda (i elt) (even? elt))
  3911. '#(3 1 4 1 5 9 2 5 6))
  3912. @result{} 3
  3913. (vector-count (lambda (i x y) (< x y))
  3914. '#(1 3 6 9) '#(2 4 6 8 10 12))
  3915. @result{} 2
  3916. @end example
  3917. @end deffn
  3918. @node SRFI-43 Searching
  3919. @subsubsection SRFI-43 Searching
  3920. @deffn {Scheme Procedure} vector-index pred? vec1 vec2 @dots{}
  3921. Find and return the index of the first elements in @var{vec1} @var{vec2}
  3922. @dots{} that satisfy @var{pred?}. If no matching element is found by
  3923. the end of the shortest vector, return @code{#f}.
  3924. @example
  3925. (vector-index even? '#(3 1 4 1 5 9))
  3926. @result{} 2
  3927. (vector-index < '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))
  3928. @result{} 1
  3929. (vector-index = '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))
  3930. @result{} #f
  3931. @end example
  3932. @end deffn
  3933. @deffn {Scheme Procedure} vector-index-right pred? vec1 vec2 @dots{}
  3934. Like @code{vector-index}, but it searches right-to-left, rather than
  3935. left-to-right. Note that the SRFI 43 specification requires that all
  3936. the vectors must have the same length, but both the SRFI 43 reference
  3937. implementation and Guile's implementation allow vectors with unequal
  3938. lengths, and start searching from the last index of the shortest vector.
  3939. @end deffn
  3940. @deffn {Scheme Procedure} vector-skip pred? vec1 vec2 @dots{}
  3941. Find and return the index of the first elements in @var{vec1} @var{vec2}
  3942. @dots{} that do not satisfy @var{pred?}. If no matching element is
  3943. found by the end of the shortest vector, return @code{#f}. Equivalent
  3944. to @code{vector-index} but with the predicate inverted.
  3945. @example
  3946. (vector-skip number? '#(1 2 a b 3 4 c d)) @result{} 2
  3947. @end example
  3948. @end deffn
  3949. @deffn {Scheme Procedure} vector-skip-right pred? vec1 vec2 @dots{}
  3950. Like @code{vector-skip}, but it searches for a non-matching element
  3951. right-to-left, rather than left-to-right. Note that the SRFI 43
  3952. specification requires that all the vectors must have the same length,
  3953. but both the SRFI 43 reference implementation and Guile's implementation
  3954. allow vectors with unequal lengths, and start searching from the last
  3955. index of the shortest vector.
  3956. @end deffn
  3957. @deffn {Scheme Procedure} vector-binary-search vec value cmp [start [end]]
  3958. Find and return an index of @var{vec} between @var{start} and @var{end}
  3959. whose value is @var{value} using a binary search. If no matching
  3960. element is found, return @code{#f}. The default @var{start} is 0 and
  3961. the default @var{end} is the length of @var{vec}.
  3962. @var{cmp} must be a procedure of two arguments such that @code{(cmp a
  3963. b)} returns a negative integer if @math{a < b}, a positive integer if
  3964. @math{a > b}, or zero if @math{a = b}. The elements of @var{vec} must
  3965. be sorted in non-decreasing order according to @var{cmp}.
  3966. Note that SRFI 43 does not document the @var{start} and @var{end}
  3967. arguments, but both its reference implementation and Guile's
  3968. implementation support them.
  3969. @example
  3970. (define (char-cmp c1 c2)
  3971. (cond ((char<? c1 c2) -1)
  3972. ((char>? c1 c2) 1)
  3973. (else 0)))
  3974. (vector-binary-search '#(#\a #\b #\c #\d #\e #\f #\g #\h)
  3975. #\g
  3976. char-cmp)
  3977. @result{} 6
  3978. @end example
  3979. @end deffn
  3980. @deffn {Scheme Procedure} vector-any pred? vec1 vec2 @dots{}
  3981. Find the first parallel set of elements from @var{vec1} @var{vec2}
  3982. @dots{} for which @var{pred?} returns a true value. If such a parallel
  3983. set of elements exists, @code{vector-any} returns the value that
  3984. @var{pred?} returned for that set of elements. The iteration is
  3985. strictly left-to-right.
  3986. @end deffn
  3987. @deffn {Scheme Procedure} vector-every pred? vec1 vec2 @dots{}
  3988. If, for every index i between 0 and the length of the shortest vector
  3989. argument, the set of elements @code{(vector-ref vec1 i)}
  3990. @code{(vector-ref vec2 i)} @dots{} satisfies @var{pred?},
  3991. @code{vector-every} returns the value that @var{pred?} returned for the
  3992. last set of elements, at the last index of the shortest vector.
  3993. Otherwise it returns @code{#f}. The iteration is strictly
  3994. left-to-right.
  3995. @end deffn
  3996. @node SRFI-43 Mutators
  3997. @subsubsection SRFI-43 Mutators
  3998. @deffn {Scheme Procedure} vector-set! vec i value
  3999. Assign the contents of the location at @var{i} in @var{vec} to
  4000. @var{value}.
  4001. @end deffn
  4002. @deffn {Scheme Procedure} vector-swap! vec i j
  4003. Swap the values of the locations in @var{vec} at @var{i} and @var{j}.
  4004. @end deffn
  4005. @deffn {Scheme Procedure} vector-fill! vec fill [start [end]]
  4006. Assign the value of every location in @var{vec} between @var{start} and
  4007. @var{end} to @var{fill}. @var{start} defaults to 0 and @var{end}
  4008. defaults to the length of @var{vec}.
  4009. @end deffn
  4010. @deffn {Scheme Procedure} vector-reverse! vec [start [end]]
  4011. Destructively reverse the contents of @var{vec} between @var{start} and
  4012. @var{end}. @var{start} defaults to 0 and @var{end} defaults to the
  4013. length of @var{vec}.
  4014. @end deffn
  4015. @deffn {Scheme Procedure} vector-copy! target tstart source [sstart [send]]
  4016. Copy a block of elements from @var{source} to @var{target}, both of
  4017. which must be vectors, starting in @var{target} at @var{tstart} and
  4018. starting in @var{source} at @var{sstart}, ending when (@var{send} -
  4019. @var{sstart}) elements have been copied. It is an error for
  4020. @var{target} to have a length less than (@var{tstart} + @var{send} -
  4021. @var{sstart}). @var{sstart} defaults to 0 and @var{send} defaults to
  4022. the length of @var{source}.
  4023. @end deffn
  4024. @deffn {Scheme Procedure} vector-reverse-copy! target tstart source [sstart [send]]
  4025. Like @code{vector-copy!}, but this copies the elements in the reverse
  4026. order. It is an error if @var{target} and @var{source} are identical
  4027. vectors and the @var{target} and @var{source} ranges overlap; however,
  4028. if @var{tstart} = @var{sstart}, @code{vector-reverse-copy!} behaves as
  4029. @code{(vector-reverse! target tstart send)} would.
  4030. @end deffn
  4031. @node SRFI-43 Conversion
  4032. @subsubsection SRFI-43 Conversion
  4033. @deffn {Scheme Procedure} vector->list vec [start [end]]
  4034. Return a newly allocated list containing the elements in @var{vec}
  4035. between @var{start} and @var{end}. @var{start} defaults to 0 and
  4036. @var{end} defaults to the length of @var{vec}.
  4037. @end deffn
  4038. @deffn {Scheme Procedure} reverse-vector->list vec [start [end]]
  4039. Like @code{vector->list}, but the resulting list contains the specified
  4040. range of elements of @var{vec} in reverse order.
  4041. @end deffn
  4042. @deffn {Scheme Procedure} list->vector proper-list [start [end]]
  4043. Return a newly allocated vector of the elements from @var{proper-list}
  4044. with indices between @var{start} and @var{end}. @var{start} defaults to
  4045. 0 and @var{end} defaults to the length of @var{proper-list}. Note that
  4046. SRFI 43 does not document the @var{start} and @var{end} arguments, but
  4047. both its reference implementation and Guile's implementation support
  4048. them.
  4049. @end deffn
  4050. @deffn {Scheme Procedure} reverse-list->vector proper-list [start [end]]
  4051. Like @code{list->vector}, but the resulting vector contains the specified
  4052. range of elements of @var{proper-list} in reverse order. Note that SRFI
  4053. 43 does not document the @var{start} and @var{end} arguments, but both
  4054. its reference implementation and Guile's implementation support them.
  4055. @end deffn
  4056. @node SRFI-45
  4057. @subsection SRFI-45 - Primitives for Expressing Iterative Lazy Algorithms
  4058. @cindex SRFI-45
  4059. This subsection is based on @uref{http://srfi.schemers.org/srfi-45/srfi-45.html, the
  4060. specification of SRFI-45} written by Andr@'e van Tonder.
  4061. @c Copyright (C) André van Tonder (2003). All Rights Reserved.
  4062. @c Permission is hereby granted, free of charge, to any person obtaining a
  4063. @c copy of this software and associated documentation files (the
  4064. @c "Software"), to deal in the Software without restriction, including
  4065. @c without limitation the rights to use, copy, modify, merge, publish,
  4066. @c distribute, sublicense, and/or sell copies of the Software, and to
  4067. @c permit persons to whom the Software is furnished to do so, subject to
  4068. @c the following conditions:
  4069. @c The above copyright notice and this permission notice shall be included
  4070. @c in all copies or substantial portions of the Software.
  4071. @c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  4072. @c OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  4073. @c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  4074. @c NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  4075. @c LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  4076. @c OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  4077. @c WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  4078. Lazy evaluation is traditionally simulated in Scheme using @code{delay}
  4079. and @code{force}. However, these primitives are not powerful enough to
  4080. express a large class of lazy algorithms that are iterative. Indeed, it
  4081. is folklore in the Scheme community that typical iterative lazy
  4082. algorithms written using delay and force will often require unbounded
  4083. memory.
  4084. This SRFI provides set of three operations: @{@code{lazy}, @code{delay},
  4085. @code{force}@}, which allow the programmer to succinctly express lazy
  4086. algorithms while retaining bounded space behavior in cases that are
  4087. properly tail-recursive. A general recipe for using these primitives is
  4088. provided. An additional procedure @code{eager} is provided for the
  4089. construction of eager promises in cases where efficiency is a concern.
  4090. Although this SRFI redefines @code{delay} and @code{force}, the
  4091. extension is conservative in the sense that the semantics of the subset
  4092. @{@code{delay}, @code{force}@} in isolation (i.e., as long as the
  4093. program does not use @code{lazy}) agrees with that in R5RS. In other
  4094. words, no program that uses the R5RS definitions of delay and force will
  4095. break if those definition are replaced by the SRFI-45 definitions of
  4096. delay and force.
  4097. Guile also adds @code{promise?} to the list of exports, which is not
  4098. part of the official SRFI-45.
  4099. @deffn {Scheme Procedure} promise? obj
  4100. Return true if @var{obj} is an SRFI-45 promise, otherwise return false.
  4101. @end deffn
  4102. @deffn {Scheme Syntax} delay expression
  4103. Takes an expression of arbitrary type @var{a} and returns a promise of
  4104. type @code{(Promise @var{a})} which at some point in the future may be
  4105. asked (by the @code{force} procedure) to evaluate the expression and
  4106. deliver the resulting value.
  4107. @end deffn
  4108. @deffn {Scheme Syntax} lazy expression
  4109. Takes an expression of type @code{(Promise @var{a})} and returns a
  4110. promise of type @code{(Promise @var{a})} which at some point in the
  4111. future may be asked (by the @code{force} procedure) to evaluate the
  4112. expression and deliver the resulting promise.
  4113. @end deffn
  4114. @deffn {Scheme Procedure} force expression
  4115. Takes an argument of type @code{(Promise @var{a})} and returns a value
  4116. of type @var{a} as follows: If a value of type @var{a} has been computed
  4117. for the promise, this value is returned. Otherwise, the promise is
  4118. first evaluated, then overwritten by the obtained promise or value, and
  4119. then force is again applied (iteratively) to the promise.
  4120. @end deffn
  4121. @deffn {Scheme Procedure} eager expression
  4122. Takes an argument of type @var{a} and returns a value of type
  4123. @code{(Promise @var{a})}. As opposed to @code{delay}, the argument is
  4124. evaluated eagerly. Semantically, writing @code{(eager expression)} is
  4125. equivalent to writing
  4126. @lisp
  4127. (let ((value expression)) (delay value)).
  4128. @end lisp
  4129. However, the former is more efficient since it does not require
  4130. unnecessary creation and evaluation of thunks. We also have the
  4131. equivalence
  4132. @lisp
  4133. (delay expression) = (lazy (eager expression))
  4134. @end lisp
  4135. @end deffn
  4136. The following reduction rules may be helpful for reasoning about these
  4137. primitives. However, they do not express the memoization and memory
  4138. usage semantics specified above:
  4139. @lisp
  4140. (force (delay expression)) -> expression
  4141. (force (lazy expression)) -> (force expression)
  4142. (force (eager value)) -> value
  4143. @end lisp
  4144. @subsubheading Correct usage
  4145. We now provide a general recipe for using the primitives @{@code{lazy},
  4146. @code{delay}, @code{force}@} to express lazy algorithms in Scheme. The
  4147. transformation is best described by way of an example: Consider the
  4148. stream-filter algorithm, expressed in a hypothetical lazy language as
  4149. @lisp
  4150. (define (stream-filter p? s)
  4151. (if (null? s) '()
  4152. (let ((h (car s))
  4153. (t (cdr s)))
  4154. (if (p? h)
  4155. (cons h (stream-filter p? t))
  4156. (stream-filter p? t)))))
  4157. @end lisp
  4158. This algorithm can be expressed as follows in Scheme:
  4159. @lisp
  4160. (define (stream-filter p? s)
  4161. (lazy
  4162. (if (null? (force s)) (delay '())
  4163. (let ((h (car (force s)))
  4164. (t (cdr (force s))))
  4165. (if (p? h)
  4166. (delay (cons h (stream-filter p? t)))
  4167. (stream-filter p? t))))))
  4168. @end lisp
  4169. In other words, we
  4170. @itemize @bullet
  4171. @item
  4172. wrap all constructors (e.g., @code{'()}, @code{cons}) with @code{delay},
  4173. @item
  4174. apply @code{force} to arguments of deconstructors (e.g., @code{car},
  4175. @code{cdr} and @code{null?}),
  4176. @item
  4177. wrap procedure bodies with @code{(lazy ...)}.
  4178. @end itemize
  4179. @node SRFI-46
  4180. @subsection SRFI-46 Basic syntax-rules Extensions
  4181. @cindex SRFI-46
  4182. Guile's core @code{syntax-rules} supports the extensions specified by
  4183. SRFI-46/R7RS. Tail patterns have been supported since at least Guile
  4184. 2.0, and custom ellipsis identifiers have been supported since Guile
  4185. 2.0.10. @xref{Syntax Rules}.
  4186. @node SRFI-55
  4187. @subsection SRFI-55 - Requiring Features
  4188. @cindex SRFI-55
  4189. SRFI-55 provides @code{require-extension} which is a portable
  4190. mechanism to load selected SRFI modules. This is implemented in the
  4191. Guile core, there's no module needed to get SRFI-55 itself.
  4192. @deffn {library syntax} require-extension clause1 clause2 @dots{}
  4193. Require the features of @var{clause1} @var{clause2} @dots{} , throwing
  4194. an error if any are unavailable.
  4195. A @var{clause} is of the form @code{(@var{identifier} arg...)}. The
  4196. only @var{identifier} currently supported is @code{srfi} and the
  4197. arguments are SRFI numbers. For example to get SRFI-1 and SRFI-6,
  4198. @example
  4199. (require-extension (srfi 1 6))
  4200. @end example
  4201. @code{require-extension} can only be used at the top-level.
  4202. A Guile-specific program can simply @code{use-modules} to load SRFIs
  4203. not already in the core, @code{require-extension} is for programs
  4204. designed to be portable to other Scheme implementations.
  4205. @end deffn
  4206. @node SRFI-60
  4207. @subsection SRFI-60 - Integers as Bits
  4208. @cindex SRFI-60
  4209. @cindex integers as bits
  4210. @cindex bitwise logical
  4211. This SRFI provides various functions for treating integers as bits and
  4212. for bitwise manipulations. These functions can be obtained with,
  4213. @example
  4214. (use-modules (srfi srfi-60))
  4215. @end example
  4216. Integers are treated as infinite precision twos-complement, the same
  4217. as in the core logical functions (@pxref{Bitwise Operations}). And
  4218. likewise bit indexes start from 0 for the least significant bit. The
  4219. following functions in this SRFI are already in the Guile core,
  4220. @quotation
  4221. @code{logand},
  4222. @code{logior},
  4223. @code{logxor},
  4224. @code{lognot},
  4225. @code{logtest},
  4226. @code{logcount},
  4227. @code{integer-length},
  4228. @code{logbit?},
  4229. @code{ash}
  4230. @end quotation
  4231. @sp 1
  4232. @defun bitwise-and n1 ...
  4233. @defunx bitwise-ior n1 ...
  4234. @defunx bitwise-xor n1 ...
  4235. @defunx bitwise-not n
  4236. @defunx any-bits-set? j k
  4237. @defunx bit-set? index n
  4238. @defunx arithmetic-shift n count
  4239. @defunx bit-field n start end
  4240. @defunx bit-count n
  4241. Aliases for @code{logand}, @code{logior}, @code{logxor},
  4242. @code{lognot}, @code{logtest}, @code{logbit?}, @code{ash},
  4243. @code{bit-extract} and @code{logcount} respectively.
  4244. Note that the name @code{bit-count} conflicts with @code{bit-count} in
  4245. the core (@pxref{Bit Vectors}).
  4246. @end defun
  4247. @defun bitwise-if mask n1 n0
  4248. @defunx bitwise-merge mask n1 n0
  4249. Return an integer with bits selected from @var{n1} and @var{n0}
  4250. according to @var{mask}. Those bits where @var{mask} has 1s are taken
  4251. from @var{n1}, and those where @var{mask} has 0s are taken from
  4252. @var{n0}.
  4253. @example
  4254. (bitwise-if 3 #b0101 #b1010) @result{} 9
  4255. @end example
  4256. @end defun
  4257. @defun log2-binary-factors n
  4258. @defunx first-set-bit n
  4259. Return a count of how many factors of 2 are present in @var{n}. This
  4260. is also the bit index of the lowest 1 bit in @var{n}. If @var{n} is
  4261. 0, the return is @math{-1}.
  4262. @example
  4263. (log2-binary-factors 6) @result{} 1
  4264. (log2-binary-factors -8) @result{} 3
  4265. @end example
  4266. @end defun
  4267. @defun copy-bit index n newbit
  4268. Return @var{n} with the bit at @var{index} set according to
  4269. @var{newbit}. @var{newbit} should be @code{#t} to set the bit to 1,
  4270. or @code{#f} to set it to 0. Bits other than at @var{index} are
  4271. unchanged in the return.
  4272. @example
  4273. (copy-bit 1 #b0101 #t) @result{} 7
  4274. @end example
  4275. @end defun
  4276. @defun copy-bit-field n newbits start end
  4277. Return @var{n} with the bits from @var{start} (inclusive) to @var{end}
  4278. (exclusive) changed to the value @var{newbits}.
  4279. The least significant bit in @var{newbits} goes to @var{start}, the
  4280. next to @math{@var{start}+1}, etc. Anything in @var{newbits} past the
  4281. @var{end} given is ignored.
  4282. @example
  4283. (copy-bit-field #b10000 #b11 1 3) @result{} #b10110
  4284. @end example
  4285. @end defun
  4286. @defun rotate-bit-field n count start end
  4287. Return @var{n} with the bit field from @var{start} (inclusive) to
  4288. @var{end} (exclusive) rotated upwards by @var{count} bits.
  4289. @var{count} can be positive or negative, and it can be more than the
  4290. field width (it'll be reduced modulo the width).
  4291. @example
  4292. (rotate-bit-field #b0110 2 1 4) @result{} #b1010
  4293. @end example
  4294. @end defun
  4295. @defun reverse-bit-field n start end
  4296. Return @var{n} with the bits from @var{start} (inclusive) to @var{end}
  4297. (exclusive) reversed.
  4298. @example
  4299. (reverse-bit-field #b101001 2 4) @result{} #b100101
  4300. @end example
  4301. @end defun
  4302. @defun integer->list n [len]
  4303. Return bits from @var{n} in the form of a list of @code{#t} for 1 and
  4304. @code{#f} for 0. The least significant @var{len} bits are returned,
  4305. and the first list element is the most significant of those bits. If
  4306. @var{len} is not given, the default is @code{(integer-length @var{n})}
  4307. (@pxref{Bitwise Operations}).
  4308. @example
  4309. (integer->list 6) @result{} (#t #t #f)
  4310. (integer->list 1 4) @result{} (#f #f #f #t)
  4311. @end example
  4312. @end defun
  4313. @defun list->integer lst
  4314. @defunx booleans->integer bool@dots{}
  4315. Return an integer formed bitwise from the given @var{lst} list of
  4316. booleans, or for @code{booleans->integer} from the @var{bool}
  4317. arguments.
  4318. Each boolean is @code{#t} for a 1 and @code{#f} for a 0. The first
  4319. element becomes the most significant bit in the return.
  4320. @example
  4321. (list->integer '(#t #f #t #f)) @result{} 10
  4322. @end example
  4323. @end defun
  4324. @node SRFI-61
  4325. @subsection SRFI-61 - A more general @code{cond} clause
  4326. This SRFI extends RnRS @code{cond} to support test expressions that
  4327. return multiple values, as well as arbitrary definitions of test
  4328. success. SRFI 61 is implemented in the Guile core; there's no module
  4329. needed to get SRFI-61 itself. Extended @code{cond} is documented in
  4330. @ref{Conditionals,, Simple Conditional Evaluation}.
  4331. @node SRFI-62
  4332. @subsection SRFI-62 - S-expression comments.
  4333. @cindex SRFI-62
  4334. Starting from version 2.0, Guile's @code{read} supports SRFI-62/R7RS
  4335. S-expression comments by default.
  4336. @node SRFI-64
  4337. @subsection SRFI-64 - A Scheme API for test suites.
  4338. @cindex SRFI-64
  4339. See @uref{http://srfi.schemers.org/srfi-64/srfi-64.html, the
  4340. specification of SRFI-64}.
  4341. @node SRFI-67
  4342. @subsection SRFI-67 - Compare procedures
  4343. @cindex SRFI-67
  4344. See @uref{http://srfi.schemers.org/srfi-67/srfi-67.html, the
  4345. specification of SRFI-67}.
  4346. @node SRFI-69
  4347. @subsection SRFI-69 - Basic hash tables
  4348. @cindex SRFI-69
  4349. This is a portable wrapper around Guile's built-in hash table and weak
  4350. table support. @xref{Hash Tables}, for information on that built-in
  4351. support. Above that, this hash-table interface provides association
  4352. of equality and hash functions with tables at creation time, so
  4353. variants of each function are not required, as well as a procedure
  4354. that takes care of most uses for Guile hash table handles, which this
  4355. SRFI does not provide as such.
  4356. Access it with:
  4357. @lisp
  4358. (use-modules (srfi srfi-69))
  4359. @end lisp
  4360. @menu
  4361. * SRFI-69 Creating hash tables::
  4362. * SRFI-69 Accessing table items::
  4363. * SRFI-69 Table properties::
  4364. * SRFI-69 Hash table algorithms::
  4365. @end menu
  4366. @node SRFI-69 Creating hash tables
  4367. @subsubsection Creating hash tables
  4368. @deffn {Scheme Procedure} make-hash-table [equal-proc hash-proc #:weak weakness start-size]
  4369. Create and answer a new hash table with @var{equal-proc} as the
  4370. equality function and @var{hash-proc} as the hashing function.
  4371. By default, @var{equal-proc} is @code{equal?}. It can be any
  4372. two-argument procedure, and should answer whether two keys are the
  4373. same for this table's purposes.
  4374. By default @var{hash-proc} assumes that @code{equal-proc} is no
  4375. coarser than @code{equal?} unless it is literally @code{string-ci=?}.
  4376. If provided, @var{hash-proc} should be a two-argument procedure that
  4377. takes a key and the current table size, and answers a reasonably good
  4378. hash integer between 0 (inclusive) and the size (exclusive).
  4379. @var{weakness} should be @code{#f} or a symbol indicating how ``weak''
  4380. the hash table is:
  4381. @table @code
  4382. @item #f
  4383. An ordinary non-weak hash table. This is the default.
  4384. @item key
  4385. When the key has no more non-weak references at GC, remove that entry.
  4386. @item value
  4387. When the value has no more non-weak references at GC, remove that
  4388. entry.
  4389. @item key-or-value
  4390. When either has no more non-weak references at GC, remove the
  4391. association.
  4392. @end table
  4393. As a legacy of the time when Guile couldn't grow hash tables,
  4394. @var{start-size} is an optional integer argument that specifies the
  4395. approximate starting size for the hash table, which will be rounded to
  4396. an algorithmically-sounder number.
  4397. @end deffn
  4398. By @dfn{coarser} than @code{equal?}, we mean that for all @var{x} and
  4399. @var{y} values where @code{(@var{equal-proc} @var{x} @var{y})},
  4400. @code{(equal? @var{x} @var{y})} as well. If that does not hold for
  4401. your @var{equal-proc}, you must provide a @var{hash-proc}.
  4402. In the case of weak tables, remember that @dfn{references} above
  4403. always refers to @code{eq?}-wise references. Just because you have a
  4404. reference to some string @code{"foo"} doesn't mean that an association
  4405. with key @code{"foo"} in a weak-key table @emph{won't} be collected;
  4406. it only counts as a reference if the two @code{"foo"}s are @code{eq?},
  4407. regardless of @var{equal-proc}. As such, it is usually only sensible
  4408. to use @code{eq?} and @code{hashq} as the equivalence and hash
  4409. functions for a weak table. @xref{Weak References}, for more
  4410. information on Guile's built-in weak table support.
  4411. @deffn {Scheme Procedure} alist->hash-table alist [equal-proc hash-proc #:weak weakness start-size]
  4412. As with @code{make-hash-table}, but initialize it with the
  4413. associations in @var{alist}. Where keys are repeated in @var{alist},
  4414. the leftmost association takes precedence.
  4415. @end deffn
  4416. @node SRFI-69 Accessing table items
  4417. @subsubsection Accessing table items
  4418. @deffn {Scheme Procedure} hash-table-ref table key [default-thunk]
  4419. @deffnx {Scheme Procedure} hash-table-ref/default table key default
  4420. Answer the value associated with @var{key} in @var{table}. If
  4421. @var{key} is not present, answer the result of invoking the thunk
  4422. @var{default-thunk}, which signals an error instead by default.
  4423. @code{hash-table-ref/default} is a variant that requires a third
  4424. argument, @var{default}, and answers @var{default} itself instead of
  4425. invoking it.
  4426. @end deffn
  4427. @deffn {Scheme Procedure} hash-table-set! table key new-value
  4428. Set @var{key} to @var{new-value} in @var{table}.
  4429. @end deffn
  4430. @deffn {Scheme Procedure} hash-table-delete! table key
  4431. Remove the association of @var{key} in @var{table}, if present. If
  4432. absent, do nothing.
  4433. @end deffn
  4434. @deffn {Scheme Procedure} hash-table-exists? table key
  4435. Answer whether @var{key} has an association in @var{table}.
  4436. @end deffn
  4437. @deffn {Scheme Procedure} hash-table-update! table key modifier [default-thunk]
  4438. @deffnx {Scheme Procedure} hash-table-update!/default table key modifier default
  4439. Replace @var{key}'s associated value in @var{table} by invoking
  4440. @var{modifier} with one argument, the old value.
  4441. If @var{key} is not present, and @var{default-thunk} is provided,
  4442. invoke it with no arguments to get the ``old value'' to be passed to
  4443. @var{modifier} as above. If @var{default-thunk} is not provided in
  4444. such a case, signal an error.
  4445. @code{hash-table-update!/default} is a variant that requires the
  4446. fourth argument, which is used directly as the ``old value'' rather
  4447. than as a thunk to be invoked to retrieve the ``old value''.
  4448. @end deffn
  4449. @node SRFI-69 Table properties
  4450. @subsubsection Table properties
  4451. @deffn {Scheme Procedure} hash-table-size table
  4452. Answer the number of associations in @var{table}. This is guaranteed
  4453. to run in constant time for non-weak tables.
  4454. @end deffn
  4455. @deffn {Scheme Procedure} hash-table-keys table
  4456. Answer an unordered list of the keys in @var{table}.
  4457. @end deffn
  4458. @deffn {Scheme Procedure} hash-table-values table
  4459. Answer an unordered list of the values in @var{table}.
  4460. @end deffn
  4461. @deffn {Scheme Procedure} hash-table-walk table proc
  4462. Invoke @var{proc} once for each association in @var{table}, passing
  4463. the key and value as arguments.
  4464. @end deffn
  4465. @deffn {Scheme Procedure} hash-table-fold table proc init
  4466. Invoke @code{(@var{proc} @var{key} @var{value} @var{previous})} for
  4467. each @var{key} and @var{value} in @var{table}, where @var{previous} is
  4468. the result of the previous invocation, using @var{init} as the first
  4469. @var{previous} value. Answer the final @var{proc} result.
  4470. @end deffn
  4471. @deffn {Scheme Procedure} hash-table->alist table
  4472. Answer an alist where each association in @var{table} is an
  4473. association in the result.
  4474. @end deffn
  4475. @node SRFI-69 Hash table algorithms
  4476. @subsubsection Hash table algorithms
  4477. Each hash table carries an @dfn{equivalence function} and a @dfn{hash
  4478. function}, used to implement key lookups. Beginning users should
  4479. follow the rules for consistency of the default @var{hash-proc}
  4480. specified above. Advanced users can use these to implement their own
  4481. equivalence and hash functions for specialized lookup semantics.
  4482. @deffn {Scheme Procedure} hash-table-equivalence-function hash-table
  4483. @deffnx {Scheme Procedure} hash-table-hash-function hash-table
  4484. Answer the equivalence and hash function of @var{hash-table}, respectively.
  4485. @end deffn
  4486. @deffn {Scheme Procedure} hash obj [size]
  4487. @deffnx {Scheme Procedure} string-hash obj [size]
  4488. @deffnx {Scheme Procedure} string-ci-hash obj [size]
  4489. @deffnx {Scheme Procedure} hash-by-identity obj [size]
  4490. Answer a hash value appropriate for equality predicate @code{equal?},
  4491. @code{string=?}, @code{string-ci=?}, and @code{eq?}, respectively.
  4492. @end deffn
  4493. @code{hash} is a backwards-compatible replacement for Guile's built-in
  4494. @code{hash}.
  4495. @node SRFI-71
  4496. @subsection SRFI-71 - Extended let-syntax for multiple values
  4497. @cindex SRFI-71
  4498. This SRFI shadows the forms for @code{let}, @code{let*}, and @code{letrec}
  4499. so that they may accept multiple values. For example:
  4500. @example
  4501. (use-modules (srfi srfi-71))
  4502. (let* ((x y (values 1 2))
  4503. (z (+ x y)))
  4504. (* z 2))
  4505. @result{} 6
  4506. @end example
  4507. See @uref{http://srfi.schemers.org/srfi-71/srfi-71.html, the
  4508. specification of SRFI-71}.
  4509. @node SRFI-87
  4510. @subsection SRFI-87 => in case clauses
  4511. @cindex SRFI-87
  4512. Starting from version 2.0.6, Guile's core @code{case} syntax supports
  4513. @code{=>} in clauses, as specified by SRFI-87/R7RS.
  4514. @xref{Conditionals}.
  4515. @node SRFI-88
  4516. @subsection SRFI-88 Keyword Objects
  4517. @cindex SRFI-88
  4518. @cindex keyword objects
  4519. @uref{http://srfi.schemers.org/srfi-88/srfi-88.html, SRFI-88} provides
  4520. @dfn{keyword objects}, which are equivalent to Guile's keywords
  4521. (@pxref{Keywords}). SRFI-88 keywords can be entered using the
  4522. @dfn{postfix keyword syntax}, which consists of an identifier followed
  4523. by @code{:} (@pxref{Scheme Read, @code{postfix} keyword syntax}).
  4524. SRFI-88 can be made available with:
  4525. @example
  4526. (use-modules (srfi srfi-88))
  4527. @end example
  4528. Doing so installs the right reader option for keyword syntax, using
  4529. @code{(read-set! keywords 'postfix)}. It also provides the procedures
  4530. described below.
  4531. @deffn {Scheme Procedure} keyword? obj
  4532. Return @code{#t} if @var{obj} is a keyword. This is the same procedure
  4533. as the same-named built-in procedure (@pxref{Keyword Procedures,
  4534. @code{keyword?}}).
  4535. @example
  4536. (keyword? foo:) @result{} #t
  4537. (keyword? 'foo:) @result{} #t
  4538. (keyword? "foo") @result{} #f
  4539. @end example
  4540. @end deffn
  4541. @deffn {Scheme Procedure} keyword->string kw
  4542. Return the name of @var{kw} as a string, i.e., without the trailing
  4543. colon. The returned string may not be modified, e.g., with
  4544. @code{string-set!}.
  4545. @example
  4546. (keyword->string foo:) @result{} "foo"
  4547. @end example
  4548. @end deffn
  4549. @deffn {Scheme Procedure} string->keyword str
  4550. Return the keyword object whose name is @var{str}.
  4551. @example
  4552. (keyword->string (string->keyword "a b c")) @result{} "a b c"
  4553. @end example
  4554. @end deffn
  4555. @node SRFI-98
  4556. @subsection SRFI-98 Accessing environment variables.
  4557. @cindex SRFI-98
  4558. @cindex environment variables
  4559. This is a portable wrapper around Guile's built-in support for
  4560. interacting with the current environment, @xref{Runtime Environment}.
  4561. @deffn {Scheme Procedure} get-environment-variable name
  4562. Returns a string containing the value of the environment variable
  4563. given by the string @code{name}, or @code{#f} if the named
  4564. environment variable is not found. This is equivalent to
  4565. @code{(getenv name)}.
  4566. @end deffn
  4567. @deffn {Scheme Procedure} get-environment-variables
  4568. Returns the names and values of all the environment variables as an
  4569. association list in which both the keys and the values are strings.
  4570. @end deffn
  4571. @node SRFI-105
  4572. @subsection SRFI-105 Curly-infix expressions.
  4573. @cindex SRFI-105
  4574. @cindex curly-infix
  4575. @cindex curly-infix-and-bracket-lists
  4576. Guile's built-in reader includes support for SRFI-105 curly-infix
  4577. expressions. See @uref{http://srfi.schemers.org/srfi-105/srfi-105.html,
  4578. the specification of SRFI-105}. Some examples:
  4579. @example
  4580. @{n <= 5@} @result{} (<= n 5)
  4581. @{a + b + c@} @result{} (+ a b c)
  4582. @{a * @{b + c@}@} @result{} (* a (+ b c))
  4583. @{(- a) / b@} @result{} (/ (- a) b)
  4584. @{-(a) / b@} @result{} (/ (- a) b) as well
  4585. @{(f a b) + (g h)@} @result{} (+ (f a b) (g h))
  4586. @{f(a b) + g(h)@} @result{} (+ (f a b) (g h)) as well
  4587. @{f[a b] + g(h)@} @result{} (+ ($bracket-apply$ f a b) (g h))
  4588. '@{a + f(b) + x@} @result{} '(+ a (f b) x)
  4589. @{length(x) >= 6@} @result{} (>= (length x) 6)
  4590. @{n-1 + n-2@} @result{} (+ n-1 n-2)
  4591. @{n * factorial@{n - 1@}@} @result{} (* n (factorial (- n 1)))
  4592. @{@{a > 0@} and @{b >= 1@}@} @result{} (and (> a 0) (>= b 1))
  4593. @{f@{n - 1@}(x)@} @result{} ((f (- n 1)) x)
  4594. @{a . z@} @result{} ($nfx$ a . z)
  4595. @{a + b - c@} @result{} ($nfx$ a + b - c)
  4596. @end example
  4597. To enable curly-infix expressions within a file, place the reader
  4598. directive @code{#!curly-infix} before the first use of curly-infix
  4599. notation. To globally enable curly-infix expressions in Guile's reader,
  4600. set the @code{curly-infix} read option.
  4601. Guile also implements the following non-standard extension to SRFI-105:
  4602. if @code{curly-infix} is enabled and there is no other meaning assigned
  4603. to square brackets (i.e. the @code{square-brackets} read option is
  4604. turned off), then lists within square brackets are read as normal lists
  4605. but with the special symbol @code{$bracket-list$} added to the front.
  4606. To enable this combination of read options within a file, use the reader
  4607. directive @code{#!curly-infix-and-bracket-lists}. For example:
  4608. @example
  4609. [a b] @result{} ($bracket-list$ a b)
  4610. [a . b] @result{} ($bracket-list$ a . b)
  4611. @end example
  4612. For more information on reader options, @xref{Scheme Read}.
  4613. @node SRFI-111
  4614. @subsection SRFI-111 Boxes.
  4615. @cindex SRFI-111
  4616. @uref{http://srfi.schemers.org/srfi-111/srfi-111.html, SRFI-111}
  4617. provides boxes: objects with a single mutable cell.
  4618. @deffn {Scheme Procedure} box value
  4619. Return a newly allocated box whose contents is initialized to
  4620. @var{value}.
  4621. @end deffn
  4622. @deffn {Scheme Procedure} box? obj
  4623. Return true if @var{obj} is a box, otherwise return false.
  4624. @end deffn
  4625. @deffn {Scheme Procedure} unbox box
  4626. Return the current contents of @var{box}.
  4627. @end deffn
  4628. @deffn {Scheme Procedure} set-box! box value
  4629. Set the contents of @var{box} to @var{value}.
  4630. @end deffn
  4631. @node SRFI-171
  4632. @subsection Transducers
  4633. @cindex SRFI-171
  4634. @cindex transducers
  4635. Some of the most common operations used in the Scheme language are those
  4636. transforming lists: map, filter, take and so on. They work well, are well
  4637. understood, and are used daily by most Scheme programmers. They are however not
  4638. general because they only work on lists, and they do not compose very well
  4639. since combining N of them builds @code{(- N 1)} intermediate lists.
  4640. Transducers are oblivious to what kind of process they are used in, and
  4641. are composable without building intermediate collections. This means we
  4642. can create a transducer that squares all odd numbers:
  4643. @example
  4644. (compose (tfilter odd?) (tmap (lambda (x) (* x x))))
  4645. @end example
  4646. and reuse it with lists, vectors, or in just about any context where
  4647. data flows in one direction. We could use it as a processing step for
  4648. asynchronous channels, with an event framework as a pre-processing step,
  4649. or even in lazy contexts where you pass a lazy collection and a
  4650. transducer to a function and get a new lazy collection back.
  4651. The traditional Scheme approach of having collection-specific procedures
  4652. is not changed. We instead specify a general form of transformations
  4653. that complement these procedures. The benefits are obvious: a clear,
  4654. well-understood way of describing common transformations in a way that
  4655. is faster than just chaining the collection-specific counterparts. For
  4656. guile in particular this means a lot better GC performance.
  4657. Notice however that @code{(compose @dots{})} composes transducers
  4658. left-to-right, due to how transducers are initiated.
  4659. @menu
  4660. * SRFI-171 General Discussion:: General information about transducers
  4661. * SRFI-171 Applying Transducers:: Documentation of collection-specific forms
  4662. * SRFI-171 Reducers:: Reducers specified by the SRFI
  4663. * SRFI-171 Transducers:: Transducers specified by the SRFI
  4664. * SRFI-171 Helpers:: Utilities for writing your own transducers
  4665. @end menu
  4666. @node SRFI-171 General Discussion
  4667. @subsubsection SRFI-171 General Discussion
  4668. @cindex transducers discussion
  4669. @subheading The concept of reducers
  4670. The central part of transducers are 3-arity reducing procedures.
  4671. @itemize
  4672. @item
  4673. no arguments: Produces the identity of the reducer.
  4674. @item
  4675. (result-so-far): completion. Returns @code{result-so-far} either with or
  4676. without transforming it first.
  4677. @item
  4678. (result-so-far input) combines @code{result-so-far} and @code{input} to produce
  4679. a new @code{result-so-far}.
  4680. @end itemize
  4681. In the case of a summing @code{+} reducer, the reducer would produce, in
  4682. arity order: @code{0}, @code{result-so-far}, @code{(+ result-so-far
  4683. input)}. This happens to be exactly what the regular @code{+} does.
  4684. @subheading The concept of transducers
  4685. A transducer is a one-arity procedure that takes a reducer and produces a
  4686. reducing function that behaves as follows:
  4687. @itemize
  4688. @item
  4689. no arguments: calls reducer with no arguments (producing its identity)
  4690. @item
  4691. (result-so-far): Maybe transform the result-so-far and call reducer with it.
  4692. @item
  4693. (result-so-far input) Maybe do something to input and maybe call the
  4694. reducer with result-so-far and the maybe-transformed input.
  4695. @end itemize
  4696. A simple example is as following:
  4697. @example
  4698. (list-transduce (tfilter odd?) + '(1 2 3 4 5)).
  4699. @end example
  4700. This first returns a transducer filtering all odd
  4701. elements, then it runs @code{+} without arguments to retrieve its
  4702. identity. It then starts the transduction by passing @code{+} to the
  4703. transducer returned by @code{(tfilter odd?)} which returns a reducing
  4704. function. It works not unlike reduce from SRFI 1, but also checks
  4705. whether one of the intermediate transducers returns a "reduced" value
  4706. (implemented as a SRFI 9 record), which means the reduction finished
  4707. early.
  4708. Because transducers compose and the final reduction is only executed in
  4709. the last step, composed transducers will not build any intermediate
  4710. result or collections. Although the normal way of thinking about
  4711. application of composed functions is right to left, due to how the
  4712. transduction is built it is applied left to right. @code{(compose
  4713. (tfilter odd?) (tmap sqrt))} will create a transducer that first filters
  4714. out any odd values and then computes the square root of the rest.
  4715. @subheading State
  4716. Even though transducers appear to be somewhat of a generalisation of
  4717. @code{map} and friends, this is not really true. Since transducers don't
  4718. know in which context they are being used, some transducers must keep
  4719. state where their collection-specific counterparts do not. The
  4720. transducers that keep state do so using hidden mutable state, and as
  4721. such all the caveats of mutation, parallelism, and multi-shot
  4722. continuations apply. Each transducer keeping state is clearly described
  4723. as doing so in the documentation.
  4724. @subheading Naming
  4725. Reducers exported from the transducers module are named as in their
  4726. SRFI-1 counterpart, but prepended with an r. Transducers also follow
  4727. that naming, but are prepended with a t.
  4728. @node SRFI-171 Applying Transducers
  4729. @subsubsection Applying Transducers
  4730. @cindex transducers applying
  4731. @deffn {Scheme Procedure} list-transduce xform f lst
  4732. @deffnx {Scheme Procedure} list-transduce xform f identity lst
  4733. Initialize the transducer @var{xform} by passing the reducer @var{f}
  4734. to it. If no identity is provided, @var{f} runs without arguments to
  4735. return the reducer identity. It then reduces over @var{lst} using the
  4736. identity as the seed.
  4737. If one of the transducers finishes early (such as @code{ttake} or
  4738. @code{tdrop}), it communicates this by returning a reduced value, which
  4739. in the guile implementation is just a value wrapped in a SRFI 9 record
  4740. type named ``reduced''. If such a value is returned by the transducer,
  4741. @code{list-transduce} must stop execution and return an unreduced value
  4742. immediately.
  4743. @end deffn
  4744. @deffn {Scheme Procedure} vector-transduce xform f vec
  4745. @deffnx {Scheme Procedure} vector-transduce xform f identity vec
  4746. @deffnx {Scheme Procedure} string-transduce xform f str
  4747. @deffnx {Scheme Procedure} string-transduce xform f identity str
  4748. @deffnx {Scheme Procedure} bytevector-u8-transduce xform f bv
  4749. @deffnx {Scheme Procedure} bytevector-u8-transduce xform f identity bv
  4750. @deffnx {Scheme Procedure} generator-transduce xform f gen
  4751. @deffnx {Scheme Procedure} generator-transduce xform f identity gen
  4752. Same as @code{list-transduce}, but for vectors, strings, u8-bytevectors
  4753. and SRFI-158-styled generators respectively.
  4754. @end deffn
  4755. @deffn {Scheme Procedure} port-transduce xform f reader
  4756. @deffnx {Scheme Procedure} port-transduce xform f reader port
  4757. @deffnx {Scheme Procedure} port-transduce xform f identity reader port
  4758. Same as @code{list-transduce} but for ports. Called without a port, it
  4759. reduces over the results of applying @var{reader} until the EOF-object
  4760. is returned, presumably to read from @code{current-input-port}. With a
  4761. port @var{reader} is applied to @var{port} instead of without any
  4762. arguments. If @var{identity} is provided, that is used as the initial
  4763. identity in the reduction.
  4764. @end deffn
  4765. @node SRFI-171 Reducers
  4766. @subsubsection Reducers
  4767. @cindex transducers reducers
  4768. @deffn {Scheme Procedure} rcons
  4769. a simple consing reducer. When called without values, it returns its
  4770. identity, @code{'()}. With one value, which will be a list, it reverses
  4771. the list (using @code{reverse!}). When called with two values, it conses
  4772. the second value to the first.
  4773. @example
  4774. (list-transduce (tmap (lambda (x) (+ x 1)) rcons (list 0 1 2 3))
  4775. @result{} (1 2 3 4)
  4776. @end example
  4777. @end deffn
  4778. @deffn {Scheme Procedure} reverse-rcons
  4779. same as rcons, but leaves the values in their reversed order.
  4780. @example
  4781. (list-transduce (tmap (lambda (x) (+ x 1))) reverse-rcons (list 0 1 2 3))
  4782. @result{} (4 3 2 1)
  4783. @end example
  4784. @end deffn
  4785. @deffn {Scheme Procedure} rany pred?
  4786. The reducer version of any. Returns @code{(reduced (pred? value))} if
  4787. any @code{(pred? value)} returns non-#f. The identity is #f.
  4788. @example
  4789. (list-transduce (tmap (lambda (x) (+ x 1))) (rany odd?) (list 1 3 5))
  4790. @result{} #f
  4791. (list-transduce (tmap (lambda (x) (+ x 1))) (rany odd?) (list 1 3 4 5))
  4792. @result{} #t
  4793. @end example
  4794. @end deffn
  4795. @deffn {Scheme Procedure} revery pred?
  4796. The reducer version of every. Stops the transduction and returns
  4797. @code{(reduced #f)} if any @code{(pred? value)} returns #f. If every
  4798. @code{(pred? value)} returns true, it returns the result of the last
  4799. invocation of @code{(pred? value)}. The identity is #t.
  4800. @example
  4801. (list-transduce
  4802. (tmap (lambda (x) (+ x 1)))
  4803. (revery (lambda (v) (if (odd? v) v #f)))
  4804. (list 2 4 6))
  4805. @result{} 7
  4806. (list-transduce (tmap (lambda (x) (+ x 1)) (revery odd?) (list 2 4 5 6))
  4807. @result{} #f
  4808. @end example
  4809. @end deffn
  4810. @deffn {Scheme Procedure} rcount
  4811. A simple counting reducer. Counts the values that pass through the
  4812. transduction.
  4813. @example
  4814. (list-transduce (tfilter odd?) rcount (list 1 2 3 4)) @result{} 2.
  4815. @end example
  4816. @end deffn
  4817. @node SRFI-171 Transducers
  4818. @subsubsection Transducers
  4819. @cindex transducers transducers
  4820. @deffn {Scheme Procedure} tmap proc
  4821. Returns a transducer that applies @var{proc} to all values. Stateless.
  4822. @end deffn
  4823. @deffn {Scheme Procedure} tfilter pred?
  4824. Returns a transducer that removes values for which @var{pred?} returns #f.
  4825. Stateless.
  4826. @end deffn
  4827. @deffn {Scheme Procedure} tremove pred?
  4828. Returns a transducer that removes values for which @var{pred?} returns non-#f.
  4829. Stateless
  4830. @end deffn
  4831. @deffn {Scheme Procedure} tfilter-map proc
  4832. The same as @code{(compose (tmap proc) (tfilter values))}. Stateless.
  4833. @end deffn
  4834. @deffn {Scheme Procedure} treplace mapping
  4835. The argument @var{mapping} is an association list (using @code{equal?}
  4836. to compare keys), a hash-table, a one-argument procedure taking one
  4837. argument and either producing that same argument or a replacement value.
  4838. Returns a transducer which checks for the presence of any value passed
  4839. through it in mapping. If a mapping is found, the value of that mapping
  4840. is returned, otherwise it just returns the original value.
  4841. Does not keep internal state, but modifying the mapping while it's in
  4842. use by treplace is an error.
  4843. @end deffn
  4844. @deffn {Scheme Procedure} tdrop n
  4845. Returns a transducer that discards the first @var{n} values.
  4846. Stateful.
  4847. @end deffn
  4848. @deffn {Scheme Procedure} ttake n
  4849. Returns a transducer that discards all values and stops the transduction
  4850. after the first @var{n} values have been let through. Any subsequent values
  4851. are ignored.
  4852. Stateful.
  4853. @end deffn
  4854. @deffn {Scheme Procedure} tdrop-while pred?
  4855. Returns a transducer that discards the first values for which
  4856. @var{pred?} returns true.
  4857. Stateful.
  4858. @end deffn
  4859. @deffn {Scheme Procedure} ttake-while pred?
  4860. @deffnx {Scheme Procedure} ttake-while pred? retf
  4861. Returns a transducer that stops the transduction after @var{pred?} has
  4862. returned #f. Any subsequent values are ignored and the last successful
  4863. value is returned. @var{retf} is a function that gets called whenever
  4864. @var{pred?} returns false. The arguments passed are the result so far
  4865. and the input for which pred? returns @code{#f}. The default function is
  4866. @code{(lambda (result input) result)}.
  4867. Stateful.
  4868. @end deffn
  4869. @deffn {Scheme Procedure} tconcatenate
  4870. tconcatenate @emph{is} a transducer that concatenates the content of
  4871. each value (that must be a list) into the reduction.
  4872. @example
  4873. (list-transduce tconcatenate rcons '((1 2) (3 4 5) (6 (7 8) 9)))
  4874. @result{} (1 2 3 4 5 6 (7 8) 9)
  4875. @end example
  4876. @end deffn
  4877. @deffn {Scheme Procedure} tappend-map proc
  4878. The same as @code{(compose (tmap proc) tconcatenate)}.
  4879. @end deffn
  4880. @deffn {Scheme Procedure} tflatten
  4881. tflatten @emph{is} a transducer that flattens an input consisting of lists.
  4882. @example
  4883. (list-transduce tflatten rcons '((1 2) 3 (4 (5 6) 7 8) 9)
  4884. @result{} (1 2 3 4 5 6 7 8 9)
  4885. @end example
  4886. @end deffn
  4887. @deffn {Scheme Procedure} tdelete-neighbor-duplicates
  4888. @deffnx {Scheme Procedure} tdelete-neighbor-duplicates equality-predicate
  4889. Returns a transducer that removes any directly following duplicate
  4890. elements. The default @var{equality-predicate} is @code{equal?}.
  4891. Stateful.
  4892. @end deffn
  4893. @deffn {Scheme Procedure} tdelete-duplicates
  4894. @deffnx {Scheme Procedure} tdelete-duplicates equality-predicate
  4895. Returns a transducer that removes any subsequent duplicate elements
  4896. compared using @var{equality-predicate}. The default
  4897. @var{equality-predicate} is @code{equal?}.
  4898. Stateful.
  4899. @end deffn
  4900. @deffn {Scheme Procedure} tsegment n
  4901. Returns a transducer that groups inputs into lists of @var{n} elements.
  4902. When the transduction stops, it flushes any remaining collection, even
  4903. if it contains fewer than @var{n} elements.
  4904. Stateful.
  4905. @end deffn
  4906. @deffn {Scheme Procedure} tpartition pred?
  4907. Returns a transducer that groups inputs in lists by whenever
  4908. @code{(pred? input)} changes value.
  4909. Stateful.
  4910. @end deffn
  4911. @deffn {Scheme Procedure} tadd-between value
  4912. Returns a transducer which interposes @var{value} between each value
  4913. and the next. This does not compose gracefully with transducers like
  4914. @code{ttake}, as you might end up ending the transduction on
  4915. @code{value}.
  4916. Stateful.
  4917. @end deffn
  4918. @deffn {Scheme Procedure} tenumerate
  4919. @deffnx {Scheme Procedure} tenumerate start
  4920. Returns a transducer that indexes values passed through it, starting at
  4921. @var{start}, which defaults to 0. The indexing is done through cons
  4922. pairs like @code{(index . input)}.
  4923. @example
  4924. (list-transduce (tenumerate 1) rcons (list 'first 'second 'third))
  4925. @result{} ((1 . first) (2 . second) (3 . third))
  4926. @end example
  4927. Stateful.
  4928. @end deffn
  4929. @deffn {Scheme Procedure} tlog
  4930. @deffnx {Scheme Procedure} tlog logger
  4931. Returns a transducer that can be used to log or print values and
  4932. results. The result of the @var{logger} procedure is discarded. The
  4933. default @var{logger} is @code{(lambda (result input) (write input)
  4934. (newline))}.
  4935. Stateless.
  4936. @end deffn
  4937. @subheading Guile-specific transducers
  4938. These transducers are available in the @code{(srfi srfi-171 gnu)}
  4939. library, and are provided outside the standard described by the SRFI-171
  4940. document.
  4941. @deffn {Scheme Procedure} tbatch reducer
  4942. @deffnx {Scheme Procedure} tbatch transducer reducer
  4943. A batching transducer that accumulates results using @var{reducer} or
  4944. @code{((transducer) reducer)} until it returns a reduced value. This can
  4945. be used to generalize something like @code{tsegment}:
  4946. @example
  4947. ;; This behaves exactly like (tsegment 4).
  4948. (list-transduce (tbatch (ttake 4) rcons) rcons (iota 10))
  4949. @result{} ((0 1 2 3) (4 5 6 7) (8 9))
  4950. @end example
  4951. @end deffn
  4952. @deffn {Scheme Procedure} tfold reducer
  4953. @deffnx {Scheme Procedure} tfold reducer seed
  4954. A folding transducer that yields the result of @code{(reducer seed
  4955. value)}, saving its result between iterations.
  4956. @example
  4957. (list-transduce (tfold +) rcons (iota 10))
  4958. @result{} (0 1 3 6 10 15 21 28 36 45)
  4959. @end example
  4960. @end deffn
  4961. @node SRFI-171 Helpers
  4962. @subsubsection Helper functions for writing transducers
  4963. @cindex transducers helpers
  4964. These functions are in the @code{(srfi srfi-171 meta)} module and are only
  4965. usable when you want to write your own transducers.
  4966. @deffn {Scheme Procedure} reduced value
  4967. Wraps a value in a @code{<reduced>} container, signalling that the
  4968. reduction should stop.
  4969. @end deffn
  4970. @deffn {Scheme Procedure} reduced? value
  4971. Returns #t if value is a @code{<reduced>} record.
  4972. @end deffn
  4973. @deffn {Scheme Procedure} unreduce reduced-container
  4974. Returns the value in reduced-container.
  4975. @end deffn
  4976. @deffn {Scheme Procedure} ensure-reduced value
  4977. Wraps value in a @code{<reduced>} container if it is not already reduced.
  4978. @end deffn
  4979. @deffn {Scheme Procedure} preserving-reduced reducer
  4980. Wraps @code{reducer} in another reducer that encapsulates any returned
  4981. reduced value in another reduced container. This is useful in places
  4982. where you re-use a reducer with [collection]-reduce. If the reducer
  4983. returns a reduced value, [collection]-reduce unwraps it. Unless handled,
  4984. this leads to the reduction continuing.
  4985. @end deffn
  4986. @deffn {Scheme Procedure} list-reduce f identity lst
  4987. The reducing function used internally by @code{list-transduce}. @var{f}
  4988. is a reducer as returned by a transducer. @var{identity} is the
  4989. identity (sometimes called "seed") of the reduction. @var{lst} is a
  4990. list. If @var{f} returns a reduced value, the reduction stops
  4991. immediately and the unreduced value is returned.
  4992. @end deffn
  4993. @deffn {Scheme Procedure} vector-reduce f identity vec
  4994. The vector version of list-reduce.
  4995. @end deffn
  4996. @deffn {Scheme Procedure} string-reduce f identity str
  4997. The string version of list-reduce.
  4998. @end deffn
  4999. @deffn {Scheme Procedure} bytevector-u8-reduce f identity bv
  5000. The bytevector-u8 version of list-reduce.
  5001. @end deffn
  5002. @deffn {Scheme Procedure} port-reduce f identity reader port
  5003. The port version of list-reduce. It reduces over port using reader
  5004. until reader returns the EOF object.
  5005. @end deffn
  5006. @deffn {Scheme Procedure} generator-reduce f identity gen
  5007. The generator version of list-reduce. It reduces over @code{gen} until
  5008. it returns the EOF object
  5009. @end deffn
  5010. @c srfi-modules.texi ends here
  5011. @c Local Variables:
  5012. @c TeX-master: "guile.texi"
  5013. @c End: