srfi-modules.texi 202 KB

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