srfi-modules.texi 203 KB

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