srfi-modules.texi 201 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559
  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 ``Data Types'' section of the manual (@pxref{SRFI-9
  1499. 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 data
  1507. and as constants or literals in source code. This feature is available
  1508. 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. Once @code{(srfi srfi-10)} has loaded, @nicode{#,()} is available
  1554. globally, there's no need to use @code{(srfi srfi-10)} in later
  1555. modules. Similarly the tags registered are global and can be used
  1556. anywhere once registered.
  1557. We do not recommend @nicode{#,()} reader extensions, however, and for
  1558. three reasons.
  1559. First of all, this SRFI is not modular: the tag is matched by name, not
  1560. as an identifier within a scope. Defining a reader extension in one
  1561. part of a program can thus affect unrelated parts of a program because
  1562. the tag is not scoped.
  1563. Secondly, reader extensions can be hard to manage from a time
  1564. perspective: when does the reader extension take effect? @xref{Eval
  1565. When}, for more discussion.
  1566. Finally, reader extensions can easily produce objects that can't be
  1567. reified to an object file by the compiler. For example if you define a
  1568. reader extension that makes a hash table (@pxref{Hash Tables}), then it
  1569. will work fine when run with the interpreter, and you think you have a
  1570. neat hack. But then if you try to compile your program, after wrangling
  1571. with the @code{eval-when} concerns mentioned above, the compiler will
  1572. carp that it doesn't know how to serialize a hash table to disk.
  1573. In the specific case of hash tables, it would be possible for Guile to
  1574. know how to pack hash tables into compiled files, but this doesn't work
  1575. in general. What if the object you produce is an instance of a record
  1576. type? Guile would then have to serialize the record type to disk too,
  1577. and then what happens if the program independently loads the code that
  1578. defines the record type? Does it define the same type or a different
  1579. type? Guile's record types are nominal, not structural, so the answer
  1580. is not clear at all.
  1581. For all of these reasons we recommend macros over reader extensions.
  1582. Macros fulfill many of the same needs while preserving modular
  1583. composition, and their interaction with @code{eval-when} is well-known.
  1584. If you need brevity, instead use @code{read-hash-extend} and make your
  1585. reader extension expand to a macro invocation. In that way we preserve
  1586. scoping as much as possible. @xref{Reader Extensions}.
  1587. @node SRFI-11
  1588. @subsection SRFI-11 - let-values
  1589. @cindex SRFI-11
  1590. @findex let-values
  1591. @findex let*-values
  1592. This module implements the binding forms for multiple values
  1593. @code{let-values} and @code{let*-values}. These forms are similar to
  1594. @code{let} and @code{let*} (@pxref{Local Bindings}), but they support
  1595. binding of the values returned by multiple-valued expressions.
  1596. Write @code{(use-modules (srfi srfi-11))} to make the bindings
  1597. available.
  1598. @lisp
  1599. (let-values (((x y) (values 1 2))
  1600. ((z f) (values 3 4)))
  1601. (+ x y z f))
  1602. @result{}
  1603. 10
  1604. @end lisp
  1605. @code{let-values} performs all bindings simultaneously, which means that
  1606. no expression in the binding clauses may refer to variables bound in the
  1607. same clause list. @code{let*-values}, on the other hand, performs the
  1608. bindings sequentially, just like @code{let*} does for single-valued
  1609. expressions.
  1610. @node SRFI-13
  1611. @subsection SRFI-13 - String Library
  1612. @cindex SRFI-13
  1613. The SRFI-13 procedures are always available, @xref{Strings}.
  1614. @node SRFI-14
  1615. @subsection SRFI-14 - Character-set Library
  1616. @cindex SRFI-14
  1617. The SRFI-14 data type and procedures are always available,
  1618. @xref{Character Sets}.
  1619. @node SRFI-16
  1620. @subsection SRFI-16 - case-lambda
  1621. @cindex SRFI-16
  1622. @cindex variable arity
  1623. @cindex arity, variable
  1624. SRFI-16 defines a variable-arity @code{lambda} form,
  1625. @code{case-lambda}. This form is available in the default Guile
  1626. environment. @xref{Case-lambda}, for more information.
  1627. @node SRFI-17
  1628. @subsection SRFI-17 - Generalized set!
  1629. @cindex SRFI-17
  1630. This SRFI implements a generalized @code{set!}, allowing some
  1631. ``referencing'' functions to be used as the target location of a
  1632. @code{set!}. This feature is available from
  1633. @example
  1634. (use-modules (srfi srfi-17))
  1635. @end example
  1636. @noindent
  1637. For example @code{vector-ref} is extended so that
  1638. @example
  1639. (set! (vector-ref vec idx) new-value)
  1640. @end example
  1641. @noindent
  1642. is equivalent to
  1643. @example
  1644. (vector-set! vec idx new-value)
  1645. @end example
  1646. The idea is that a @code{vector-ref} expression identifies a location,
  1647. which may be either fetched or stored. The same form is used for the
  1648. location in both cases, encouraging visual clarity. This is similar
  1649. to the idea of an ``lvalue'' in C.
  1650. The mechanism for this kind of @code{set!} is in the Guile core
  1651. (@pxref{Procedures with Setters}). This module adds definitions of
  1652. the following functions as procedures with setters, allowing them to
  1653. be targets of a @code{set!},
  1654. @quotation
  1655. @nicode{car}, @nicode{cdr}, @nicode{caar}, @nicode{cadr},
  1656. @nicode{cdar}, @nicode{cddr}, @nicode{caaar}, @nicode{caadr},
  1657. @nicode{cadar}, @nicode{caddr}, @nicode{cdaar}, @nicode{cdadr},
  1658. @nicode{cddar}, @nicode{cdddr}, @nicode{caaaar}, @nicode{caaadr},
  1659. @nicode{caadar}, @nicode{caaddr}, @nicode{cadaar}, @nicode{cadadr},
  1660. @nicode{caddar}, @nicode{cadddr}, @nicode{cdaaar}, @nicode{cdaadr},
  1661. @nicode{cdadar}, @nicode{cdaddr}, @nicode{cddaar}, @nicode{cddadr},
  1662. @nicode{cdddar}, @nicode{cddddr}
  1663. @nicode{string-ref}, @nicode{vector-ref}
  1664. @end quotation
  1665. The SRFI specifies @code{setter} (@pxref{Procedures with Setters}) as
  1666. a procedure with setter, allowing the setter for a procedure to be
  1667. changed, eg.@: @code{(set! (setter foo) my-new-setter-handler)}.
  1668. Currently Guile does not implement this, a setter can only be
  1669. specified on creation (@code{getter-with-setter} below).
  1670. @defun getter-with-setter
  1671. The same as the Guile core @code{make-procedure-with-setter}
  1672. (@pxref{Procedures with Setters}).
  1673. @end defun
  1674. @node SRFI-18
  1675. @subsection SRFI-18 - Multithreading support
  1676. @cindex SRFI-18
  1677. This is an implementation of the SRFI-18 threading and synchronization
  1678. library. The functions and variables described here are provided by
  1679. @example
  1680. (use-modules (srfi srfi-18))
  1681. @end example
  1682. SRFI-18 defines facilities for threads, mutexes, condition variables,
  1683. time, and exception handling. Because these facilities are at a higher
  1684. level than Guile's primitives, they are implemented as a layer on top of
  1685. what Guile provides. In particular this means that a Guile mutex is not
  1686. a SRFI-18 mutex, and a Guile thread is not a SRFI-18 thread, and so on.
  1687. Guile provides a set of primitives and SRFI-18 is one of the systems built in terms of those primitives.
  1688. @menu
  1689. * SRFI-18 Threads:: Executing code
  1690. * SRFI-18 Mutexes:: Mutual exclusion devices
  1691. * SRFI-18 Condition variables:: Synchronizing of groups of threads
  1692. * SRFI-18 Time:: Representation of times and durations
  1693. * SRFI-18 Exceptions:: Signalling and handling errors
  1694. @end menu
  1695. @node SRFI-18 Threads
  1696. @subsubsection SRFI-18 Threads
  1697. Threads created by SRFI-18 differ in two ways from threads created by
  1698. Guile's built-in thread functions. First, a thread created by SRFI-18
  1699. @code{make-thread} begins in a blocked state and will not start
  1700. execution until @code{thread-start!} is called on it. Second, SRFI-18
  1701. threads are constructed with a top-level exception handler that
  1702. captures any exceptions that are thrown on thread exit.
  1703. SRFI-18 threads are disjoint from Guile's primitive threads.
  1704. @xref{Threads}, for more on Guile's primitive facility.
  1705. @defun current-thread
  1706. Returns the thread that called this function. This is the same
  1707. procedure as the same-named built-in procedure @code{current-thread}
  1708. (@pxref{Threads}).
  1709. @end defun
  1710. @defun thread? obj
  1711. Returns @code{#t} if @var{obj} is a thread, @code{#f} otherwise. This
  1712. is the same procedure as the same-named built-in procedure
  1713. @code{thread?} (@pxref{Threads}).
  1714. @end defun
  1715. @defun make-thread thunk [name]
  1716. Call @code{thunk} in a new thread and with a new dynamic state,
  1717. returning the new thread and optionally assigning it the object name
  1718. @var{name}, which may be any Scheme object.
  1719. Note that the name @code{make-thread} conflicts with the
  1720. @code{(ice-9 threads)} function @code{make-thread}. Applications
  1721. wanting to use both of these functions will need to refer to them by
  1722. different names.
  1723. @end defun
  1724. @defun thread-name thread
  1725. Returns the name assigned to @var{thread} at the time of its creation,
  1726. or @code{#f} if it was not given a name.
  1727. @end defun
  1728. @defun thread-specific thread
  1729. @defunx thread-specific-set! thread obj
  1730. Get or set the ``object-specific'' property of @var{thread}. In
  1731. Guile's implementation of SRFI-18, this value is stored as an object
  1732. property, and will be @code{#f} if not set.
  1733. @end defun
  1734. @defun thread-start! thread
  1735. Unblocks @var{thread} and allows it to begin execution if it has not
  1736. done so already.
  1737. @end defun
  1738. @defun thread-yield!
  1739. If one or more threads are waiting to execute, calling
  1740. @code{thread-yield!} forces an immediate context switch to one of them.
  1741. Otherwise, @code{thread-yield!} has no effect. @code{thread-yield!}
  1742. behaves identically to the Guile built-in function @code{yield}.
  1743. @end defun
  1744. @defun thread-sleep! timeout
  1745. The current thread waits until the point specified by the time object
  1746. @var{timeout} is reached (@pxref{SRFI-18 Time}). This blocks the
  1747. thread only if @var{timeout} represents a point in the future. it is
  1748. an error for @var{timeout} to be @code{#f}.
  1749. @end defun
  1750. @defun thread-terminate! thread
  1751. Causes an abnormal termination of @var{thread}. If @var{thread} is
  1752. not already terminated, all mutexes owned by @var{thread} become
  1753. unlocked/abandoned. If @var{thread} is the current thread,
  1754. @code{thread-terminate!} does not return. Otherwise
  1755. @code{thread-terminate!} returns an unspecified value; the termination
  1756. of @var{thread} will occur before @code{thread-terminate!} returns.
  1757. Subsequent attempts to join on @var{thread} will cause a ``terminated
  1758. thread exception'' to be raised.
  1759. @code{thread-terminate!} is compatible with the thread cancellation
  1760. procedures in the core threads API (@pxref{Threads}) in that if a
  1761. cleanup handler has been installed for the target thread, it will be
  1762. called before the thread exits and its return value (or exception, if
  1763. any) will be stored for later retrieval via a call to
  1764. @code{thread-join!}.
  1765. @end defun
  1766. @defun thread-join! thread [timeout [timeout-val]]
  1767. Wait for @var{thread} to terminate and return its exit value. When a
  1768. time value @var{timeout} is given, it specifies a point in time where
  1769. the waiting should be aborted. When the waiting is aborted,
  1770. @var{timeout-val} is returned if it is specified; otherwise, a
  1771. @code{join-timeout-exception} exception is raised
  1772. (@pxref{SRFI-18 Exceptions}). Exceptions may also be raised if the
  1773. thread was terminated by a call to @code{thread-terminate!}
  1774. (@code{terminated-thread-exception} will be raised) or if the thread
  1775. exited by raising an exception that was handled by the top-level
  1776. exception handler (@code{uncaught-exception} will be raised; the
  1777. original exception can be retrieved using
  1778. @code{uncaught-exception-reason}).
  1779. @end defun
  1780. @node SRFI-18 Mutexes
  1781. @subsubsection SRFI-18 Mutexes
  1782. SRFI-18 mutexes are disjoint from Guile's primitive mutexes.
  1783. @xref{Mutexes and Condition Variables}, for more on Guile's primitive
  1784. facility.
  1785. @defun make-mutex [name]
  1786. Returns a new mutex, optionally assigning it the object name @var{name},
  1787. which may be any Scheme object. The returned mutex will be created with
  1788. the configuration described above.
  1789. @end defun
  1790. @defun mutex-name mutex
  1791. Returns the name assigned to @var{mutex} at the time of its creation, or
  1792. @code{#f} if it was not given a name.
  1793. @end defun
  1794. @defun mutex-specific mutex
  1795. Return the ``object-specific'' property of @var{mutex}, or @code{#f} if
  1796. none is set.
  1797. @end defun
  1798. @defun mutex-specific-set! mutex obj
  1799. Set the ``object-specific'' property of @var{mutex}.
  1800. @end defun
  1801. @defun mutex-state mutex
  1802. Returns information about the state of @var{mutex}. Possible values
  1803. are:
  1804. @itemize @bullet
  1805. @item
  1806. thread @var{t}: the mutex is in the locked/owned state and thread
  1807. @var{t} is the owner of the mutex
  1808. @item
  1809. symbol @code{not-owned}: the mutex is in the locked/not-owned state
  1810. @item
  1811. symbol @code{abandoned}: the mutex is in the unlocked/abandoned state
  1812. @item
  1813. symbol @code{not-abandoned}: the mutex is in the
  1814. unlocked/not-abandoned state
  1815. @end itemize
  1816. @end defun
  1817. @defun mutex-lock! mutex [timeout [thread]]
  1818. Lock @var{mutex}, optionally specifying a time object @var{timeout}
  1819. after which to abort the lock attempt and a thread @var{thread} giving
  1820. a new owner for @var{mutex} different than the current thread.
  1821. @end defun
  1822. @defun mutex-unlock! mutex [condition-variable [timeout]]
  1823. Unlock @var{mutex}, optionally specifying a condition variable
  1824. @var{condition-variable} on which to wait, either indefinitely or,
  1825. optionally, until the time object @var{timeout} has passed, to be
  1826. signalled.
  1827. @end defun
  1828. @node SRFI-18 Condition variables
  1829. @subsubsection SRFI-18 Condition variables
  1830. SRFI-18 does not specify a ``wait'' function for condition variables.
  1831. Waiting on a condition variable can be simulated using the SRFI-18
  1832. @code{mutex-unlock!} function described in the previous section.
  1833. SRFI-18 condition variables are disjoint from Guile's primitive
  1834. condition variables. @xref{Mutexes and Condition Variables}, for more
  1835. on Guile's primitive facility.
  1836. @defun condition-variable? obj
  1837. Returns @code{#t} if @var{obj} is a condition variable, @code{#f}
  1838. otherwise.
  1839. @end defun
  1840. @defun make-condition-variable [name]
  1841. Returns a new condition variable, optionally assigning it the object
  1842. name @var{name}, which may be any Scheme object.
  1843. @end defun
  1844. @defun condition-variable-name condition-variable
  1845. Returns the name assigned to @var{condition-variable} at the time of its
  1846. creation, or @code{#f} if it was not given a name.
  1847. @end defun
  1848. @defun condition-variable-specific condition-variable
  1849. Return the ``object-specific'' property of @var{condition-variable}, or
  1850. @code{#f} if none is set.
  1851. @end defun
  1852. @defun condition-variable-specific-set! condition-variable obj
  1853. Set the ``object-specific'' property of @var{condition-variable}.
  1854. @end defun
  1855. @defun condition-variable-signal! condition-variable
  1856. @defunx condition-variable-broadcast! condition-variable
  1857. Wake up one thread that is waiting for @var{condition-variable}, in
  1858. the case of @code{condition-variable-signal!}, or all threads waiting
  1859. for it, in the case of @code{condition-variable-broadcast!}.
  1860. @end defun
  1861. @node SRFI-18 Time
  1862. @subsubsection SRFI-18 Time
  1863. The SRFI-18 time functions manipulate time in two formats: a
  1864. ``time object'' type that represents an absolute point in time in some
  1865. implementation-specific way; and the number of seconds since some
  1866. unspecified ``epoch''. In Guile's implementation, the epoch is the
  1867. Unix epoch, 00:00:00 UTC, January 1, 1970.
  1868. @defun current-time
  1869. Return the current time as a time object. This procedure replaces
  1870. the procedure of the same name in the core library, which returns the
  1871. current time in seconds since the epoch.
  1872. @end defun
  1873. @defun time? obj
  1874. Returns @code{#t} if @var{obj} is a time object, @code{#f} otherwise.
  1875. @end defun
  1876. @defun time->seconds time
  1877. @defunx seconds->time seconds
  1878. Convert between time objects and numerical values representing the
  1879. number of seconds since the epoch. When converting from a time object
  1880. to seconds, the return value is the number of seconds between
  1881. @var{time} and the epoch. When converting from seconds to a time
  1882. object, the return value is a time object that represents a time
  1883. @var{seconds} seconds after the epoch.
  1884. @end defun
  1885. @node SRFI-18 Exceptions
  1886. @subsubsection SRFI-18 Exceptions
  1887. SRFI-18 exceptions are identical to the exceptions provided by
  1888. Guile's implementation of SRFI-34. The behavior of exception
  1889. handlers invoked to handle exceptions thrown from SRFI-18 functions,
  1890. however, differs from the conventional behavior of SRFI-34 in that
  1891. the continuation of the handler is the same as that of the call to
  1892. the function. Handlers are called in a tail-recursive manner; the
  1893. exceptions do not ``bubble up''.
  1894. @defun current-exception-handler
  1895. Returns the current exception handler.
  1896. @end defun
  1897. @defun with-exception-handler handler thunk
  1898. Installs @var{handler} as the current exception handler and calls the
  1899. procedure @var{thunk} with no arguments, returning its value as the
  1900. value of the exception. @var{handler} must be a procedure that accepts
  1901. a single argument. The current exception handler at the time this
  1902. procedure is called will be restored after the call returns.
  1903. @end defun
  1904. @defun raise obj
  1905. Raise @var{obj} as an exception. This is the same procedure as the
  1906. same-named procedure defined in SRFI 34.
  1907. @end defun
  1908. @defun join-timeout-exception? obj
  1909. Returns @code{#t} if @var{obj} is an exception raised as the result of
  1910. performing a timed join on a thread that does not exit within the
  1911. specified timeout, @code{#f} otherwise.
  1912. @end defun
  1913. @defun abandoned-mutex-exception? obj
  1914. Returns @code{#t} if @var{obj} is an exception raised as the result of
  1915. attempting to lock a mutex that has been abandoned by its owner thread,
  1916. @code{#f} otherwise.
  1917. @end defun
  1918. @defun terminated-thread-exception? obj
  1919. Returns @code{#t} if @var{obj} is an exception raised as the result of
  1920. joining on a thread that exited as the result of a call to
  1921. @code{thread-terminate!}.
  1922. @end defun
  1923. @defun uncaught-exception? obj
  1924. @defunx uncaught-exception-reason exc
  1925. @code{uncaught-exception?} returns @code{#t} if @var{obj} is an
  1926. exception thrown as the result of joining a thread that exited by
  1927. raising an exception that was handled by the top-level exception
  1928. handler installed by @code{make-thread}. When this occurs, the
  1929. original exception is preserved as part of the exception thrown by
  1930. @code{thread-join!} and can be accessed by calling
  1931. @code{uncaught-exception-reason} on that exception. Note that
  1932. because this exception-preservation mechanism is a side-effect of
  1933. @code{make-thread}, joining on threads that exited as described above
  1934. but were created by other means will not raise this
  1935. @code{uncaught-exception} error.
  1936. @end defun
  1937. @node SRFI-19
  1938. @subsection SRFI-19 - Time/Date Library
  1939. @cindex SRFI-19
  1940. @cindex time
  1941. @cindex date
  1942. This is an implementation of the SRFI-19 time/date library. The
  1943. functions and variables described here are provided by
  1944. @example
  1945. (use-modules (srfi srfi-19))
  1946. @end example
  1947. @strong{Caution}: The current code in this module incorrectly extends
  1948. the Gregorian calendar leap year rule back prior to the introduction
  1949. of those reforms in 1582 (or the appropriate year in various
  1950. countries). The Julian calendar was used prior to 1582, and there
  1951. were 10 days skipped for the reform, but the code doesn't implement
  1952. that.
  1953. This will be fixed some time. Until then calculations for 1583
  1954. onwards are correct, but prior to that any day/month/year and day of
  1955. the week calculations are wrong.
  1956. @menu
  1957. * SRFI-19 Introduction::
  1958. * SRFI-19 Time::
  1959. * SRFI-19 Date::
  1960. * SRFI-19 Time/Date conversions::
  1961. * SRFI-19 Date to string::
  1962. * SRFI-19 String to date::
  1963. @end menu
  1964. @node SRFI-19 Introduction
  1965. @subsubsection SRFI-19 Introduction
  1966. @cindex universal time
  1967. @cindex atomic time
  1968. @cindex UTC
  1969. @cindex TAI
  1970. This module implements time and date representations and calculations,
  1971. in various time systems, including universal time (UTC) and atomic
  1972. time (TAI).
  1973. For those not familiar with these time systems, TAI is based on a
  1974. fixed length second derived from oscillations of certain atoms. UTC
  1975. differs from TAI by an integral number of seconds, which is increased
  1976. or decreased at announced times to keep UTC aligned to a mean solar
  1977. day (the orbit and rotation of the earth are not quite constant).
  1978. @cindex leap second
  1979. So far, only increases in the TAI
  1980. @tex
  1981. $\leftrightarrow$
  1982. @end tex
  1983. @ifnottex
  1984. <->
  1985. @end ifnottex
  1986. UTC difference have been needed. Such an increase is a ``leap
  1987. second'', an extra second of TAI introduced at the end of a UTC day.
  1988. When working entirely within UTC this is never seen, every day simply
  1989. has 86400 seconds. But when converting from TAI to a UTC date, an
  1990. extra 23:59:60 is present, where normally a day would end at 23:59:59.
  1991. Effectively the UTC second from 23:59:59 to 00:00:00 has taken two TAI
  1992. seconds.
  1993. @cindex system clock
  1994. In the current implementation, the system clock is assumed to be UTC,
  1995. and a table of leap seconds in the code converts to TAI. See comments
  1996. in @file{srfi-19.scm} for how to update this table.
  1997. @cindex julian day
  1998. @cindex modified julian day
  1999. Also, for those not familiar with the terminology, a @dfn{Julian Day}
  2000. is a real number which is a count of days and fraction of a day, in
  2001. UTC, starting from -4713-01-01T12:00:00Z, ie.@: midday Monday 1 Jan
  2002. 4713 B.C. A @dfn{Modified Julian Day} is the same, but starting from
  2003. 1858-11-17T00:00:00Z, ie.@: midnight 17 November 1858 UTC. That time
  2004. is julian day 2400000.5.
  2005. @c The SRFI-1 spec says -4714-11-24T12:00:00Z (November 24, -4714 at
  2006. @c noon, UTC), but this is incorrect. It looks like it might have
  2007. @c arisen from the code incorrectly treating years a multiple of 100
  2008. @c but not 400 prior to 1582 as non-leap years, where instead the Julian
  2009. @c calendar should be used so all multiples of 4 before 1582 are leap
  2010. @c years.
  2011. @node SRFI-19 Time
  2012. @subsubsection SRFI-19 Time
  2013. @cindex time
  2014. A @dfn{time} object has type, seconds and nanoseconds fields
  2015. representing a point in time starting from some epoch. This is an
  2016. arbitrary point in time, not just a time of day. Although times are
  2017. represented in nanoseconds, the actual resolution may be lower.
  2018. The following variables hold the possible time types. For instance
  2019. @code{(current-time time-process)} would give the current CPU process
  2020. time.
  2021. @defvar time-utc
  2022. Universal Coordinated Time (UTC).
  2023. @cindex UTC
  2024. @end defvar
  2025. @defvar time-tai
  2026. International Atomic Time (TAI).
  2027. @cindex TAI
  2028. @end defvar
  2029. @defvar time-monotonic
  2030. Monotonic time, meaning a monotonically increasing time starting from
  2031. an unspecified epoch.
  2032. Note that in the current implementation @code{time-monotonic} is the
  2033. same as @code{time-tai}, and unfortunately is therefore affected by
  2034. adjustments to the system clock. Perhaps this will change in the
  2035. future.
  2036. @end defvar
  2037. @defvar time-duration
  2038. A duration, meaning simply a difference between two times.
  2039. @end defvar
  2040. @defvar time-process
  2041. CPU time spent in the current process, starting from when the process
  2042. began.
  2043. @cindex process time
  2044. @end defvar
  2045. @defvar time-thread
  2046. CPU time spent in the current thread. Not currently implemented.
  2047. @cindex thread time
  2048. @end defvar
  2049. @sp 1
  2050. @defun time? obj
  2051. Return @code{#t} if @var{obj} is a time object, or @code{#f} if not.
  2052. @end defun
  2053. @defun make-time type nanoseconds seconds
  2054. Create a time object with the given @var{type}, @var{seconds} and
  2055. @var{nanoseconds}.
  2056. @end defun
  2057. @defun time-type time
  2058. @defunx time-nanosecond time
  2059. @defunx time-second time
  2060. @defunx set-time-type! time type
  2061. @defunx set-time-nanosecond! time nsec
  2062. @defunx set-time-second! time sec
  2063. Get or set the type, seconds or nanoseconds fields of a time object.
  2064. @code{set-time-type!} merely changes the field, it doesn't convert the
  2065. time value. For conversions, see @ref{SRFI-19 Time/Date conversions}.
  2066. @end defun
  2067. @defun copy-time time
  2068. Return a new time object, which is a copy of the given @var{time}.
  2069. @end defun
  2070. @defun current-time [type]
  2071. Return the current time of the given @var{type}. The default
  2072. @var{type} is @code{time-utc}.
  2073. Note that the name @code{current-time} conflicts with the Guile core
  2074. @code{current-time} function (@pxref{Time}) as well as the SRFI-18
  2075. @code{current-time} function (@pxref{SRFI-18 Time}). Applications
  2076. wanting to use more than one of these functions will need to refer to
  2077. them by different names.
  2078. @end defun
  2079. @defun time-resolution [type]
  2080. Return the resolution, in nanoseconds, of the given time @var{type}.
  2081. The default @var{type} is @code{time-utc}.
  2082. @end defun
  2083. @defun time<=? t1 t2
  2084. @defunx time<? t1 t2
  2085. @defunx time=? t1 t2
  2086. @defunx time>=? t1 t2
  2087. @defunx time>? t1 t2
  2088. Return @code{#t} or @code{#f} according to the respective relation
  2089. between time objects @var{t1} and @var{t2}. @var{t1} and @var{t2}
  2090. must be the same time type.
  2091. @end defun
  2092. @defun time-difference t1 t2
  2093. @defunx time-difference! t1 t2
  2094. Return a time object of type @code{time-duration} representing the
  2095. period between @var{t1} and @var{t2}. @var{t1} and @var{t2} must be
  2096. the same time type.
  2097. @code{time-difference} returns a new time object,
  2098. @code{time-difference!} may modify @var{t1} to form its return.
  2099. @end defun
  2100. @defun add-duration time duration
  2101. @defunx add-duration! time duration
  2102. @defunx subtract-duration time duration
  2103. @defunx subtract-duration! time duration
  2104. Return a time object which is @var{time} with the given @var{duration}
  2105. added or subtracted. @var{duration} must be a time object of type
  2106. @code{time-duration}.
  2107. @code{add-duration} and @code{subtract-duration} return a new time
  2108. object. @code{add-duration!} and @code{subtract-duration!} may modify
  2109. the given @var{time} to form their return.
  2110. @end defun
  2111. @node SRFI-19 Date
  2112. @subsubsection SRFI-19 Date
  2113. @cindex date
  2114. A @dfn{date} object represents a date in the Gregorian calendar and a
  2115. time of day on that date in some timezone.
  2116. The fields are year, month, day, hour, minute, second, nanoseconds and
  2117. timezone. A date object is immutable, its fields can be read but they
  2118. cannot be modified once the object is created.
  2119. @defun date? obj
  2120. Return @code{#t} if @var{obj} is a date object, or @code{#f} if not.
  2121. @end defun
  2122. @defun make-date nsecs seconds minutes hours date month year zone-offset
  2123. Create a new date object.
  2124. @c
  2125. @c FIXME: What can we say about the ranges of the values. The
  2126. @c current code looks it doesn't normalize, but expects then in their
  2127. @c usual range already.
  2128. @c
  2129. @end defun
  2130. @defun date-nanosecond date
  2131. Nanoseconds, 0 to 999999999.
  2132. @end defun
  2133. @defun date-second date
  2134. Seconds, 0 to 59, or 60 for a leap second. 60 is never seen when working
  2135. entirely within UTC, it's only when converting to or from TAI.
  2136. @end defun
  2137. @defun date-minute date
  2138. Minutes, 0 to 59.
  2139. @end defun
  2140. @defun date-hour date
  2141. Hour, 0 to 23.
  2142. @end defun
  2143. @defun date-day date
  2144. Day of the month, 1 to 31 (or less, according to the month).
  2145. @end defun
  2146. @defun date-month date
  2147. Month, 1 to 12.
  2148. @end defun
  2149. @defun date-year date
  2150. Year, eg.@: 2003. Dates B.C.@: are negative, eg.@: @math{-46} is 46
  2151. B.C. There is no year 0, year @math{-1} is followed by year 1.
  2152. @end defun
  2153. @defun date-zone-offset date
  2154. Time zone, an integer number of seconds east of Greenwich.
  2155. @end defun
  2156. @defun date-year-day date
  2157. Day of the year, starting from 1 for 1st January.
  2158. @end defun
  2159. @defun date-week-day date
  2160. Day of the week, starting from 0 for Sunday.
  2161. @end defun
  2162. @defun date-week-number date dstartw
  2163. Week of the year, ignoring a first partial week. @var{dstartw} is the
  2164. day of the week which is taken to start a week, 0 for Sunday, 1 for
  2165. Monday, etc.
  2166. @c
  2167. @c FIXME: The spec doesn't say whether numbering starts at 0 or 1.
  2168. @c The code looks like it's 0, if that's the correct intention.
  2169. @c
  2170. @end defun
  2171. @c The SRFI text doesn't actually give the default for tz-offset, but
  2172. @c the reference implementation has the local timezone and the
  2173. @c conversions functions all specify that, so it should be ok to
  2174. @c document it here.
  2175. @c
  2176. @defun current-date [tz-offset]
  2177. Return a date object representing the current date/time, in UTC offset
  2178. by @var{tz-offset}. @var{tz-offset} is seconds east of Greenwich and
  2179. defaults to the local timezone.
  2180. @end defun
  2181. @defun current-julian-day
  2182. @cindex julian day
  2183. Return the current Julian Day.
  2184. @end defun
  2185. @defun current-modified-julian-day
  2186. @cindex modified julian day
  2187. Return the current Modified Julian Day.
  2188. @end defun
  2189. @node SRFI-19 Time/Date conversions
  2190. @subsubsection SRFI-19 Time/Date conversions
  2191. @cindex time conversion
  2192. @cindex date conversion
  2193. @defun date->julian-day date
  2194. @defunx date->modified-julian-day date
  2195. @defunx date->time-monotonic date
  2196. @defunx date->time-tai date
  2197. @defunx date->time-utc date
  2198. @end defun
  2199. @defun julian-day->date jdn [tz-offset]
  2200. @defunx julian-day->time-monotonic jdn
  2201. @defunx julian-day->time-tai jdn
  2202. @defunx julian-day->time-utc jdn
  2203. @end defun
  2204. @defun modified-julian-day->date jdn [tz-offset]
  2205. @defunx modified-julian-day->time-monotonic jdn
  2206. @defunx modified-julian-day->time-tai jdn
  2207. @defunx modified-julian-day->time-utc jdn
  2208. @end defun
  2209. @defun time-monotonic->date time [tz-offset]
  2210. @defunx time-monotonic->time-tai time
  2211. @defunx time-monotonic->time-tai! time
  2212. @defunx time-monotonic->time-utc time
  2213. @defunx time-monotonic->time-utc! time
  2214. @end defun
  2215. @defun time-tai->date time [tz-offset]
  2216. @defunx time-tai->julian-day time
  2217. @defunx time-tai->modified-julian-day time
  2218. @defunx time-tai->time-monotonic time
  2219. @defunx time-tai->time-monotonic! time
  2220. @defunx time-tai->time-utc time
  2221. @defunx time-tai->time-utc! time
  2222. @end defun
  2223. @defun time-utc->date time [tz-offset]
  2224. @defunx time-utc->julian-day time
  2225. @defunx time-utc->modified-julian-day time
  2226. @defunx time-utc->time-monotonic time
  2227. @defunx time-utc->time-monotonic! time
  2228. @defunx time-utc->time-tai time
  2229. @defunx time-utc->time-tai! time
  2230. @sp 1
  2231. Convert between dates, times and days of the respective types. For
  2232. instance @code{time-tai->time-utc} accepts a @var{time} object of type
  2233. @code{time-tai} and returns an object of type @code{time-utc}.
  2234. The @code{!} variants may modify their @var{time} argument to form
  2235. their return. The plain functions create a new object.
  2236. For conversions to dates, @var{tz-offset} is seconds east of
  2237. Greenwich. The default is the local timezone, at the given time, as
  2238. provided by the system, using @code{localtime} (@pxref{Time}).
  2239. On 32-bit systems, @code{localtime} is limited to a 32-bit
  2240. @code{time_t}, so a default @var{tz-offset} is only available for
  2241. times between Dec 1901 and Jan 2038. For prior dates an application
  2242. might like to use the value in 1902, though some locations have zone
  2243. changes prior to that. For future dates an application might like to
  2244. assume today's rules extend indefinitely. But for correct daylight
  2245. savings transitions it will be necessary to take an offset for the
  2246. same day and time but a year in range and which has the same starting
  2247. weekday and same leap/non-leap (to support rules like last Sunday in
  2248. October).
  2249. @end defun
  2250. @node SRFI-19 Date to string
  2251. @subsubsection SRFI-19 Date to string
  2252. @cindex date to string
  2253. @cindex string, from date
  2254. @defun date->string date [format]
  2255. Convert a date to a string under the control of a format.
  2256. @var{format} should be a string containing @samp{~} escapes, which
  2257. will be expanded as per the following conversion table. The default
  2258. @var{format} is @samp{~c}, a locale-dependent date and time.
  2259. Many of these conversion characters are the same as POSIX
  2260. @code{strftime} (@pxref{Time}), but there are some extras and some
  2261. variations.
  2262. @multitable {MMMM} {MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM}
  2263. @item @nicode{~~} @tab literal ~
  2264. @item @nicode{~a} @tab locale abbreviated weekday, eg.@: @samp{Sun}
  2265. @item @nicode{~A} @tab locale full weekday, eg.@: @samp{Sunday}
  2266. @item @nicode{~b} @tab locale abbreviated month, eg.@: @samp{Jan}
  2267. @item @nicode{~B} @tab locale full month, eg.@: @samp{January}
  2268. @item @nicode{~c} @tab locale date and time, eg.@: @*
  2269. @samp{Fri Jul 14 20:28:42-0400 2000}
  2270. @item @nicode{~d} @tab day of month, zero padded, @samp{01} to @samp{31}
  2271. @c Spec says d/m/y, reference implementation says m/d/y.
  2272. @c Apparently the reference code was the intention, but would like to
  2273. @c see an errata published for the spec before contradicting it here.
  2274. @c
  2275. @c @item @nicode{~D} @tab date @nicode{~d/~m/~y}
  2276. @item @nicode{~e} @tab day of month, blank padded, @samp{ 1} to @samp{31}
  2277. @item @nicode{~f} @tab seconds and fractional seconds,
  2278. with locale decimal point, eg.@: @samp{5.2}
  2279. @item @nicode{~h} @tab same as @nicode{~b}
  2280. @item @nicode{~H} @tab hour, 24-hour clock, zero padded, @samp{00} to @samp{23}
  2281. @item @nicode{~I} @tab hour, 12-hour clock, zero padded, @samp{01} to @samp{12}
  2282. @item @nicode{~j} @tab day of year, zero padded, @samp{001} to @samp{366}
  2283. @item @nicode{~k} @tab hour, 24-hour clock, blank padded, @samp{ 0} to @samp{23}
  2284. @item @nicode{~l} @tab hour, 12-hour clock, blank padded, @samp{ 1} to @samp{12}
  2285. @item @nicode{~m} @tab month, zero padded, @samp{01} to @samp{12}
  2286. @item @nicode{~M} @tab minute, zero padded, @samp{00} to @samp{59}
  2287. @item @nicode{~n} @tab newline
  2288. @item @nicode{~N} @tab nanosecond, zero padded, @samp{000000000} to @samp{999999999}
  2289. @item @nicode{~p} @tab locale AM or PM
  2290. @item @nicode{~r} @tab time, 12 hour clock, @samp{~I:~M:~S ~p}
  2291. @item @nicode{~s} @tab number of full seconds since ``the epoch'' in UTC
  2292. @item @nicode{~S} @tab second, zero padded @samp{00} to @samp{60} @*
  2293. (usual limit is 59, 60 is a leap second)
  2294. @item @nicode{~t} @tab horizontal tab character
  2295. @item @nicode{~T} @tab time, 24 hour clock, @samp{~H:~M:~S}
  2296. @item @nicode{~U} @tab week of year, Sunday first day of week,
  2297. @samp{00} to @samp{52}
  2298. @item @nicode{~V} @tab week of year, Monday first day of week,
  2299. @samp{01} to @samp{53}
  2300. @item @nicode{~w} @tab day of week, 0 for Sunday, @samp{0} to @samp{6}
  2301. @item @nicode{~W} @tab week of year, Monday first day of week,
  2302. @samp{00} to @samp{52}
  2303. @c The spec has ~x as an apparent duplicate of ~W, and ~X as a locale
  2304. @c date. The reference code has ~x as the locale date and ~X as a
  2305. @c locale time. The rule is apparently that the code should be
  2306. @c believed, but would like to see an errata for the spec before
  2307. @c contradicting it here.
  2308. @c
  2309. @c @item @nicode{~x} @tab week of year, Monday as first day of week,
  2310. @c @samp{00} to @samp{53}
  2311. @c @item @nicode{~X} @tab locale date, eg.@: @samp{07/31/00}
  2312. @item @nicode{~y} @tab year, two digits, @samp{00} to @samp{99}
  2313. @item @nicode{~Y} @tab year, full, eg.@: @samp{2003}
  2314. @item @nicode{~z} @tab time zone, RFC-822 style
  2315. @item @nicode{~Z} @tab time zone symbol (not currently implemented)
  2316. @item @nicode{~1} @tab ISO-8601 date, @samp{~Y-~m-~d}
  2317. @item @nicode{~2} @tab ISO-8601 time+zone, @samp{~H:~M:~S~z}
  2318. @item @nicode{~3} @tab ISO-8601 time, @samp{~H:~M:~S}
  2319. @item @nicode{~4} @tab ISO-8601 date/time+zone, @samp{~Y-~m-~dT~H:~M:~S~z}
  2320. @item @nicode{~5} @tab ISO-8601 date/time, @samp{~Y-~m-~dT~H:~M:~S}
  2321. @end multitable
  2322. @end defun
  2323. Conversions @samp{~D}, @samp{~x} and @samp{~X} are not currently
  2324. described here, since the specification and reference implementation
  2325. differ.
  2326. Conversion is locale-dependent on systems that support it
  2327. (@pxref{Accessing Locale Information}). @xref{Locales,
  2328. @code{setlocale}}, for information on how to change the current
  2329. locale.
  2330. @node SRFI-19 String to date
  2331. @subsubsection SRFI-19 String to date
  2332. @cindex string to date
  2333. @cindex date, from string
  2334. @c FIXME: Can we say what happens when an incomplete date is
  2335. @c converted? I.e. fields left as 0, or what? The spec seems to be
  2336. @c silent on this.
  2337. @defun string->date input template
  2338. Convert an @var{input} string to a date under the control of a
  2339. @var{template} string. Return a newly created date object.
  2340. Literal characters in @var{template} must match characters in
  2341. @var{input} and @samp{~} escapes must match the input forms described
  2342. in the table below. ``Skip to'' means characters up to one of the
  2343. given type are ignored, or ``no skip'' for no skipping. ``Read'' is
  2344. what's then read, and ``Set'' is the field affected in the date
  2345. object.
  2346. For example @samp{~Y} skips input characters until a digit is reached,
  2347. at which point it expects a year and stores that to the year field of
  2348. the date.
  2349. @multitable {MMMM} {@nicode{char-alphabetic?}} {MMMMMMMMMMMMMMMMMMMMMMMMM} {@nicode{date-zone-offset}}
  2350. @item
  2351. @tab Skip to
  2352. @tab Read
  2353. @tab Set
  2354. @item @nicode{~~}
  2355. @tab no skip
  2356. @tab literal ~
  2357. @tab nothing
  2358. @item @nicode{~a}
  2359. @tab @nicode{char-alphabetic?}
  2360. @tab locale abbreviated weekday name
  2361. @tab nothing
  2362. @item @nicode{~A}
  2363. @tab @nicode{char-alphabetic?}
  2364. @tab locale full weekday name
  2365. @tab nothing
  2366. @c Note that the SRFI spec says that ~b and ~B don't set anything,
  2367. @c but that looks like a mistake. The reference implementation sets
  2368. @c the month field, which seems sensible and is what we describe
  2369. @c here.
  2370. @item @nicode{~b}
  2371. @tab @nicode{char-alphabetic?}
  2372. @tab locale abbreviated month name
  2373. @tab @nicode{date-month}
  2374. @item @nicode{~B}
  2375. @tab @nicode{char-alphabetic?}
  2376. @tab locale full month name
  2377. @tab @nicode{date-month}
  2378. @item @nicode{~d}
  2379. @tab @nicode{char-numeric?}
  2380. @tab day of month
  2381. @tab @nicode{date-day}
  2382. @item @nicode{~e}
  2383. @tab no skip
  2384. @tab day of month, blank padded
  2385. @tab @nicode{date-day}
  2386. @item @nicode{~h}
  2387. @tab same as @samp{~b}
  2388. @item @nicode{~H}
  2389. @tab @nicode{char-numeric?}
  2390. @tab hour
  2391. @tab @nicode{date-hour}
  2392. @item @nicode{~k}
  2393. @tab no skip
  2394. @tab hour, blank padded
  2395. @tab @nicode{date-hour}
  2396. @item @nicode{~m}
  2397. @tab @nicode{char-numeric?}
  2398. @tab month
  2399. @tab @nicode{date-month}
  2400. @item @nicode{~M}
  2401. @tab @nicode{char-numeric?}
  2402. @tab minute
  2403. @tab @nicode{date-minute}
  2404. @item @nicode{~S}
  2405. @tab @nicode{char-numeric?}
  2406. @tab second
  2407. @tab @nicode{date-second}
  2408. @item @nicode{~y}
  2409. @tab no skip
  2410. @tab 2-digit year
  2411. @tab @nicode{date-year} within 50 years
  2412. @item @nicode{~Y}
  2413. @tab @nicode{char-numeric?}
  2414. @tab year
  2415. @tab @nicode{date-year}
  2416. @item @nicode{~z}
  2417. @tab no skip
  2418. @tab time zone
  2419. @tab date-zone-offset
  2420. @end multitable
  2421. Notice that the weekday matching forms don't affect the date object
  2422. returned, instead the weekday will be derived from the day, month and
  2423. year.
  2424. Conversion is locale-dependent on systems that support it
  2425. (@pxref{Accessing Locale Information}). @xref{Locales,
  2426. @code{setlocale}}, for information on how to change the current
  2427. locale.
  2428. @end defun
  2429. @node SRFI-23
  2430. @subsection SRFI-23 - Error Reporting
  2431. @cindex SRFI-23
  2432. The SRFI-23 @code{error} procedure is always available.
  2433. @node SRFI-26
  2434. @subsection SRFI-26 - specializing parameters
  2435. @cindex SRFI-26
  2436. @cindex parameter specialize
  2437. @cindex argument specialize
  2438. @cindex specialize parameter
  2439. This SRFI provides a syntax for conveniently specializing selected
  2440. parameters of a function. It can be used with,
  2441. @example
  2442. (use-modules (srfi srfi-26))
  2443. @end example
  2444. @deffn {library syntax} cut slot1 slot2 @dots{}
  2445. @deffnx {library syntax} cute slot1 slot2 @dots{}
  2446. Return a new procedure which will make a call (@var{slot1} @var{slot2}
  2447. @dots{}) but with selected parameters specialized to given expressions.
  2448. An example will illustrate the idea. The following is a
  2449. specialization of @code{write}, sending output to
  2450. @code{my-output-port},
  2451. @example
  2452. (cut write <> my-output-port)
  2453. @result{}
  2454. (lambda (obj) (write obj my-output-port))
  2455. @end example
  2456. The special symbol @code{<>} indicates a slot to be filled by an
  2457. argument to the new procedure. @code{my-output-port} on the other
  2458. hand is an expression to be evaluated and passed, ie.@: it specializes
  2459. the behaviour of @code{write}.
  2460. @table @nicode
  2461. @item <>
  2462. A slot to be filled by an argument from the created procedure.
  2463. Arguments are assigned to @code{<>} slots in the order they appear in
  2464. the @code{cut} form, there's no way to re-arrange arguments.
  2465. The first argument to @code{cut} is usually a procedure (or expression
  2466. giving a procedure), but @code{<>} is allowed there too. For example,
  2467. @example
  2468. (cut <> 1 2 3)
  2469. @result{}
  2470. (lambda (proc) (proc 1 2 3))
  2471. @end example
  2472. @item <...>
  2473. A slot to be filled by all remaining arguments from the new procedure.
  2474. This can only occur at the end of a @code{cut} form.
  2475. For example, a procedure taking a variable number of arguments like
  2476. @code{max} but in addition enforcing a lower bound,
  2477. @example
  2478. (define my-lower-bound 123)
  2479. (cut max my-lower-bound <...>)
  2480. @result{}
  2481. (lambda arglist (apply max my-lower-bound arglist))
  2482. @end example
  2483. @end table
  2484. For @code{cut} the specializing expressions are evaluated each time
  2485. the new procedure is called. For @code{cute} they're evaluated just
  2486. once, when the new procedure is created. The name @code{cute} stands
  2487. for ``@code{cut} with evaluated arguments''. In all cases the
  2488. evaluations take place in an unspecified order.
  2489. The following illustrates the difference between @code{cut} and
  2490. @code{cute},
  2491. @example
  2492. (cut format <> "the time is ~s" (current-time))
  2493. @result{}
  2494. (lambda (port) (format port "the time is ~s" (current-time)))
  2495. (cute format <> "the time is ~s" (current-time))
  2496. @result{}
  2497. (let ((val (current-time)))
  2498. (lambda (port) (format port "the time is ~s" val))
  2499. @end example
  2500. (There's no provision for a mixture of @code{cut} and @code{cute}
  2501. where some expressions would be evaluated every time but others
  2502. evaluated only once.)
  2503. @code{cut} is really just a shorthand for the sort of @code{lambda}
  2504. forms shown in the above examples. But notice @code{cut} avoids the
  2505. need to name unspecialized parameters, and is more compact. Use in
  2506. functional programming style or just with @code{map}, @code{for-each}
  2507. or similar is typical.
  2508. @example
  2509. (map (cut * 2 <>) '(1 2 3 4))
  2510. (for-each (cut write <> my-port) my-list)
  2511. @end example
  2512. @end deffn
  2513. @node SRFI-27
  2514. @subsection SRFI-27 - Sources of Random Bits
  2515. @cindex SRFI-27
  2516. This subsection is based on the
  2517. @uref{http://srfi.schemers.org/srfi-27/srfi-27.html, specification of
  2518. SRFI-27} written by Sebastian Egner.
  2519. @c The copyright notice and license text of the SRFI-27 specification is
  2520. @c reproduced below:
  2521. @c Copyright (C) Sebastian Egner (2002). All Rights Reserved.
  2522. @c Permission is hereby granted, free of charge, to any person obtaining a
  2523. @c copy of this software and associated documentation files (the
  2524. @c "Software"), to deal in the Software without restriction, including
  2525. @c without limitation the rights to use, copy, modify, merge, publish,
  2526. @c distribute, sublicense, and/or sell copies of the Software, and to
  2527. @c permit persons to whom the Software is furnished to do so, subject to
  2528. @c the following conditions:
  2529. @c The above copyright notice and this permission notice shall be included
  2530. @c in all copies or substantial portions of the Software.
  2531. @c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  2532. @c OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2533. @c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  2534. @c NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  2535. @c LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  2536. @c OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  2537. @c WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  2538. This SRFI provides access to a (pseudo) random number generator; for
  2539. Guile's built-in random number facilities, which SRFI-27 is implemented
  2540. upon, @xref{Random}. With SRFI-27, random numbers are obtained from a
  2541. @emph{random source}, which encapsulates a random number generation
  2542. algorithm and its state.
  2543. @menu
  2544. * SRFI-27 Default Random Source:: Obtaining random numbers
  2545. * SRFI-27 Random Sources:: Creating and manipulating random sources
  2546. * SRFI-27 Random Number Generators:: Obtaining random number generators
  2547. @end menu
  2548. @node SRFI-27 Default Random Source
  2549. @subsubsection The Default Random Source
  2550. @cindex SRFI-27
  2551. @defun random-integer n
  2552. Return a random number between zero (inclusive) and @var{n} (exclusive),
  2553. using the default random source. The numbers returned have a uniform
  2554. distribution.
  2555. @end defun
  2556. @defun random-real
  2557. Return a random number in (0,1), using the default random source. The
  2558. numbers returned have a uniform distribution.
  2559. @end defun
  2560. @defun default-random-source
  2561. A random source from which @code{random-integer} and @code{random-real}
  2562. have been derived using @code{random-source-make-integers} and
  2563. @code{random-source-make-reals} (@pxref{SRFI-27 Random Number Generators}
  2564. for those procedures). Note that an assignment to
  2565. @code{default-random-source} does not change @code{random-integer} or
  2566. @code{random-real}; it is also strongly recommended not to assign a new
  2567. value.
  2568. @end defun
  2569. @node SRFI-27 Random Sources
  2570. @subsubsection Random Sources
  2571. @cindex SRFI-27
  2572. @defun make-random-source
  2573. Create a new random source. The stream of random numbers obtained from
  2574. each random source created by this procedure will be identical, unless
  2575. its state is changed by one of the procedures below.
  2576. @end defun
  2577. @defun random-source? object
  2578. Tests whether @var{object} is a random source. Random sources are a
  2579. disjoint type.
  2580. @end defun
  2581. @defun random-source-randomize! source
  2582. Attempt to set the state of the random source to a truly random value.
  2583. The current implementation uses a seed based on the current system time.
  2584. @end defun
  2585. @defun random-source-pseudo-randomize! source i j
  2586. Changes the state of the random source s into the initial state of the
  2587. (@var{i}, @var{j})-th independent random source, where @var{i} and
  2588. @var{j} are non-negative integers. This procedure provides a mechanism
  2589. to obtain a large number of independent random sources (usually all
  2590. derived from the same backbone generator), indexed by two integers. In
  2591. contrast to @code{random-source-randomize!}, this procedure is entirely
  2592. deterministic.
  2593. @end defun
  2594. The state associated with a random state can be obtained an reinstated
  2595. with the following procedures:
  2596. @defun random-source-state-ref source
  2597. @defunx random-source-state-set! source state
  2598. Get and set the state of a random source. No assumptions should be made
  2599. about the nature of the state object, besides it having an external
  2600. representation (i.e.@: it can be passed to @code{write} and subsequently
  2601. @code{read} back).
  2602. @end defun
  2603. @node SRFI-27 Random Number Generators
  2604. @subsubsection Obtaining random number generator procedures
  2605. @cindex SRFI-27
  2606. @defun random-source-make-integers source
  2607. Obtains a procedure to generate random integers using the random source
  2608. @var{source}. The returned procedure takes a single argument @var{n},
  2609. which must be a positive integer, and returns the next uniformly
  2610. distributed random integer from the interval @{0, ..., @var{n}-1@} by
  2611. advancing the state of @var{source}.
  2612. If an application obtains and uses several generators for the same
  2613. random source @var{source}, a call to any of these generators advances
  2614. the state of @var{source}. Hence, the generators do not produce the
  2615. same sequence of random integers each but rather share a state. This
  2616. also holds for all other types of generators derived from a fixed random
  2617. sources.
  2618. While the SRFI text specifies that ``Implementations that support
  2619. concurrency make sure that the state of a generator is properly
  2620. advanced'', this is currently not the case in Guile's implementation of
  2621. SRFI-27, as it would cause a severe performance penalty. So in
  2622. multi-threaded programs, you either must perform locking on random
  2623. sources shared between threads yourself, or use different random sources
  2624. for multiple threads.
  2625. @end defun
  2626. @defun random-source-make-reals source
  2627. @defunx random-source-make-reals source unit
  2628. Obtains a procedure to generate random real numbers @math{0 < x < 1}
  2629. using the random source @var{source}. The procedure rand is called
  2630. without arguments.
  2631. The optional parameter @var{unit} determines the type of numbers being
  2632. produced by the returned procedure and the quantization of the output.
  2633. @var{unit} must be a number such that @math{0 < @var{unit} < 1}. The
  2634. numbers created by the returned procedure are of the same numerical type
  2635. as @var{unit} and the potential output values are spaced by at most
  2636. @var{unit}. One can imagine rand to create numbers as @var{x} *
  2637. @var{unit} where @var{x} is a random integer in @{1, ...,
  2638. floor(1/unit)-1@}. Note, however, that this need not be the way the
  2639. values are actually created and that the actual resolution of rand can
  2640. be much higher than unit. In case @var{unit} is absent it defaults to a
  2641. reasonably small value (related to the width of the mantissa of an
  2642. efficient number format).
  2643. @end defun
  2644. @node SRFI-28
  2645. @subsection SRFI-28 - Basic Format Strings
  2646. @cindex SRFI-28
  2647. SRFI-28 provides a basic @code{format} procedure that provides only
  2648. the @code{~a}, @code{~s}, @code{~%}, and @code{~~} format specifiers.
  2649. You can import this procedure by using:
  2650. @lisp
  2651. (use-modules (srfi srfi-28))
  2652. @end lisp
  2653. @deffn {Scheme Procedure} format message arg @dots{}
  2654. Returns a formatted message, using @var{message} as the format string,
  2655. which can contain the following format specifiers:
  2656. @table @code
  2657. @item ~a
  2658. Insert the textual representation of the next @var{arg}, as if printed
  2659. by @code{display}.
  2660. @item ~s
  2661. Insert the textual representation of the next @var{arg}, as if printed
  2662. by @code{write}.
  2663. @item ~%
  2664. Insert a newline.
  2665. @item ~~
  2666. Insert a tilde.
  2667. @end table
  2668. This procedure is the same as calling @code{simple-format}
  2669. (@pxref{Simple Output}) with @code{#f} as the destination.
  2670. @end deffn
  2671. @node SRFI-30
  2672. @subsection SRFI-30 - Nested Multi-line Comments
  2673. @cindex SRFI-30
  2674. Starting from version 2.0, Guile's @code{read} supports SRFI-30/R6RS
  2675. nested multi-line comments by default, @ref{Block Comments}.
  2676. @node SRFI-31
  2677. @subsection SRFI-31 - A special form `rec' for recursive evaluation
  2678. @cindex SRFI-31
  2679. @cindex recursive expression
  2680. @findex rec
  2681. SRFI-31 defines a special form that can be used to create
  2682. self-referential expressions more conveniently. The syntax is as
  2683. follows:
  2684. @example
  2685. @group
  2686. <rec expression> --> (rec <variable> <expression>)
  2687. <rec expression> --> (rec (<variable>+) <body>)
  2688. @end group
  2689. @end example
  2690. The first syntax can be used to create self-referential expressions,
  2691. for example:
  2692. @lisp
  2693. guile> (define tmp (rec ones (cons 1 (delay ones))))
  2694. @end lisp
  2695. The second syntax can be used to create anonymous recursive functions:
  2696. @lisp
  2697. guile> (define tmp (rec (display-n item n)
  2698. (if (positive? n)
  2699. (begin (display n) (display-n (- n 1))))))
  2700. guile> (tmp 42 3)
  2701. 424242
  2702. guile>
  2703. @end lisp
  2704. @node SRFI-34
  2705. @subsection SRFI-34 - Exception handling for programs
  2706. @cindex SRFI-34
  2707. Guile provides an implementation of
  2708. @uref{http://srfi.schemers.org/srfi-34/srfi-34.html, SRFI-34's exception
  2709. handling mechanisms} as an alternative to its own built-in mechanisms
  2710. (@pxref{Exceptions}). It can be made available as follows:
  2711. @lisp
  2712. (use-modules (srfi srfi-34))
  2713. @end lisp
  2714. @c FIXME: Document it.
  2715. @node SRFI-35
  2716. @subsection SRFI-35 - Conditions
  2717. @cindex SRFI-35
  2718. @cindex conditions
  2719. @cindex exceptions
  2720. @uref{http://srfi.schemers.org/srfi-35/srfi-35.html, SRFI-35} implements
  2721. @dfn{conditions}, a data structure akin to records designed to convey
  2722. information about exceptional conditions between parts of a program. It
  2723. is normally used in conjunction with SRFI-34's @code{raise}:
  2724. @lisp
  2725. (raise (condition (&message
  2726. (message "An error occurred"))))
  2727. @end lisp
  2728. Users can define @dfn{condition types} containing arbitrary information.
  2729. Condition types may inherit from one another. This allows the part of
  2730. the program that handles (or ``catches'') conditions to get accurate
  2731. information about the exceptional condition that arose.
  2732. SRFI-35 conditions are made available using:
  2733. @lisp
  2734. (use-modules (srfi srfi-35))
  2735. @end lisp
  2736. The procedures available to manipulate condition types are the
  2737. following:
  2738. @deffn {Scheme Procedure} make-condition-type id parent field-names
  2739. Return a new condition type named @var{id}, inheriting from
  2740. @var{parent}, and with the fields whose names are listed in
  2741. @var{field-names}. @var{field-names} must be a list of symbols and must
  2742. not contain names already used by @var{parent} or one of its supertypes.
  2743. @end deffn
  2744. @deffn {Scheme Procedure} condition-type? obj
  2745. Return true if @var{obj} is a condition type.
  2746. @end deffn
  2747. Conditions can be created and accessed with the following procedures:
  2748. @deffn {Scheme Procedure} make-condition type . field+value
  2749. Return a new condition of type @var{type} with fields initialized as
  2750. specified by @var{field+value}, a sequence of field names (symbols) and
  2751. values as in the following example:
  2752. @lisp
  2753. (let ((&ct (make-condition-type 'foo &condition '(a b c))))
  2754. (make-condition &ct 'a 1 'b 2 'c 3))
  2755. @end lisp
  2756. Note that all fields of @var{type} and its supertypes must be specified.
  2757. @end deffn
  2758. @deffn {Scheme Procedure} make-compound-condition condition1 condition2 @dots{}
  2759. Return a new compound condition composed of @var{condition1}
  2760. @var{condition2} @enddots{}. The returned condition has the type of
  2761. each condition of condition1 condition2 @dots{} (per
  2762. @code{condition-has-type?}).
  2763. @end deffn
  2764. @deffn {Scheme Procedure} condition-has-type? c type
  2765. Return true if condition @var{c} has type @var{type}.
  2766. @end deffn
  2767. @deffn {Scheme Procedure} condition-ref c field-name
  2768. Return the value of the field named @var{field-name} from condition @var{c}.
  2769. If @var{c} is a compound condition and several underlying condition
  2770. types contain a field named @var{field-name}, then the value of the
  2771. first such field is returned, using the order in which conditions were
  2772. passed to @code{make-compound-condition}.
  2773. @end deffn
  2774. @deffn {Scheme Procedure} extract-condition c type
  2775. Return a condition of condition type @var{type} with the field values
  2776. specified by @var{c}.
  2777. If @var{c} is a compound condition, extract the field values from the
  2778. subcondition belonging to @var{type} that appeared first in the call to
  2779. @code{make-compound-condition} that created the condition.
  2780. @end deffn
  2781. Convenience macros are also available to create condition types and
  2782. conditions.
  2783. @deffn {library syntax} define-condition-type type supertype predicate field-spec...
  2784. Define a new condition type named @var{type} that inherits from
  2785. @var{supertype}. In addition, bind @var{predicate} to a type predicate
  2786. that returns true when passed a condition of type @var{type} or any of
  2787. its subtypes. @var{field-spec} must have the form @code{(field
  2788. accessor)} where @var{field} is the name of field of @var{type} and
  2789. @var{accessor} is the name of a procedure to access field @var{field} in
  2790. conditions of type @var{type}.
  2791. The example below defines condition type @code{&foo}, inheriting from
  2792. @code{&condition} with fields @code{a}, @code{b} and @code{c}:
  2793. @lisp
  2794. (define-condition-type &foo &condition
  2795. foo-condition?
  2796. (a foo-a)
  2797. (b foo-b)
  2798. (c foo-c))
  2799. @end lisp
  2800. @end deffn
  2801. @deffn {library syntax} condition type-field-binding1 type-field-binding2 @dots{}
  2802. Return a new condition or compound condition, initialized according to
  2803. @var{type-field-binding1} @var{type-field-binding2} @enddots{}. Each
  2804. @var{type-field-binding} must have the form @code{(type
  2805. field-specs...)}, where @var{type} is the name of a variable bound to a
  2806. condition type; each @var{field-spec} must have the form
  2807. @code{(field-name value)} where @var{field-name} is a symbol denoting
  2808. the field being initialized to @var{value}. As for
  2809. @code{make-condition}, all fields must be specified.
  2810. The following example returns a simple condition:
  2811. @lisp
  2812. (condition (&message (message "An error occurred")))
  2813. @end lisp
  2814. The one below returns a compound condition:
  2815. @lisp
  2816. (condition (&message (message "An error occurred"))
  2817. (&serious))
  2818. @end lisp
  2819. @end deffn
  2820. Finally, SRFI-35 defines a several standard condition types.
  2821. @defvar &condition
  2822. This condition type is the root of all condition types. It has no
  2823. fields.
  2824. @end defvar
  2825. @defvar &message
  2826. A condition type that carries a message describing the nature of the
  2827. condition to humans.
  2828. @end defvar
  2829. @deffn {Scheme Procedure} message-condition? c
  2830. Return true if @var{c} is of type @code{&message} or one of its
  2831. subtypes.
  2832. @end deffn
  2833. @deffn {Scheme Procedure} condition-message c
  2834. Return the message associated with message condition @var{c}.
  2835. @end deffn
  2836. @defvar &serious
  2837. This type describes conditions serious enough that they cannot safely be
  2838. ignored. It has no fields.
  2839. @end defvar
  2840. @deffn {Scheme Procedure} serious-condition? c
  2841. Return true if @var{c} is of type @code{&serious} or one of its
  2842. subtypes.
  2843. @end deffn
  2844. @defvar &error
  2845. This condition describes errors, typically caused by something that has
  2846. gone wrong in the interaction of the program with the external world or
  2847. the user.
  2848. @end defvar
  2849. @deffn {Scheme Procedure} error? c
  2850. Return true if @var{c} is of type @code{&error} or one of its subtypes.
  2851. @end deffn
  2852. @node SRFI-37
  2853. @subsection SRFI-37 - args-fold
  2854. @cindex SRFI-37
  2855. This is a processor for GNU @code{getopt_long}-style program
  2856. arguments. It provides an alternative, less declarative interface
  2857. than @code{getopt-long} in @code{(ice-9 getopt-long)}
  2858. (@pxref{getopt-long,,The (ice-9 getopt-long) Module}). Unlike
  2859. @code{getopt-long}, it supports repeated options and any number of
  2860. short and long names per option. Access it with:
  2861. @lisp
  2862. (use-modules (srfi srfi-37))
  2863. @end lisp
  2864. @acronym{SRFI}-37 principally provides an @code{option} type and the
  2865. @code{args-fold} function. To use the library, create a set of
  2866. options with @code{option} and use it as a specification for invoking
  2867. @code{args-fold}.
  2868. Here is an example of a simple argument processor for the typical
  2869. @samp{--version} and @samp{--help} options, which returns a backwards
  2870. list of files given on the command line:
  2871. @lisp
  2872. (args-fold (cdr (program-arguments))
  2873. (let ((display-and-exit-proc
  2874. (lambda (msg)
  2875. (lambda (opt name arg loads)
  2876. (display msg) (quit)))))
  2877. (list (option '(#\v "version") #f #f
  2878. (display-and-exit-proc "Foo version 42.0\n"))
  2879. (option '(#\h "help") #f #f
  2880. (display-and-exit-proc
  2881. "Usage: foo scheme-file ..."))))
  2882. (lambda (opt name arg loads)
  2883. (error "Unrecognized option `~A'" name))
  2884. (lambda (op loads) (cons op loads))
  2885. '())
  2886. @end lisp
  2887. @deffn {Scheme Procedure} option names required-arg? optional-arg? processor
  2888. Return an object that specifies a single kind of program option.
  2889. @var{names} is a list of command-line option names, and should consist of
  2890. characters for traditional @code{getopt} short options and strings for
  2891. @code{getopt_long}-style long options.
  2892. @var{required-arg?} and @var{optional-arg?} are mutually exclusive;
  2893. one or both must be @code{#f}. If @var{required-arg?}, the option
  2894. must be followed by an argument on the command line, such as
  2895. @samp{--opt=value} for long options, or an error will be signalled.
  2896. If @var{optional-arg?}, an argument will be taken if available.
  2897. @var{processor} is a procedure that takes at least 3 arguments, called
  2898. when @code{args-fold} encounters the option: the containing option
  2899. object, the name used on the command line, and the argument given for
  2900. the option (or @code{#f} if none). The rest of the arguments are
  2901. @code{args-fold} ``seeds'', and the @var{processor} should return
  2902. seeds as well.
  2903. @end deffn
  2904. @deffn {Scheme Procedure} option-names opt
  2905. @deffnx {Scheme Procedure} option-required-arg? opt
  2906. @deffnx {Scheme Procedure} option-optional-arg? opt
  2907. @deffnx {Scheme Procedure} option-processor opt
  2908. Return the specified field of @var{opt}, an option object, as
  2909. described above for @code{option}.
  2910. @end deffn
  2911. @deffn {Scheme Procedure} args-fold args options unrecognized-option-proc operand-proc seed @dots{}
  2912. Process @var{args}, a list of program arguments such as that returned by
  2913. @code{(cdr (program-arguments))}, in order against @var{options}, a list
  2914. of option objects as described above. All functions called take the
  2915. ``seeds'', or the last multiple-values as multiple arguments, starting
  2916. with @var{seed} @dots{}, and must return the new seeds. Return the
  2917. final seeds.
  2918. Call @code{unrecognized-option-proc}, which is like an option object's
  2919. processor, for any options not found in @var{options}.
  2920. Call @code{operand-proc} with any items on the command line that are
  2921. not named options. This includes arguments after @samp{--}. It is
  2922. called with the argument in question, as well as the seeds.
  2923. @end deffn
  2924. @node SRFI-38
  2925. @subsection SRFI-38 - External Representation for Data With Shared Structure
  2926. @cindex SRFI-38
  2927. This subsection is based on
  2928. @uref{http://srfi.schemers.org/srfi-38/srfi-38.html, the specification
  2929. of SRFI-38} written by Ray Dillinger.
  2930. @c Copyright (C) Ray Dillinger 2003. All Rights Reserved.
  2931. @c Permission is hereby granted, free of charge, to any person obtaining a
  2932. @c copy of this software and associated documentation files (the
  2933. @c "Software"), to deal in the Software without restriction, including
  2934. @c without limitation the rights to use, copy, modify, merge, publish,
  2935. @c distribute, sublicense, and/or sell copies of the Software, and to
  2936. @c permit persons to whom the Software is furnished to do so, subject to
  2937. @c the following conditions:
  2938. @c The above copyright notice and this permission notice shall be included
  2939. @c in all copies or substantial portions of the Software.
  2940. @c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  2941. @c OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2942. @c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  2943. @c NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  2944. @c LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  2945. @c OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  2946. @c WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  2947. This SRFI creates an alternative external representation for data
  2948. written and read using @code{write-with-shared-structure} and
  2949. @code{read-with-shared-structure}. It is identical to the grammar for
  2950. external representation for data written and read with @code{write} and
  2951. @code{read} given in section 7 of R5RS, except that the single
  2952. production
  2953. @example
  2954. <datum> --> <simple datum> | <compound datum>
  2955. @end example
  2956. is replaced by the following five productions:
  2957. @example
  2958. <datum> --> <defining datum> | <nondefining datum> | <defined datum>
  2959. <defining datum> --> #<indexnum>=<nondefining datum>
  2960. <defined datum> --> #<indexnum>#
  2961. <nondefining datum> --> <simple datum> | <compound datum>
  2962. <indexnum> --> <digit 10>+
  2963. @end example
  2964. @deffn {Scheme procedure} write-with-shared-structure obj
  2965. @deffnx {Scheme procedure} write-with-shared-structure obj port
  2966. @deffnx {Scheme procedure} write-with-shared-structure obj port optarg
  2967. Writes an external representation of @var{obj} to the given port.
  2968. Strings that appear in the written representation are enclosed in
  2969. doublequotes, and within those strings backslash and doublequote
  2970. characters are escaped by backslashes. Character objects are written
  2971. using the @code{#\} notation.
  2972. Objects which denote locations rather than values (cons cells, vectors,
  2973. and non-zero-length strings in R5RS scheme; also Guile's structs,
  2974. bytevectors and ports and hash-tables), if they appear at more than one
  2975. point in the data being written, are preceded by @samp{#@var{N}=} the
  2976. first time they are written and replaced by @samp{#@var{N}#} all
  2977. subsequent times they are written, where @var{N} is a natural number
  2978. used to identify that particular object. If objects which denote
  2979. locations occur only once in the structure, then
  2980. @code{write-with-shared-structure} must produce the same external
  2981. representation for those objects as @code{write}.
  2982. @code{write-with-shared-structure} terminates in finite time and
  2983. produces a finite representation when writing finite data.
  2984. @code{write-with-shared-structure} returns an unspecified value. The
  2985. @var{port} argument may be omitted, in which case it defaults to the
  2986. value returned by @code{(current-output-port)}. The @var{optarg}
  2987. argument may also be omitted. If present, its effects on the output and
  2988. return value are unspecified but @code{write-with-shared-structure} must
  2989. still write a representation that can be read by
  2990. @code{read-with-shared-structure}. Some implementations may wish to use
  2991. @var{optarg} to specify formatting conventions, numeric radixes, or
  2992. return values. Guile's implementation ignores @var{optarg}.
  2993. For example, the code
  2994. @lisp
  2995. (begin (define a (cons 'val1 'val2))
  2996. (set-cdr! a a)
  2997. (write-with-shared-structure a))
  2998. @end lisp
  2999. should produce the output @code{#1=(val1 . #1#)}. This shows a cons
  3000. cell whose @code{cdr} contains itself.
  3001. @end deffn
  3002. @deffn {Scheme procedure} read-with-shared-structure
  3003. @deffnx {Scheme procedure} read-with-shared-structure port
  3004. @code{read-with-shared-structure} converts the external representations
  3005. of Scheme objects produced by @code{write-with-shared-structure} into
  3006. Scheme objects. That is, it is a parser for the nonterminal
  3007. @samp{<datum>} in the augmented external representation grammar defined
  3008. above. @code{read-with-shared-structure} returns the next object
  3009. parsable from the given input port, updating @var{port} to point to the
  3010. first character past the end of the external representation of the
  3011. object.
  3012. If an end-of-file is encountered in the input before any characters are
  3013. found that can begin an object, then an end-of-file object is returned.
  3014. The port remains open, and further attempts to read it (by
  3015. @code{read-with-shared-structure} or @code{read} will also return an
  3016. end-of-file object. If an end of file is encountered after the
  3017. beginning of an object's external representation, but the external
  3018. representation is incomplete and therefore not parsable, an error is
  3019. signalled.
  3020. The @var{port} argument may be omitted, in which case it defaults to the
  3021. value returned by @code{(current-input-port)}. It is an error to read
  3022. from a closed port.
  3023. @end deffn
  3024. @node SRFI-39
  3025. @subsection SRFI-39 - Parameters
  3026. @cindex SRFI-39
  3027. This SRFI adds support for dynamically-scoped parameters. SRFI 39 is
  3028. implemented in the Guile core; there's no module needed to get SRFI-39
  3029. itself. Parameters are documented in @ref{Parameters}.
  3030. This module does export one extra function: @code{with-parameters*}.
  3031. This is a Guile-specific addition to the SRFI, similar to the core
  3032. @code{with-fluids*} (@pxref{Fluids and Dynamic States}).
  3033. @defun with-parameters* param-list value-list thunk
  3034. Establish a new dynamic scope, as per @code{parameterize} above,
  3035. taking parameters from @var{param-list} and corresponding values from
  3036. @var{value-list}. A call @code{(@var{thunk})} is made in the new
  3037. scope and the result from that @var{thunk} is the return from
  3038. @code{with-parameters*}.
  3039. @end defun
  3040. @node SRFI-41
  3041. @subsection SRFI-41 - Streams
  3042. @cindex SRFI-41
  3043. This subsection is based on the
  3044. @uref{http://srfi.schemers.org/srfi-41/srfi-41.html, specification of
  3045. SRFI-41} by Philip L.@: Bewig.
  3046. @c The copyright notice and license text of the SRFI-41 specification is
  3047. @c reproduced below:
  3048. @c Copyright (C) Philip L. Bewig (2007). All Rights Reserved.
  3049. @c Permission is hereby granted, free of charge, to any person obtaining a
  3050. @c copy of this software and associated documentation files (the
  3051. @c "Software"), to deal in the Software without restriction, including
  3052. @c without limitation the rights to use, copy, modify, merge, publish,
  3053. @c distribute, sublicense, and/or sell copies of the Software, and to
  3054. @c permit persons to whom the Software is furnished to do so, subject to
  3055. @c the following conditions:
  3056. @c The above copyright notice and this permission notice shall be included
  3057. @c in all copies or substantial portions of the Software.
  3058. @c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  3059. @c OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  3060. @c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  3061. @c NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  3062. @c LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  3063. @c OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  3064. @c WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  3065. @noindent
  3066. This SRFI implements streams, sometimes called lazy lists, a sequential
  3067. data structure containing elements computed only on demand. A stream is
  3068. either null or is a pair with a stream in its cdr. Since elements of a
  3069. stream are computed only when accessed, streams can be infinite. Once
  3070. computed, the value of a stream element is cached in case it is needed
  3071. again. SRFI-41 can be made available with:
  3072. @example
  3073. (use-modules (srfi srfi-41))
  3074. @end example
  3075. @menu
  3076. * SRFI-41 Stream Fundamentals::
  3077. * SRFI-41 Stream Primitives::
  3078. * SRFI-41 Stream Library::
  3079. @end menu
  3080. @node SRFI-41 Stream Fundamentals
  3081. @subsubsection SRFI-41 Stream Fundamentals
  3082. SRFI-41 Streams are based on two mutually-recursive abstract data types:
  3083. An object of the @code{stream} abstract data type is a promise that,
  3084. when forced, is either @code{stream-null} or is an object of type
  3085. @code{stream-pair}. An object of the @code{stream-pair} abstract data
  3086. type contains a @code{stream-car} and a @code{stream-cdr}, which must be
  3087. a @code{stream}. The essential feature of streams is the systematic
  3088. suspensions of the recursive promises between the two data types.
  3089. The object stored in the @code{stream-car} of a @code{stream-pair} is a
  3090. promise that is forced the first time the @code{stream-car} is accessed;
  3091. its value is cached in case it is needed again. The object may have any
  3092. type, and different stream elements may have different types. If the
  3093. @code{stream-car} is never accessed, the object stored there is never
  3094. evaluated. Likewise, the @code{stream-cdr} is a promise to return a
  3095. stream, and is only forced on demand.
  3096. @node SRFI-41 Stream Primitives
  3097. @subsubsection SRFI-41 Stream Primitives
  3098. This library provides eight operators: constructors for
  3099. @code{stream-null} and @code{stream-pair}s, type predicates for streams
  3100. and the two kinds of streams, accessors for both fields of a
  3101. @code{stream-pair}, and a lambda that creates procedures that return
  3102. streams.
  3103. @defvr {Scheme Variable} stream-null
  3104. A promise that, when forced, is a single object, distinguishable from
  3105. all other objects, that represents the null stream. @code{stream-null}
  3106. is immutable and unique.
  3107. @end defvr
  3108. @deffn {Scheme Syntax} stream-cons object-expr stream-expr
  3109. Creates a newly-allocated stream containing a promise that, when forced,
  3110. is a @code{stream-pair} with @var{object-expr} in its @code{stream-car}
  3111. and @var{stream-expr} in its @code{stream-cdr}. Neither
  3112. @var{object-expr} nor @var{stream-expr} is evaluated when
  3113. @code{stream-cons} is called.
  3114. Once created, a @code{stream-pair} is immutable; there is no
  3115. @code{stream-set-car!} or @code{stream-set-cdr!} that modifies an
  3116. existing stream-pair. There is no dotted-pair or improper stream as
  3117. with lists.
  3118. @end deffn
  3119. @deffn {Scheme Procedure} stream? object
  3120. Returns true if @var{object} is a stream, otherwise returns false. If
  3121. @var{object} is a stream, its promise will not be forced. If
  3122. @code{(stream? obj)} returns true, then one of @code{(stream-null? obj)}
  3123. or @code{(stream-pair? obj)} will return true and the other will return
  3124. false.
  3125. @end deffn
  3126. @deffn {Scheme Procedure} stream-null? object
  3127. Returns true if @var{object} is the distinguished null stream, otherwise
  3128. returns false. If @var{object} is a stream, its promise will be forced.
  3129. @end deffn
  3130. @deffn {Scheme Procedure} stream-pair? object
  3131. Returns true if @var{object} is a @code{stream-pair} constructed by
  3132. @code{stream-cons}, otherwise returns false. If @var{object} is a
  3133. stream, its promise will be forced.
  3134. @end deffn
  3135. @deffn {Scheme Procedure} stream-car stream
  3136. Returns the object stored in the @code{stream-car} of @var{stream}. An
  3137. error is signalled if the argument is not a @code{stream-pair}. This
  3138. causes the @var{object-expr} passed to @code{stream-cons} to be
  3139. evaluated if it had not yet been; the value is cached in case it is
  3140. needed again.
  3141. @end deffn
  3142. @deffn {Scheme Procedure} stream-cdr stream
  3143. Returns the stream stored in the @code{stream-cdr} of @var{stream}. An
  3144. error is signalled if the argument is not a @code{stream-pair}.
  3145. @end deffn
  3146. @deffn {Scheme Syntax} stream-lambda formals body @dots{}
  3147. Creates a procedure that returns a promise to evaluate the @var{body} of
  3148. the procedure. The last @var{body} expression to be evaluated must
  3149. yield a stream. As with normal @code{lambda}, @var{formals} may be a
  3150. single variable name, in which case all the formal arguments are
  3151. collected into a single list, or a list of variable names, which may be
  3152. null if there are no arguments, proper if there are an exact number of
  3153. arguments, or dotted if a fixed number of arguments is to be followed by
  3154. zero or more arguments collected into a list. @var{Body} must contain
  3155. at least one expression, and may contain internal definitions preceding
  3156. any expressions to be evaluated.
  3157. @end deffn
  3158. @example
  3159. (define strm123
  3160. (stream-cons 1
  3161. (stream-cons 2
  3162. (stream-cons 3
  3163. stream-null))))
  3164. (stream-car strm123) @result{} 1
  3165. (stream-car (stream-cdr strm123) @result{} 2
  3166. (stream-pair?
  3167. (stream-cdr
  3168. (stream-cons (/ 1 0) stream-null))) @result{} #f
  3169. (stream? (list 1 2 3)) @result{} #f
  3170. (define iter
  3171. (stream-lambda (f x)
  3172. (stream-cons x (iter f (f x)))))
  3173. (define nats (iter (lambda (x) (+ x 1)) 0))
  3174. (stream-car (stream-cdr nats)) @result{} 1
  3175. (define stream-add
  3176. (stream-lambda (s1 s2)
  3177. (stream-cons
  3178. (+ (stream-car s1) (stream-car s2))
  3179. (stream-add (stream-cdr s1)
  3180. (stream-cdr s2)))))
  3181. (define evens (stream-add nats nats))
  3182. (stream-car evens) @result{} 0
  3183. (stream-car (stream-cdr evens)) @result{} 2
  3184. (stream-car (stream-cdr (stream-cdr evens))) @result{} 4
  3185. @end example
  3186. @node SRFI-41 Stream Library
  3187. @subsubsection SRFI-41 Stream Library
  3188. @deffn {Scheme Syntax} define-stream (name args @dots{}) body @dots{}
  3189. Creates a procedure that returns a stream, and may appear anywhere a
  3190. normal @code{define} may appear, including as an internal definition.
  3191. It may contain internal definitions of its own. The defined procedure
  3192. takes arguments in the same way as @code{stream-lambda}.
  3193. @code{define-stream} is syntactic sugar on @code{stream-lambda}; see
  3194. also @code{stream-let}, which is also a sugaring of
  3195. @code{stream-lambda}.
  3196. A simple version of @code{stream-map} that takes only a single input
  3197. stream calls itself recursively:
  3198. @example
  3199. (define-stream (stream-map proc strm)
  3200. (if (stream-null? strm)
  3201. stream-null
  3202. (stream-cons
  3203. (proc (stream-car strm))
  3204. (stream-map proc (stream-cdr strm))))))
  3205. @end example
  3206. @end deffn
  3207. @deffn {Scheme Procedure} list->stream list
  3208. Returns a newly-allocated stream containing the elements from
  3209. @var{list}.
  3210. @end deffn
  3211. @deffn {Scheme Procedure} port->stream [port]
  3212. Returns a newly-allocated stream containing in its elements the
  3213. characters on the port. If @var{port} is not given it defaults to the
  3214. current input port. The returned stream has finite length and is
  3215. terminated by @code{stream-null}.
  3216. It looks like one use of @code{port->stream} would be this:
  3217. @example
  3218. (define s ;wrong!
  3219. (with-input-from-file filename
  3220. (lambda () (port->stream))))
  3221. @end example
  3222. But that fails, because @code{with-input-from-file} is eager, and closes
  3223. the input port prematurely, before the first character is read. To read
  3224. a file into a stream, say:
  3225. @example
  3226. (define-stream (file->stream filename)
  3227. (let ((p (open-input-file filename)))
  3228. (stream-let loop ((c (read-char p)))
  3229. (if (eof-object? c)
  3230. (begin (close-input-port p)
  3231. stream-null)
  3232. (stream-cons c
  3233. (loop (read-char p)))))))
  3234. @end example
  3235. @end deffn
  3236. @deffn {Scheme Syntax} stream object-expr @dots{}
  3237. Creates a newly-allocated stream containing in its elements the objects,
  3238. in order. The @var{object-expr}s are evaluated when they are accessed,
  3239. not when the stream is created. If no objects are given, as in
  3240. (stream), the null stream is returned. See also @code{list->stream}.
  3241. @example
  3242. (define strm123 (stream 1 2 3))
  3243. ; (/ 1 0) not evaluated when stream is created
  3244. (define s (stream 1 (/ 1 0) -1))
  3245. @end example
  3246. @end deffn
  3247. @deffn {Scheme Procedure} stream->list [n] stream
  3248. Returns a newly-allocated list containing in its elements the first
  3249. @var{n} items in @var{stream}. If @var{stream} has less than @var{n}
  3250. items, all the items in the stream will be included in the returned
  3251. list. If @var{n} is not given it defaults to infinity, which means that
  3252. unless @var{stream} is finite @code{stream->list} will never return.
  3253. @example
  3254. (stream->list 10
  3255. (stream-map (lambda (x) (* x x))
  3256. (stream-from 0)))
  3257. @result{} (0 1 4 9 16 25 36 49 64 81)
  3258. @end example
  3259. @end deffn
  3260. @deffn {Scheme Procedure} stream-append stream @dots{}
  3261. Returns a newly-allocated stream containing in its elements those
  3262. elements contained in its input @var{stream}s, in order of input. If
  3263. any of the input streams is infinite, no elements of any of the
  3264. succeeding input streams will appear in the output stream. See also
  3265. @code{stream-concat}.
  3266. @end deffn
  3267. @deffn {Scheme Procedure} stream-concat stream
  3268. Takes a @var{stream} consisting of one or more streams and returns a
  3269. newly-allocated stream containing all the elements of the input streams.
  3270. If any of the streams in the input @var{stream} is infinite, any
  3271. remaining streams in the input stream will never appear in the output
  3272. stream. See also @code{stream-append}.
  3273. @end deffn
  3274. @deffn {Scheme Procedure} stream-constant object @dots{}
  3275. Returns a newly-allocated stream containing in its elements the
  3276. @var{object}s, repeating in succession forever.
  3277. @example
  3278. (stream-constant 1) @result{} 1 1 1 @dots{}
  3279. (stream-constant #t #f) @result{} #t #f #t #f #t #f @dots{}
  3280. @end example
  3281. @end deffn
  3282. @deffn {Scheme Procedure} stream-drop n stream
  3283. Returns the suffix of the input @var{stream} that starts at the next
  3284. element after the first @var{n} elements. The output stream shares
  3285. structure with the input @var{stream}; thus, promises forced in one
  3286. instance of the stream are also forced in the other instance of the
  3287. stream. If the input @var{stream} has less than @var{n} elements,
  3288. @code{stream-drop} returns the null stream. See also
  3289. @code{stream-take}.
  3290. @end deffn
  3291. @deffn {Scheme Procedure} stream-drop-while pred stream
  3292. Returns the suffix of the input @var{stream} that starts at the first
  3293. element @var{x} for which @code{(pred x)} returns false. The output
  3294. stream shares structure with the input @var{stream}. See also
  3295. @code{stream-take-while}.
  3296. @end deffn
  3297. @deffn {Scheme Procedure} stream-filter pred stream
  3298. Returns a newly-allocated stream that contains only those elements
  3299. @var{x} of the input @var{stream} which satisfy the predicate
  3300. @code{pred}.
  3301. @example
  3302. (stream-filter odd? (stream-from 0))
  3303. @result{} 1 3 5 7 9 @dots{}
  3304. @end example
  3305. @end deffn
  3306. @deffn {Scheme Procedure} stream-fold proc base stream
  3307. Applies a binary procedure @var{proc} to @var{base} and the first
  3308. element of @var{stream} to compute a new @var{base}, then applies the
  3309. procedure to the new @var{base} and the next element of @var{stream} to
  3310. compute a succeeding @var{base}, and so on, accumulating a value that is
  3311. finally returned as the value of @code{stream-fold} when the end of the
  3312. stream is reached. @var{stream} must be finite, or @code{stream-fold}
  3313. will enter an infinite loop. See also @code{stream-scan}, which is
  3314. similar to @code{stream-fold}, but useful for infinite streams. For
  3315. readers familiar with other functional languages, this is a left-fold;
  3316. there is no corresponding right-fold, since right-fold relies on finite
  3317. streams that are fully-evaluated, in which case they may as well be
  3318. converted to a list.
  3319. @end deffn
  3320. @deffn {Scheme Procedure} stream-for-each proc stream @dots{}
  3321. Applies @var{proc} element-wise to corresponding elements of the input
  3322. @var{stream}s for side-effects; it returns nothing.
  3323. @code{stream-for-each} stops as soon as any of its input streams is
  3324. exhausted.
  3325. @end deffn
  3326. @deffn {Scheme Procedure} stream-from first [step]
  3327. Creates a newly-allocated stream that contains @var{first} as its first
  3328. element and increments each succeeding element by @var{step}. If
  3329. @var{step} is not given it defaults to 1. @var{first} and @var{step}
  3330. may be of any numeric type. @code{stream-from} is frequently useful as
  3331. a generator in @code{stream-of} expressions. See also
  3332. @code{stream-range} for a similar procedure that creates finite streams.
  3333. @end deffn
  3334. @deffn {Scheme Procedure} stream-iterate proc base
  3335. Creates a newly-allocated stream containing @var{base} in its first
  3336. element and applies @var{proc} to each element in turn to determine the
  3337. succeeding element. See also @code{stream-unfold} and
  3338. @code{stream-unfolds}.
  3339. @end deffn
  3340. @deffn {Scheme Procedure} stream-length stream
  3341. Returns the number of elements in the @var{stream}; it does not evaluate
  3342. its elements. @code{stream-length} may only be used on finite streams;
  3343. it enters an infinite loop with infinite streams.
  3344. @end deffn
  3345. @deffn {Scheme Syntax} stream-let tag ((var expr) @dots{}) body @dots{}
  3346. Creates a local scope that binds each variable to the value of its
  3347. corresponding expression. It additionally binds @var{tag} to a
  3348. procedure which takes the bound variables as arguments and @var{body} as
  3349. its defining expressions, binding the @var{tag} with
  3350. @code{stream-lambda}. @var{tag} is in scope within body, and may be
  3351. called recursively. When the expanded expression defined by the
  3352. @code{stream-let} is evaluated, @code{stream-let} evaluates the
  3353. expressions in its @var{body} in an environment containing the
  3354. newly-bound variables, returning the value of the last expression
  3355. evaluated, which must yield a stream.
  3356. @code{stream-let} provides syntactic sugar on @code{stream-lambda}, in
  3357. the same manner as normal @code{let} provides syntactic sugar on normal
  3358. @code{lambda}. However, unlike normal @code{let}, the @var{tag} is
  3359. required, not optional, because unnamed @code{stream-let} is
  3360. meaningless.
  3361. For example, @code{stream-member} returns the first @code{stream-pair}
  3362. of the input @var{strm} with a @code{stream-car} @var{x} that satisfies
  3363. @code{(eql? obj x)}, or the null stream if @var{x} is not present in
  3364. @var{strm}.
  3365. @example
  3366. (define-stream (stream-member eql? obj strm)
  3367. (stream-let loop ((strm strm))
  3368. (cond ((stream-null? strm) strm)
  3369. ((eql? obj (stream-car strm)) strm)
  3370. (else (loop (stream-cdr strm))))))
  3371. @end example
  3372. @end deffn
  3373. @deffn {Scheme Procedure} stream-map proc stream @dots{}
  3374. Applies @var{proc} element-wise to corresponding elements of the input
  3375. @var{stream}s, returning a newly-allocated stream containing elements
  3376. that are the results of those procedure applications. The output stream
  3377. has as many elements as the minimum-length input stream, and may be
  3378. infinite.
  3379. @end deffn
  3380. @deffn {Scheme Syntax} stream-match stream clause @dots{}
  3381. Provides pattern-matching for streams. The input @var{stream} is an
  3382. expression that evaluates to a stream. Clauses are of the form
  3383. @code{(pattern [fender] expression)}, consisting of a @var{pattern} that
  3384. matches a stream of a particular shape, an optional @var{fender} that
  3385. must succeed if the pattern is to match, and an @var{expression} that is
  3386. evaluated if the pattern matches. There are four types of patterns:
  3387. @itemize @bullet
  3388. @item
  3389. () matches the null stream.
  3390. @item
  3391. (@var{pat0} @var{pat1} @dots{}) matches a finite stream with length
  3392. exactly equal to the number of pattern elements.
  3393. @item
  3394. (@var{pat0} @var{pat1} @dots{} @code{.} @var{pat-rest}) matches an
  3395. infinite stream, or a finite stream with length at least as great as the
  3396. number of pattern elements before the literal dot.
  3397. @item
  3398. @var{pat} matches an entire stream. Should always appear last in the
  3399. list of clauses; it's not an error to appear elsewhere, but subsequent
  3400. clauses could never match.
  3401. @end itemize
  3402. Each pattern element may be either:
  3403. @itemize @bullet
  3404. @item
  3405. An identifier, which matches any stream element. Additionally, the
  3406. value of the stream element is bound to the variable named by the
  3407. identifier, which is in scope in the @var{fender} and @var{expression}
  3408. of the corresponding @var{clause}. Each identifier in a single pattern
  3409. must be unique.
  3410. @item
  3411. A literal underscore (@code{_}), which matches any stream element but
  3412. creates no bindings.
  3413. @end itemize
  3414. The @var{pattern}s are tested in order, left-to-right, until a matching
  3415. pattern is found; if @var{fender} is present, it must evaluate to a true
  3416. value for the match to be successful. Pattern variables are bound in
  3417. the corresponding @var{fender} and @var{expression}. Once the matching
  3418. @var{pattern} is found, the corresponding @var{expression} is evaluated
  3419. and returned as the result of the match. An error is signaled if no
  3420. pattern matches the input @var{stream}.
  3421. @code{stream-match} is often used to distinguish null streams from
  3422. non-null streams, binding @var{head} and @var{tail}:
  3423. @example
  3424. (define (len strm)
  3425. (stream-match strm
  3426. (() 0)
  3427. ((head . tail) (+ 1 (len tail)))))
  3428. @end example
  3429. Fenders can test the common case where two stream elements must be
  3430. identical; the @code{else} pattern is an identifier bound to the entire
  3431. stream, not a keyword as in @code{cond}.
  3432. @example
  3433. (stream-match strm
  3434. ((x y . _) (equal? x y) 'ok)
  3435. (else 'error))
  3436. @end example
  3437. A more complex example uses two nested matchers to match two different
  3438. stream arguments; @code{(stream-merge lt? . strms)} stably merges two or
  3439. more streams ordered by the @code{lt?} predicate:
  3440. @example
  3441. (define-stream (stream-merge lt? . strms)
  3442. (define-stream (merge xx yy)
  3443. (stream-match xx (() yy) ((x . xs)
  3444. (stream-match yy (() xx) ((y . ys)
  3445. (if (lt? y x)
  3446. (stream-cons y (merge xx ys))
  3447. (stream-cons x (merge xs yy))))))))
  3448. (stream-let loop ((strms strms))
  3449. (cond ((null? strms) stream-null)
  3450. ((null? (cdr strms)) (car strms))
  3451. (else (merge (car strms)
  3452. (apply stream-merge lt?
  3453. (cdr strms)))))))
  3454. @end example
  3455. @end deffn
  3456. @deffn {Scheme Syntax} stream-of expr clause @dots{}
  3457. Provides the syntax of stream comprehensions, which generate streams by
  3458. means of looping expressions. The result is a stream of objects of the
  3459. type returned by @var{expr}. There are four types of clauses:
  3460. @itemize @bullet
  3461. @item
  3462. (@var{var} @code{in} @var{stream-expr}) loops over the elements of
  3463. @var{stream-expr}, in order from the start of the stream, binding each
  3464. element of the stream in turn to @var{var}. @code{stream-from} and
  3465. @code{stream-range} are frequently useful as generators for
  3466. @var{stream-expr}.
  3467. @item
  3468. (@var{var} @code{is} @var{expr}) binds @var{var} to the value obtained
  3469. by evaluating @var{expr}.
  3470. @item
  3471. (@var{pred} @var{expr}) includes in the output stream only those
  3472. elements @var{x} which satisfy the predicate @var{pred}.
  3473. @end itemize
  3474. The scope of variables bound in the stream comprehension is the clauses
  3475. to the right of the binding clause (but not the binding clause itself)
  3476. plus the result expression.
  3477. When two or more generators are present, the loops are processed as if
  3478. they are nested from left to right; that is, the rightmost generator
  3479. varies fastest. A consequence of this is that only the first generator
  3480. may be infinite and all subsequent generators must be finite. If no
  3481. generators are present, the result of a stream comprehension is a stream
  3482. containing the result expression; thus, @samp{(stream-of 1)} produces a
  3483. finite stream containing only the element 1.
  3484. @example
  3485. (stream-of (* x x)
  3486. (x in (stream-range 0 10))
  3487. (even? x))
  3488. @result{} 0 4 16 36 64
  3489. (stream-of (list a b)
  3490. (a in (stream-range 1 4))
  3491. (b in (stream-range 1 3)))
  3492. @result{} (1 1) (1 2) (2 1) (2 2) (3 1) (3 2)
  3493. (stream-of (list i j)
  3494. (i in (stream-range 1 5))
  3495. (j in (stream-range (+ i 1) 5)))
  3496. @result{} (1 2) (1 3) (1 4) (2 3) (2 4) (3 4)
  3497. @end example
  3498. @end deffn
  3499. @deffn {Scheme Procedure} stream-range first past [step]
  3500. Creates a newly-allocated stream that contains @var{first} as its first
  3501. element and increments each succeeding element by @var{step}. The
  3502. stream is finite and ends before @var{past}, which is not an element of
  3503. the stream. If @var{step} is not given it defaults to 1 if @var{first}
  3504. is less than past and -1 otherwise. @var{first}, @var{past} and
  3505. @var{step} may be of any real numeric type. @code{stream-range} is
  3506. frequently useful as a generator in @code{stream-of} expressions. See
  3507. also @code{stream-from} for a similar procedure that creates infinite
  3508. streams.
  3509. @example
  3510. (stream-range 0 10) @result{} 0 1 2 3 4 5 6 7 8 9
  3511. (stream-range 0 10 2) @result{} 0 2 4 6 8
  3512. @end example
  3513. Successive elements of the stream are calculated by adding @var{step} to
  3514. @var{first}, so if any of @var{first}, @var{past} or @var{step} are
  3515. inexact, the length of the output stream may differ from
  3516. @code{(ceiling (- (/ (- past first) step) 1)}.
  3517. @end deffn
  3518. @deffn {Scheme Procedure} stream-ref stream n
  3519. Returns the @var{n}th element of stream, counting from zero. An error
  3520. is signaled if @var{n} is greater than or equal to the length of stream.
  3521. @example
  3522. (define (fact n)
  3523. (stream-ref
  3524. (stream-scan * 1 (stream-from 1))
  3525. n))
  3526. @end example
  3527. @end deffn
  3528. @deffn {Scheme Procedure} stream-reverse stream
  3529. Returns a newly-allocated stream containing the elements of the input
  3530. @var{stream} but in reverse order. @code{stream-reverse} may only be
  3531. used with finite streams; it enters an infinite loop with infinite
  3532. streams. @code{stream-reverse} does not force evaluation of the
  3533. elements of the stream.
  3534. @end deffn
  3535. @deffn {Scheme Procedure} stream-scan proc base stream
  3536. Accumulates the partial folds of an input @var{stream} into a
  3537. newly-allocated output stream. The output stream is the @var{base}
  3538. followed by @code{(stream-fold proc base (stream-take i stream))} for
  3539. each of the first @var{i} elements of @var{stream}.
  3540. @example
  3541. (stream-scan + 0 (stream-from 1))
  3542. @result{} (stream 0 1 3 6 10 15 @dots{})
  3543. (stream-scan * 1 (stream-from 1))
  3544. @result{} (stream 1 1 2 6 24 120 @dots{})
  3545. @end example
  3546. @end deffn
  3547. @deffn {Scheme Procedure} stream-take n stream
  3548. Returns a newly-allocated stream containing the first @var{n} elements
  3549. of the input @var{stream}. If the input @var{stream} has less than
  3550. @var{n} elements, so does the output stream. See also
  3551. @code{stream-drop}.
  3552. @end deffn
  3553. @deffn {Scheme Procedure} stream-take-while pred stream
  3554. Takes a predicate and a @code{stream} and returns a newly-allocated
  3555. stream containing those elements @code{x} that form the maximal prefix
  3556. of the input stream which satisfy @var{pred}. See also
  3557. @code{stream-drop-while}.
  3558. @end deffn
  3559. @deffn {Scheme Procedure} stream-unfold map pred gen base
  3560. The fundamental recursive stream constructor. It constructs a stream by
  3561. repeatedly applying @var{gen} to successive values of @var{base}, in the
  3562. manner of @code{stream-iterate}, then applying @var{map} to each of the
  3563. values so generated, appending each of the mapped values to the output
  3564. stream as long as @code{(pred? base)} returns a true value. See also
  3565. @code{stream-iterate} and @code{stream-unfolds}.
  3566. The expression below creates the finite stream @samp{0 1 4 9 16 25 36 49
  3567. 64 81}. Initially the @var{base} is 0, which is less than 10, so
  3568. @var{map} squares the @var{base} and the mapped value becomes the first
  3569. element of the output stream. Then @var{gen} increments the @var{base}
  3570. by 1, so it becomes 1; this is less than 10, so @var{map} squares the
  3571. new @var{base} and 1 becomes the second element of the output stream.
  3572. And so on, until the base becomes 10, when @var{pred} stops the
  3573. recursion and stream-null ends the output stream.
  3574. @example
  3575. (stream-unfold
  3576. (lambda (x) (expt x 2)) ; map
  3577. (lambda (x) (< x 10)) ; pred?
  3578. (lambda (x) (+ x 1)) ; gen
  3579. 0) ; base
  3580. @end example
  3581. @end deffn
  3582. @deffn {Scheme Procedure} stream-unfolds proc seed
  3583. Returns @var{n} newly-allocated streams containing those elements
  3584. produced by successive calls to the generator @var{proc}, which takes
  3585. the current @var{seed} as its argument and returns @var{n}+1 values
  3586. (@var{proc} @var{seed}) @result{} @var{seed} @var{result_0} @dots{} @var{result_n-1}
  3587. where the returned @var{seed} is the input @var{seed} to the next call
  3588. to the generator and @var{result_i} indicates how to produce the next
  3589. element of the @var{i}th result stream:
  3590. @itemize @bullet
  3591. @item
  3592. (@var{value}): @var{value} is the next car of the result stream.
  3593. @item
  3594. @code{#f}: no value produced by this iteration of the generator
  3595. @var{proc} for the result stream.
  3596. @item
  3597. (): the end of the result stream.
  3598. @end itemize
  3599. It may require multiple calls of @var{proc} to produce the next element
  3600. of any particular result stream. See also @code{stream-iterate} and
  3601. @code{stream-unfold}.
  3602. @example
  3603. (define (stream-partition pred? strm)
  3604. (stream-unfolds
  3605. (lambda (s)
  3606. (if (stream-null? s)
  3607. (values s '() '())
  3608. (let ((a (stream-car s))
  3609. (d (stream-cdr s)))
  3610. (if (pred? a)
  3611. (values d (list a) #f)
  3612. (values d #f (list a))))))
  3613. strm))
  3614. (call-with-values
  3615. (lambda ()
  3616. (stream-partition odd?
  3617. (stream-range 1 6)))
  3618. (lambda (odds evens)
  3619. (list (stream->list odds)
  3620. (stream->list evens))))
  3621. @result{} ((1 3 5) (2 4))
  3622. @end example
  3623. @end deffn
  3624. @deffn {Scheme Procedure} stream-zip stream @dots{}
  3625. Returns a newly-allocated stream in which each element is a list (not a
  3626. stream) of the corresponding elements of the input @var{stream}s. The
  3627. output stream is as long as the shortest input @var{stream}, if any of
  3628. the input @var{stream}s is finite, or is infinite if all the input
  3629. @var{stream}s are infinite.
  3630. @end deffn
  3631. @node SRFI-42
  3632. @subsection SRFI-42 - Eager Comprehensions
  3633. @cindex SRFI-42
  3634. See @uref{http://srfi.schemers.org/srfi-42/srfi-42.html, the
  3635. specification of SRFI-42}.
  3636. @node SRFI-43
  3637. @subsection SRFI-43 - Vector Library
  3638. @cindex SRFI-43
  3639. This subsection is based on the
  3640. @uref{http://srfi.schemers.org/srfi-43/srfi-43.html, specification of
  3641. SRFI-43} by Taylor Campbell.
  3642. @c The copyright notice and license text of the SRFI-43 specification is
  3643. @c reproduced below:
  3644. @c Copyright (C) Taylor Campbell (2003). All Rights Reserved.
  3645. @c Permission is hereby granted, free of charge, to any person obtaining a
  3646. @c copy of this software and associated documentation files (the
  3647. @c "Software"), to deal in the Software without restriction, including
  3648. @c without limitation the rights to use, copy, modify, merge, publish,
  3649. @c distribute, sublicense, and/or sell copies of the Software, and to
  3650. @c permit persons to whom the Software is furnished to do so, subject to
  3651. @c the following conditions:
  3652. @c The above copyright notice and this permission notice shall be included
  3653. @c in all copies or substantial portions of the Software.
  3654. @c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  3655. @c OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  3656. @c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  3657. @c NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  3658. @c LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  3659. @c OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  3660. @c WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  3661. @noindent
  3662. SRFI-43 implements a comprehensive library of vector operations. It can
  3663. be made available with:
  3664. @example
  3665. (use-modules (srfi srfi-43))
  3666. @end example
  3667. @menu
  3668. * SRFI-43 Constructors::
  3669. * SRFI-43 Predicates::
  3670. * SRFI-43 Selectors::
  3671. * SRFI-43 Iteration::
  3672. * SRFI-43 Searching::
  3673. * SRFI-43 Mutators::
  3674. * SRFI-43 Conversion::
  3675. @end menu
  3676. @node SRFI-43 Constructors
  3677. @subsubsection SRFI-43 Constructors
  3678. @deffn {Scheme Procedure} make-vector size [fill]
  3679. Create and return a vector of size @var{size}, optionally filling it
  3680. with @var{fill}. The default value of @var{fill} is unspecified.
  3681. @example
  3682. (make-vector 5 3) @result{} #(3 3 3 3 3)
  3683. @end example
  3684. @end deffn
  3685. @deffn {Scheme Procedure} vector x @dots{}
  3686. Create and return a vector whose elements are @var{x} @enddots{}.
  3687. @example
  3688. (vector 0 1 2 3 4) @result{} #(0 1 2 3 4)
  3689. @end example
  3690. @end deffn
  3691. @deffn {Scheme Procedure} vector-unfold f length initial-seed @dots{}
  3692. The fundamental vector constructor. Create a vector whose length
  3693. is @var{length} and iterates across each index k from 0 up to
  3694. @var{length} - 1, applying @var{f} at each iteration to the current
  3695. index and current seeds, in that order, to receive n + 1 values: the
  3696. element to put in the kth slot of the new vector, and n new seeds for
  3697. the next iteration. It is an error for the number of seeds to vary
  3698. between iterations.
  3699. @example
  3700. (vector-unfold (lambda (i x) (values x (- x 1)))
  3701. 10 0)
  3702. @result{} #(0 -1 -2 -3 -4 -5 -6 -7 -8 -9)
  3703. (vector-unfold values 10)
  3704. @result{} #(0 1 2 3 4 5 6 7 8 9)
  3705. @end example
  3706. @end deffn
  3707. @deffn {Scheme Procedure} vector-unfold-right f length initial-seed @dots{}
  3708. Like @code{vector-unfold}, but it uses @var{f} to generate elements from
  3709. right-to-left, rather than left-to-right.
  3710. @example
  3711. (vector-unfold-right (lambda (i x) (values x (+ x 1)))
  3712. 10 0)
  3713. @result{} #(9 8 7 6 5 4 3 2 1 0)
  3714. @end example
  3715. @end deffn
  3716. @deffn {Scheme Procedure} vector-copy vec [start [end [fill]]]
  3717. Allocate a new vector whose length is @var{end} - @var{start} and fills
  3718. it with elements from @var{vec}, taking elements from @var{vec} starting
  3719. at index @var{start} and stopping at index @var{end}. @var{start}
  3720. defaults to 0 and @var{end} defaults to the value of
  3721. @code{(vector-length vec)}. If @var{end} extends beyond the length of
  3722. @var{vec}, the slots in the new vector that obviously cannot be filled
  3723. by elements from @var{vec} are filled with @var{fill}, whose default
  3724. value is unspecified.
  3725. @example
  3726. (vector-copy '#(a b c d e f g h i))
  3727. @result{} #(a b c d e f g h i)
  3728. (vector-copy '#(a b c d e f g h i) 6)
  3729. @result{} #(g h i)
  3730. (vector-copy '#(a b c d e f g h i) 3 6)
  3731. @result{} #(d e f)
  3732. (vector-copy '#(a b c d e f g h i) 6 12 'x)
  3733. @result{} #(g h i x x x)
  3734. @end example
  3735. @end deffn
  3736. @deffn {Scheme Procedure} vector-reverse-copy vec [start [end]]
  3737. Like @code{vector-copy}, but it copies the elements in the reverse order
  3738. from @var{vec}.
  3739. @example
  3740. (vector-reverse-copy '#(5 4 3 2 1 0) 1 5)
  3741. @result{} #(1 2 3 4)
  3742. @end example
  3743. @end deffn
  3744. @deffn {Scheme Procedure} vector-append vec @dots{}
  3745. Return a newly allocated vector that contains all elements in order from
  3746. the subsequent locations in @var{vec} @enddots{}.
  3747. @example
  3748. (vector-append '#(a) '#(b c d))
  3749. @result{} #(a b c d)
  3750. @end example
  3751. @end deffn
  3752. @deffn {Scheme Procedure} vector-concatenate list-of-vectors
  3753. Append each vector in @var{list-of-vectors}. Equivalent to
  3754. @code{(apply vector-append list-of-vectors)}.
  3755. @example
  3756. (vector-concatenate '(#(a b) #(c d)))
  3757. @result{} #(a b c d)
  3758. @end example
  3759. @end deffn
  3760. @node SRFI-43 Predicates
  3761. @subsubsection SRFI-43 Predicates
  3762. @deffn {Scheme Procedure} vector? obj
  3763. Return true if @var{obj} is a vector, else return false.
  3764. @end deffn
  3765. @deffn {Scheme Procedure} vector-empty? vec
  3766. Return true if @var{vec} is empty, i.e. its length is 0, else return
  3767. false.
  3768. @end deffn
  3769. @deffn {Scheme Procedure} vector= elt=? vec @dots{}
  3770. Return true if the vectors @var{vec} @dots{} have equal lengths and
  3771. equal elements according to @var{elt=?}. @var{elt=?} is always applied
  3772. to two arguments. Element comparison must be consistent with @code{eq?}
  3773. in the following sense: if @code{(eq? a b)} returns true, then
  3774. @code{(elt=? a b)} must also return true. The order in which
  3775. comparisons are performed is unspecified.
  3776. @end deffn
  3777. @node SRFI-43 Selectors
  3778. @subsubsection SRFI-43 Selectors
  3779. @deffn {Scheme Procedure} vector-ref vec i
  3780. Return the element at index @var{i} in @var{vec}. Indexing is based on
  3781. zero.
  3782. @end deffn
  3783. @deffn {Scheme Procedure} vector-length vec
  3784. Return the length of @var{vec}.
  3785. @end deffn
  3786. @node SRFI-43 Iteration
  3787. @subsubsection SRFI-43 Iteration
  3788. @deffn {Scheme Procedure} vector-fold kons knil vec1 vec2 @dots{}
  3789. The fundamental vector iterator. @var{kons} is iterated over each index
  3790. in all of the vectors, stopping at the end of the shortest; @var{kons}
  3791. is applied as
  3792. @smalllisp
  3793. (kons i state (vector-ref vec1 i) (vector-ref vec2 i) ...)
  3794. @end smalllisp
  3795. where @var{state} is the current state value, and @var{i} is the current
  3796. index. The current state value begins with @var{knil}, and becomes
  3797. whatever @var{kons} returned at the respective iteration. The iteration
  3798. is strictly left-to-right.
  3799. @end deffn
  3800. @deffn {Scheme Procedure} vector-fold-right kons knil vec1 vec2 @dots{}
  3801. Similar to @code{vector-fold}, but it iterates right-to-left instead of
  3802. left-to-right.
  3803. @end deffn
  3804. @deffn {Scheme Procedure} vector-map f vec1 vec2 @dots{}
  3805. Return a new vector of the shortest size of the vector arguments. Each
  3806. element at index i of the new vector is mapped from the old vectors by
  3807. @smalllisp
  3808. (f i (vector-ref vec1 i) (vector-ref vec2 i) ...)
  3809. @end smalllisp
  3810. The dynamic order of application of @var{f} is unspecified.
  3811. @end deffn
  3812. @deffn {Scheme Procedure} vector-map! f vec1 vec2 @dots{}
  3813. Similar to @code{vector-map}, but rather than mapping the new elements
  3814. into a new vector, the new mapped elements are destructively inserted
  3815. into @var{vec1}. The dynamic order of application of @var{f} is
  3816. unspecified.
  3817. @end deffn
  3818. @deffn {Scheme Procedure} vector-for-each f vec1 vec2 @dots{}
  3819. Call @code{(f i (vector-ref vec1 i) (vector-ref vec2 i) ...)} for each
  3820. index i less than the length of the shortest vector passed. The
  3821. iteration is strictly left-to-right.
  3822. @end deffn
  3823. @deffn {Scheme Procedure} vector-count pred? vec1 vec2 @dots{}
  3824. Count the number of parallel elements in the vectors that satisfy
  3825. @var{pred?}, which is applied, for each index i less than the length of
  3826. the smallest vector, to i and each parallel element in the vectors at
  3827. that index, in order.
  3828. @example
  3829. (vector-count (lambda (i elt) (even? elt))
  3830. '#(3 1 4 1 5 9 2 5 6))
  3831. @result{} 3
  3832. (vector-count (lambda (i x y) (< x y))
  3833. '#(1 3 6 9) '#(2 4 6 8 10 12))
  3834. @result{} 2
  3835. @end example
  3836. @end deffn
  3837. @node SRFI-43 Searching
  3838. @subsubsection SRFI-43 Searching
  3839. @deffn {Scheme Procedure} vector-index pred? vec1 vec2 @dots{}
  3840. Find and return the index of the first elements in @var{vec1} @var{vec2}
  3841. @dots{} that satisfy @var{pred?}. If no matching element is found by
  3842. the end of the shortest vector, return @code{#f}.
  3843. @example
  3844. (vector-index even? '#(3 1 4 1 5 9))
  3845. @result{} 2
  3846. (vector-index < '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))
  3847. @result{} 1
  3848. (vector-index = '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))
  3849. @result{} #f
  3850. @end example
  3851. @end deffn
  3852. @deffn {Scheme Procedure} vector-index-right pred? vec1 vec2 @dots{}
  3853. Like @code{vector-index}, but it searches right-to-left, rather than
  3854. left-to-right. Note that the SRFI 43 specification requires that all
  3855. the vectors must have the same length, but both the SRFI 43 reference
  3856. implementation and Guile's implementation allow vectors with unequal
  3857. lengths, and start searching from the last index of the shortest vector.
  3858. @end deffn
  3859. @deffn {Scheme Procedure} vector-skip pred? vec1 vec2 @dots{}
  3860. Find and return the index of the first elements in @var{vec1} @var{vec2}
  3861. @dots{} that do not satisfy @var{pred?}. If no matching element is
  3862. found by the end of the shortest vector, return @code{#f}. Equivalent
  3863. to @code{vector-index} but with the predicate inverted.
  3864. @example
  3865. (vector-skip number? '#(1 2 a b 3 4 c d)) @result{} 2
  3866. @end example
  3867. @end deffn
  3868. @deffn {Scheme Procedure} vector-skip-right pred? vec1 vec2 @dots{}
  3869. Like @code{vector-skip}, but it searches for a non-matching element
  3870. right-to-left, rather than left-to-right. Note that the SRFI 43
  3871. specification requires that all the vectors must have the same length,
  3872. but both the SRFI 43 reference implementation and Guile's implementation
  3873. allow vectors with unequal lengths, and start searching from the last
  3874. index of the shortest vector.
  3875. @end deffn
  3876. @deffn {Scheme Procedure} vector-binary-search vec value cmp [start [end]]
  3877. Find and return an index of @var{vec} between @var{start} and @var{end}
  3878. whose value is @var{value} using a binary search. If no matching
  3879. element is found, return @code{#f}. The default @var{start} is 0 and
  3880. the default @var{end} is the length of @var{vec}.
  3881. @var{cmp} must be a procedure of two arguments such that @code{(cmp a
  3882. b)} returns a negative integer if @math{a < b}, a positive integer if
  3883. @math{a > b}, or zero if @math{a = b}. The elements of @var{vec} must
  3884. be sorted in non-decreasing order according to @var{cmp}.
  3885. Note that SRFI 43 does not document the @var{start} and @var{end}
  3886. arguments, but both its reference implementation and Guile's
  3887. implementation support them.
  3888. @example
  3889. (define (char-cmp c1 c2)
  3890. (cond ((char<? c1 c2) -1)
  3891. ((char>? c1 c2) 1)
  3892. (else 0)))
  3893. (vector-binary-search '#(#\a #\b #\c #\d #\e #\f #\g #\h)
  3894. #\g
  3895. char-cmp)
  3896. @result{} 6
  3897. @end example
  3898. @end deffn
  3899. @deffn {Scheme Procedure} vector-any pred? vec1 vec2 @dots{}
  3900. Find the first parallel set of elements from @var{vec1} @var{vec2}
  3901. @dots{} for which @var{pred?} returns a true value. If such a parallel
  3902. set of elements exists, @code{vector-any} returns the value that
  3903. @var{pred?} returned for that set of elements. The iteration is
  3904. strictly left-to-right.
  3905. @end deffn
  3906. @deffn {Scheme Procedure} vector-every pred? vec1 vec2 @dots{}
  3907. If, for every index i between 0 and the length of the shortest vector
  3908. argument, the set of elements @code{(vector-ref vec1 i)}
  3909. @code{(vector-ref vec2 i)} @dots{} satisfies @var{pred?},
  3910. @code{vector-every} returns the value that @var{pred?} returned for the
  3911. last set of elements, at the last index of the shortest vector.
  3912. Otherwise it returns @code{#f}. The iteration is strictly
  3913. left-to-right.
  3914. @end deffn
  3915. @node SRFI-43 Mutators
  3916. @subsubsection SRFI-43 Mutators
  3917. @deffn {Scheme Procedure} vector-set! vec i value
  3918. Assign the contents of the location at @var{i} in @var{vec} to
  3919. @var{value}.
  3920. @end deffn
  3921. @deffn {Scheme Procedure} vector-swap! vec i j
  3922. Swap the values of the locations in @var{vec} at @var{i} and @var{j}.
  3923. @end deffn
  3924. @deffn {Scheme Procedure} vector-fill! vec fill [start [end]]
  3925. Assign the value of every location in @var{vec} between @var{start} and
  3926. @var{end} to @var{fill}. @var{start} defaults to 0 and @var{end}
  3927. defaults to the length of @var{vec}.
  3928. @end deffn
  3929. @deffn {Scheme Procedure} vector-reverse! vec [start [end]]
  3930. Destructively reverse the contents of @var{vec} between @var{start} and
  3931. @var{end}. @var{start} defaults to 0 and @var{end} defaults to the
  3932. length of @var{vec}.
  3933. @end deffn
  3934. @deffn {Scheme Procedure} vector-copy! target tstart source [sstart [send]]
  3935. Copy a block of elements from @var{source} to @var{target}, both of
  3936. which must be vectors, starting in @var{target} at @var{tstart} and
  3937. starting in @var{source} at @var{sstart}, ending when (@var{send} -
  3938. @var{sstart}) elements have been copied. It is an error for
  3939. @var{target} to have a length less than (@var{tstart} + @var{send} -
  3940. @var{sstart}). @var{sstart} defaults to 0 and @var{send} defaults to
  3941. the length of @var{source}.
  3942. @end deffn
  3943. @deffn {Scheme Procedure} vector-reverse-copy! target tstart source [sstart [send]]
  3944. Like @code{vector-copy!}, but this copies the elements in the reverse
  3945. order. It is an error if @var{target} and @var{source} are identical
  3946. vectors and the @var{target} and @var{source} ranges overlap; however,
  3947. if @var{tstart} = @var{sstart}, @code{vector-reverse-copy!} behaves as
  3948. @code{(vector-reverse! target tstart send)} would.
  3949. @end deffn
  3950. @node SRFI-43 Conversion
  3951. @subsubsection SRFI-43 Conversion
  3952. @deffn {Scheme Procedure} vector->list vec [start [end]]
  3953. Return a newly allocated list containing the elements in @var{vec}
  3954. between @var{start} and @var{end}. @var{start} defaults to 0 and
  3955. @var{end} defaults to the length of @var{vec}.
  3956. @end deffn
  3957. @deffn {Scheme Procedure} reverse-vector->list vec [start [end]]
  3958. Like @code{vector->list}, but the resulting list contains the specified
  3959. range of elements of @var{vec} in reverse order.
  3960. @end deffn
  3961. @deffn {Scheme Procedure} list->vector proper-list [start [end]]
  3962. Return a newly allocated vector of the elements from @var{proper-list}
  3963. with indices between @var{start} and @var{end}. @var{start} defaults to
  3964. 0 and @var{end} defaults to the length of @var{proper-list}. Note that
  3965. SRFI 43 does not document the @var{start} and @var{end} arguments, but
  3966. both its reference implementation and Guile's implementation support
  3967. them.
  3968. @end deffn
  3969. @deffn {Scheme Procedure} reverse-list->vector proper-list [start [end]]
  3970. Like @code{list->vector}, but the resulting vector contains the specified
  3971. range of elements of @var{proper-list} in reverse order. Note that SRFI
  3972. 43 does not document the @var{start} and @var{end} arguments, but both
  3973. its reference implementation and Guile's implementation support them.
  3974. @end deffn
  3975. @node SRFI-45
  3976. @subsection SRFI-45 - Primitives for Expressing Iterative Lazy Algorithms
  3977. @cindex SRFI-45
  3978. This subsection is based on @uref{http://srfi.schemers.org/srfi-45/srfi-45.html, the
  3979. specification of SRFI-45} written by Andr@'e van Tonder.
  3980. @c Copyright (C) André van Tonder (2003). All Rights Reserved.
  3981. @c Permission is hereby granted, free of charge, to any person obtaining a
  3982. @c copy of this software and associated documentation files (the
  3983. @c "Software"), to deal in the Software without restriction, including
  3984. @c without limitation the rights to use, copy, modify, merge, publish,
  3985. @c distribute, sublicense, and/or sell copies of the Software, and to
  3986. @c permit persons to whom the Software is furnished to do so, subject to
  3987. @c the following conditions:
  3988. @c The above copyright notice and this permission notice shall be included
  3989. @c in all copies or substantial portions of the Software.
  3990. @c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  3991. @c OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  3992. @c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  3993. @c NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  3994. @c LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  3995. @c OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  3996. @c WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  3997. Lazy evaluation is traditionally simulated in Scheme using @code{delay}
  3998. and @code{force}. However, these primitives are not powerful enough to
  3999. express a large class of lazy algorithms that are iterative. Indeed, it
  4000. is folklore in the Scheme community that typical iterative lazy
  4001. algorithms written using delay and force will often require unbounded
  4002. memory.
  4003. This SRFI provides set of three operations: @{@code{lazy}, @code{delay},
  4004. @code{force}@}, which allow the programmer to succinctly express lazy
  4005. algorithms while retaining bounded space behavior in cases that are
  4006. properly tail-recursive. A general recipe for using these primitives is
  4007. provided. An additional procedure @code{eager} is provided for the
  4008. construction of eager promises in cases where efficiency is a concern.
  4009. Although this SRFI redefines @code{delay} and @code{force}, the
  4010. extension is conservative in the sense that the semantics of the subset
  4011. @{@code{delay}, @code{force}@} in isolation (i.e., as long as the
  4012. program does not use @code{lazy}) agrees with that in R5RS. In other
  4013. words, no program that uses the R5RS definitions of delay and force will
  4014. break if those definition are replaced by the SRFI-45 definitions of
  4015. delay and force.
  4016. Guile also adds @code{promise?} to the list of exports, which is not
  4017. part of the official SRFI-45.
  4018. @deffn {Scheme Procedure} promise? obj
  4019. Return true if @var{obj} is an SRFI-45 promise, otherwise return false.
  4020. @end deffn
  4021. @deffn {Scheme Syntax} delay expression
  4022. Takes an expression of arbitrary type @var{a} and returns a promise of
  4023. type @code{(Promise @var{a})} which at some point in the future may be
  4024. asked (by the @code{force} procedure) to evaluate the expression and
  4025. deliver the resulting value.
  4026. @end deffn
  4027. @deffn {Scheme Syntax} lazy expression
  4028. Takes an expression of type @code{(Promise @var{a})} and returns a
  4029. promise of type @code{(Promise @var{a})} which at some point in the
  4030. future may be asked (by the @code{force} procedure) to evaluate the
  4031. expression and deliver the resulting promise.
  4032. @end deffn
  4033. @deffn {Scheme Procedure} force expression
  4034. Takes an argument of type @code{(Promise @var{a})} and returns a value
  4035. of type @var{a} as follows: If a value of type @var{a} has been computed
  4036. for the promise, this value is returned. Otherwise, the promise is
  4037. first evaluated, then overwritten by the obtained promise or value, and
  4038. then force is again applied (iteratively) to the promise.
  4039. @end deffn
  4040. @deffn {Scheme Procedure} eager expression
  4041. Takes an argument of type @var{a} and returns a value of type
  4042. @code{(Promise @var{a})}. As opposed to @code{delay}, the argument is
  4043. evaluated eagerly. Semantically, writing @code{(eager expression)} is
  4044. equivalent to writing
  4045. @lisp
  4046. (let ((value expression)) (delay value)).
  4047. @end lisp
  4048. However, the former is more efficient since it does not require
  4049. unnecessary creation and evaluation of thunks. We also have the
  4050. equivalence
  4051. @lisp
  4052. (delay expression) = (lazy (eager expression))
  4053. @end lisp
  4054. @end deffn
  4055. The following reduction rules may be helpful for reasoning about these
  4056. primitives. However, they do not express the memoization and memory
  4057. usage semantics specified above:
  4058. @lisp
  4059. (force (delay expression)) -> expression
  4060. (force (lazy expression)) -> (force expression)
  4061. (force (eager value)) -> value
  4062. @end lisp
  4063. @subsubheading Correct usage
  4064. We now provide a general recipe for using the primitives @{@code{lazy},
  4065. @code{delay}, @code{force}@} to express lazy algorithms in Scheme. The
  4066. transformation is best described by way of an example: Consider the
  4067. stream-filter algorithm, expressed in a hypothetical lazy language as
  4068. @lisp
  4069. (define (stream-filter p? s)
  4070. (if (null? s) '()
  4071. (let ((h (car s))
  4072. (t (cdr s)))
  4073. (if (p? h)
  4074. (cons h (stream-filter p? t))
  4075. (stream-filter p? t)))))
  4076. @end lisp
  4077. This algorithm can be expressed as follows in Scheme:
  4078. @lisp
  4079. (define (stream-filter p? s)
  4080. (lazy
  4081. (if (null? (force s)) (delay '())
  4082. (let ((h (car (force s)))
  4083. (t (cdr (force s))))
  4084. (if (p? h)
  4085. (delay (cons h (stream-filter p? t)))
  4086. (stream-filter p? t))))))
  4087. @end lisp
  4088. In other words, we
  4089. @itemize @bullet
  4090. @item
  4091. wrap all constructors (e.g., @code{'()}, @code{cons}) with @code{delay},
  4092. @item
  4093. apply @code{force} to arguments of deconstructors (e.g., @code{car},
  4094. @code{cdr} and @code{null?}),
  4095. @item
  4096. wrap procedure bodies with @code{(lazy ...)}.
  4097. @end itemize
  4098. @node SRFI-46
  4099. @subsection SRFI-46 Basic syntax-rules Extensions
  4100. @cindex SRFI-46
  4101. Guile's core @code{syntax-rules} supports the extensions specified by
  4102. SRFI-46/R7RS. Tail patterns have been supported since at least Guile
  4103. 2.0, and custom ellipsis identifiers have been supported since Guile
  4104. 2.0.10. @xref{Syntax Rules}.
  4105. @node SRFI-55
  4106. @subsection SRFI-55 - Requiring Features
  4107. @cindex SRFI-55
  4108. SRFI-55 provides @code{require-extension} which is a portable
  4109. mechanism to load selected SRFI modules. This is implemented in the
  4110. Guile core, there's no module needed to get SRFI-55 itself.
  4111. @deffn {library syntax} require-extension clause1 clause2 @dots{}
  4112. Require the features of @var{clause1} @var{clause2} @dots{} , throwing
  4113. an error if any are unavailable.
  4114. A @var{clause} is of the form @code{(@var{identifier} arg...)}. The
  4115. only @var{identifier} currently supported is @code{srfi} and the
  4116. arguments are SRFI numbers. For example to get SRFI-1 and SRFI-6,
  4117. @example
  4118. (require-extension (srfi 1 6))
  4119. @end example
  4120. @code{require-extension} can only be used at the top-level.
  4121. A Guile-specific program can simply @code{use-modules} to load SRFIs
  4122. not already in the core, @code{require-extension} is for programs
  4123. designed to be portable to other Scheme implementations.
  4124. @end deffn
  4125. @node SRFI-60
  4126. @subsection SRFI-60 - Integers as Bits
  4127. @cindex SRFI-60
  4128. @cindex integers as bits
  4129. @cindex bitwise logical
  4130. This SRFI provides various functions for treating integers as bits and
  4131. for bitwise manipulations. These functions can be obtained with,
  4132. @example
  4133. (use-modules (srfi srfi-60))
  4134. @end example
  4135. Integers are treated as infinite precision twos-complement, the same
  4136. as in the core logical functions (@pxref{Bitwise Operations}). And
  4137. likewise bit indexes start from 0 for the least significant bit. The
  4138. following functions in this SRFI are already in the Guile core,
  4139. @quotation
  4140. @code{logand},
  4141. @code{logior},
  4142. @code{logxor},
  4143. @code{lognot},
  4144. @code{logtest},
  4145. @code{logcount},
  4146. @code{integer-length},
  4147. @code{logbit?},
  4148. @code{ash}
  4149. @end quotation
  4150. @sp 1
  4151. @defun bitwise-and n1 ...
  4152. @defunx bitwise-ior n1 ...
  4153. @defunx bitwise-xor n1 ...
  4154. @defunx bitwise-not n
  4155. @defunx any-bits-set? j k
  4156. @defunx bit-set? index n
  4157. @defunx arithmetic-shift n count
  4158. @defunx bit-field n start end
  4159. @defunx bit-count n
  4160. Aliases for @code{logand}, @code{logior}, @code{logxor},
  4161. @code{lognot}, @code{logtest}, @code{logbit?}, @code{ash},
  4162. @code{bit-extract} and @code{logcount} respectively.
  4163. Note that the name @code{bit-count} conflicts with @code{bit-count} in
  4164. the core (@pxref{Bit Vectors}).
  4165. @end defun
  4166. @defun bitwise-if mask n1 n0
  4167. @defunx bitwise-merge mask n1 n0
  4168. Return an integer with bits selected from @var{n1} and @var{n0}
  4169. according to @var{mask}. Those bits where @var{mask} has 1s are taken
  4170. from @var{n1}, and those where @var{mask} has 0s are taken from
  4171. @var{n0}.
  4172. @example
  4173. (bitwise-if 3 #b0101 #b1010) @result{} 9
  4174. @end example
  4175. @end defun
  4176. @defun log2-binary-factors n
  4177. @defunx first-set-bit n
  4178. Return a count of how many factors of 2 are present in @var{n}. This
  4179. is also the bit index of the lowest 1 bit in @var{n}. If @var{n} is
  4180. 0, the return is @math{-1}.
  4181. @example
  4182. (log2-binary-factors 6) @result{} 1
  4183. (log2-binary-factors -8) @result{} 3
  4184. @end example
  4185. @end defun
  4186. @defun copy-bit index n newbit
  4187. Return @var{n} with the bit at @var{index} set according to
  4188. @var{newbit}. @var{newbit} should be @code{#t} to set the bit to 1,
  4189. or @code{#f} to set it to 0. Bits other than at @var{index} are
  4190. unchanged in the return.
  4191. @example
  4192. (copy-bit 1 #b0101 #t) @result{} 7
  4193. @end example
  4194. @end defun
  4195. @defun copy-bit-field n newbits start end
  4196. Return @var{n} with the bits from @var{start} (inclusive) to @var{end}
  4197. (exclusive) changed to the value @var{newbits}.
  4198. The least significant bit in @var{newbits} goes to @var{start}, the
  4199. next to @math{@var{start}+1}, etc. Anything in @var{newbits} past the
  4200. @var{end} given is ignored.
  4201. @example
  4202. (copy-bit-field #b10000 #b11 1 3) @result{} #b10110
  4203. @end example
  4204. @end defun
  4205. @defun rotate-bit-field n count start end
  4206. Return @var{n} with the bit field from @var{start} (inclusive) to
  4207. @var{end} (exclusive) rotated upwards by @var{count} bits.
  4208. @var{count} can be positive or negative, and it can be more than the
  4209. field width (it'll be reduced modulo the width).
  4210. @example
  4211. (rotate-bit-field #b0110 2 1 4) @result{} #b1010
  4212. @end example
  4213. @end defun
  4214. @defun reverse-bit-field n start end
  4215. Return @var{n} with the bits from @var{start} (inclusive) to @var{end}
  4216. (exclusive) reversed.
  4217. @example
  4218. (reverse-bit-field #b101001 2 4) @result{} #b100101
  4219. @end example
  4220. @end defun
  4221. @defun integer->list n [len]
  4222. Return bits from @var{n} in the form of a list of @code{#t} for 1 and
  4223. @code{#f} for 0. The least significant @var{len} bits are returned,
  4224. and the first list element is the most significant of those bits. If
  4225. @var{len} is not given, the default is @code{(integer-length @var{n})}
  4226. (@pxref{Bitwise Operations}).
  4227. @example
  4228. (integer->list 6) @result{} (#t #t #f)
  4229. (integer->list 1 4) @result{} (#f #f #f #t)
  4230. @end example
  4231. @end defun
  4232. @defun list->integer lst
  4233. @defunx booleans->integer bool@dots{}
  4234. Return an integer formed bitwise from the given @var{lst} list of
  4235. booleans, or for @code{booleans->integer} from the @var{bool}
  4236. arguments.
  4237. Each boolean is @code{#t} for a 1 and @code{#f} for a 0. The first
  4238. element becomes the most significant bit in the return.
  4239. @example
  4240. (list->integer '(#t #f #t #f)) @result{} 10
  4241. @end example
  4242. @end defun
  4243. @node SRFI-61
  4244. @subsection SRFI-61 - A more general @code{cond} clause
  4245. This SRFI extends RnRS @code{cond} to support test expressions that
  4246. return multiple values, as well as arbitrary definitions of test
  4247. success. SRFI 61 is implemented in the Guile core; there's no module
  4248. needed to get SRFI-61 itself. Extended @code{cond} is documented in
  4249. @ref{Conditionals,, Simple Conditional Evaluation}.
  4250. @node SRFI-62
  4251. @subsection SRFI-62 - S-expression comments.
  4252. @cindex SRFI-62
  4253. Starting from version 2.0, Guile's @code{read} supports SRFI-62/R7RS
  4254. S-expression comments by default.
  4255. @node SRFI-64
  4256. @subsection SRFI-64 - A Scheme API for test suites.
  4257. @cindex SRFI-64
  4258. See @uref{http://srfi.schemers.org/srfi-64/srfi-64.html, the
  4259. specification of SRFI-64}.
  4260. @node SRFI-67
  4261. @subsection SRFI-67 - Compare procedures
  4262. @cindex SRFI-67
  4263. See @uref{http://srfi.schemers.org/srfi-67/srfi-67.html, the
  4264. specification of SRFI-67}.
  4265. @node SRFI-69
  4266. @subsection SRFI-69 - Basic hash tables
  4267. @cindex SRFI-69
  4268. This is a portable wrapper around Guile's built-in hash table and weak
  4269. table support. @xref{Hash Tables}, for information on that built-in
  4270. support. Above that, this hash-table interface provides association
  4271. of equality and hash functions with tables at creation time, so
  4272. variants of each function are not required, as well as a procedure
  4273. that takes care of most uses for Guile hash table handles, which this
  4274. SRFI does not provide as such.
  4275. Access it with:
  4276. @lisp
  4277. (use-modules (srfi srfi-69))
  4278. @end lisp
  4279. @menu
  4280. * SRFI-69 Creating hash tables::
  4281. * SRFI-69 Accessing table items::
  4282. * SRFI-69 Table properties::
  4283. * SRFI-69 Hash table algorithms::
  4284. @end menu
  4285. @node SRFI-69 Creating hash tables
  4286. @subsubsection Creating hash tables
  4287. @deffn {Scheme Procedure} make-hash-table [equal-proc hash-proc #:weak weakness start-size]
  4288. Create and answer a new hash table with @var{equal-proc} as the
  4289. equality function and @var{hash-proc} as the hashing function.
  4290. By default, @var{equal-proc} is @code{equal?}. It can be any
  4291. two-argument procedure, and should answer whether two keys are the
  4292. same for this table's purposes.
  4293. My default @var{hash-proc} assumes that @code{equal-proc} is no
  4294. coarser than @code{equal?} unless it is literally @code{string-ci=?}.
  4295. If provided, @var{hash-proc} should be a two-argument procedure that
  4296. takes a key and the current table size, and answers a reasonably good
  4297. hash integer between 0 (inclusive) and the size (exclusive).
  4298. @var{weakness} should be @code{#f} or a symbol indicating how ``weak''
  4299. the hash table is:
  4300. @table @code
  4301. @item #f
  4302. An ordinary non-weak hash table. This is the default.
  4303. @item key
  4304. When the key has no more non-weak references at GC, remove that entry.
  4305. @item value
  4306. When the value has no more non-weak references at GC, remove that
  4307. entry.
  4308. @item key-or-value
  4309. When either has no more non-weak references at GC, remove the
  4310. association.
  4311. @end table
  4312. As a legacy of the time when Guile couldn't grow hash tables,
  4313. @var{start-size} is an optional integer argument that specifies the
  4314. approximate starting size for the hash table, which will be rounded to
  4315. an algorithmically-sounder number.
  4316. @end deffn
  4317. By @dfn{coarser} than @code{equal?}, we mean that for all @var{x} and
  4318. @var{y} values where @code{(@var{equal-proc} @var{x} @var{y})},
  4319. @code{(equal? @var{x} @var{y})} as well. If that does not hold for
  4320. your @var{equal-proc}, you must provide a @var{hash-proc}.
  4321. In the case of weak tables, remember that @dfn{references} above
  4322. always refers to @code{eq?}-wise references. Just because you have a
  4323. reference to some string @code{"foo"} doesn't mean that an association
  4324. with key @code{"foo"} in a weak-key table @emph{won't} be collected;
  4325. it only counts as a reference if the two @code{"foo"}s are @code{eq?},
  4326. regardless of @var{equal-proc}. As such, it is usually only sensible
  4327. to use @code{eq?} and @code{hashq} as the equivalence and hash
  4328. functions for a weak table. @xref{Weak References}, for more
  4329. information on Guile's built-in weak table support.
  4330. @deffn {Scheme Procedure} alist->hash-table alist [equal-proc hash-proc #:weak weakness start-size]
  4331. As with @code{make-hash-table}, but initialize it with the
  4332. associations in @var{alist}. Where keys are repeated in @var{alist},
  4333. the leftmost association takes precedence.
  4334. @end deffn
  4335. @node SRFI-69 Accessing table items
  4336. @subsubsection Accessing table items
  4337. @deffn {Scheme Procedure} hash-table-ref table key [default-thunk]
  4338. @deffnx {Scheme Procedure} hash-table-ref/default table key default
  4339. Answer the value associated with @var{key} in @var{table}. If
  4340. @var{key} is not present, answer the result of invoking the thunk
  4341. @var{default-thunk}, which signals an error instead by default.
  4342. @code{hash-table-ref/default} is a variant that requires a third
  4343. argument, @var{default}, and answers @var{default} itself instead of
  4344. invoking it.
  4345. @end deffn
  4346. @deffn {Scheme Procedure} hash-table-set! table key new-value
  4347. Set @var{key} to @var{new-value} in @var{table}.
  4348. @end deffn
  4349. @deffn {Scheme Procedure} hash-table-delete! table key
  4350. Remove the association of @var{key} in @var{table}, if present. If
  4351. absent, do nothing.
  4352. @end deffn
  4353. @deffn {Scheme Procedure} hash-table-exists? table key
  4354. Answer whether @var{key} has an association in @var{table}.
  4355. @end deffn
  4356. @deffn {Scheme Procedure} hash-table-update! table key modifier [default-thunk]
  4357. @deffnx {Scheme Procedure} hash-table-update!/default table key modifier default
  4358. Replace @var{key}'s associated value in @var{table} by invoking
  4359. @var{modifier} with one argument, the old value.
  4360. If @var{key} is not present, and @var{default-thunk} is provided,
  4361. invoke it with no arguments to get the ``old value'' to be passed to
  4362. @var{modifier} as above. If @var{default-thunk} is not provided in
  4363. such a case, signal an error.
  4364. @code{hash-table-update!/default} is a variant that requires the
  4365. fourth argument, which is used directly as the ``old value'' rather
  4366. than as a thunk to be invoked to retrieve the ``old value''.
  4367. @end deffn
  4368. @node SRFI-69 Table properties
  4369. @subsubsection Table properties
  4370. @deffn {Scheme Procedure} hash-table-size table
  4371. Answer the number of associations in @var{table}. This is guaranteed
  4372. to run in constant time for non-weak tables.
  4373. @end deffn
  4374. @deffn {Scheme Procedure} hash-table-keys table
  4375. Answer an unordered list of the keys in @var{table}.
  4376. @end deffn
  4377. @deffn {Scheme Procedure} hash-table-values table
  4378. Answer an unordered list of the values in @var{table}.
  4379. @end deffn
  4380. @deffn {Scheme Procedure} hash-table-walk table proc
  4381. Invoke @var{proc} once for each association in @var{table}, passing
  4382. the key and value as arguments.
  4383. @end deffn
  4384. @deffn {Scheme Procedure} hash-table-fold table proc init
  4385. Invoke @code{(@var{proc} @var{key} @var{value} @var{previous})} for
  4386. each @var{key} and @var{value} in @var{table}, where @var{previous} is
  4387. the result of the previous invocation, using @var{init} as the first
  4388. @var{previous} value. Answer the final @var{proc} result.
  4389. @end deffn
  4390. @deffn {Scheme Procedure} hash-table->alist table
  4391. Answer an alist where each association in @var{table} is an
  4392. association in the result.
  4393. @end deffn
  4394. @node SRFI-69 Hash table algorithms
  4395. @subsubsection Hash table algorithms
  4396. Each hash table carries an @dfn{equivalence function} and a @dfn{hash
  4397. function}, used to implement key lookups. Beginning users should
  4398. follow the rules for consistency of the default @var{hash-proc}
  4399. specified above. Advanced users can use these to implement their own
  4400. equivalence and hash functions for specialized lookup semantics.
  4401. @deffn {Scheme Procedure} hash-table-equivalence-function hash-table
  4402. @deffnx {Scheme Procedure} hash-table-hash-function hash-table
  4403. Answer the equivalence and hash function of @var{hash-table}, respectively.
  4404. @end deffn
  4405. @deffn {Scheme Procedure} hash obj [size]
  4406. @deffnx {Scheme Procedure} string-hash obj [size]
  4407. @deffnx {Scheme Procedure} string-ci-hash obj [size]
  4408. @deffnx {Scheme Procedure} hash-by-identity obj [size]
  4409. Answer a hash value appropriate for equality predicate @code{equal?},
  4410. @code{string=?}, @code{string-ci=?}, and @code{eq?}, respectively.
  4411. @end deffn
  4412. @code{hash} is a backwards-compatible replacement for Guile's built-in
  4413. @code{hash}.
  4414. @node SRFI-87
  4415. @subsection SRFI-87 => in case clauses
  4416. @cindex SRFI-87
  4417. Starting from version 2.0.6, Guile's core @code{case} syntax supports
  4418. @code{=>} in clauses, as specified by SRFI-87/R7RS.
  4419. @xref{Conditionals}.
  4420. @node SRFI-88
  4421. @subsection SRFI-88 Keyword Objects
  4422. @cindex SRFI-88
  4423. @cindex keyword objects
  4424. @uref{http://srfi.schemers.org/srfi-88/srfi-88.html, SRFI-88} provides
  4425. @dfn{keyword objects}, which are equivalent to Guile's keywords
  4426. (@pxref{Keywords}). SRFI-88 keywords can be entered using the
  4427. @dfn{postfix keyword syntax}, which consists of an identifier followed
  4428. by @code{:} (@pxref{Scheme Read, @code{postfix} keyword syntax}).
  4429. SRFI-88 can be made available with:
  4430. @example
  4431. (use-modules (srfi srfi-88))
  4432. @end example
  4433. Doing so installs the right reader option for keyword syntax, using
  4434. @code{(read-set! keywords 'postfix)}. It also provides the procedures
  4435. described below.
  4436. @deffn {Scheme Procedure} keyword? obj
  4437. Return @code{#t} if @var{obj} is a keyword. This is the same procedure
  4438. as the same-named built-in procedure (@pxref{Keyword Procedures,
  4439. @code{keyword?}}).
  4440. @example
  4441. (keyword? foo:) @result{} #t
  4442. (keyword? 'foo:) @result{} #t
  4443. (keyword? "foo") @result{} #f
  4444. @end example
  4445. @end deffn
  4446. @deffn {Scheme Procedure} keyword->string kw
  4447. Return the name of @var{kw} as a string, i.e., without the trailing
  4448. colon. The returned string may not be modified, e.g., with
  4449. @code{string-set!}.
  4450. @example
  4451. (keyword->string foo:) @result{} "foo"
  4452. @end example
  4453. @end deffn
  4454. @deffn {Scheme Procedure} string->keyword str
  4455. Return the keyword object whose name is @var{str}.
  4456. @example
  4457. (keyword->string (string->keyword "a b c")) @result{} "a b c"
  4458. @end example
  4459. @end deffn
  4460. @node SRFI-98
  4461. @subsection SRFI-98 Accessing environment variables.
  4462. @cindex SRFI-98
  4463. @cindex environment variables
  4464. This is a portable wrapper around Guile's built-in support for
  4465. interacting with the current environment, @xref{Runtime Environment}.
  4466. @deffn {Scheme Procedure} get-environment-variable name
  4467. Returns a string containing the value of the environment variable
  4468. given by the string @code{name}, or @code{#f} if the named
  4469. environment variable is not found. This is equivalent to
  4470. @code{(getenv name)}.
  4471. @end deffn
  4472. @deffn {Scheme Procedure} get-environment-variables
  4473. Returns the names and values of all the environment variables as an
  4474. association list in which both the keys and the values are strings.
  4475. @end deffn
  4476. @node SRFI-105
  4477. @subsection SRFI-105 Curly-infix expressions.
  4478. @cindex SRFI-105
  4479. @cindex curly-infix
  4480. @cindex curly-infix-and-bracket-lists
  4481. Guile's built-in reader includes support for SRFI-105 curly-infix
  4482. expressions. See @uref{http://srfi.schemers.org/srfi-105/srfi-105.html,
  4483. the specification of SRFI-105}. Some examples:
  4484. @example
  4485. @{n <= 5@} @result{} (<= n 5)
  4486. @{a + b + c@} @result{} (+ a b c)
  4487. @{a * @{b + c@}@} @result{} (* a (+ b c))
  4488. @{(- a) / b@} @result{} (/ (- a) b)
  4489. @{-(a) / b@} @result{} (/ (- a) b) as well
  4490. @{(f a b) + (g h)@} @result{} (+ (f a b) (g h))
  4491. @{f(a b) + g(h)@} @result{} (+ (f a b) (g h)) as well
  4492. @{f[a b] + g(h)@} @result{} (+ ($bracket-apply$ f a b) (g h))
  4493. '@{a + f(b) + x@} @result{} '(+ a (f b) x)
  4494. @{length(x) >= 6@} @result{} (>= (length x) 6)
  4495. @{n-1 + n-2@} @result{} (+ n-1 n-2)
  4496. @{n * factorial@{n - 1@}@} @result{} (* n (factorial (- n 1)))
  4497. @{@{a > 0@} and @{b >= 1@}@} @result{} (and (> a 0) (>= b 1))
  4498. @{f@{n - 1@}(x)@} @result{} ((f (- n 1)) x)
  4499. @{a . z@} @result{} ($nfx$ a . z)
  4500. @{a + b - c@} @result{} ($nfx$ a + b - c)
  4501. @end example
  4502. To enable curly-infix expressions within a file, place the reader
  4503. directive @code{#!curly-infix} before the first use of curly-infix
  4504. notation. To globally enable curly-infix expressions in Guile's reader,
  4505. set the @code{curly-infix} read option.
  4506. Guile also implements the following non-standard extension to SRFI-105:
  4507. if @code{curly-infix} is enabled and there is no other meaning assigned
  4508. to square brackets (i.e. the @code{square-brackets} read option is
  4509. turned off), then lists within square brackets are read as normal lists
  4510. but with the special symbol @code{$bracket-list$} added to the front.
  4511. To enable this combination of read options within a file, use the reader
  4512. directive @code{#!curly-infix-and-bracket-lists}. For example:
  4513. @example
  4514. [a b] @result{} ($bracket-list$ a b)
  4515. [a . b] @result{} ($bracket-list$ a . b)
  4516. @end example
  4517. For more information on reader options, @xref{Scheme Read}.
  4518. @node SRFI-111
  4519. @subsection SRFI-111 Boxes.
  4520. @cindex SRFI-111
  4521. @uref{http://srfi.schemers.org/srfi-111/srfi-111.html, SRFI-111}
  4522. provides boxes: objects with a single mutable cell.
  4523. @deffn {Scheme Procedure} box value
  4524. Return a newly allocated box whose contents is initialized to
  4525. @var{value}.
  4526. @end deffn
  4527. @deffn {Scheme Procedure} box? obj
  4528. Return true if @var{obj} is a box, otherwise return false.
  4529. @end deffn
  4530. @deffn {Scheme Procedure} unbox box
  4531. Return the current contents of @var{box}.
  4532. @end deffn
  4533. @deffn {Scheme Procedure} set-box! box value
  4534. Set the contents of @var{box} to @var{value}.
  4535. @end deffn
  4536. @c srfi-modules.texi ends here
  4537. @c Local Variables:
  4538. @c TeX-master: "guile.texi"
  4539. @c End: