srfi-modules.texi 223 KB

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