srfi-modules.texi 162 KB

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