srfi-modules.texi 219 KB

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