srfi-modules.texi 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275
  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
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @page
  7. @node SRFI Support
  8. @section SRFI Support Modules
  9. @cindex SRFI
  10. SRFI is an acronym for Scheme Request For Implementation. The SRFI
  11. documents define a lot of syntactic and procedure extensions to standard
  12. Scheme as defined in R5RS.
  13. Guile has support for a number of SRFIs. This chapter gives an overview
  14. over the available SRFIs and some usage hints. For complete
  15. documentation, design rationales and further examples, we advise you to
  16. get the relevant SRFI documents from the SRFI home page
  17. @url{http://srfi.schemers.org}.
  18. @menu
  19. * About SRFI Usage:: What to know about Guile's SRFI support.
  20. * SRFI-0:: cond-expand
  21. * SRFI-1:: List library.
  22. * SRFI-2:: and-let*.
  23. * SRFI-4:: Homogeneous numeric vector datatypes.
  24. * SRFI-6:: Basic String Ports.
  25. * SRFI-8:: receive.
  26. * SRFI-9:: define-record-type.
  27. * SRFI-10:: Hash-Comma Reader Extension.
  28. * SRFI-11:: let-values and let*-values.
  29. * SRFI-13:: String library.
  30. * SRFI-14:: Character-set library.
  31. * SRFI-16:: case-lambda
  32. * SRFI-17:: Generalized set!
  33. * SRFI-19:: Time/Date library.
  34. * SRFI-26:: Specializing parameters
  35. * SRFI-31:: A special form `rec' for recursive evaluation
  36. * SRFI-34:: Exception handling.
  37. * SRFI-35:: Conditions.
  38. * SRFI-37:: args-fold program argument processor
  39. * SRFI-39:: Parameter objects
  40. * SRFI-55:: Requiring Features.
  41. * SRFI-60:: Integers as bits.
  42. * SRFI-61:: A more general `cond' clause
  43. * SRFI-69:: Basic hash tables.
  44. * SRFI-88:: Keyword objects.
  45. @end menu
  46. @node About SRFI Usage
  47. @subsection About SRFI Usage
  48. @c FIXME::martin: Review me!
  49. SRFI support in Guile is currently implemented partly in the core
  50. library, and partly as add-on modules. That means that some SRFIs are
  51. automatically available when the interpreter is started, whereas the
  52. other SRFIs require you to use the appropriate support module
  53. explicitly.
  54. There are several reasons for this inconsistency. First, the feature
  55. checking syntactic form @code{cond-expand} (@pxref{SRFI-0}) must be
  56. available immediately, because it must be there when the user wants to
  57. check for the Scheme implementation, that is, before she can know that
  58. it is safe to use @code{use-modules} to load SRFI support modules. The
  59. second reason is that some features defined in SRFIs had been
  60. implemented in Guile before the developers started to add SRFI
  61. implementations as modules (for example SRFI-6 (@pxref{SRFI-6})). In
  62. the future, it is possible that SRFIs in the core library might be
  63. factored out into separate modules, requiring explicit module loading
  64. when they are needed. So you should be prepared to have to use
  65. @code{use-modules} someday in the future to access SRFI-6 bindings. If
  66. you want, you can do that already. We have included the module
  67. @code{(srfi srfi-6)} in the distribution, which currently does nothing,
  68. but ensures that you can write future-safe code.
  69. Generally, support for a specific SRFI is made available by using
  70. modules named @code{(srfi srfi-@var{number})}, where @var{number} is the
  71. number of the SRFI needed. Another possibility is to use the command
  72. line option @code{--use-srfi}, which will load the necessary modules
  73. automatically (@pxref{Invoking Guile}).
  74. @node SRFI-0
  75. @subsection SRFI-0 - cond-expand
  76. @cindex SRFI-0
  77. This SRFI lets a portable Scheme program test for the presence of
  78. certain features, and adapt itself by using different blocks of code,
  79. or fail if the necessary features are not available. There's no
  80. module to load, this is in the Guile core.
  81. A program designed only for Guile will generally not need this
  82. mechanism, such a program can of course directly use the various
  83. documented parts of Guile.
  84. @deffn syntax cond-expand (feature body@dots{}) @dots{}
  85. Expand to the @var{body} of the first clause whose @var{feature}
  86. specification is satisfied. It is an error if no @var{feature} is
  87. satisfied.
  88. Features are symbols such as @code{srfi-1}, and a feature
  89. specification can use @code{and}, @code{or} and @code{not} forms to
  90. test combinations. The last clause can be an @code{else}, to be used
  91. if no other passes.
  92. For example, define a private version of @code{alist-cons} if SRFI-1
  93. is not available.
  94. @example
  95. (cond-expand (srfi-1
  96. )
  97. (else
  98. (define (alist-cons key val alist)
  99. (cons (cons key val) alist))))
  100. @end example
  101. Or demand a certain set of SRFIs (list operations, string ports,
  102. @code{receive} and string operations), failing if they're not
  103. available.
  104. @example
  105. (cond-expand ((and srfi-1 srfi-6 srfi-8 srfi-13)
  106. ))
  107. @end example
  108. @end deffn
  109. @noindent
  110. The Guile core has the following features,
  111. @example
  112. guile
  113. r5rs
  114. srfi-0
  115. srfi-4
  116. srfi-6
  117. srfi-13
  118. srfi-14
  119. @end example
  120. Other SRFI feature symbols are defined once their code has been loaded
  121. with @code{use-modules}, since only then are their bindings available.
  122. The @samp{--use-srfi} command line option (@pxref{Invoking Guile}) is
  123. a good way to load SRFIs to satisfy @code{cond-expand} when running a
  124. portable program.
  125. Testing the @code{guile} feature allows a program to adapt itself to
  126. the Guile module system, but still run on other Scheme systems. For
  127. example the following demands SRFI-8 (@code{receive}), but also knows
  128. how to load it with the Guile mechanism.
  129. @example
  130. (cond-expand (srfi-8
  131. )
  132. (guile
  133. (use-modules (srfi srfi-8))))
  134. @end example
  135. It should be noted that @code{cond-expand} is separate from the
  136. @code{*features*} mechanism (@pxref{Feature Tracking}), feature
  137. symbols in one are unrelated to those in the other.
  138. @node SRFI-1
  139. @subsection SRFI-1 - List library
  140. @cindex SRFI-1
  141. @cindex list
  142. @c FIXME::martin: Review me!
  143. The list library defined in SRFI-1 contains a lot of useful list
  144. processing procedures for construction, examining, destructuring and
  145. manipulating lists and pairs.
  146. Since SRFI-1 also defines some procedures which are already contained
  147. in R5RS and thus are supported by the Guile core library, some list
  148. and pair procedures which appear in the SRFI-1 document may not appear
  149. in this section. So when looking for a particular list/pair
  150. processing procedure, you should also have a look at the sections
  151. @ref{Lists} and @ref{Pairs}.
  152. @menu
  153. * SRFI-1 Constructors:: Constructing new lists.
  154. * SRFI-1 Predicates:: Testing list for specific properties.
  155. * SRFI-1 Selectors:: Selecting elements from lists.
  156. * SRFI-1 Length Append etc:: Length calculation and list appending.
  157. * SRFI-1 Fold and Map:: Higher-order list processing.
  158. * SRFI-1 Filtering and Partitioning:: Filter lists based on predicates.
  159. * SRFI-1 Searching:: Search for elements.
  160. * SRFI-1 Deleting:: Delete elements from lists.
  161. * SRFI-1 Association Lists:: Handle association lists.
  162. * SRFI-1 Set Operations:: Use lists for representing sets.
  163. @end menu
  164. @node SRFI-1 Constructors
  165. @subsubsection Constructors
  166. @cindex list constructor
  167. @c FIXME::martin: Review me!
  168. New lists can be constructed by calling one of the following
  169. procedures.
  170. @deffn {Scheme Procedure} xcons d a
  171. Like @code{cons}, but with interchanged arguments. Useful mostly when
  172. passed to higher-order procedures.
  173. @end deffn
  174. @deffn {Scheme Procedure} list-tabulate n init-proc
  175. Return an @var{n}-element list, where each list element is produced by
  176. applying the procedure @var{init-proc} to the corresponding list
  177. index. The order in which @var{init-proc} is applied to the indices
  178. is not specified.
  179. @end deffn
  180. @deffn {Scheme Procedure} list-copy lst
  181. Return a new list containing the elements of the list @var{lst}.
  182. This function differs from the core @code{list-copy} (@pxref{List
  183. Constructors}) in accepting improper lists too. And if @var{lst} is
  184. not a pair at all then it's treated as the final tail of an improper
  185. list and simply returned.
  186. @end deffn
  187. @deffn {Scheme Procedure} circular-list elt1 elt2 @dots{}
  188. Return a circular list containing the given arguments @var{elt1}
  189. @var{elt2} @dots{}.
  190. @end deffn
  191. @deffn {Scheme Procedure} iota count [start step]
  192. Return a list containing @var{count} numbers, starting from
  193. @var{start} and adding @var{step} each time. The default @var{start}
  194. is 0, the default @var{step} is 1. For example,
  195. @example
  196. (iota 6) @result{} (0 1 2 3 4 5)
  197. (iota 4 2.5 -2) @result{} (2.5 0.5 -1.5 -3.5)
  198. @end example
  199. This function takes its name from the corresponding primitive in the
  200. APL language.
  201. @end deffn
  202. @node SRFI-1 Predicates
  203. @subsubsection Predicates
  204. @cindex list predicate
  205. @c FIXME::martin: Review me!
  206. The procedures in this section test specific properties of lists.
  207. @deffn {Scheme Procedure} proper-list? obj
  208. Return @code{#t} if @var{obj} is a proper list, or @code{#f}
  209. otherwise. This is the same as the core @code{list?} (@pxref{List
  210. Predicates}).
  211. A proper list is a list which ends with the empty list @code{()} in
  212. the usual way. The empty list @code{()} itself is a proper list too.
  213. @example
  214. (proper-list? '(1 2 3)) @result{} #t
  215. (proper-list? '()) @result{} #t
  216. @end example
  217. @end deffn
  218. @deffn {Scheme Procedure} circular-list? obj
  219. Return @code{#t} if @var{obj} is a circular list, or @code{#f}
  220. otherwise.
  221. A circular list is a list where at some point the @code{cdr} refers
  222. back to a previous pair in the list (either the start or some later
  223. point), so that following the @code{cdr}s takes you around in a
  224. circle, with no end.
  225. @example
  226. (define x (list 1 2 3 4))
  227. (set-cdr! (last-pair x) (cddr x))
  228. x @result{} (1 2 3 4 3 4 3 4 ...)
  229. (circular-list? x) @result{} #t
  230. @end example
  231. @end deffn
  232. @deffn {Scheme Procedure} dotted-list? obj
  233. Return @code{#t} if @var{obj} is a dotted list, or @code{#f}
  234. otherwise.
  235. A dotted list is a list where the @code{cdr} of the last pair is not
  236. the empty list @code{()}. Any non-pair @var{obj} is also considered a
  237. dotted list, with length zero.
  238. @example
  239. (dotted-list? '(1 2 . 3)) @result{} #t
  240. (dotted-list? 99) @result{} #t
  241. @end example
  242. @end deffn
  243. It will be noted that any Scheme object passes exactly one of the
  244. above three tests @code{proper-list?}, @code{circular-list?} and
  245. @code{dotted-list?}. Non-lists are @code{dotted-list?}, finite lists
  246. are either @code{proper-list?} or @code{dotted-list?}, and infinite
  247. lists are @code{circular-list?}.
  248. @sp 1
  249. @deffn {Scheme Procedure} null-list? lst
  250. Return @code{#t} if @var{lst} is the empty list @code{()}, @code{#f}
  251. otherwise. If something else than a proper or circular list is passed
  252. as @var{lst}, an error is signalled. This procedure is recommended
  253. for checking for the end of a list in contexts where dotted lists are
  254. not allowed.
  255. @end deffn
  256. @deffn {Scheme Procedure} not-pair? obj
  257. Return @code{#t} is @var{obj} is not a pair, @code{#f} otherwise.
  258. This is shorthand notation @code{(not (pair? @var{obj}))} and is
  259. supposed to be used for end-of-list checking in contexts where dotted
  260. lists are allowed.
  261. @end deffn
  262. @deffn {Scheme Procedure} list= elt= list1 @dots{}
  263. Return @code{#t} if all argument lists are equal, @code{#f} otherwise.
  264. List equality is determined by testing whether all lists have the same
  265. length and the corresponding elements are equal in the sense of the
  266. equality predicate @var{elt=}. If no or only one list is given,
  267. @code{#t} is returned.
  268. @end deffn
  269. @node SRFI-1 Selectors
  270. @subsubsection Selectors
  271. @cindex list selector
  272. @c FIXME::martin: Review me!
  273. @deffn {Scheme Procedure} first pair
  274. @deffnx {Scheme Procedure} second pair
  275. @deffnx {Scheme Procedure} third pair
  276. @deffnx {Scheme Procedure} fourth pair
  277. @deffnx {Scheme Procedure} fifth pair
  278. @deffnx {Scheme Procedure} sixth pair
  279. @deffnx {Scheme Procedure} seventh pair
  280. @deffnx {Scheme Procedure} eighth pair
  281. @deffnx {Scheme Procedure} ninth pair
  282. @deffnx {Scheme Procedure} tenth pair
  283. These are synonyms for @code{car}, @code{cadr}, @code{caddr}, @dots{}.
  284. @end deffn
  285. @deffn {Scheme Procedure} car+cdr pair
  286. Return two values, the @sc{car} and the @sc{cdr} of @var{pair}.
  287. @end deffn
  288. @deffn {Scheme Procedure} take lst i
  289. @deffnx {Scheme Procedure} take! lst i
  290. Return a list containing the first @var{i} elements of @var{lst}.
  291. @code{take!} may modify the structure of the argument list @var{lst}
  292. in order to produce the result.
  293. @end deffn
  294. @deffn {Scheme Procedure} drop lst i
  295. Return a list containing all but the first @var{i} elements of
  296. @var{lst}.
  297. @end deffn
  298. @deffn {Scheme Procedure} take-right lst i
  299. Return the a list containing the @var{i} last elements of @var{lst}.
  300. The return shares a common tail with @var{lst}.
  301. @end deffn
  302. @deffn {Scheme Procedure} drop-right lst i
  303. @deffnx {Scheme Procedure} drop-right! lst i
  304. Return the a list containing all but the @var{i} last elements of
  305. @var{lst}.
  306. @code{drop-right} always returns a new list, even when @var{i} is
  307. zero. @code{drop-right!} may modify the structure of the argument
  308. list @var{lst} in order to produce the result.
  309. @end deffn
  310. @deffn {Scheme Procedure} split-at lst i
  311. @deffnx {Scheme Procedure} split-at! lst i
  312. Return two values, a list containing the first @var{i} elements of the
  313. list @var{lst} and a list containing the remaining elements.
  314. @code{split-at!} may modify the structure of the argument list
  315. @var{lst} in order to produce the result.
  316. @end deffn
  317. @deffn {Scheme Procedure} last lst
  318. Return the last element of the non-empty, finite list @var{lst}.
  319. @end deffn
  320. @node SRFI-1 Length Append etc
  321. @subsubsection Length, Append, Concatenate, etc.
  322. @c FIXME::martin: Review me!
  323. @deffn {Scheme Procedure} length+ lst
  324. Return the length of the argument list @var{lst}. When @var{lst} is a
  325. circular list, @code{#f} is returned.
  326. @end deffn
  327. @deffn {Scheme Procedure} concatenate list-of-lists
  328. @deffnx {Scheme Procedure} concatenate! list-of-lists
  329. Construct a list by appending all lists in @var{list-of-lists}.
  330. @code{concatenate!} may modify the structure of the given lists in
  331. order to produce the result.
  332. @code{concatenate} is the same as @code{(apply append
  333. @var{list-of-lists})}. It exists because some Scheme implementations
  334. have a limit on the number of arguments a function takes, which the
  335. @code{apply} might exceed. In Guile there is no such limit.
  336. @end deffn
  337. @deffn {Scheme Procedure} append-reverse rev-head tail
  338. @deffnx {Scheme Procedure} append-reverse! rev-head tail
  339. Reverse @var{rev-head}, append @var{tail} to it, and return the
  340. result. This is equivalent to @code{(append (reverse @var{rev-head})
  341. @var{tail})}, but its implementation is more efficient.
  342. @example
  343. (append-reverse '(1 2 3) '(4 5 6)) @result{} (3 2 1 4 5 6)
  344. @end example
  345. @code{append-reverse!} may modify @var{rev-head} in order to produce
  346. the result.
  347. @end deffn
  348. @deffn {Scheme Procedure} zip lst1 lst2 @dots{}
  349. Return a list as long as the shortest of the argument lists, where
  350. each element is a list. The first list contains the first elements of
  351. the argument lists, the second list contains the second elements, and
  352. so on.
  353. @end deffn
  354. @deffn {Scheme Procedure} unzip1 lst
  355. @deffnx {Scheme Procedure} unzip2 lst
  356. @deffnx {Scheme Procedure} unzip3 lst
  357. @deffnx {Scheme Procedure} unzip4 lst
  358. @deffnx {Scheme Procedure} unzip5 lst
  359. @code{unzip1} takes a list of lists, and returns a list containing the
  360. first elements of each list, @code{unzip2} returns two lists, the
  361. first containing the first elements of each lists and the second
  362. containing the second elements of each lists, and so on.
  363. @end deffn
  364. @deffn {Scheme Procedure} count pred lst1 @dots{} lstN
  365. Return a count of the number of times @var{pred} returns true when
  366. called on elements from the given lists.
  367. @var{pred} is called with @var{N} parameters @code{(@var{pred}
  368. @var{elem1} @dots{} @var{elemN})}, each element being from the
  369. corresponding @var{lst1} @dots{} @var{lstN}. The first call is with
  370. the first element of each list, the second with the second element
  371. from each, and so on.
  372. Counting stops when the end of the shortest list is reached. At least
  373. one list must be non-circular.
  374. @end deffn
  375. @node SRFI-1 Fold and Map
  376. @subsubsection Fold, Unfold & Map
  377. @cindex list fold
  378. @cindex list map
  379. @c FIXME::martin: Review me!
  380. @deffn {Scheme Procedure} fold proc init lst1 @dots{} lstN
  381. @deffnx {Scheme Procedure} fold-right proc init lst1 @dots{} lstN
  382. Apply @var{proc} to the elements of @var{lst1} @dots{} @var{lstN} to
  383. build a result, and return that result.
  384. Each @var{proc} call is @code{(@var{proc} @var{elem1} @dots{}
  385. @var{elemN} @var{previous})}, where @var{elem1} is from @var{lst1},
  386. through @var{elemN} from @var{lstN}. @var{previous} is the return
  387. from the previous call to @var{proc}, or the given @var{init} for the
  388. first call. If any list is empty, just @var{init} is returned.
  389. @code{fold} works through the list elements from first to last. The
  390. following shows a list reversal and the calls it makes,
  391. @example
  392. (fold cons '() '(1 2 3))
  393. (cons 1 '())
  394. (cons 2 '(1))
  395. (cons 3 '(2 1)
  396. @result{} (3 2 1)
  397. @end example
  398. @code{fold-right} works through the list elements from last to first,
  399. ie.@: from the right. So for example the following finds the longest
  400. string, and the last among equal longest,
  401. @example
  402. (fold-right (lambda (str prev)
  403. (if (> (string-length str) (string-length prev))
  404. str
  405. prev))
  406. ""
  407. '("x" "abc" "xyz" "jk"))
  408. @result{} "xyz"
  409. @end example
  410. If @var{lst1} through @var{lstN} have different lengths, @code{fold}
  411. stops when the end of the shortest is reached; @code{fold-right}
  412. commences at the last element of the shortest. Ie.@: elements past
  413. the length of the shortest are ignored in the other @var{lst}s. At
  414. least one @var{lst} must be non-circular.
  415. @code{fold} should be preferred over @code{fold-right} if the order of
  416. processing doesn't matter, or can be arranged either way, since
  417. @code{fold} is a little more efficient.
  418. The way @code{fold} builds a result from iterating is quite general,
  419. it can do more than other iterations like say @code{map} or
  420. @code{filter}. The following for example removes adjacent duplicate
  421. elements from a list,
  422. @example
  423. (define (delete-adjacent-duplicates lst)
  424. (fold-right (lambda (elem ret)
  425. (if (equal? elem (first ret))
  426. ret
  427. (cons elem ret)))
  428. (list (last lst))
  429. lst))
  430. (delete-adjacent-duplicates '(1 2 3 3 4 4 4 5))
  431. @result{} (1 2 3 4 5)
  432. @end example
  433. Clearly the same sort of thing can be done with a @code{for-each} and
  434. a variable in which to build the result, but a self-contained
  435. @var{proc} can be re-used in multiple contexts, where a
  436. @code{for-each} would have to be written out each time.
  437. @end deffn
  438. @deffn {Scheme Procedure} pair-fold proc init lst1 @dots{} lstN
  439. @deffnx {Scheme Procedure} pair-fold-right proc init lst1 @dots{} lstN
  440. The same as @code{fold} and @code{fold-right}, but apply @var{proc} to
  441. the pairs of the lists instead of the list elements.
  442. @end deffn
  443. @deffn {Scheme Procedure} reduce proc default lst
  444. @deffnx {Scheme Procedure} reduce-right proc default lst
  445. @code{reduce} is a variant of @code{fold}, where the first call to
  446. @var{proc} is on two elements from @var{lst}, rather than one element
  447. and a given initial value.
  448. If @var{lst} is empty, @code{reduce} returns @var{default} (this is
  449. the only use for @var{default}). If @var{lst} has just one element
  450. then that's the return value. Otherwise @var{proc} is called on the
  451. elements of @var{lst}.
  452. Each @var{proc} call is @code{(@var{proc} @var{elem} @var{previous})},
  453. where @var{elem} is from @var{lst} (the second and subsequent elements
  454. of @var{lst}), and @var{previous} is the return from the previous call
  455. to @var{proc}. The first element of @var{lst} is the @var{previous}
  456. for the first call to @var{proc}.
  457. For example, the following adds a list of numbers, the calls made to
  458. @code{+} are shown. (Of course @code{+} accepts multiple arguments
  459. and can add a list directly, with @code{apply}.)
  460. @example
  461. (reduce + 0 '(5 6 7)) @result{} 18
  462. (+ 6 5) @result{} 11
  463. (+ 7 11) @result{} 18
  464. @end example
  465. @code{reduce} can be used instead of @code{fold} where the @var{init}
  466. value is an ``identity'', meaning a value which under @var{proc}
  467. doesn't change the result, in this case 0 is an identity since
  468. @code{(+ 5 0)} is just 5. @code{reduce} avoids that unnecessary call.
  469. @code{reduce-right} is a similar variation on @code{fold-right},
  470. working from the end (ie.@: the right) of @var{lst}. The last element
  471. of @var{lst} is the @var{previous} for the first call to @var{proc},
  472. and the @var{elem} values go from the second last.
  473. @code{reduce} should be preferred over @code{reduce-right} if the
  474. order of processing doesn't matter, or can be arranged either way,
  475. since @code{reduce} is a little more efficient.
  476. @end deffn
  477. @deffn {Scheme Procedure} unfold p f g seed [tail-gen]
  478. @code{unfold} is defined as follows:
  479. @lisp
  480. (unfold p f g seed) =
  481. (if (p seed) (tail-gen seed)
  482. (cons (f seed)
  483. (unfold p f g (g seed))))
  484. @end lisp
  485. @table @var
  486. @item p
  487. Determines when to stop unfolding.
  488. @item f
  489. Maps each seed value to the corresponding list element.
  490. @item g
  491. Maps each seed value to next seed valu.
  492. @item seed
  493. The state value for the unfold.
  494. @item tail-gen
  495. Creates the tail of the list; defaults to @code{(lambda (x) '())}.
  496. @end table
  497. @var{g} produces a series of seed values, which are mapped to list
  498. elements by @var{f}. These elements are put into a list in
  499. left-to-right order, and @var{p} tells when to stop unfolding.
  500. @end deffn
  501. @deffn {Scheme Procedure} unfold-right p f g seed [tail]
  502. Construct a list with the following loop.
  503. @lisp
  504. (let lp ((seed seed) (lis tail))
  505. (if (p seed) lis
  506. (lp (g seed)
  507. (cons (f seed) lis))))
  508. @end lisp
  509. @table @var
  510. @item p
  511. Determines when to stop unfolding.
  512. @item f
  513. Maps each seed value to the corresponding list element.
  514. @item g
  515. Maps each seed value to next seed valu.
  516. @item seed
  517. The state value for the unfold.
  518. @item tail-gen
  519. Creates the tail of the list; defaults to @code{(lambda (x) '())}.
  520. @end table
  521. @end deffn
  522. @deffn {Scheme Procedure} map f lst1 lst2 @dots{}
  523. Map the procedure over the list(s) @var{lst1}, @var{lst2}, @dots{} and
  524. return a list containing the results of the procedure applications.
  525. This procedure is extended with respect to R5RS, because the argument
  526. lists may have different lengths. The result list will have the same
  527. length as the shortest argument lists. The order in which @var{f}
  528. will be applied to the list element(s) is not specified.
  529. @end deffn
  530. @deffn {Scheme Procedure} for-each f lst1 lst2 @dots{}
  531. Apply the procedure @var{f} to each pair of corresponding elements of
  532. the list(s) @var{lst1}, @var{lst2}, @dots{}. The return value is not
  533. specified. This procedure is extended with respect to R5RS, because
  534. the argument lists may have different lengths. The shortest argument
  535. list determines the number of times @var{f} is called. @var{f} will
  536. be applied to the list elements in left-to-right order.
  537. @end deffn
  538. @deffn {Scheme Procedure} append-map f lst1 lst2 @dots{}
  539. @deffnx {Scheme Procedure} append-map! f lst1 lst2 @dots{}
  540. Equivalent to
  541. @lisp
  542. (apply append (map f clist1 clist2 ...))
  543. @end lisp
  544. and
  545. @lisp
  546. (apply append! (map f clist1 clist2 ...))
  547. @end lisp
  548. Map @var{f} over the elements of the lists, just as in the @code{map}
  549. function. However, the results of the applications are appended
  550. together to make the final result. @code{append-map} uses
  551. @code{append} to append the results together; @code{append-map!} uses
  552. @code{append!}.
  553. The dynamic order in which the various applications of @var{f} are
  554. made is not specified.
  555. @end deffn
  556. @deffn {Scheme Procedure} map! f lst1 lst2 @dots{}
  557. Linear-update variant of @code{map} -- @code{map!} is allowed, but not
  558. required, to alter the cons cells of @var{lst1} to construct the
  559. result list.
  560. The dynamic order in which the various applications of @var{f} are
  561. made is not specified. In the n-ary case, @var{lst2}, @var{lst3},
  562. @dots{} must have at least as many elements as @var{lst1}.
  563. @end deffn
  564. @deffn {Scheme Procedure} pair-for-each f lst1 lst2 @dots{}
  565. Like @code{for-each}, but applies the procedure @var{f} to the pairs
  566. from which the argument lists are constructed, instead of the list
  567. elements. The return value is not specified.
  568. @end deffn
  569. @deffn {Scheme Procedure} filter-map f lst1 lst2 @dots{}
  570. Like @code{map}, but only results from the applications of @var{f}
  571. which are true are saved in the result list.
  572. @end deffn
  573. @node SRFI-1 Filtering and Partitioning
  574. @subsubsection Filtering and Partitioning
  575. @cindex list filter
  576. @cindex list partition
  577. @c FIXME::martin: Review me!
  578. Filtering means to collect all elements from a list which satisfy a
  579. specific condition. Partitioning a list means to make two groups of
  580. list elements, one which contains the elements satisfying a condition,
  581. and the other for the elements which don't.
  582. The @code{filter} and @code{filter!} functions are implemented in the
  583. Guile core, @xref{List Modification}.
  584. @deffn {Scheme Procedure} partition pred lst
  585. @deffnx {Scheme Procedure} partition! pred lst
  586. Split @var{lst} into those elements which do and don't satisfy the
  587. predicate @var{pred}.
  588. The return is two values (@pxref{Multiple Values}), the first being a
  589. list of all elements from @var{lst} which satisfy @var{pred}, the
  590. second a list of those which do not.
  591. The elements in the result lists are in the same order as in @var{lst}
  592. but the order in which the calls @code{(@var{pred} elem)} are made on
  593. the list elements is unspecified.
  594. @code{partition} does not change @var{lst}, but one of the returned
  595. lists may share a tail with it. @code{partition!} may modify
  596. @var{lst} to construct its return.
  597. @end deffn
  598. @deffn {Scheme Procedure} remove pred lst
  599. @deffnx {Scheme Procedure} remove! pred lst
  600. Return a list containing all elements from @var{lst} which do not
  601. satisfy the predicate @var{pred}. The elements in the result list
  602. have the same order as in @var{lst}. The order in which @var{pred} is
  603. applied to the list elements is not specified.
  604. @code{remove!} is allowed, but not required to modify the structure of
  605. the input list.
  606. @end deffn
  607. @node SRFI-1 Searching
  608. @subsubsection Searching
  609. @cindex list search
  610. @c FIXME::martin: Review me!
  611. The procedures for searching elements in lists either accept a
  612. predicate or a comparison object for determining which elements are to
  613. be searched.
  614. @deffn {Scheme Procedure} find pred lst
  615. Return the first element of @var{lst} which satisfies the predicate
  616. @var{pred} and @code{#f} if no such element is found.
  617. @end deffn
  618. @deffn {Scheme Procedure} find-tail pred lst
  619. Return the first pair of @var{lst} whose @sc{car} satisfies the
  620. predicate @var{pred} and @code{#f} if no such element is found.
  621. @end deffn
  622. @deffn {Scheme Procedure} take-while pred lst
  623. @deffnx {Scheme Procedure} take-while! pred lst
  624. Return the longest initial prefix of @var{lst} whose elements all
  625. satisfy the predicate @var{pred}.
  626. @code{take-while!} is allowed, but not required to modify the input
  627. list while producing the result.
  628. @end deffn
  629. @deffn {Scheme Procedure} drop-while pred lst
  630. Drop the longest initial prefix of @var{lst} whose elements all
  631. satisfy the predicate @var{pred}.
  632. @end deffn
  633. @deffn {Scheme Procedure} span pred lst
  634. @deffnx {Scheme Procedure} span! pred lst
  635. @deffnx {Scheme Procedure} break pred lst
  636. @deffnx {Scheme Procedure} break! pred lst
  637. @code{span} splits the list @var{lst} into the longest initial prefix
  638. whose elements all satisfy the predicate @var{pred}, and the remaining
  639. tail. @code{break} inverts the sense of the predicate.
  640. @code{span!} and @code{break!} are allowed, but not required to modify
  641. the structure of the input list @var{lst} in order to produce the
  642. result.
  643. Note that the name @code{break} conflicts with the @code{break}
  644. binding established by @code{while} (@pxref{while do}). Applications
  645. wanting to use @code{break} from within a @code{while} loop will need
  646. to make a new define under a different name.
  647. @end deffn
  648. @deffn {Scheme Procedure} any pred lst1 lst2 @dots{} lstN
  649. Test whether any set of elements from @var{lst1} @dots{} lstN
  650. satisfies @var{pred}. If so the return value is the return from the
  651. successful @var{pred} call, or if not the return is @code{#f}.
  652. Each @var{pred} call is @code{(@var{pred} @var{elem1} @dots{}
  653. @var{elemN})} taking an element from each @var{lst}. The calls are
  654. made successively for the first, second, etc elements of the lists,
  655. stopping when @var{pred} returns non-@code{#f}, or when the end of the
  656. shortest list is reached.
  657. The @var{pred} call on the last set of elements (ie.@: when the end of
  658. the shortest list has been reached), if that point is reached, is a
  659. tail call.
  660. @end deffn
  661. @deffn {Scheme Procedure} every pred lst1 lst2 @dots{} lstN
  662. Test whether every set of elements from @var{lst1} @dots{} lstN
  663. satisfies @var{pred}. If so the return value is the return from the
  664. final @var{pred} call, or if not the return is @code{#f}.
  665. Each @var{pred} call is @code{(@var{pred} @var{elem1} @dots{}
  666. @var{elemN})} taking an element from each @var{lst}. The calls are
  667. made successively for the first, second, etc elements of the lists,
  668. stopping if @var{pred} returns @code{#f}, or when the end of any of
  669. the lists is reached.
  670. The @var{pred} call on the last set of elements (ie.@: when the end of
  671. the shortest list has been reached) is a tail call.
  672. If one of @var{lst1} @dots{} @var{lstN} is empty then no calls to
  673. @var{pred} are made, and the return is @code{#t}.
  674. @end deffn
  675. @deffn {Scheme Procedure} list-index pred lst1 @dots{} lstN
  676. Return the index of the first set of elements, one from each of
  677. @var{lst1}@dots{}@var{lstN}, which satisfies @var{pred}.
  678. @var{pred} is called as @code{(@var{pred} elem1 @dots{} elemN)}.
  679. Searching stops when the end of the shortest @var{lst} is reached.
  680. The return index starts from 0 for the first set of elements. If no
  681. set of elements pass then the return is @code{#f}.
  682. @example
  683. (list-index odd? '(2 4 6 9)) @result{} 3
  684. (list-index = '(1 2 3) '(3 1 2)) @result{} #f
  685. @end example
  686. @end deffn
  687. @deffn {Scheme Procedure} member x lst [=]
  688. Return the first sublist of @var{lst} whose @sc{car} is equal to
  689. @var{x}. If @var{x} does not appear in @var{lst}, return @code{#f}.
  690. Equality is determined by @code{equal?}, or by the equality predicate
  691. @var{=} if given. @var{=} is called @code{(= @var{x} elem)},
  692. ie.@: with the given @var{x} first, so for example to find the first
  693. element greater than 5,
  694. @example
  695. (member 5 '(3 5 1 7 2 9) <) @result{} (7 2 9)
  696. @end example
  697. This version of @code{member} extends the core @code{member}
  698. (@pxref{List Searching}) by accepting an equality predicate.
  699. @end deffn
  700. @node SRFI-1 Deleting
  701. @subsubsection Deleting
  702. @cindex list delete
  703. @deffn {Scheme Procedure} delete x lst [=]
  704. @deffnx {Scheme Procedure} delete! x lst [=]
  705. Return a list containing the elements of @var{lst} but with those
  706. equal to @var{x} deleted. The returned elements will be in the same
  707. order as they were in @var{lst}.
  708. Equality is determined by the @var{=} predicate, or @code{equal?} if
  709. not given. An equality call is made just once for each element, but
  710. the order in which the calls are made on the elements is unspecified.
  711. The equality calls are always @code{(= x elem)}, ie.@: the given @var{x}
  712. is first. This means for instance elements greater than 5 can be
  713. deleted with @code{(delete 5 lst <)}.
  714. @code{delete} does not modify @var{lst}, but the return might share a
  715. common tail with @var{lst}. @code{delete!} may modify the structure
  716. of @var{lst} to construct its return.
  717. These functions extend the core @code{delete} and @code{delete!}
  718. (@pxref{List Modification}) in accepting an equality predicate. See
  719. also @code{lset-difference} (@pxref{SRFI-1 Set Operations}) for
  720. deleting multiple elements from a list.
  721. @end deffn
  722. @deffn {Scheme Procedure} delete-duplicates lst [=]
  723. @deffnx {Scheme Procedure} delete-duplicates! lst [=]
  724. Return a list containing the elements of @var{lst} but without
  725. duplicates.
  726. When elements are equal, only the first in @var{lst} is retained.
  727. Equal elements can be anywhere in @var{lst}, they don't have to be
  728. adjacent. The returned list will have the retained elements in the
  729. same order as they were in @var{lst}.
  730. Equality is determined by the @var{=} predicate, or @code{equal?} if
  731. not given. Calls @code{(= x y)} are made with element @var{x} being
  732. before @var{y} in @var{lst}. A call is made at most once for each
  733. combination, but the sequence of the calls across the elements is
  734. unspecified.
  735. @code{delete-duplicates} does not modify @var{lst}, but the return
  736. might share a common tail with @var{lst}. @code{delete-duplicates!}
  737. may modify the structure of @var{lst} to construct its return.
  738. In the worst case, this is an @math{O(N^2)} algorithm because it must
  739. check each element against all those preceding it. For long lists it
  740. is more efficient to sort and then compare only adjacent elements.
  741. @end deffn
  742. @node SRFI-1 Association Lists
  743. @subsubsection Association Lists
  744. @cindex association list
  745. @cindex alist
  746. @c FIXME::martin: Review me!
  747. Association lists are described in detail in section @ref{Association
  748. Lists}. The present section only documents the additional procedures
  749. for dealing with association lists defined by SRFI-1.
  750. @deffn {Scheme Procedure} assoc key alist [=]
  751. Return the pair from @var{alist} which matches @var{key}. This
  752. extends the core @code{assoc} (@pxref{Retrieving Alist Entries}) by
  753. taking an optional @var{=} comparison procedure.
  754. The default comparison is @code{equal?}. If an @var{=} parameter is
  755. given it's called @code{(@var{=} @var{key} @var{alistcar})}, ie. the
  756. given target @var{key} is the first argument, and a @code{car} from
  757. @var{alist} is second.
  758. For example a case-insensitive string lookup,
  759. @example
  760. (assoc "yy" '(("XX" . 1) ("YY" . 2)) string-ci=?)
  761. @result{} ("YY" . 2)
  762. @end example
  763. @end deffn
  764. @deffn {Scheme Procedure} alist-cons key datum alist
  765. Cons a new association @var{key} and @var{datum} onto @var{alist} and
  766. return the result. This is equivalent to
  767. @lisp
  768. (cons (cons @var{key} @var{datum}) @var{alist})
  769. @end lisp
  770. @code{acons} (@pxref{Adding or Setting Alist Entries}) in the Guile
  771. core does the same thing.
  772. @end deffn
  773. @deffn {Scheme Procedure} alist-copy alist
  774. Return a newly allocated copy of @var{alist}, that means that the
  775. spine of the list as well as the pairs are copied.
  776. @end deffn
  777. @deffn {Scheme Procedure} alist-delete key alist [=]
  778. @deffnx {Scheme Procedure} alist-delete! key alist [=]
  779. Return a list containing the elements of @var{alist} but with those
  780. elements whose keys are equal to @var{key} deleted. The returned
  781. elements will be in the same order as they were in @var{alist}.
  782. Equality is determined by the @var{=} predicate, or @code{equal?} if
  783. not given. The order in which elements are tested is unspecified, but
  784. each equality call is made @code{(= key alistkey)}, ie. the given
  785. @var{key} parameter is first and the key from @var{alist} second.
  786. This means for instance all associations with a key greater than 5 can
  787. be removed with @code{(alist-delete 5 alist <)}.
  788. @code{alist-delete} does not modify @var{alist}, but the return might
  789. share a common tail with @var{alist}. @code{alist-delete!} may modify
  790. the list structure of @var{alist} to construct its return.
  791. @end deffn
  792. @node SRFI-1 Set Operations
  793. @subsubsection Set Operations on Lists
  794. @cindex list set operation
  795. Lists can be used to represent sets of objects. The procedures in
  796. this section operate on such lists as sets.
  797. Note that lists are not an efficient way to implement large sets. The
  798. procedures here typically take time @math{@var{m}@cross{}@var{n}} when
  799. operating on @var{m} and @var{n} element lists. Other data structures
  800. like trees, bitsets (@pxref{Bit Vectors}) or hash tables (@pxref{Hash
  801. Tables}) are faster.
  802. All these procedures take an equality predicate as the first argument.
  803. This predicate is used for testing the objects in the list sets for
  804. sameness. This predicate must be consistent with @code{eq?}
  805. (@pxref{Equality}) in the sense that if two list elements are
  806. @code{eq?} then they must also be equal under the predicate. This
  807. simply means a given object must be equal to itself.
  808. @deffn {Scheme Procedure} lset<= = list1 list2 @dots{}
  809. Return @code{#t} if each list is a subset of the one following it.
  810. Ie.@: @var{list1} a subset of @var{list2}, @var{list2} a subset of
  811. @var{list3}, etc, for as many lists as given. If only one list or no
  812. lists are given then the return is @code{#t}.
  813. A list @var{x} is a subset of @var{y} if each element of @var{x} is
  814. equal to some element in @var{y}. Elements are compared using the
  815. given @var{=} procedure, called as @code{(@var{=} xelem yelem)}.
  816. @example
  817. (lset<= eq?) @result{} #t
  818. (lset<= eqv? '(1 2 3) '(1)) @result{} #f
  819. (lset<= eqv? '(1 3 2) '(4 3 1 2)) @result{} #t
  820. @end example
  821. @end deffn
  822. @deffn {Scheme Procedure} lset= = list1 list2 @dots{}
  823. Return @code{#t} if all argument lists are set-equal. @var{list1} is
  824. compared to @var{list2}, @var{list2} to @var{list3}, etc, for as many
  825. lists as given. If only one list or no lists are given then the
  826. return is @code{#t}.
  827. Two lists @var{x} and @var{y} are set-equal if each element of @var{x}
  828. is equal to some element of @var{y} and conversely each element of
  829. @var{y} is equal to some element of @var{x}. The order of the
  830. elements in the lists doesn't matter. Element equality is determined
  831. with the given @var{=} procedure, called as @code{(@var{=} xelem
  832. yelem)}, but exactly which calls are made is unspecified.
  833. @example
  834. (lset= eq?) @result{} #t
  835. (lset= eqv? '(1 2 3) '(3 2 1)) @result{} #t
  836. (lset= string-ci=? '("a" "A" "b") '("B" "b" "a")) @result{} #t
  837. @end example
  838. @end deffn
  839. @deffn {Scheme Procedure} lset-adjoin = list elem1 @dots{}
  840. Add to @var{list} any of the given @var{elem}s not already in the
  841. list. @var{elem}s are @code{cons}ed onto the start of @var{list} (so
  842. the return shares a common tail with @var{list}), but the order
  843. they're added is unspecified.
  844. The given @var{=} procedure is used for comparing elements, called as
  845. @code{(@var{=} listelem elem)}, ie.@: the second argument is one of
  846. the given @var{elem} parameters.
  847. @example
  848. (lset-adjoin eqv? '(1 2 3) 4 1 5) @result{} (5 4 1 2 3)
  849. @end example
  850. @end deffn
  851. @deffn {Scheme Procedure} lset-union = list1 list2 @dots{}
  852. @deffnx {Scheme Procedure} lset-union! = list1 list2 @dots{}
  853. Return the union of the argument list sets. The result is built by
  854. taking the union of @var{list1} and @var{list2}, then the union of
  855. that with @var{list3}, etc, for as many lists as given. For one list
  856. argument that list itself is the result, for no list arguments the
  857. result is the empty list.
  858. The union of two lists @var{x} and @var{y} is formed as follows. If
  859. @var{x} is empty then the result is @var{y}. Otherwise start with
  860. @var{x} as the result and consider each @var{y} element (from first to
  861. last). A @var{y} element not equal to something already in the result
  862. is @code{cons}ed onto the result.
  863. The given @var{=} procedure is used for comparing elements, called as
  864. @code{(@var{=} relem yelem)}. The first argument is from the result
  865. accumulated so far, and the second is from the list being union-ed in.
  866. But exactly which calls are made is otherwise unspecified.
  867. Notice that duplicate elements in @var{list1} (or the first non-empty
  868. list) are preserved, but that repeated elements in subsequent lists
  869. are only added once.
  870. @example
  871. (lset-union eqv?) @result{} ()
  872. (lset-union eqv? '(1 2 3)) @result{} (1 2 3)
  873. (lset-union eqv? '(1 2 1 3) '(2 4 5) '(5)) @result{} (5 4 1 2 1 3)
  874. @end example
  875. @code{lset-union} doesn't change the given lists but the result may
  876. share a tail with the first non-empty list. @code{lset-union!} can
  877. modify all of the given lists to form the result.
  878. @end deffn
  879. @deffn {Scheme Procedure} lset-intersection = list1 list2 @dots{}
  880. @deffnx {Scheme Procedure} lset-intersection! = list1 list2 @dots{}
  881. Return the intersection of @var{list1} with the other argument lists,
  882. meaning those elements of @var{list1} which are also in all of
  883. @var{list2} etc. For one list argument, just that list is returned.
  884. The test for an element of @var{list1} to be in the return is simply
  885. that it's equal to some element in each of @var{list2} etc. Notice
  886. this means an element appearing twice in @var{list1} but only once in
  887. each of @var{list2} etc will go into the return twice. The return has
  888. its elements in the same order as they were in @var{list1}.
  889. The given @var{=} procedure is used for comparing elements, called as
  890. @code{(@var{=} elem1 elemN)}. The first argument is from @var{list1}
  891. and the second is from one of the subsequent lists. But exactly which
  892. calls are made and in what order is unspecified.
  893. @example
  894. (lset-intersection eqv? '(x y)) @result{} (x y)
  895. (lset-intersection eqv? '(1 2 3) '(4 3 2)) @result{} (2 3)
  896. (lset-intersection eqv? '(1 1 2 2) '(1 2) '(2 1) '(2)) @result{} (2 2)
  897. @end example
  898. The return from @code{lset-intersection} may share a tail with
  899. @var{list1}. @code{lset-intersection!} may modify @var{list1} to form
  900. its result.
  901. @end deffn
  902. @deffn {Scheme Procedure} lset-difference = list1 list2 @dots{}
  903. @deffnx {Scheme Procedure} lset-difference! = list1 list2 @dots{}
  904. Return @var{list1} with any elements in @var{list2}, @var{list3} etc
  905. removed (ie.@: subtracted). For one list argument, just that list is
  906. returned.
  907. The given @var{=} procedure is used for comparing elements, called as
  908. @code{(@var{=} elem1 elemN)}. The first argument is from @var{list1}
  909. and the second from one of the subsequent lists. But exactly which
  910. calls are made and in what order is unspecified.
  911. @example
  912. (lset-difference eqv? '(x y)) @result{} (x y)
  913. (lset-difference eqv? '(1 2 3) '(3 1)) @result{} (2)
  914. (lset-difference eqv? '(1 2 3) '(3) '(2)) @result{} (1)
  915. @end example
  916. The return from @code{lset-difference} may share a tail with
  917. @var{list1}. @code{lset-difference!} may modify @var{list1} to form
  918. its result.
  919. @end deffn
  920. @deffn {Scheme Procedure} lset-diff+intersection = list1 list2 @dots{}
  921. @deffnx {Scheme Procedure} lset-diff+intersection! = list1 list2 @dots{}
  922. Return two values (@pxref{Multiple Values}), the difference and
  923. intersection of the argument lists as per @code{lset-difference} and
  924. @code{lset-intersection} above.
  925. For two list arguments this partitions @var{list1} into those elements
  926. of @var{list1} which are in @var{list2} and not in @var{list2}. (But
  927. for more than two arguments there can be elements of @var{list1} which
  928. are neither part of the difference nor the intersection.)
  929. One of the return values from @code{lset-diff+intersection} may share
  930. a tail with @var{list1}. @code{lset-diff+intersection!} may modify
  931. @var{list1} to form its results.
  932. @end deffn
  933. @deffn {Scheme Procedure} lset-xor = list1 list2 @dots{}
  934. @deffnx {Scheme Procedure} lset-xor! = list1 list2 @dots{}
  935. Return an XOR of the argument lists. For two lists this means those
  936. elements which are in exactly one of the lists. For more than two
  937. lists it means those elements which appear in an odd number of the
  938. lists.
  939. To be precise, the XOR of two lists @var{x} and @var{y} is formed by
  940. taking those elements of @var{x} not equal to any element of @var{y},
  941. plus those elements of @var{y} not equal to any element of @var{x}.
  942. Equality is determined with the given @var{=} procedure, called as
  943. @code{(@var{=} e1 e2)}. One argument is from @var{x} and the other
  944. from @var{y}, but which way around is unspecified. Exactly which
  945. calls are made is also unspecified, as is the order of the elements in
  946. the result.
  947. @example
  948. (lset-xor eqv? '(x y)) @result{} (x y)
  949. (lset-xor eqv? '(1 2 3) '(4 3 2)) @result{} (4 1)
  950. @end example
  951. The return from @code{lset-xor} may share a tail with one of the list
  952. arguments. @code{lset-xor!} may modify @var{list1} to form its
  953. result.
  954. @end deffn
  955. @node SRFI-2
  956. @subsection SRFI-2 - and-let*
  957. @cindex SRFI-2
  958. @noindent
  959. The following syntax can be obtained with
  960. @lisp
  961. (use-modules (srfi srfi-2))
  962. @end lisp
  963. @deffn {library syntax} and-let* (clause @dots{}) body @dots{}
  964. A combination of @code{and} and @code{let*}.
  965. Each @var{clause} is evaluated in turn, and if @code{#f} is obtained
  966. then evaluation stops and @code{#f} is returned. If all are
  967. non-@code{#f} then @var{body} is evaluated and the last form gives the
  968. return value, or if @var{body} is empty then the result is @code{#t}.
  969. Each @var{clause} should be one of the following,
  970. @table @code
  971. @item (symbol expr)
  972. Evaluate @var{expr}, check for @code{#f}, and bind it to @var{symbol}.
  973. Like @code{let*}, that binding is available to subsequent clauses.
  974. @item (expr)
  975. Evaluate @var{expr} and check for @code{#f}.
  976. @item symbol
  977. Get the value bound to @var{symbol} and check for @code{#f}.
  978. @end table
  979. Notice that @code{(expr)} has an ``extra'' pair of parentheses, for
  980. instance @code{((eq? x y))}. One way to remember this is to imagine
  981. the @code{symbol} in @code{(symbol expr)} is omitted.
  982. @code{and-let*} is good for calculations where a @code{#f} value means
  983. termination, but where a non-@code{#f} value is going to be needed in
  984. subsequent expressions.
  985. The following illustrates this, it returns text between brackets
  986. @samp{[...]} in a string, or @code{#f} if there are no such brackets
  987. (ie.@: either @code{string-index} gives @code{#f}).
  988. @example
  989. (define (extract-brackets str)
  990. (and-let* ((start (string-index str #\[))
  991. (end (string-index str #\] start)))
  992. (substring str (1+ start) end)))
  993. @end example
  994. The following shows plain variables and expressions tested too.
  995. @code{diagnostic-levels} is taken to be an alist associating a
  996. diagnostic type with a level. @code{str} is printed only if the type
  997. is known and its level is high enough.
  998. @example
  999. (define (show-diagnostic type str)
  1000. (and-let* (want-diagnostics
  1001. (level (assq-ref diagnostic-levels type))
  1002. ((>= level current-diagnostic-level)))
  1003. (display str)))
  1004. @end example
  1005. The advantage of @code{and-let*} is that an extended sequence of
  1006. expressions and tests doesn't require lots of nesting as would arise
  1007. from separate @code{and} and @code{let*}, or from @code{cond} with
  1008. @code{=>}.
  1009. @end deffn
  1010. @node SRFI-4
  1011. @subsection SRFI-4 - Homogeneous numeric vector datatypes
  1012. @cindex SRFI-4
  1013. The SRFI-4 procedures and data types are always available, @xref{Uniform
  1014. Numeric Vectors}.
  1015. @node SRFI-6
  1016. @subsection SRFI-6 - Basic String Ports
  1017. @cindex SRFI-6
  1018. SRFI-6 defines the procedures @code{open-input-string},
  1019. @code{open-output-string} and @code{get-output-string}. These
  1020. procedures are included in the Guile core, so using this module does not
  1021. make any difference at the moment. But it is possible that support for
  1022. SRFI-6 will be factored out of the core library in the future, so using
  1023. this module does not hurt, after all.
  1024. @node SRFI-8
  1025. @subsection SRFI-8 - receive
  1026. @cindex SRFI-8
  1027. @code{receive} is a syntax for making the handling of multiple-value
  1028. procedures easier. It is documented in @xref{Multiple Values}.
  1029. @node SRFI-9
  1030. @subsection SRFI-9 - define-record-type
  1031. @cindex SRFI-9
  1032. @cindex record
  1033. This SRFI is a syntax for defining new record types and creating
  1034. predicate, constructor, and field getter and setter functions. In
  1035. Guile this is simply an alternate interface to the core record
  1036. functionality (@pxref{Records}). It can be used with,
  1037. @example
  1038. (use-modules (srfi srfi-9))
  1039. @end example
  1040. @deffn {library syntax} define-record-type type @* (constructor fieldname @dots{}) @* predicate @* (fieldname accessor [modifier]) @dots{}
  1041. @sp 1
  1042. Create a new record type, and make various @code{define}s for using
  1043. it. This syntax can only occur at the top-level, not nested within
  1044. some other form.
  1045. @var{type} is bound to the record type, which is as per the return
  1046. from the core @code{make-record-type}. @var{type} also provides the
  1047. name for the record, as per @code{record-type-name}.
  1048. @var{constructor} is bound to a function to be called as
  1049. @code{(@var{constructor} fieldval @dots{})} to create a new record of
  1050. this type. The arguments are initial values for the fields, one
  1051. argument for each field, in the order they appear in the
  1052. @code{define-record-type} form.
  1053. The @var{fieldname}s provide the names for the record fields, as per
  1054. the core @code{record-type-fields} etc, and are referred to in the
  1055. subsequent accessor/modifier forms.
  1056. @var{predictate} is bound to a function to be called as
  1057. @code{(@var{predicate} obj)}. It returns @code{#t} or @code{#f}
  1058. according to whether @var{obj} is a record of this type.
  1059. Each @var{accessor} is bound to a function to be called
  1060. @code{(@var{accessor} record)} to retrieve the respective field from a
  1061. @var{record}. Similarly each @var{modifier} is bound to a function to
  1062. be called @code{(@var{modifier} record val)} to set the respective
  1063. field in a @var{record}.
  1064. @end deffn
  1065. @noindent
  1066. An example will illustrate typical usage,
  1067. @example
  1068. (define-record-type employee-type
  1069. (make-employee name age salary)
  1070. employee?
  1071. (name get-employee-name)
  1072. (age get-employee-age set-employee-age)
  1073. (salary get-employee-salary set-employee-salary))
  1074. @end example
  1075. This creates a new employee data type, with name, age and salary
  1076. fields. Accessor functions are created for each field, but no
  1077. modifier function for the name (the intention in this example being
  1078. that it's established only when an employee object is created). These
  1079. can all then be used as for example,
  1080. @example
  1081. employee-type @result{} #<record-type employee-type>
  1082. (define fred (make-employee "Fred" 45 20000.00))
  1083. (employee? fred) @result{} #t
  1084. (get-employee-age fred) @result{} 45
  1085. (set-employee-salary fred 25000.00) ;; pay rise
  1086. @end example
  1087. The functions created by @code{define-record-type} are ordinary
  1088. top-level @code{define}s. They can be redefined or @code{set!} as
  1089. desired, exported from a module, etc.
  1090. @node SRFI-10
  1091. @subsection SRFI-10 - Hash-Comma Reader Extension
  1092. @cindex SRFI-10
  1093. @cindex hash-comma
  1094. @cindex #,()
  1095. This SRFI implements a reader extension @code{#,()} called hash-comma.
  1096. It allows the reader to give new kinds of objects, for use both in
  1097. data and as constants or literals in source code. This feature is
  1098. available with
  1099. @example
  1100. (use-modules (srfi srfi-10))
  1101. @end example
  1102. @noindent
  1103. The new read syntax is of the form
  1104. @example
  1105. #,(@var{tag} @var{arg}@dots{})
  1106. @end example
  1107. @noindent
  1108. where @var{tag} is a symbol and the @var{arg}s are objects taken as
  1109. parameters. @var{tag}s are registered with the following procedure.
  1110. @deffn {Scheme Procedure} define-reader-ctor tag proc
  1111. Register @var{proc} as the constructor for a hash-comma read syntax
  1112. starting with symbol @var{tag}, ie. @nicode{#,(@var{tag} arg@dots{})}.
  1113. @var{proc} is called with the given arguments @code{(@var{proc}
  1114. arg@dots{})} and the object it returns is the result of the read.
  1115. @end deffn
  1116. @noindent
  1117. For example, a syntax giving a list of @var{N} copies of an object.
  1118. @example
  1119. (define-reader-ctor 'repeat
  1120. (lambda (obj reps)
  1121. (make-list reps obj)))
  1122. (display '#,(repeat 99 3))
  1123. @print{} (99 99 99)
  1124. @end example
  1125. Notice the quote @nicode{'} when the @nicode{#,( )} is used. The
  1126. @code{repeat} handler returns a list and the program must quote to use
  1127. it literally, the same as any other list. Ie.
  1128. @example
  1129. (display '#,(repeat 99 3))
  1130. @result{}
  1131. (display '(99 99 99))
  1132. @end example
  1133. When a handler returns an object which is self-evaluating, like a
  1134. number or a string, then there's no need for quoting, just as there's
  1135. no need when giving those directly as literals. For example an
  1136. addition,
  1137. @example
  1138. (define-reader-ctor 'sum
  1139. (lambda (x y)
  1140. (+ x y)))
  1141. (display #,(sum 123 456)) @print{} 579
  1142. @end example
  1143. A typical use for @nicode{#,()} is to get a read syntax for objects
  1144. which don't otherwise have one. For example, the following allows a
  1145. hash table to be given literally, with tags and values, ready for fast
  1146. lookup.
  1147. @example
  1148. (define-reader-ctor 'hash
  1149. (lambda elems
  1150. (let ((table (make-hash-table)))
  1151. (for-each (lambda (elem)
  1152. (apply hash-set! table elem))
  1153. elems)
  1154. table)))
  1155. (define (animal->family animal)
  1156. (hash-ref '#,(hash ("tiger" "cat")
  1157. ("lion" "cat")
  1158. ("wolf" "dog"))
  1159. animal))
  1160. (animal->family "lion") @result{} "cat"
  1161. @end example
  1162. Or for example the following is a syntax for a compiled regular
  1163. expression (@pxref{Regular Expressions}).
  1164. @example
  1165. (use-modules (ice-9 regex))
  1166. (define-reader-ctor 'regexp make-regexp)
  1167. (define (extract-angs str)
  1168. (let ((match (regexp-exec '#,(regexp "<([A-Z0-9]+)>") str)))
  1169. (and match
  1170. (match:substring match 1))))
  1171. (extract-angs "foo <BAR> quux") @result{} "BAR"
  1172. @end example
  1173. @sp 1
  1174. @nicode{#,()} is somewhat similar to @code{define-macro}
  1175. (@pxref{Macros}) in that handler code is run to produce a result, but
  1176. @nicode{#,()} operates at the read stage, so it can appear in data for
  1177. @code{read} (@pxref{Scheme Read}), not just in code to be executed.
  1178. Because @nicode{#,()} is handled at read-time it has no direct access
  1179. to variables etc. A symbol in the arguments is just a symbol, not a
  1180. variable reference. The arguments are essentially constants, though
  1181. the handler procedure can use them in any complicated way it might
  1182. want.
  1183. Once @code{(srfi srfi-10)} has loaded, @nicode{#,()} is available
  1184. globally, there's no need to use @code{(srfi srfi-10)} in later
  1185. modules. Similarly the tags registered are global and can be used
  1186. anywhere once registered.
  1187. There's no attempt to record what previous @nicode{#,()} forms have
  1188. been seen, if two identical forms occur then two calls are made to the
  1189. handler procedure. The handler might like to maintain a cache or
  1190. similar to avoid making copies of large objects, depending on expected
  1191. usage.
  1192. In code the best uses of @nicode{#,()} are generally when there's a
  1193. lot of objects of a particular kind as literals or constants. If
  1194. there's just a few then some local variables and initializers are
  1195. fine, but that becomes tedious and error prone when there's a lot, and
  1196. the anonymous and compact syntax of @nicode{#,()} is much better.
  1197. @node SRFI-11
  1198. @subsection SRFI-11 - let-values
  1199. @cindex SRFI-11
  1200. @findex let-values
  1201. @findex let*-values
  1202. This module implements the binding forms for multiple values
  1203. @code{let-values} and @code{let*-values}. These forms are similar to
  1204. @code{let} and @code{let*} (@pxref{Local Bindings}), but they support
  1205. binding of the values returned by multiple-valued expressions.
  1206. Write @code{(use-modules (srfi srfi-11))} to make the bindings
  1207. available.
  1208. @lisp
  1209. (let-values (((x y) (values 1 2))
  1210. ((z f) (values 3 4)))
  1211. (+ x y z f))
  1212. @result{}
  1213. 10
  1214. @end lisp
  1215. @code{let-values} performs all bindings simultaneously, which means that
  1216. no expression in the binding clauses may refer to variables bound in the
  1217. same clause list. @code{let*-values}, on the other hand, performs the
  1218. bindings sequentially, just like @code{let*} does for single-valued
  1219. expressions.
  1220. @node SRFI-13
  1221. @subsection SRFI-13 - String Library
  1222. @cindex SRFI-13
  1223. The SRFI-13 procedures are always available, @xref{Strings}.
  1224. @node SRFI-14
  1225. @subsection SRFI-14 - Character-set Library
  1226. @cindex SRFI-14
  1227. The SRFI-14 data type and procedures are always available,
  1228. @xref{Character Sets}.
  1229. @node SRFI-16
  1230. @subsection SRFI-16 - case-lambda
  1231. @cindex SRFI-16
  1232. @cindex variable arity
  1233. @cindex arity, variable
  1234. @c FIXME::martin: Review me!
  1235. @findex case-lambda
  1236. The syntactic form @code{case-lambda} creates procedures, just like
  1237. @code{lambda}, but has syntactic extensions for writing procedures of
  1238. varying arity easier.
  1239. The syntax of the @code{case-lambda} form is defined in the following
  1240. EBNF grammar.
  1241. @example
  1242. @group
  1243. <case-lambda>
  1244. --> (case-lambda <case-lambda-clause>)
  1245. <case-lambda-clause>
  1246. --> (<formals> <definition-or-command>*)
  1247. <formals>
  1248. --> (<identifier>*)
  1249. | (<identifier>* . <identifier>)
  1250. | <identifier>
  1251. @end group
  1252. @end example
  1253. The value returned by a @code{case-lambda} form is a procedure which
  1254. matches the number of actual arguments against the formals in the
  1255. various clauses, in order. @dfn{Formals} means a formal argument list
  1256. just like with @code{lambda} (@pxref{Lambda}). The first matching clause
  1257. is selected, the corresponding values from the actual parameter list are
  1258. bound to the variable names in the clauses and the body of the clause is
  1259. evaluated. If no clause matches, an error is signalled.
  1260. The following (silly) definition creates a procedure @var{foo} which
  1261. acts differently, depending on the number of actual arguments. If one
  1262. argument is given, the constant @code{#t} is returned, two arguments are
  1263. added and if more arguments are passed, their product is calculated.
  1264. @lisp
  1265. (define foo (case-lambda
  1266. ((x) #t)
  1267. ((x y) (+ x y))
  1268. (z
  1269. (apply * z))))
  1270. (foo 'bar)
  1271. @result{}
  1272. #t
  1273. (foo 2 4)
  1274. @result{}
  1275. 6
  1276. (foo 3 3 3)
  1277. @result{}
  1278. 27
  1279. (foo)
  1280. @result{}
  1281. 1
  1282. @end lisp
  1283. The last expression evaluates to 1 because the last clause is matched,
  1284. @var{z} is bound to the empty list and the following multiplication,
  1285. applied to zero arguments, yields 1.
  1286. @node SRFI-17
  1287. @subsection SRFI-17 - Generalized set!
  1288. @cindex SRFI-17
  1289. This SRFI implements a generalized @code{set!}, allowing some
  1290. ``referencing'' functions to be used as the target location of a
  1291. @code{set!}. This feature is available from
  1292. @example
  1293. (use-modules (srfi srfi-17))
  1294. @end example
  1295. @noindent
  1296. For example @code{vector-ref} is extended so that
  1297. @example
  1298. (set! (vector-ref vec idx) new-value)
  1299. @end example
  1300. @noindent
  1301. is equivalent to
  1302. @example
  1303. (vector-set! vec idx new-value)
  1304. @end example
  1305. The idea is that a @code{vector-ref} expression identifies a location,
  1306. which may be either fetched or stored. The same form is used for the
  1307. location in both cases, encouraging visual clarity. This is similar
  1308. to the idea of an ``lvalue'' in C.
  1309. The mechanism for this kind of @code{set!} is in the Guile core
  1310. (@pxref{Procedures with Setters}). This module adds definitions of
  1311. the following functions as procedures with setters, allowing them to
  1312. be targets of a @code{set!},
  1313. @quotation
  1314. @nicode{car}, @nicode{cdr}, @nicode{caar}, @nicode{cadr},
  1315. @nicode{cdar}, @nicode{cddr}, @nicode{caaar}, @nicode{caadr},
  1316. @nicode{cadar}, @nicode{caddr}, @nicode{cdaar}, @nicode{cdadr},
  1317. @nicode{cddar}, @nicode{cdddr}, @nicode{caaaar}, @nicode{caaadr},
  1318. @nicode{caadar}, @nicode{caaddr}, @nicode{cadaar}, @nicode{cadadr},
  1319. @nicode{caddar}, @nicode{cadddr}, @nicode{cdaaar}, @nicode{cdaadr},
  1320. @nicode{cdadar}, @nicode{cdaddr}, @nicode{cddaar}, @nicode{cddadr},
  1321. @nicode{cdddar}, @nicode{cddddr}
  1322. @nicode{string-ref}, @nicode{vector-ref}
  1323. @end quotation
  1324. The SRFI specifies @code{setter} (@pxref{Procedures with Setters}) as
  1325. a procedure with setter, allowing the setter for a procedure to be
  1326. changed, eg.@: @code{(set! (setter foo) my-new-setter-handler)}.
  1327. Currently Guile does not implement this, a setter can only be
  1328. specified on creation (@code{getter-with-setter} below).
  1329. @defun getter-with-setter
  1330. The same as the Guile core @code{make-procedure-with-setter}
  1331. (@pxref{Procedures with Setters}).
  1332. @end defun
  1333. @node SRFI-19
  1334. @subsection SRFI-19 - Time/Date Library
  1335. @cindex SRFI-19
  1336. @cindex time
  1337. @cindex date
  1338. This is an implementation of the SRFI-19 time/date library. The
  1339. functions and variables described here are provided by
  1340. @example
  1341. (use-modules (srfi srfi-19))
  1342. @end example
  1343. @strong{Caution}: The current code in this module incorrectly extends
  1344. the Gregorian calendar leap year rule back prior to the introduction
  1345. of those reforms in 1582 (or the appropriate year in various
  1346. countries). The Julian calendar was used prior to 1582, and there
  1347. were 10 days skipped for the reform, but the code doesn't implement
  1348. that.
  1349. This will be fixed some time. Until then calculations for 1583
  1350. onwards are correct, but prior to that any day/month/year and day of
  1351. the week calculations are wrong.
  1352. @menu
  1353. * SRFI-19 Introduction::
  1354. * SRFI-19 Time::
  1355. * SRFI-19 Date::
  1356. * SRFI-19 Time/Date conversions::
  1357. * SRFI-19 Date to string::
  1358. * SRFI-19 String to date::
  1359. @end menu
  1360. @node SRFI-19 Introduction
  1361. @subsubsection SRFI-19 Introduction
  1362. @cindex universal time
  1363. @cindex atomic time
  1364. @cindex UTC
  1365. @cindex TAI
  1366. This module implements time and date representations and calculations,
  1367. in various time systems, including universal time (UTC) and atomic
  1368. time (TAI).
  1369. For those not familiar with these time systems, TAI is based on a
  1370. fixed length second derived from oscillations of certain atoms. UTC
  1371. differs from TAI by an integral number of seconds, which is increased
  1372. or decreased at announced times to keep UTC aligned to a mean solar
  1373. day (the orbit and rotation of the earth are not quite constant).
  1374. @cindex leap second
  1375. So far, only increases in the TAI
  1376. @tex
  1377. $\leftrightarrow$
  1378. @end tex
  1379. @ifnottex
  1380. <->
  1381. @end ifnottex
  1382. UTC difference have been needed. Such an increase is a ``leap
  1383. second'', an extra second of TAI introduced at the end of a UTC day.
  1384. When working entirely within UTC this is never seen, every day simply
  1385. has 86400 seconds. But when converting from TAI to a UTC date, an
  1386. extra 23:59:60 is present, where normally a day would end at 23:59:59.
  1387. Effectively the UTC second from 23:59:59 to 00:00:00 has taken two TAI
  1388. seconds.
  1389. @cindex system clock
  1390. In the current implementation, the system clock is assumed to be UTC,
  1391. and a table of leap seconds in the code converts to TAI. See comments
  1392. in @file{srfi-19.scm} for how to update this table.
  1393. @cindex julian day
  1394. @cindex modified julian day
  1395. Also, for those not familiar with the terminology, a @dfn{Julian Day}
  1396. is a real number which is a count of days and fraction of a day, in
  1397. UTC, starting from -4713-01-01T12:00:00Z, ie.@: midday Monday 1 Jan
  1398. 4713 B.C. A @dfn{Modified Julian Day} is the same, but starting from
  1399. 1858-11-17T00:00:00Z, ie.@: midnight 17 November 1858 UTC. That time
  1400. is julian day 2400000.5.
  1401. @c The SRFI-1 spec says -4714-11-24T12:00:00Z (November 24, -4714 at
  1402. @c noon, UTC), but this is incorrect. It looks like it might have
  1403. @c arisen from the code incorrectly treating years a multiple of 100
  1404. @c but not 400 prior to 1582 as non-leap years, where instead the Julian
  1405. @c calendar should be used so all multiples of 4 before 1582 are leap
  1406. @c years.
  1407. @node SRFI-19 Time
  1408. @subsubsection SRFI-19 Time
  1409. @cindex time
  1410. A @dfn{time} object has type, seconds and nanoseconds fields
  1411. representing a point in time starting from some epoch. This is an
  1412. arbitrary point in time, not just a time of day. Although times are
  1413. represented in nanoseconds, the actual resolution may be lower.
  1414. The following variables hold the possible time types. For instance
  1415. @code{(current-time time-process)} would give the current CPU process
  1416. time.
  1417. @defvar time-utc
  1418. Universal Coordinated Time (UTC).
  1419. @cindex UTC
  1420. @end defvar
  1421. @defvar time-tai
  1422. International Atomic Time (TAI).
  1423. @cindex TAI
  1424. @end defvar
  1425. @defvar time-monotonic
  1426. Monotonic time, meaning a monotonically increasing time starting from
  1427. an unspecified epoch.
  1428. Note that in the current implementation @code{time-monotonic} is the
  1429. same as @code{time-tai}, and unfortunately is therefore affected by
  1430. adjustments to the system clock. Perhaps this will change in the
  1431. future.
  1432. @end defvar
  1433. @defvar time-duration
  1434. A duration, meaning simply a difference between two times.
  1435. @end defvar
  1436. @defvar time-process
  1437. CPU time spent in the current process, starting from when the process
  1438. began.
  1439. @cindex process time
  1440. @end defvar
  1441. @defvar time-thread
  1442. CPU time spent in the current thread. Not currently implemented.
  1443. @cindex thread time
  1444. @end defvar
  1445. @sp 1
  1446. @defun time? obj
  1447. Return @code{#t} if @var{obj} is a time object, or @code{#f} if not.
  1448. @end defun
  1449. @defun make-time type nanoseconds seconds
  1450. Create a time object with the given @var{type}, @var{seconds} and
  1451. @var{nanoseconds}.
  1452. @end defun
  1453. @defun time-type time
  1454. @defunx time-nanosecond time
  1455. @defunx time-second time
  1456. @defunx set-time-type! time type
  1457. @defunx set-time-nanosecond! time nsec
  1458. @defunx set-time-second! time sec
  1459. Get or set the type, seconds or nanoseconds fields of a time object.
  1460. @code{set-time-type!} merely changes the field, it doesn't convert the
  1461. time value. For conversions, see @ref{SRFI-19 Time/Date conversions}.
  1462. @end defun
  1463. @defun copy-time time
  1464. Return a new time object, which is a copy of the given @var{time}.
  1465. @end defun
  1466. @defun current-time [type]
  1467. Return the current time of the given @var{type}. The default
  1468. @var{type} is @code{time-utc}.
  1469. Note that the name @code{current-time} conflicts with the Guile core
  1470. @code{current-time} function (@pxref{Time}). Applications wanting to
  1471. use both will need to use a different name for one of them.
  1472. @end defun
  1473. @defun time-resolution [type]
  1474. Return the resolution, in nanoseconds, of the given time @var{type}.
  1475. The default @var{type} is @code{time-utc}.
  1476. @end defun
  1477. @defun time<=? t1 t2
  1478. @defunx time<? t1 t2
  1479. @defunx time=? t1 t2
  1480. @defunx time>=? t1 t2
  1481. @defunx time>? t1 t2
  1482. Return @code{#t} or @code{#f} according to the respective relation
  1483. between time objects @var{t1} and @var{t2}. @var{t1} and @var{t2}
  1484. must be the same time type.
  1485. @end defun
  1486. @defun time-difference t1 t2
  1487. @defunx time-difference! t1 t2
  1488. Return a time object of type @code{time-duration} representing the
  1489. period between @var{t1} and @var{t2}. @var{t1} and @var{t2} must be
  1490. the same time type.
  1491. @code{time-difference} returns a new time object,
  1492. @code{time-difference!} may modify @var{t1} to form its return.
  1493. @end defun
  1494. @defun add-duration time duration
  1495. @defunx add-duration! time duration
  1496. @defunx subtract-duration time duration
  1497. @defunx subtract-duration! time duration
  1498. Return a time object which is @var{time} with the given @var{duration}
  1499. added or subtracted. @var{duration} must be a time object of type
  1500. @code{time-duration}.
  1501. @code{add-duration} and @code{subtract-duration} return a new time
  1502. object. @code{add-duration!} and @code{subtract-duration!} may modify
  1503. the given @var{time} to form their return.
  1504. @end defun
  1505. @node SRFI-19 Date
  1506. @subsubsection SRFI-19 Date
  1507. @cindex date
  1508. A @dfn{date} object represents a date in the Gregorian calendar and a
  1509. time of day on that date in some timezone.
  1510. The fields are year, month, day, hour, minute, second, nanoseconds and
  1511. timezone. A date object is immutable, its fields can be read but they
  1512. cannot be modified once the object is created.
  1513. @defun date? obj
  1514. Return @code{#t} if @var{obj} is a date object, or @code{#f} if not.
  1515. @end defun
  1516. @defun make-date nsecs seconds minutes hours date month year zone-offset
  1517. Create a new date object.
  1518. @c
  1519. @c FIXME: What can we say about the ranges of the values. The
  1520. @c current code looks it doesn't normalize, but expects then in their
  1521. @c usual range already.
  1522. @c
  1523. @end defun
  1524. @defun date-nanosecond date
  1525. Nanoseconds, 0 to 999999999.
  1526. @end defun
  1527. @defun date-second date
  1528. Seconds, 0 to 59, or 60 for a leap second. 60 is never seen when working
  1529. entirely within UTC, it's only when converting to or from TAI.
  1530. @end defun
  1531. @defun date-minute date
  1532. Minutes, 0 to 59.
  1533. @end defun
  1534. @defun date-hour date
  1535. Hour, 0 to 23.
  1536. @end defun
  1537. @defun date-day date
  1538. Day of the month, 1 to 31 (or less, according to the month).
  1539. @end defun
  1540. @defun date-month date
  1541. Month, 1 to 12.
  1542. @end defun
  1543. @defun date-year date
  1544. Year, eg.@: 2003. Dates B.C.@: are negative, eg.@: @math{-46} is 46
  1545. B.C. There is no year 0, year @math{-1} is followed by year 1.
  1546. @end defun
  1547. @defun date-zone-offset date
  1548. Time zone, an integer number of seconds east of Greenwich.
  1549. @end defun
  1550. @defun date-year-day date
  1551. Day of the year, starting from 1 for 1st January.
  1552. @end defun
  1553. @defun date-week-day date
  1554. Day of the week, starting from 0 for Sunday.
  1555. @end defun
  1556. @defun date-week-number date dstartw
  1557. Week of the year, ignoring a first partial week. @var{dstartw} is the
  1558. day of the week which is taken to start a week, 0 for Sunday, 1 for
  1559. Monday, etc.
  1560. @c
  1561. @c FIXME: The spec doesn't say whether numbering starts at 0 or 1.
  1562. @c The code looks like it's 0, if that's the correct intention.
  1563. @c
  1564. @end defun
  1565. @c The SRFI text doesn't actually give the default for tz-offset, but
  1566. @c the reference implementation has the local timezone and the
  1567. @c conversions functions all specify that, so it should be ok to
  1568. @c document it here.
  1569. @c
  1570. @defun current-date [tz-offset]
  1571. Return a date object representing the current date/time, in UTC offset
  1572. by @var{tz-offset}. @var{tz-offset} is seconds east of Greenwich and
  1573. defaults to the local timezone.
  1574. @end defun
  1575. @defun current-julian-day
  1576. @cindex julian day
  1577. Return the current Julian Day.
  1578. @end defun
  1579. @defun current-modified-julian-day
  1580. @cindex modified julian day
  1581. Return the current Modified Julian Day.
  1582. @end defun
  1583. @node SRFI-19 Time/Date conversions
  1584. @subsubsection SRFI-19 Time/Date conversions
  1585. @cindex time conversion
  1586. @cindex date conversion
  1587. @defun date->julian-day date
  1588. @defunx date->modified-julian-day date
  1589. @defunx date->time-monotonic date
  1590. @defunx date->time-tai date
  1591. @defunx date->time-utc date
  1592. @end defun
  1593. @defun julian-day->date jdn [tz-offset]
  1594. @defunx julian-day->time-monotonic jdn
  1595. @defunx julian-day->time-tai jdn
  1596. @defunx julian-day->time-utc jdn
  1597. @end defun
  1598. @defun modified-julian-day->date jdn [tz-offset]
  1599. @defunx modified-julian-day->time-monotonic jdn
  1600. @defunx modified-julian-day->time-tai jdn
  1601. @defunx modified-julian-day->time-utc jdn
  1602. @end defun
  1603. @defun time-monotonic->date time [tz-offset]
  1604. @defunx time-monotonic->time-tai time
  1605. @defunx time-monotonic->time-tai! time
  1606. @defunx time-monotonic->time-utc time
  1607. @defunx time-monotonic->time-utc! time
  1608. @end defun
  1609. @defun time-tai->date time [tz-offset]
  1610. @defunx time-tai->julian-day time
  1611. @defunx time-tai->modified-julian-day time
  1612. @defunx time-tai->time-monotonic time
  1613. @defunx time-tai->time-monotonic! time
  1614. @defunx time-tai->time-utc time
  1615. @defunx time-tai->time-utc! time
  1616. @end defun
  1617. @defun time-utc->date time [tz-offset]
  1618. @defunx time-utc->julian-day time
  1619. @defunx time-utc->modified-julian-day time
  1620. @defunx time-utc->time-monotonic time
  1621. @defunx time-utc->time-monotonic! time
  1622. @defunx time-utc->time-tai time
  1623. @defunx time-utc->time-tai! time
  1624. @sp 1
  1625. Convert between dates, times and days of the respective types. For
  1626. instance @code{time-tai->time-utc} accepts a @var{time} object of type
  1627. @code{time-tai} and returns an object of type @code{time-utc}.
  1628. The @code{!} variants may modify their @var{time} argument to form
  1629. their return. The plain functions create a new object.
  1630. For conversions to dates, @var{tz-offset} is seconds east of
  1631. Greenwich. The default is the local timezone, at the given time, as
  1632. provided by the system, using @code{localtime} (@pxref{Time}).
  1633. On 32-bit systems, @code{localtime} is limited to a 32-bit
  1634. @code{time_t}, so a default @var{tz-offset} is only available for
  1635. times between Dec 1901 and Jan 2038. For prior dates an application
  1636. might like to use the value in 1902, though some locations have zone
  1637. changes prior to that. For future dates an application might like to
  1638. assume today's rules extend indefinitely. But for correct daylight
  1639. savings transitions it will be necessary to take an offset for the
  1640. same day and time but a year in range and which has the same starting
  1641. weekday and same leap/non-leap (to support rules like last Sunday in
  1642. October).
  1643. @end defun
  1644. @node SRFI-19 Date to string
  1645. @subsubsection SRFI-19 Date to string
  1646. @cindex date to string
  1647. @cindex string, from date
  1648. @defun date->string date [format]
  1649. Convert a date to a string under the control of a format.
  1650. @var{format} should be a string containing @samp{~} escapes, which
  1651. will be expanded as per the following conversion table. The default
  1652. @var{format} is @samp{~c}, a locale-dependent date and time.
  1653. Many of these conversion characters are the same as POSIX
  1654. @code{strftime} (@pxref{Time}), but there are some extras and some
  1655. variations.
  1656. @multitable {MMMM} {MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM}
  1657. @item @nicode{~~} @tab literal ~
  1658. @item @nicode{~a} @tab locale abbreviated weekday, eg.@: @samp{Sun}
  1659. @item @nicode{~A} @tab locale full weekday, eg.@: @samp{Sunday}
  1660. @item @nicode{~b} @tab locale abbreviated month, eg.@: @samp{Jan}
  1661. @item @nicode{~B} @tab locale full month, eg.@: @samp{January}
  1662. @item @nicode{~c} @tab locale date and time, eg.@: @*
  1663. @samp{Fri Jul 14 20:28:42-0400 2000}
  1664. @item @nicode{~d} @tab day of month, zero padded, @samp{01} to @samp{31}
  1665. @c Spec says d/m/y, reference implementation says m/d/y.
  1666. @c Apparently the reference code was the intention, but would like to
  1667. @c see an errata published for the spec before contradicting it here.
  1668. @c
  1669. @c @item @nicode{~D} @tab date @nicode{~d/~m/~y}
  1670. @item @nicode{~e} @tab day of month, blank padded, @samp{ 1} to @samp{31}
  1671. @item @nicode{~f} @tab seconds and fractional seconds,
  1672. with locale decimal point, eg.@: @samp{5.2}
  1673. @item @nicode{~h} @tab same as @nicode{~b}
  1674. @item @nicode{~H} @tab hour, 24-hour clock, zero padded, @samp{00} to @samp{23}
  1675. @item @nicode{~I} @tab hour, 12-hour clock, zero padded, @samp{01} to @samp{12}
  1676. @item @nicode{~j} @tab day of year, zero padded, @samp{001} to @samp{366}
  1677. @item @nicode{~k} @tab hour, 24-hour clock, blank padded, @samp{ 0} to @samp{23}
  1678. @item @nicode{~l} @tab hour, 12-hour clock, blank padded, @samp{ 1} to @samp{12}
  1679. @item @nicode{~m} @tab month, zero padded, @samp{01} to @samp{12}
  1680. @item @nicode{~M} @tab minute, zero padded, @samp{00} to @samp{59}
  1681. @item @nicode{~n} @tab newline
  1682. @item @nicode{~N} @tab nanosecond, zero padded, @samp{000000000} to @samp{999999999}
  1683. @item @nicode{~p} @tab locale AM or PM
  1684. @item @nicode{~r} @tab time, 12 hour clock, @samp{~I:~M:~S ~p}
  1685. @item @nicode{~s} @tab number of full seconds since ``the epoch'' in UTC
  1686. @item @nicode{~S} @tab second, zero padded @samp{00} to @samp{60} @*
  1687. (usual limit is 59, 60 is a leap second)
  1688. @item @nicode{~t} @tab horizontal tab character
  1689. @item @nicode{~T} @tab time, 24 hour clock, @samp{~H:~M:~S}
  1690. @item @nicode{~U} @tab week of year, Sunday first day of week,
  1691. @samp{00} to @samp{52}
  1692. @item @nicode{~V} @tab week of year, Monday first day of week,
  1693. @samp{01} to @samp{53}
  1694. @item @nicode{~w} @tab day of week, 0 for Sunday, @samp{0} to @samp{6}
  1695. @item @nicode{~W} @tab week of year, Monday first day of week,
  1696. @samp{00} to @samp{52}
  1697. @c The spec has ~x as an apparent duplicate of ~W, and ~X as a locale
  1698. @c date. The reference code has ~x as the locale date and ~X as a
  1699. @c locale time. The rule is apparently that the code should be
  1700. @c believed, but would like to see an errata for the spec before
  1701. @c contradicting it here.
  1702. @c
  1703. @c @item @nicode{~x} @tab week of year, Monday as first day of week,
  1704. @c @samp{00} to @samp{53}
  1705. @c @item @nicode{~X} @tab locale date, eg.@: @samp{07/31/00}
  1706. @item @nicode{~y} @tab year, two digits, @samp{00} to @samp{99}
  1707. @item @nicode{~Y} @tab year, full, eg.@: @samp{2003}
  1708. @item @nicode{~z} @tab time zone, RFC-822 style
  1709. @item @nicode{~Z} @tab time zone symbol (not currently implemented)
  1710. @item @nicode{~1} @tab ISO-8601 date, @samp{~Y-~m-~d}
  1711. @item @nicode{~2} @tab ISO-8601 time+zone, @samp{~k:~M:~S~z}
  1712. @item @nicode{~3} @tab ISO-8601 time, @samp{~k:~M:~S}
  1713. @item @nicode{~4} @tab ISO-8601 date/time+zone, @samp{~Y-~m-~dT~k:~M:~S~z}
  1714. @item @nicode{~5} @tab ISO-8601 date/time, @samp{~Y-~m-~dT~k:~M:~S}
  1715. @end multitable
  1716. @end defun
  1717. Conversions @samp{~D}, @samp{~x} and @samp{~X} are not currently
  1718. described here, since the specification and reference implementation
  1719. differ.
  1720. Currently Guile doesn't implement any localizations for the above, all
  1721. outputs are in English, and the @samp{~c} conversion is POSIX
  1722. @code{ctime} style @samp{~a ~b ~d ~H:~M:~S~z ~Y}. This may change in
  1723. the future.
  1724. @node SRFI-19 String to date
  1725. @subsubsection SRFI-19 String to date
  1726. @cindex string to date
  1727. @cindex date, from string
  1728. @c FIXME: Can we say what happens when an incomplete date is
  1729. @c converted? Ie. fields left as 0, or what? The spec seems to be
  1730. @c silent on this.
  1731. @defun string->date input template
  1732. Convert an @var{input} string to a date under the control of a
  1733. @var{template} string. Return a newly created date object.
  1734. Literal characters in @var{template} must match characters in
  1735. @var{input} and @samp{~} escapes must match the input forms described
  1736. in the table below. ``Skip to'' means characters up to one of the
  1737. given type are ignored, or ``no skip'' for no skipping. ``Read'' is
  1738. what's then read, and ``Set'' is the field affected in the date
  1739. object.
  1740. For example @samp{~Y} skips input characters until a digit is reached,
  1741. at which point it expects a year and stores that to the year field of
  1742. the date.
  1743. @multitable {MMMM} {@nicode{char-alphabetic?}} {MMMMMMMMMMMMMMMMMMMMMMMMM} {@nicode{date-zone-offset}}
  1744. @item
  1745. @tab Skip to
  1746. @tab Read
  1747. @tab Set
  1748. @item @nicode{~~}
  1749. @tab no skip
  1750. @tab literal ~
  1751. @tab nothing
  1752. @item @nicode{~a}
  1753. @tab @nicode{char-alphabetic?}
  1754. @tab locale abbreviated weekday name
  1755. @tab nothing
  1756. @item @nicode{~A}
  1757. @tab @nicode{char-alphabetic?}
  1758. @tab locale full weekday name
  1759. @tab nothing
  1760. @c Note that the SRFI spec says that ~b and ~B don't set anything,
  1761. @c but that looks like a mistake. The reference implementation sets
  1762. @c the month field, which seems sensible and is what we describe
  1763. @c here.
  1764. @item @nicode{~b}
  1765. @tab @nicode{char-alphabetic?}
  1766. @tab locale abbreviated month name
  1767. @tab @nicode{date-month}
  1768. @item @nicode{~B}
  1769. @tab @nicode{char-alphabetic?}
  1770. @tab locale full month name
  1771. @tab @nicode{date-month}
  1772. @item @nicode{~d}
  1773. @tab @nicode{char-numeric?}
  1774. @tab day of month
  1775. @tab @nicode{date-day}
  1776. @item @nicode{~e}
  1777. @tab no skip
  1778. @tab day of month, blank padded
  1779. @tab @nicode{date-day}
  1780. @item @nicode{~h}
  1781. @tab same as @samp{~b}
  1782. @item @nicode{~H}
  1783. @tab @nicode{char-numeric?}
  1784. @tab hour
  1785. @tab @nicode{date-hour}
  1786. @item @nicode{~k}
  1787. @tab no skip
  1788. @tab hour, blank padded
  1789. @tab @nicode{date-hour}
  1790. @item @nicode{~m}
  1791. @tab @nicode{char-numeric?}
  1792. @tab month
  1793. @tab @nicode{date-month}
  1794. @item @nicode{~M}
  1795. @tab @nicode{char-numeric?}
  1796. @tab minute
  1797. @tab @nicode{date-minute}
  1798. @item @nicode{~S}
  1799. @tab @nicode{char-numeric?}
  1800. @tab second
  1801. @tab @nicode{date-second}
  1802. @item @nicode{~y}
  1803. @tab no skip
  1804. @tab 2-digit year
  1805. @tab @nicode{date-year} within 50 years
  1806. @item @nicode{~Y}
  1807. @tab @nicode{char-numeric?}
  1808. @tab year
  1809. @tab @nicode{date-year}
  1810. @item @nicode{~z}
  1811. @tab no skip
  1812. @tab time zone
  1813. @tab date-zone-offset
  1814. @end multitable
  1815. Notice that the weekday matching forms don't affect the date object
  1816. returned, instead the weekday will be derived from the day, month and
  1817. year.
  1818. Currently Guile doesn't implement any localizations for the above,
  1819. month and weekday names are always expected in English. This may
  1820. change in the future.
  1821. @end defun
  1822. @node SRFI-26
  1823. @subsection SRFI-26 - specializing parameters
  1824. @cindex SRFI-26
  1825. @cindex parameter specialize
  1826. @cindex argument specialize
  1827. @cindex specialize parameter
  1828. This SRFI provides a syntax for conveniently specializing selected
  1829. parameters of a function. It can be used with,
  1830. @example
  1831. (use-modules (srfi srfi-26))
  1832. @end example
  1833. @deffn {library syntax} cut slot @dots{}
  1834. @deffnx {library syntax} cute slot @dots{}
  1835. Return a new procedure which will make a call (@var{slot} @dots{}) but
  1836. with selected parameters specialized to given expressions.
  1837. An example will illustrate the idea. The following is a
  1838. specialization of @code{write}, sending output to
  1839. @code{my-output-port},
  1840. @example
  1841. (cut write <> my-output-port)
  1842. @result{}
  1843. (lambda (obj) (write obj my-output-port))
  1844. @end example
  1845. The special symbol @code{<>} indicates a slot to be filled by an
  1846. argument to the new procedure. @code{my-output-port} on the other
  1847. hand is an expression to be evaluated and passed, ie.@: it specializes
  1848. the behaviour of @code{write}.
  1849. @table @nicode
  1850. @item <>
  1851. A slot to be filled by an argument from the created procedure.
  1852. Arguments are assigned to @code{<>} slots in the order they appear in
  1853. the @code{cut} form, there's no way to re-arrange arguments.
  1854. The first argument to @code{cut} is usually a procedure (or expression
  1855. giving a procedure), but @code{<>} is allowed there too. For example,
  1856. @example
  1857. (cut <> 1 2 3)
  1858. @result{}
  1859. (lambda (proc) (proc 1 2 3))
  1860. @end example
  1861. @item <...>
  1862. A slot to be filled by all remaining arguments from the new procedure.
  1863. This can only occur at the end of a @code{cut} form.
  1864. For example, a procedure taking a variable number of arguments like
  1865. @code{max} but in addition enforcing a lower bound,
  1866. @example
  1867. (define my-lower-bound 123)
  1868. (cut max my-lower-bound <...>)
  1869. @result{}
  1870. (lambda arglist (apply max my-lower-bound arglist))
  1871. @end example
  1872. @end table
  1873. For @code{cut} the specializing expressions are evaluated each time
  1874. the new procedure is called. For @code{cute} they're evaluated just
  1875. once, when the new procedure is created. The name @code{cute} stands
  1876. for ``@code{cut} with evaluated arguments''. In all cases the
  1877. evaluations take place in an unspecified order.
  1878. The following illustrates the difference between @code{cut} and
  1879. @code{cute},
  1880. @example
  1881. (cut format <> "the time is ~s" (current-time))
  1882. @result{}
  1883. (lambda (port) (format port "the time is ~s" (current-time)))
  1884. (cute format <> "the time is ~s" (current-time))
  1885. @result{}
  1886. (let ((val (current-time)))
  1887. (lambda (port) (format port "the time is ~s" val))
  1888. @end example
  1889. (There's no provision for a mixture of @code{cut} and @code{cute}
  1890. where some expressions would be evaluated every time but others
  1891. evaluated only once.)
  1892. @code{cut} is really just a shorthand for the sort of @code{lambda}
  1893. forms shown in the above examples. But notice @code{cut} avoids the
  1894. need to name unspecialized parameters, and is more compact. Use in
  1895. functional programming style or just with @code{map}, @code{for-each}
  1896. or similar is typical.
  1897. @example
  1898. (map (cut * 2 <>) '(1 2 3 4))
  1899. (for-each (cut write <> my-port) my-list)
  1900. @end example
  1901. @end deffn
  1902. @node SRFI-31
  1903. @subsection SRFI-31 - A special form `rec' for recursive evaluation
  1904. @cindex SRFI-31
  1905. @cindex recursive expression
  1906. @findex rec
  1907. SRFI-31 defines a special form that can be used to create
  1908. self-referential expressions more conveniently. The syntax is as
  1909. follows:
  1910. @example
  1911. @group
  1912. <rec expression> --> (rec <variable> <expression>)
  1913. <rec expression> --> (rec (<variable>+) <body>)
  1914. @end group
  1915. @end example
  1916. The first syntax can be used to create self-referential expressions,
  1917. for example:
  1918. @lisp
  1919. guile> (define tmp (rec ones (cons 1 (delay ones))))
  1920. @end lisp
  1921. The second syntax can be used to create anonymous recursive functions:
  1922. @lisp
  1923. guile> (define tmp (rec (display-n item n)
  1924. (if (positive? n)
  1925. (begin (display n) (display-n (- n 1))))))
  1926. guile> (tmp 42 3)
  1927. 424242
  1928. guile>
  1929. @end lisp
  1930. @node SRFI-34
  1931. @subsection SRFI-34 - Exception handling for programs
  1932. @cindex SRFI-34
  1933. Guile provides an implementation of
  1934. @uref{http://srfi.schemers.org/srfi-34/srfi-34.html, SRFI-34's exception
  1935. handling mechanisms} as an alternative to its own built-in mechanisms
  1936. (@pxref{Exceptions}). It can be made available as follows:
  1937. @lisp
  1938. (use-modules (srfi srfi-34))
  1939. @end lisp
  1940. @c FIXME: Document it.
  1941. @node SRFI-35
  1942. @subsection SRFI-35 - Conditions
  1943. @cindex SRFI-35
  1944. @cindex conditions
  1945. @cindex exceptions
  1946. @uref{http://srfi.schemers.org/srfi-35/srfi-35.html, SRFI-35} implements
  1947. @dfn{conditions}, a data structure akin to records designed to convey
  1948. information about exceptional conditions between parts of a program. It
  1949. is normally used in conjunction with SRFI-34's @code{raise}:
  1950. @lisp
  1951. (raise (condition (&message
  1952. (message "An error occurred"))))
  1953. @end lisp
  1954. Users can define @dfn{condition types} containing arbitrary information.
  1955. Condition types may inherit from one another. This allows the part of
  1956. the program that handles (or ``catches'') conditions to get accurate
  1957. information about the exceptional condition that arose.
  1958. SRFI-35 conditions are made available using:
  1959. @lisp
  1960. (use-modules (srfi srfi-35))
  1961. @end lisp
  1962. The procedures available to manipulate condition types are the
  1963. following:
  1964. @deffn {Scheme Procedure} make-condition-type id parent field-names
  1965. Return a new condition type named @var{id}, inheriting from
  1966. @var{parent}, and with the fields whose names are listed in
  1967. @var{field-names}. @var{field-names} must be a list of symbols and must
  1968. not contain names already used by @var{parent} or one of its supertypes.
  1969. @end deffn
  1970. @deffn {Scheme Procedure} condition-type? obj
  1971. Return true if @var{obj} is a condition type.
  1972. @end deffn
  1973. Conditions can be created and accessed with the following procedures:
  1974. @deffn {Scheme Procedure} make-condition type . field+value
  1975. Return a new condition of type @var{type} with fields initialized as
  1976. specified by @var{field+value}, a sequence of field names (symbols) and
  1977. values as in the following example:
  1978. @lisp
  1979. (let ((&ct (make-condition-type 'foo &condition '(a b c))))
  1980. (make-condition &ct 'a 1 'b 2 'c 3))
  1981. @end lisp
  1982. Note that all fields of @var{type} and its supertypes must be specified.
  1983. @end deffn
  1984. @deffn {Scheme Procedure} make-compound-condition . conditions
  1985. Return a new compound condition composed of @var{conditions}. The
  1986. returned condition has the type of each condition of @var{conditions}
  1987. (per @code{condition-has-type?}).
  1988. @end deffn
  1989. @deffn {Scheme Procedure} condition-has-type? c type
  1990. Return true if condition @var{c} has type @var{type}.
  1991. @end deffn
  1992. @deffn {Scheme Procedure} condition-ref c field-name
  1993. Return the value of the field named @var{field-name} from condition @var{c}.
  1994. If @var{c} is a compound condition and several underlying condition
  1995. types contain a field named @var{field-name}, then the value of the
  1996. first such field is returned, using the order in which conditions were
  1997. passed to @var{make-compound-condition}.
  1998. @end deffn
  1999. @deffn {Scheme Procedure} extract-condition c type
  2000. Return a condition of condition type @var{type} with the field values
  2001. specified by @var{c}.
  2002. If @var{c} is a compound condition, extract the field values from the
  2003. subcondition belonging to @var{type} that appeared first in the call to
  2004. @code{make-compound-condition} that created the the condition.
  2005. @end deffn
  2006. Convenience macros are also available to create condition types and
  2007. conditions.
  2008. @deffn {library syntax} define-condition-type type supertype predicate field-spec...
  2009. Define a new condition type named @var{type} that inherits from
  2010. @var{supertype}. In addition, bind @var{predicate} to a type predicate
  2011. that returns true when passed a condition of type @var{type} or any of
  2012. its subtypes. @var{field-spec} must have the form @code{(field
  2013. accessor)} where @var{field} is the name of field of @var{type} and
  2014. @var{accessor} is the name of a procedure to access field @var{field} in
  2015. conditions of type @var{type}.
  2016. The example below defines condition type @code{&foo}, inheriting from
  2017. @code{&condition} with fields @code{a}, @code{b} and @code{c}:
  2018. @lisp
  2019. (define-condition-type &foo &condition
  2020. foo-condition?
  2021. (a foo-a)
  2022. (b foo-b)
  2023. (c foo-c))
  2024. @end lisp
  2025. @end deffn
  2026. @deffn {library syntax} condition type-field-bindings...
  2027. Return a new condition, or compound condition, initialized according to
  2028. @var{type-field-bindings}. Each @var{type-field-binding} must have the
  2029. form @code{(type field-specs...)}, where @var{type} is the name of a
  2030. variable bound to condition type; each @var{field-spec} must have the
  2031. form @code{(field-name value)} where @var{field-name} is a symbol
  2032. denoting the field being initialized to @var{value}. As for
  2033. @code{make-condition}, all fields must be specified.
  2034. The following example returns a simple condition:
  2035. @lisp
  2036. (condition (&message (message "An error occurred")))
  2037. @end lisp
  2038. The one below returns a compound condition:
  2039. @lisp
  2040. (condition (&message (message "An error occurred"))
  2041. (&serious))
  2042. @end lisp
  2043. @end deffn
  2044. Finally, SRFI-35 defines a several standard condition types.
  2045. @defvar &condition
  2046. This condition type is the root of all condition types. It has no
  2047. fields.
  2048. @end defvar
  2049. @defvar &message
  2050. A condition type that carries a message describing the nature of the
  2051. condition to humans.
  2052. @end defvar
  2053. @deffn {Scheme Procedure} message-condition? c
  2054. Return true if @var{c} is of type @code{&message} or one of its
  2055. subtypes.
  2056. @end deffn
  2057. @deffn {Scheme Procedure} condition-message c
  2058. Return the message associated with message condition @var{c}.
  2059. @end deffn
  2060. @defvar &serious
  2061. This type describes conditions serious enough that they cannot safely be
  2062. ignored. It has no fields.
  2063. @end defvar
  2064. @deffn {Scheme Procedure} serious-condition? c
  2065. Return true if @var{c} is of type @code{&serious} or one of its
  2066. subtypes.
  2067. @end deffn
  2068. @defvar &error
  2069. This condition describes errors, typically caused by something that has
  2070. gone wrong in the interaction of the program with the external world or
  2071. the user.
  2072. @end defvar
  2073. @deffn {Scheme Procedure} error? c
  2074. Return true if @var{c} is of type @code{&error} or one of its subtypes.
  2075. @end deffn
  2076. @node SRFI-37
  2077. @subsection SRFI-37 - args-fold
  2078. @cindex SRFI-37
  2079. This is a processor for GNU @code{getopt_long}-style program
  2080. arguments. It provides an alternative, less declarative interface
  2081. than @code{getopt-long} in @code{(ice-9 getopt-long)}
  2082. (@pxref{getopt-long,,The (ice-9 getopt-long) Module}). Unlike
  2083. @code{getopt-long}, it supports repeated options and any number of
  2084. short and long names per option. Access it with:
  2085. @lisp
  2086. (use-modules (srfi srfi-37))
  2087. @end lisp
  2088. @acronym{SRFI}-37 principally provides an @code{option} type and the
  2089. @code{args-fold} function. To use the library, create a set of
  2090. options with @code{option} and use it as a specification for invoking
  2091. @code{args-fold}.
  2092. Here is an example of a simple argument processor for the typical
  2093. @samp{--version} and @samp{--help} options, which returns a backwards
  2094. list of files given on the command line:
  2095. @lisp
  2096. (args-fold (cdr (program-arguments))
  2097. (let ((display-and-exit-proc
  2098. (lambda (msg)
  2099. (lambda (opt name arg loads)
  2100. (display msg) (quit)))))
  2101. (list (option '(#\v "version") #f #f
  2102. (display-and-exit-proc "Foo version 42.0\n"))
  2103. (option '(#\h "help") #f #f
  2104. (display-and-exit-proc
  2105. "Usage: foo scheme-file ..."))))
  2106. (lambda (opt name arg loads)
  2107. (error "Unrecognized option `~A'" name))
  2108. (lambda (op loads) (cons op loads))
  2109. '())
  2110. @end lisp
  2111. @deffn {Scheme Procedure} option names required-arg? optional-arg? processor
  2112. Return an object that specifies a single kind of program option.
  2113. @var{names} is a list of command-line option names, and should consist of
  2114. characters for traditional @code{getopt} short options and strings for
  2115. @code{getopt_long}-style long options.
  2116. @var{required-arg?} and @var{optional-arg?} are mutually exclusive;
  2117. one or both must be @code{#f}. If @var{required-arg?}, the option
  2118. must be followed by an argument on the command line, such as
  2119. @samp{--opt=value} for long options, or an error will be signalled.
  2120. If @var{optional-arg?}, an argument will be taken if available.
  2121. @var{processor} is a procedure that takes at least 3 arguments, called
  2122. when @code{args-fold} encounters the option: the containing option
  2123. object, the name used on the command line, and the argument given for
  2124. the option (or @code{#f} if none). The rest of the arguments are
  2125. @code{args-fold} ``seeds'', and the @var{processor} should return
  2126. seeds as well.
  2127. @end deffn
  2128. @deffn {Scheme Procedure} option-names opt
  2129. @deffnx {Scheme Procedure} option-required-arg? opt
  2130. @deffnx {Scheme Procedure} option-optional-arg? opt
  2131. @deffnx {Scheme Procedure} option-processor opt
  2132. Return the specified field of @var{opt}, an option object, as
  2133. described above for @code{option}.
  2134. @end deffn
  2135. @deffn {Scheme Procedure} args-fold args options unrecognized-option-proc operand-proc seeds @dots{}
  2136. Process @var{args}, a list of program arguments such as that returned
  2137. by @code{(cdr (program-arguments))}, in order against @var{options}, a
  2138. list of option objects as described above. All functions called take
  2139. the ``seeds'', or the last multiple-values as multiple arguments,
  2140. starting with @var{seeds}, and must return the new seeds. Return the
  2141. final seeds.
  2142. Call @code{unrecognized-option-proc}, which is like an option object's
  2143. processor, for any options not found in @var{options}.
  2144. Call @code{operand-proc} with any items on the command line that are
  2145. not named options. This includes arguments after @samp{--}. It is
  2146. called with the argument in question, as well as the seeds.
  2147. @end deffn
  2148. @node SRFI-39
  2149. @subsection SRFI-39 - Parameters
  2150. @cindex SRFI-39
  2151. @cindex parameter object
  2152. @tindex Parameter
  2153. This SRFI provides parameter objects, which implement dynamically
  2154. bound locations for values. The functions below are available from
  2155. @example
  2156. (use-modules (srfi srfi-39))
  2157. @end example
  2158. A parameter object is a procedure. Called with no arguments it
  2159. returns its value, called with one argument it sets the value.
  2160. @example
  2161. (define my-param (make-parameter 123))
  2162. (my-param) @result{} 123
  2163. (my-param 456)
  2164. (my-param) @result{} 456
  2165. @end example
  2166. The @code{parameterize} special form establishes new locations for
  2167. parameters, those new locations having effect within the dynamic scope
  2168. of the @code{parameterize} body. Leaving restores the previous
  2169. locations, or re-entering through a saved continuation will again use
  2170. the new locations.
  2171. @example
  2172. (parameterize ((my-param 789))
  2173. (my-param) @result{} 789
  2174. )
  2175. (my-param) @result{} 456
  2176. @end example
  2177. Parameters are like dynamically bound variables in other Lisp dialets.
  2178. They allow an application to establish parameter settings (as the name
  2179. suggests) just for the execution of a particular bit of code,
  2180. restoring when done. Examples of such parameters might be
  2181. case-sensitivity for a search, or a prompt for user input.
  2182. Global variables are not as good as parameter objects for this sort of
  2183. thing. Changes to them are visible to all threads, but in Guile
  2184. parameter object locations are per-thread, thereby truely limiting the
  2185. effect of @code{parameterize} to just its dynamic execution.
  2186. Passing arguments to functions is thread-safe, but that soon becomes
  2187. tedious when there's more than a few or when they need to pass down
  2188. through several layers of calls before reaching the point they should
  2189. affect. And introducing a new setting to existing code is often
  2190. easier with a parameter object than adding arguments.
  2191. @sp 1
  2192. @defun make-parameter init [converter]
  2193. Return a new parameter object, with initial value @var{init}.
  2194. A parameter object is a procedure. When called @code{(param)} it
  2195. returns its value, or a call @code{(param val)} sets its value. For
  2196. example,
  2197. @example
  2198. (define my-param (make-parameter 123))
  2199. (my-param) @result{} 123
  2200. (my-param 456)
  2201. (my-param) @result{} 456
  2202. @end example
  2203. If a @var{converter} is given, then a call @code{(@var{converter}
  2204. val)} is made for each value set, its return is the value stored.
  2205. Such a call is made for the @var{init} initial value too.
  2206. A @var{converter} allows values to be validated, or put into a
  2207. canonical form. For example,
  2208. @example
  2209. (define my-param (make-parameter 123
  2210. (lambda (val)
  2211. (if (not (number? val))
  2212. (error "must be a number"))
  2213. (inexact->exact val))))
  2214. (my-param 0.75)
  2215. (my-param) @result{} 3/4
  2216. @end example
  2217. @end defun
  2218. @deffn {library syntax} parameterize ((param value) @dots{}) body @dots{}
  2219. Establish a new dynamic scope with the given @var{param}s bound to new
  2220. locations and set to the given @var{value}s. @var{body} is evaluated
  2221. in that environment, the result is the return from the last form in
  2222. @var{body}.
  2223. Each @var{param} is an expression which is evaluated to get the
  2224. parameter object. Often this will just be the name of a variable
  2225. holding the object, but it can be anything that evaluates to a
  2226. parameter.
  2227. The @var{param} expressions and @var{value} expressions are all
  2228. evaluated before establishing the new dynamic bindings, and they're
  2229. evaluated in an unspecified order.
  2230. For example,
  2231. @example
  2232. (define prompt (make-parameter "Type something: "))
  2233. (define (get-input)
  2234. (display (prompt))
  2235. ...)
  2236. (parameterize ((prompt "Type a number: "))
  2237. (get-input)
  2238. ...)
  2239. @end example
  2240. @end deffn
  2241. @deffn {Parameter object} current-input-port [new-port]
  2242. @deffnx {Parameter object} current-output-port [new-port]
  2243. @deffnx {Parameter object} current-error-port [new-port]
  2244. This SRFI extends the core @code{current-input-port} and
  2245. @code{current-output-port}, making them parameter objects. The
  2246. Guile-specific @code{current-error-port} is extended too, for
  2247. consistency. (@pxref{Default Ports}.)
  2248. This is an upwardly compatible extension, a plain call like
  2249. @code{(current-input-port)} still returns the current input port, and
  2250. @code{set-current-input-port} can still be used. But the port can now
  2251. also be set with @code{(current-input-port my-port)} and bound
  2252. dynamically with @code{parameterize}.
  2253. @end deffn
  2254. @defun with-parameters* param-list value-list thunk
  2255. Establish a new dynamic scope, as per @code{parameterize} above,
  2256. taking parameters from @var{param-list} and corresponding values from
  2257. @var{values-list}. A call @code{(@var{thunk})} is made in the new
  2258. scope and the result from that @var{thunk} is the return from
  2259. @code{with-parameters*}.
  2260. This function is a Guile-specific addition to the SRFI, it's similar
  2261. to the core @code{with-fluids*} (@pxref{Fluids and Dynamic States}).
  2262. @end defun
  2263. @sp 1
  2264. Parameter objects are implemented using fluids (@pxref{Fluids and
  2265. Dynamic States}), so each dynamic state has it's own parameter
  2266. locations. That includes the separate locations when outside any
  2267. @code{parameterize} form. When a parameter is created it gets a
  2268. separate initial location in each dynamic state, all initialized to
  2269. the given @var{init} value.
  2270. As alluded to above, because each thread usually has a separate
  2271. dynamic state, each thread has it's own locations behind parameter
  2272. objects, and changes in one thread are not visible to any other. When
  2273. a new dynamic state or thread is created, the values of parameters in
  2274. the originating context are copied, into new locations.
  2275. SRFI-39 doesn't specify the interaction between parameter objects and
  2276. threads, so the threading behaviour described here should be regarded
  2277. as Guile-specific.
  2278. @node SRFI-55
  2279. @subsection SRFI-55 - Requiring Features
  2280. @cindex SRFI-55
  2281. SRFI-55 provides @code{require-extension} which is a portable
  2282. mechanism to load selected SRFI modules. This is implemented in the
  2283. Guile core, there's no module needed to get SRFI-55 itself.
  2284. @deffn {library syntax} require-extension clause@dots{}
  2285. Require each of the given @var{clause} features, throwing an error if
  2286. any are unavailable.
  2287. A @var{clause} is of the form @code{(@var{identifier} arg...)}. The
  2288. only @var{identifier} currently supported is @code{srfi} and the
  2289. arguments are SRFI numbers. For example to get SRFI-1 and SRFI-6,
  2290. @example
  2291. (require-extension (srfi 1 6))
  2292. @end example
  2293. @code{require-extension} can only be used at the top-level.
  2294. A Guile-specific program can simply @code{use-modules} to load SRFIs
  2295. not already in the core, @code{require-extension} is for programs
  2296. designed to be portable to other Scheme implementations.
  2297. @end deffn
  2298. @node SRFI-60
  2299. @subsection SRFI-60 - Integers as Bits
  2300. @cindex SRFI-60
  2301. @cindex integers as bits
  2302. @cindex bitwise logical
  2303. This SRFI provides various functions for treating integers as bits and
  2304. for bitwise manipulations. These functions can be obtained with,
  2305. @example
  2306. (use-modules (srfi srfi-60))
  2307. @end example
  2308. Integers are treated as infinite precision twos-complement, the same
  2309. as in the core logical functions (@pxref{Bitwise Operations}). And
  2310. likewise bit indexes start from 0 for the least significant bit. The
  2311. following functions in this SRFI are already in the Guile core,
  2312. @quotation
  2313. @code{logand},
  2314. @code{logior},
  2315. @code{logxor},
  2316. @code{lognot},
  2317. @code{logtest},
  2318. @code{logcount},
  2319. @code{integer-length},
  2320. @code{logbit?},
  2321. @code{ash}
  2322. @end quotation
  2323. @sp 1
  2324. @defun bitwise-and n1 ...
  2325. @defunx bitwise-ior n1 ...
  2326. @defunx bitwise-xor n1 ...
  2327. @defunx bitwise-not n
  2328. @defunx any-bits-set? j k
  2329. @defunx bit-set? index n
  2330. @defunx arithmetic-shift n count
  2331. @defunx bit-field n start end
  2332. @defunx bit-count n
  2333. Aliases for @code{logand}, @code{logior}, @code{logxor},
  2334. @code{lognot}, @code{logtest}, @code{logbit?}, @code{ash},
  2335. @code{bit-extract} and @code{logcount} respectively.
  2336. Note that the name @code{bit-count} conflicts with @code{bit-count} in
  2337. the core (@pxref{Bit Vectors}).
  2338. @end defun
  2339. @defun bitwise-if mask n1 n0
  2340. @defunx bitwise-merge mask n1 n0
  2341. Return an integer with bits selected from @var{n1} and @var{n0}
  2342. according to @var{mask}. Those bits where @var{mask} has 1s are taken
  2343. from @var{n1}, and those where @var{mask} has 0s are taken from
  2344. @var{n0}.
  2345. @example
  2346. (bitwise-if 3 #b0101 #b1010) @result{} 9
  2347. @end example
  2348. @end defun
  2349. @defun log2-binary-factors n
  2350. @defunx first-set-bit n
  2351. Return a count of how many factors of 2 are present in @var{n}. This
  2352. is also the bit index of the lowest 1 bit in @var{n}. If @var{n} is
  2353. 0, the return is @math{-1}.
  2354. @example
  2355. (log2-binary-factors 6) @result{} 1
  2356. (log2-binary-factors -8) @result{} 3
  2357. @end example
  2358. @end defun
  2359. @defun copy-bit index n newbit
  2360. Return @var{n} with the bit at @var{index} set according to
  2361. @var{newbit}. @var{newbit} should be @code{#t} to set the bit to 1,
  2362. or @code{#f} to set it to 0. Bits other than at @var{index} are
  2363. unchanged in the return.
  2364. @example
  2365. (copy-bit 1 #b0101 #t) @result{} 7
  2366. @end example
  2367. @end defun
  2368. @defun copy-bit-field n newbits start end
  2369. Return @var{n} with the bits from @var{start} (inclusive) to @var{end}
  2370. (exclusive) changed to the value @var{newbits}.
  2371. The least significant bit in @var{newbits} goes to @var{start}, the
  2372. next to @math{@var{start}+1}, etc. Anything in @var{newbits} past the
  2373. @var{end} given is ignored.
  2374. @example
  2375. (copy-bit-field #b10000 #b11 1 3) @result{} #b10110
  2376. @end example
  2377. @end defun
  2378. @defun rotate-bit-field n count start end
  2379. Return @var{n} with the bit field from @var{start} (inclusive) to
  2380. @var{end} (exclusive) rotated upwards by @var{count} bits.
  2381. @var{count} can be positive or negative, and it can be more than the
  2382. field width (it'll be reduced modulo the width).
  2383. @example
  2384. (rotate-bit-field #b0110 2 1 4) @result{} #b1010
  2385. @end example
  2386. @end defun
  2387. @defun reverse-bit-field n start end
  2388. Return @var{n} with the bits from @var{start} (inclusive) to @var{end}
  2389. (exclusive) reversed.
  2390. @example
  2391. (reverse-bit-field #b101001 2 4) @result{} #b100101
  2392. @end example
  2393. @end defun
  2394. @defun integer->list n [len]
  2395. Return bits from @var{n} in the form of a list of @code{#t} for 1 and
  2396. @code{#f} for 0. The least significant @var{len} bits are returned,
  2397. and the first list element is the most significant of those bits. If
  2398. @var{len} is not given, the default is @code{(integer-length @var{n})}
  2399. (@pxref{Bitwise Operations}).
  2400. @example
  2401. (integer->list 6) @result{} (#t #t #f)
  2402. (integer->list 1 4) @result{} (#f #f #f #t)
  2403. @end example
  2404. @end defun
  2405. @defun list->integer lst
  2406. @defunx booleans->integer bool@dots{}
  2407. Return an integer formed bitwise from the given @var{lst} list of
  2408. booleans, or for @code{booleans->integer} from the @var{bool}
  2409. arguments.
  2410. Each boolean is @code{#t} for a 1 and @code{#f} for a 0. The first
  2411. element becomes the most significant bit in the return.
  2412. @example
  2413. (list->integer '(#t #f #t #f)) @result{} 10
  2414. @end example
  2415. @end defun
  2416. @node SRFI-61
  2417. @subsection SRFI-61 - A more general @code{cond} clause
  2418. This SRFI extends RnRS @code{cond} to support test expressions that
  2419. return multiple values, as well as arbitrary definitions of test
  2420. success. SRFI 61 is implemented in the Guile core; there's no module
  2421. needed to get SRFI-61 itself. Extended @code{cond} is documented in
  2422. @ref{if cond case,, Simple Conditional Evaluation}.
  2423. @node SRFI-69
  2424. @subsection SRFI-69 - Basic hash tables
  2425. @cindex SRFI-69
  2426. This is a portable wrapper around Guile's built-in hash table and weak
  2427. table support. @xref{Hash Tables}, for information on that built-in
  2428. support. Above that, this hash-table interface provides association
  2429. of equality and hash functions with tables at creation time, so
  2430. variants of each function are not required, as well as a procedure
  2431. that takes care of most uses for Guile hash table handles, which this
  2432. SRFI does not provide as such.
  2433. Access it with:
  2434. @lisp
  2435. (use-modules (srfi srfi-69))
  2436. @end lisp
  2437. @menu
  2438. * SRFI-69 Creating hash tables::
  2439. * SRFI-69 Accessing table items::
  2440. * SRFI-69 Table properties::
  2441. * SRFI-69 Hash table algorithms::
  2442. @end menu
  2443. @node SRFI-69 Creating hash tables
  2444. @subsubsection Creating hash tables
  2445. @deffn {Scheme Procedure} make-hash-table [equal-proc hash-proc #:weak weakness start-size]
  2446. Create and answer a new hash table with @var{equal-proc} as the
  2447. equality function and @var{hash-proc} as the hashing function.
  2448. By default, @var{equal-proc} is @code{equal?}. It can be any
  2449. two-argument procedure, and should answer whether two keys are the
  2450. same for this table's purposes.
  2451. My default @var{hash-proc} assumes that @code{equal-proc} is no
  2452. coarser than @code{equal?} unless it is literally @code{string-ci=?}.
  2453. If provided, @var{hash-proc} should be a two-argument procedure that
  2454. takes a key and the current table size, and answers a reasonably good
  2455. hash integer between 0 (inclusive) and the size (exclusive).
  2456. @var{weakness} should be @code{#f} or a symbol indicating how ``weak''
  2457. the hash table is:
  2458. @table @code
  2459. @item #f
  2460. An ordinary non-weak hash table. This is the default.
  2461. @item key
  2462. When the key has no more non-weak references at GC, remove that entry.
  2463. @item value
  2464. When the value has no more non-weak references at GC, remove that
  2465. entry.
  2466. @item key-or-value
  2467. When either has no more non-weak references at GC, remove the
  2468. association.
  2469. @end table
  2470. As a legacy of the time when Guile couldn't grow hash tables,
  2471. @var{start-size} is an optional integer argument that specifies the
  2472. approximate starting size for the hash table, which will be rounded to
  2473. an algorithmically-sounder number.
  2474. @end deffn
  2475. By @dfn{coarser} than @code{equal?}, we mean that for all @var{x} and
  2476. @var{y} values where @code{(@var{equal-proc} @var{x} @var{y})},
  2477. @code{(equal? @var{x} @var{y})} as well. If that does not hold for
  2478. your @var{equal-proc}, you must provide a @var{hash-proc}.
  2479. In the case of weak tables, remember that @dfn{references} above
  2480. always refers to @code{eq?}-wise references. Just because you have a
  2481. reference to some string @code{"foo"} doesn't mean that an association
  2482. with key @code{"foo"} in a weak-key table @emph{won't} be collected;
  2483. it only counts as a reference if the two @code{"foo"}s are @code{eq?},
  2484. regardless of @var{equal-proc}. As such, it is usually only sensible
  2485. to use @code{eq?} and @code{hashq} as the equivalence and hash
  2486. functions for a weak table. @xref{Weak References}, for more
  2487. information on Guile's built-in weak table support.
  2488. @deffn {Scheme Procedure} alist->hash-table alist [equal-proc hash-proc #:weak weakness start-size]
  2489. As with @code{make-hash-table}, but initialize it with the
  2490. associations in @var{alist}. Where keys are repeated in @var{alist},
  2491. the leftmost association takes precedence.
  2492. @end deffn
  2493. @node SRFI-69 Accessing table items
  2494. @subsubsection Accessing table items
  2495. @deffn {Scheme Procedure} hash-table-ref table key [default-thunk]
  2496. @deffnx {Scheme Procedure} hash-table-ref/default table key default
  2497. Answer the value associated with @var{key} in @var{table}. If
  2498. @var{key} is not present, answer the result of invoking the thunk
  2499. @var{default-thunk}, which signals an error instead by default.
  2500. @code{hash-table-ref/default} is a variant that requires a third
  2501. argument, @var{default}, and answers @var{default} itself instead of
  2502. invoking it.
  2503. @end deffn
  2504. @deffn {Scheme Procedure} hash-table-set! table key new-value
  2505. Set @var{key} to @var{new-value} in @var{table}.
  2506. @end deffn
  2507. @deffn {Scheme Procedure} hash-table-delete! table key
  2508. Remove the association of @var{key} in @var{table}, if present. If
  2509. absent, do nothing.
  2510. @end deffn
  2511. @deffn {Scheme Procedure} hash-table-exists? table key
  2512. Answer whether @var{key} has an association in @var{table}.
  2513. @end deffn
  2514. @deffn {Scheme Procedure} hash-table-update! table key modifier [default-thunk]
  2515. @deffnx {Scheme Procedure} hash-table-update!/default table key modifier default
  2516. Replace @var{key}'s associated value in @var{table} by invoking
  2517. @var{modifier} with one argument, the old value.
  2518. If @var{key} is not present, and @var{default-thunk} is provided,
  2519. invoke it with no arguments to get the ``old value'' to be passed to
  2520. @var{modifier} as above. If @var{default-thunk} is not provided in
  2521. such a case, signal an error.
  2522. @code{hash-table-update!/default} is a variant that requires the
  2523. fourth argument, which is used directly as the ``old value'' rather
  2524. than as a thunk to be invoked to retrieve the ``old value''.
  2525. @end deffn
  2526. @node SRFI-69 Table properties
  2527. @subsubsection Table properties
  2528. @deffn {Scheme Procedure} hash-table-size table
  2529. Answer the number of associations in @var{table}. This is guaranteed
  2530. to run in constant time for non-weak tables.
  2531. @end deffn
  2532. @deffn {Scheme Procedure} hash-table-keys table
  2533. Answer an unordered list of the keys in @var{table}.
  2534. @end deffn
  2535. @deffn {Scheme Procedure} hash-table-values table
  2536. Answer an unordered list of the values in @var{table}.
  2537. @end deffn
  2538. @deffn {Scheme Procedure} hash-table-walk table proc
  2539. Invoke @var{proc} once for each association in @var{table}, passing
  2540. the key and value as arguments.
  2541. @end deffn
  2542. @deffn {Scheme Procedure} hash-table-fold table proc init
  2543. Invoke @code{(@var{proc} @var{key} @var{value} @var{previous})} for
  2544. each @var{key} and @var{value} in @var{table}, where @var{previous} is
  2545. the result of the previous invocation, using @var{init} as the first
  2546. @var{previous} value. Answer the final @var{proc} result.
  2547. @end deffn
  2548. @deffn {Scheme Procedure} hash-table->alist table
  2549. Answer an alist where each association in @var{table} is an
  2550. association in the result.
  2551. @end deffn
  2552. @node SRFI-69 Hash table algorithms
  2553. @subsubsection Hash table algorithms
  2554. Each hash table carries an @dfn{equivalence function} and a @dfn{hash
  2555. function}, used to implement key lookups. Beginning users should
  2556. follow the rules for consistency of the default @var{hash-proc}
  2557. specified above. Advanced users can use these to implement their own
  2558. equivalence and hash functions for specialized lookup semantics.
  2559. @deffn {Scheme Procedure} hash-table-equivalence-function hash-table
  2560. @deffnx {Scheme Procedure} hash-table-hash-function hash-table
  2561. Answer the equivalence and hash function of @var{hash-table}, respectively.
  2562. @end deffn
  2563. @deffn {Scheme Procedure} hash obj [size]
  2564. @deffnx {Scheme Procedure} string-hash obj [size]
  2565. @deffnx {Scheme Procedure} string-ci-hash obj [size]
  2566. @deffnx {Scheme Procedure} hash-by-identity obj [size]
  2567. Answer a hash value appropriate for equality predicate @code{equal?},
  2568. @code{string=?}, @code{string-ci=?}, and @code{eq?}, respectively.
  2569. @end deffn
  2570. @code{hash} is a backwards-compatible replacement for Guile's built-in
  2571. @code{hash}.
  2572. @node SRFI-88
  2573. @subsection SRFI-88 Keyword Objects
  2574. @cindex SRFI-88
  2575. @cindex keyword objects
  2576. @uref{http://srfi.schemers.org/srfi-88/srfi-88.html, SRFI-88} provides
  2577. @dfn{keyword objects}, which are equivalent to Guile's keywords
  2578. (@pxref{Keywords}). SRFI-88 keywords can be entered using the
  2579. @dfn{postfix keyword syntax}, which consists of an identifier followed
  2580. by @code{:} (@pxref{Reader options, @code{postfix} keyword syntax}).
  2581. SRFI-88 can be made available with:
  2582. @example
  2583. (use-modules (srfi srfi-88))
  2584. @end example
  2585. Doing so installs the right reader option for keyword syntax, using
  2586. @code{(read-set! keywords 'postfix)}. It also provides the procedures
  2587. described below.
  2588. @deffn {Scheme Procedure} keyword? obj
  2589. Return @code{#t} if @var{obj} is a keyword. This is the same procedure
  2590. as the same-named built-in procedure (@pxref{Keyword Procedures,
  2591. @code{keyword?}}).
  2592. @example
  2593. (keyword? foo:) @result{} #t
  2594. (keyword? 'foo:) @result{} #t
  2595. (keyword? "foo") @result{} #f
  2596. @end example
  2597. @end deffn
  2598. @deffn {Scheme Procedure} keyword->string kw
  2599. Return the name of @var{kw} as a string, i.e., without the trailing
  2600. colon. The returned string may not be modified, e.g., with
  2601. @code{string-set!}.
  2602. @example
  2603. (keyword->string foo:) @result{} "foo"
  2604. @end example
  2605. @end deffn
  2606. @deffn {Scheme Procedure} string->keyword str
  2607. Return the keyword object whose name is @var{str}.
  2608. @example
  2609. (keyword->string (string->keyword "a b c")) @result{} "a b c"
  2610. @end example
  2611. @end deffn
  2612. @c srfi-modules.texi ends here
  2613. @c Local Variables:
  2614. @c TeX-master: "guile.texi"
  2615. @c End: