srfi-modules.texi 202 KB

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