api-compound.texi 149 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898
  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, 2005, 2006, 2007
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @page
  7. @node Compound Data Types
  8. @section Compound Data Types
  9. This chapter describes Guile's compound data types. By @dfn{compound}
  10. we mean that the primary purpose of these data types is to act as
  11. containers for other kinds of data (including other compound objects).
  12. For instance, a (non-uniform) vector with length 5 is a container that
  13. can hold five arbitrary Scheme objects.
  14. The various kinds of container object differ from each other in how
  15. their memory is allocated, how they are indexed, and how particular
  16. values can be looked up within them.
  17. @menu
  18. * Pairs:: Scheme's basic building block.
  19. * Lists:: Special list functions supported by Guile.
  20. * Vectors:: One-dimensional arrays of Scheme objects.
  21. * Uniform Numeric Vectors:: Vectors with elements of a single numeric type.
  22. * Bit Vectors:: Vectors of bits.
  23. * Generalized Vectors:: Treating all vector-like things uniformly.
  24. * Arrays:: Matrices, etc.
  25. * Records::
  26. * Structures::
  27. * Dictionary Types:: About dictionary types in general.
  28. * Association Lists:: List-based dictionaries.
  29. * Hash Tables:: Table-based dictionaries.
  30. @end menu
  31. @node Pairs
  32. @subsection Pairs
  33. @tpindex Pairs
  34. Pairs are used to combine two Scheme objects into one compound object.
  35. Hence the name: A pair stores a pair of objects.
  36. The data type @dfn{pair} is extremely important in Scheme, just like in
  37. any other Lisp dialect. The reason is that pairs are not only used to
  38. make two values available as one object, but that pairs are used for
  39. constructing lists of values. Because lists are so important in Scheme,
  40. they are described in a section of their own (@pxref{Lists}).
  41. Pairs can literally get entered in source code or at the REPL, in the
  42. so-called @dfn{dotted list} syntax. This syntax consists of an opening
  43. parentheses, the first element of the pair, a dot, the second element
  44. and a closing parentheses. The following example shows how a pair
  45. consisting of the two numbers 1 and 2, and a pair containing the symbols
  46. @code{foo} and @code{bar} can be entered. It is very important to write
  47. the whitespace before and after the dot, because otherwise the Scheme
  48. parser would not be able to figure out where to split the tokens.
  49. @lisp
  50. (1 . 2)
  51. (foo . bar)
  52. @end lisp
  53. But beware, if you want to try out these examples, you have to
  54. @dfn{quote} the expressions. More information about quotation is
  55. available in the section @ref{Expression Syntax}. The correct way
  56. to try these examples is as follows.
  57. @lisp
  58. '(1 . 2)
  59. @result{}
  60. (1 . 2)
  61. '(foo . bar)
  62. @result{}
  63. (foo . bar)
  64. @end lisp
  65. A new pair is made by calling the procedure @code{cons} with two
  66. arguments. Then the argument values are stored into a newly allocated
  67. pair, and the pair is returned. The name @code{cons} stands for
  68. "construct". Use the procedure @code{pair?} to test whether a
  69. given Scheme object is a pair or not.
  70. @rnindex cons
  71. @deffn {Scheme Procedure} cons x y
  72. @deffnx {C Function} scm_cons (x, y)
  73. Return a newly allocated pair whose car is @var{x} and whose
  74. cdr is @var{y}. The pair is guaranteed to be different (in the
  75. sense of @code{eq?}) from every previously existing object.
  76. @end deffn
  77. @rnindex pair?
  78. @deffn {Scheme Procedure} pair? x
  79. @deffnx {C Function} scm_pair_p (x)
  80. Return @code{#t} if @var{x} is a pair; otherwise return
  81. @code{#f}.
  82. @end deffn
  83. @deftypefn {C Function} int scm_is_pair (SCM x)
  84. Return 1 when @var{x} is a pair; otherwise return 0.
  85. @end deftypefn
  86. The two parts of a pair are traditionally called @dfn{car} and
  87. @dfn{cdr}. They can be retrieved with procedures of the same name
  88. (@code{car} and @code{cdr}), and can be modified with the procedures
  89. @code{set-car!} and @code{set-cdr!}. Since a very common operation in
  90. Scheme programs is to access the car of a car of a pair, or the car of
  91. the cdr of a pair, etc., the procedures called @code{caar},
  92. @code{cadr} and so on are also predefined.
  93. @rnindex car
  94. @rnindex cdr
  95. @deffn {Scheme Procedure} car pair
  96. @deffnx {Scheme Procedure} cdr pair
  97. @deffnx {C Function} scm_car (pair)
  98. @deffnx {C Function} scm_cdr (pair)
  99. Return the car or the cdr of @var{pair}, respectively.
  100. @end deffn
  101. @deftypefn {C Macro} SCM SCM_CAR (SCM pair)
  102. @deftypefnx {C Macro} SCM SCM_CDR (SCM pair)
  103. These two macros are the fastest way to access the car or cdr of a
  104. pair; they can be thought of as compiling into a single memory
  105. reference.
  106. These macros do no checking at all. The argument @var{pair} must be a
  107. valid pair.
  108. @end deftypefn
  109. @deffn {Scheme Procedure} cddr pair
  110. @deffnx {Scheme Procedure} cdar pair
  111. @deffnx {Scheme Procedure} cadr pair
  112. @deffnx {Scheme Procedure} caar pair
  113. @deffnx {Scheme Procedure} cdddr pair
  114. @deffnx {Scheme Procedure} cddar pair
  115. @deffnx {Scheme Procedure} cdadr pair
  116. @deffnx {Scheme Procedure} cdaar pair
  117. @deffnx {Scheme Procedure} caddr pair
  118. @deffnx {Scheme Procedure} cadar pair
  119. @deffnx {Scheme Procedure} caadr pair
  120. @deffnx {Scheme Procedure} caaar pair
  121. @deffnx {Scheme Procedure} cddddr pair
  122. @deffnx {Scheme Procedure} cdddar pair
  123. @deffnx {Scheme Procedure} cddadr pair
  124. @deffnx {Scheme Procedure} cddaar pair
  125. @deffnx {Scheme Procedure} cdaddr pair
  126. @deffnx {Scheme Procedure} cdadar pair
  127. @deffnx {Scheme Procedure} cdaadr pair
  128. @deffnx {Scheme Procedure} cdaaar pair
  129. @deffnx {Scheme Procedure} cadddr pair
  130. @deffnx {Scheme Procedure} caddar pair
  131. @deffnx {Scheme Procedure} cadadr pair
  132. @deffnx {Scheme Procedure} cadaar pair
  133. @deffnx {Scheme Procedure} caaddr pair
  134. @deffnx {Scheme Procedure} caadar pair
  135. @deffnx {Scheme Procedure} caaadr pair
  136. @deffnx {Scheme Procedure} caaaar pair
  137. @deffnx {C Function} scm_cddr (pair)
  138. @deffnx {C Function} scm_cdar (pair)
  139. @deffnx {C Function} scm_cadr (pair)
  140. @deffnx {C Function} scm_caar (pair)
  141. @deffnx {C Function} scm_cdddr (pair)
  142. @deffnx {C Function} scm_cddar (pair)
  143. @deffnx {C Function} scm_cdadr (pair)
  144. @deffnx {C Function} scm_cdaar (pair)
  145. @deffnx {C Function} scm_caddr (pair)
  146. @deffnx {C Function} scm_cadar (pair)
  147. @deffnx {C Function} scm_caadr (pair)
  148. @deffnx {C Function} scm_caaar (pair)
  149. @deffnx {C Function} scm_cddddr (pair)
  150. @deffnx {C Function} scm_cdddar (pair)
  151. @deffnx {C Function} scm_cddadr (pair)
  152. @deffnx {C Function} scm_cddaar (pair)
  153. @deffnx {C Function} scm_cdaddr (pair)
  154. @deffnx {C Function} scm_cdadar (pair)
  155. @deffnx {C Function} scm_cdaadr (pair)
  156. @deffnx {C Function} scm_cdaaar (pair)
  157. @deffnx {C Function} scm_cadddr (pair)
  158. @deffnx {C Function} scm_caddar (pair)
  159. @deffnx {C Function} scm_cadadr (pair)
  160. @deffnx {C Function} scm_cadaar (pair)
  161. @deffnx {C Function} scm_caaddr (pair)
  162. @deffnx {C Function} scm_caadar (pair)
  163. @deffnx {C Function} scm_caaadr (pair)
  164. @deffnx {C Function} scm_caaaar (pair)
  165. These procedures are compositions of @code{car} and @code{cdr}, where
  166. for example @code{caddr} could be defined by
  167. @lisp
  168. (define caddr (lambda (x) (car (cdr (cdr x)))))
  169. @end lisp
  170. @code{cadr}, @code{caddr} and @code{cadddr} pick out the second, third
  171. or fourth elements of a list, respectively. SRFI-1 provides the same
  172. under the names @code{second}, @code{third} and @code{fourth}
  173. (@pxref{SRFI-1 Selectors}).
  174. @end deffn
  175. @rnindex set-car!
  176. @deffn {Scheme Procedure} set-car! pair value
  177. @deffnx {C Function} scm_set_car_x (pair, value)
  178. Stores @var{value} in the car field of @var{pair}. The value returned
  179. by @code{set-car!} is unspecified.
  180. @end deffn
  181. @rnindex set-cdr!
  182. @deffn {Scheme Procedure} set-cdr! pair value
  183. @deffnx {C Function} scm_set_cdr_x (pair, value)
  184. Stores @var{value} in the cdr field of @var{pair}. The value returned
  185. by @code{set-cdr!} is unspecified.
  186. @end deffn
  187. @node Lists
  188. @subsection Lists
  189. @tpindex Lists
  190. A very important data type in Scheme---as well as in all other Lisp
  191. dialects---is the data type @dfn{list}.@footnote{Strictly speaking,
  192. Scheme does not have a real datatype @dfn{list}. Lists are made up of
  193. @dfn{chained pairs}, and only exist by definition---a list is a chain
  194. of pairs which looks like a list.}
  195. This is the short definition of what a list is:
  196. @itemize @bullet
  197. @item
  198. Either the empty list @code{()},
  199. @item
  200. or a pair which has a list in its cdr.
  201. @end itemize
  202. @c FIXME::martin: Describe the pair chaining in more detail.
  203. @c FIXME::martin: What is a proper, what an improper list?
  204. @c What is a circular list?
  205. @c FIXME::martin: Maybe steal some graphics from the Elisp reference
  206. @c manual?
  207. @menu
  208. * List Syntax:: Writing literal lists.
  209. * List Predicates:: Testing lists.
  210. * List Constructors:: Creating new lists.
  211. * List Selection:: Selecting from lists, getting their length.
  212. * Append/Reverse:: Appending and reversing lists.
  213. * List Modification:: Modifying existing lists.
  214. * List Searching:: Searching for list elements
  215. * List Mapping:: Applying procedures to lists.
  216. @end menu
  217. @node List Syntax
  218. @subsubsection List Read Syntax
  219. The syntax for lists is an opening parentheses, then all the elements of
  220. the list (separated by whitespace) and finally a closing
  221. parentheses.@footnote{Note that there is no separation character between
  222. the list elements, like a comma or a semicolon.}.
  223. @lisp
  224. (1 2 3) ; @r{a list of the numbers 1, 2 and 3}
  225. ("foo" bar 3.1415) ; @r{a string, a symbol and a real number}
  226. () ; @r{the empty list}
  227. @end lisp
  228. The last example needs a bit more explanation. A list with no elements,
  229. called the @dfn{empty list}, is special in some ways. It is used for
  230. terminating lists by storing it into the cdr of the last pair that makes
  231. up a list. An example will clear that up:
  232. @lisp
  233. (car '(1))
  234. @result{}
  235. 1
  236. (cdr '(1))
  237. @result{}
  238. ()
  239. @end lisp
  240. This example also shows that lists have to be quoted when written
  241. (@pxref{Expression Syntax}), because they would otherwise be
  242. mistakingly taken as procedure applications (@pxref{Simple
  243. Invocation}).
  244. @node List Predicates
  245. @subsubsection List Predicates
  246. Often it is useful to test whether a given Scheme object is a list or
  247. not. List-processing procedures could use this information to test
  248. whether their input is valid, or they could do different things
  249. depending on the datatype of their arguments.
  250. @rnindex list?
  251. @deffn {Scheme Procedure} list? x
  252. @deffnx {C Function} scm_list_p (x)
  253. Return @code{#t} iff @var{x} is a proper list, else @code{#f}.
  254. @end deffn
  255. The predicate @code{null?} is often used in list-processing code to
  256. tell whether a given list has run out of elements. That is, a loop
  257. somehow deals with the elements of a list until the list satisfies
  258. @code{null?}. Then, the algorithm terminates.
  259. @rnindex null?
  260. @deffn {Scheme Procedure} null? x
  261. @deffnx {C Function} scm_null_p (x)
  262. Return @code{#t} iff @var{x} is the empty list, else @code{#f}.
  263. @end deffn
  264. @deftypefn {C Function} int scm_is_null (SCM x)
  265. Return 1 when @var{x} is the empty list; otherwise return 0.
  266. @end deftypefn
  267. @node List Constructors
  268. @subsubsection List Constructors
  269. This section describes the procedures for constructing new lists.
  270. @code{list} simply returns a list where the elements are the arguments,
  271. @code{cons*} is similar, but the last argument is stored in the cdr of
  272. the last pair of the list.
  273. @c C Function scm_list(rest) used to be documented here, but it's a
  274. @c no-op since it does nothing but return the list the caller must
  275. @c have already created.
  276. @c
  277. @deffn {Scheme Procedure} list elem1 @dots{} elemN
  278. @deffnx {C Function} scm_list_1 (elem1)
  279. @deffnx {C Function} scm_list_2 (elem1, elem2)
  280. @deffnx {C Function} scm_list_3 (elem1, elem2, elem3)
  281. @deffnx {C Function} scm_list_4 (elem1, elem2, elem3, elem4)
  282. @deffnx {C Function} scm_list_5 (elem1, elem2, elem3, elem4, elem5)
  283. @deffnx {C Function} scm_list_n (elem1, @dots{}, elemN, @nicode{SCM_UNDEFINED})
  284. @rnindex list
  285. Return a new list containing elements @var{elem1} to @var{elemN}.
  286. @code{scm_list_n} takes a variable number of arguments, terminated by
  287. the special @code{SCM_UNDEFINED}. That final @code{SCM_UNDEFINED} is
  288. not included in the list. None of @var{elem1} to @var{elemN} can
  289. themselves be @code{SCM_UNDEFINED}, or @code{scm_list_n} will
  290. terminate at that point.
  291. @end deffn
  292. @c C Function scm_cons_star(arg1,rest) used to be documented here,
  293. @c but it's not really a useful interface, since it expects the
  294. @c caller to have already consed up all but the first argument
  295. @c already.
  296. @c
  297. @deffn {Scheme Procedure} cons* arg1 arg2 @dots{}
  298. Like @code{list}, but the last arg provides the tail of the
  299. constructed list, returning @code{(cons @var{arg1} (cons
  300. @var{arg2} (cons @dots{} @var{argn})))}. Requires at least one
  301. argument. If given one argument, that argument is returned as
  302. result. This function is called @code{list*} in some other
  303. Schemes and in Common LISP.
  304. @end deffn
  305. @deffn {Scheme Procedure} list-copy lst
  306. @deffnx {C Function} scm_list_copy (lst)
  307. Return a (newly-created) copy of @var{lst}.
  308. @end deffn
  309. @deffn {Scheme Procedure} make-list n [init]
  310. Create a list containing of @var{n} elements, where each element is
  311. initialized to @var{init}. @var{init} defaults to the empty list
  312. @code{()} if not given.
  313. @end deffn
  314. Note that @code{list-copy} only makes a copy of the pairs which make up
  315. the spine of the lists. The list elements are not copied, which means
  316. that modifying the elements of the new list also modifies the elements
  317. of the old list. On the other hand, applying procedures like
  318. @code{set-cdr!} or @code{delv!} to the new list will not alter the old
  319. list. If you also need to copy the list elements (making a deep copy),
  320. use the procedure @code{copy-tree} (@pxref{Copying}).
  321. @node List Selection
  322. @subsubsection List Selection
  323. These procedures are used to get some information about a list, or to
  324. retrieve one or more elements of a list.
  325. @rnindex length
  326. @deffn {Scheme Procedure} length lst
  327. @deffnx {C Function} scm_length (lst)
  328. Return the number of elements in list @var{lst}.
  329. @end deffn
  330. @deffn {Scheme Procedure} last-pair lst
  331. @deffnx {C Function} scm_last_pair (lst)
  332. Return the last pair in @var{lst}, signalling an error if
  333. @var{lst} is circular.
  334. @end deffn
  335. @rnindex list-ref
  336. @deffn {Scheme Procedure} list-ref list k
  337. @deffnx {C Function} scm_list_ref (list, k)
  338. Return the @var{k}th element from @var{list}.
  339. @end deffn
  340. @rnindex list-tail
  341. @deffn {Scheme Procedure} list-tail lst k
  342. @deffnx {Scheme Procedure} list-cdr-ref lst k
  343. @deffnx {C Function} scm_list_tail (lst, k)
  344. Return the "tail" of @var{lst} beginning with its @var{k}th element.
  345. The first element of the list is considered to be element 0.
  346. @code{list-tail} and @code{list-cdr-ref} are identical. It may help to
  347. think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,
  348. or returning the results of cdring @var{k} times down @var{lst}.
  349. @end deffn
  350. @deffn {Scheme Procedure} list-head lst k
  351. @deffnx {C Function} scm_list_head (lst, k)
  352. Copy the first @var{k} elements from @var{lst} into a new list, and
  353. return it.
  354. @end deffn
  355. @node Append/Reverse
  356. @subsubsection Append and Reverse
  357. @code{append} and @code{append!} are used to concatenate two or more
  358. lists in order to form a new list. @code{reverse} and @code{reverse!}
  359. return lists with the same elements as their arguments, but in reverse
  360. order. The procedure variants with an @code{!} directly modify the
  361. pairs which form the list, whereas the other procedures create new
  362. pairs. This is why you should be careful when using the side-effecting
  363. variants.
  364. @rnindex append
  365. @deffn {Scheme Procedure} append lst1 @dots{} lstN
  366. @deffnx {Scheme Procedure} append! lst1 @dots{} lstN
  367. @deffnx {C Function} scm_append (lstlst)
  368. @deffnx {C Function} scm_append_x (lstlst)
  369. Return a list comprising all the elements of lists @var{lst1} to
  370. @var{lstN}.
  371. @lisp
  372. (append '(x) '(y)) @result{} (x y)
  373. (append '(a) '(b c d)) @result{} (a b c d)
  374. (append '(a (b)) '((c))) @result{} (a (b) (c))
  375. @end lisp
  376. The last argument @var{lstN} may actually be any object; an improper
  377. list results if the last argument is not a proper list.
  378. @lisp
  379. (append '(a b) '(c . d)) @result{} (a b c . d)
  380. (append '() 'a) @result{} a
  381. @end lisp
  382. @code{append} doesn't modify the given lists, but the return may share
  383. structure with the final @var{lstN}. @code{append!} modifies the
  384. given lists to form its return.
  385. For @code{scm_append} and @code{scm_append_x}, @var{lstlst} is a list
  386. of the list operands @var{lst1} @dots{} @var{lstN}. That @var{lstlst}
  387. itself is not modified or used in the return.
  388. @end deffn
  389. @rnindex reverse
  390. @deffn {Scheme Procedure} reverse lst
  391. @deffnx {Scheme Procedure} reverse! lst [newtail]
  392. @deffnx {C Function} scm_reverse (lst)
  393. @deffnx {C Function} scm_reverse_x (lst, newtail)
  394. Return a list comprising the elements of @var{lst}, in reverse order.
  395. @code{reverse} constructs a new list, @code{reverse!} modifies
  396. @var{lst} in constructing its return.
  397. For @code{reverse!}, the optional @var{newtail} is appended to to the
  398. result. @var{newtail} isn't reversed, it simply becomes the list
  399. tail. For @code{scm_reverse_x}, the @var{newtail} parameter is
  400. mandatory, but can be @code{SCM_EOL} if no further tail is required.
  401. @end deffn
  402. @node List Modification
  403. @subsubsection List Modification
  404. The following procedures modify an existing list, either by changing
  405. elements of the list, or by changing the list structure itself.
  406. @deffn {Scheme Procedure} list-set! list k val
  407. @deffnx {C Function} scm_list_set_x (list, k, val)
  408. Set the @var{k}th element of @var{list} to @var{val}.
  409. @end deffn
  410. @deffn {Scheme Procedure} list-cdr-set! list k val
  411. @deffnx {C Function} scm_list_cdr_set_x (list, k, val)
  412. Set the @var{k}th cdr of @var{list} to @var{val}.
  413. @end deffn
  414. @deffn {Scheme Procedure} delq item lst
  415. @deffnx {C Function} scm_delq (item, lst)
  416. Return a newly-created copy of @var{lst} with elements
  417. @code{eq?} to @var{item} removed. This procedure mirrors
  418. @code{memq}: @code{delq} compares elements of @var{lst} against
  419. @var{item} with @code{eq?}.
  420. @end deffn
  421. @deffn {Scheme Procedure} delv item lst
  422. @deffnx {C Function} scm_delv (item, lst)
  423. Return a newly-created copy of @var{lst} with elements
  424. @code{eqv?} to @var{item} removed. This procedure mirrors
  425. @code{memv}: @code{delv} compares elements of @var{lst} against
  426. @var{item} with @code{eqv?}.
  427. @end deffn
  428. @deffn {Scheme Procedure} delete item lst
  429. @deffnx {C Function} scm_delete (item, lst)
  430. Return a newly-created copy of @var{lst} with elements
  431. @code{equal?} to @var{item} removed. This procedure mirrors
  432. @code{member}: @code{delete} compares elements of @var{lst}
  433. against @var{item} with @code{equal?}.
  434. See also SRFI-1 which has an extended @code{delete} (@ref{SRFI-1
  435. Deleting}), and also an @code{lset-difference} which can delete
  436. multiple @var{item}s in one call (@ref{SRFI-1 Set Operations}).
  437. @end deffn
  438. @deffn {Scheme Procedure} delq! item lst
  439. @deffnx {Scheme Procedure} delv! item lst
  440. @deffnx {Scheme Procedure} delete! item lst
  441. @deffnx {C Function} scm_delq_x (item, lst)
  442. @deffnx {C Function} scm_delv_x (item, lst)
  443. @deffnx {C Function} scm_delete_x (item, lst)
  444. These procedures are destructive versions of @code{delq}, @code{delv}
  445. and @code{delete}: they modify the pointers in the existing @var{lst}
  446. rather than creating a new list. Caveat evaluator: Like other
  447. destructive list functions, these functions cannot modify the binding of
  448. @var{lst}, and so cannot be used to delete the first element of
  449. @var{lst} destructively.
  450. @end deffn
  451. @deffn {Scheme Procedure} delq1! item lst
  452. @deffnx {C Function} scm_delq1_x (item, lst)
  453. Like @code{delq!}, but only deletes the first occurrence of
  454. @var{item} from @var{lst}. Tests for equality using
  455. @code{eq?}. See also @code{delv1!} and @code{delete1!}.
  456. @end deffn
  457. @deffn {Scheme Procedure} delv1! item lst
  458. @deffnx {C Function} scm_delv1_x (item, lst)
  459. Like @code{delv!}, but only deletes the first occurrence of
  460. @var{item} from @var{lst}. Tests for equality using
  461. @code{eqv?}. See also @code{delq1!} and @code{delete1!}.
  462. @end deffn
  463. @deffn {Scheme Procedure} delete1! item lst
  464. @deffnx {C Function} scm_delete1_x (item, lst)
  465. Like @code{delete!}, but only deletes the first occurrence of
  466. @var{item} from @var{lst}. Tests for equality using
  467. @code{equal?}. See also @code{delq1!} and @code{delv1!}.
  468. @end deffn
  469. @deffn {Scheme Procedure} filter pred lst
  470. @deffnx {Scheme Procedure} filter! pred lst
  471. Return a list containing all elements from @var{lst} which satisfy the
  472. predicate @var{pred}. The elements in the result list have the same
  473. order as in @var{lst}. The order in which @var{pred} is applied to
  474. the list elements is not specified.
  475. @code{filter} does not change @var{lst}, but the result may share a
  476. tail with it. @code{filter!} may modify @var{lst} to construct its
  477. return.
  478. @end deffn
  479. @node List Searching
  480. @subsubsection List Searching
  481. The following procedures search lists for particular elements. They use
  482. different comparison predicates for comparing list elements with the
  483. object to be searched. When they fail, they return @code{#f}, otherwise
  484. they return the sublist whose car is equal to the search object, where
  485. equality depends on the equality predicate used.
  486. @rnindex memq
  487. @deffn {Scheme Procedure} memq x lst
  488. @deffnx {C Function} scm_memq (x, lst)
  489. Return the first sublist of @var{lst} whose car is @code{eq?}
  490. to @var{x} where the sublists of @var{lst} are the non-empty
  491. lists returned by @code{(list-tail @var{lst} @var{k})} for
  492. @var{k} less than the length of @var{lst}. If @var{x} does not
  493. occur in @var{lst}, then @code{#f} (not the empty list) is
  494. returned.
  495. @end deffn
  496. @rnindex memv
  497. @deffn {Scheme Procedure} memv x lst
  498. @deffnx {C Function} scm_memv (x, lst)
  499. Return the first sublist of @var{lst} whose car is @code{eqv?}
  500. to @var{x} where the sublists of @var{lst} are the non-empty
  501. lists returned by @code{(list-tail @var{lst} @var{k})} for
  502. @var{k} less than the length of @var{lst}. If @var{x} does not
  503. occur in @var{lst}, then @code{#f} (not the empty list) is
  504. returned.
  505. @end deffn
  506. @rnindex member
  507. @deffn {Scheme Procedure} member x lst
  508. @deffnx {C Function} scm_member (x, lst)
  509. Return the first sublist of @var{lst} whose car is
  510. @code{equal?} to @var{x} where the sublists of @var{lst} are
  511. the non-empty lists returned by @code{(list-tail @var{lst}
  512. @var{k})} for @var{k} less than the length of @var{lst}. If
  513. @var{x} does not occur in @var{lst}, then @code{#f} (not the
  514. empty list) is returned.
  515. See also SRFI-1 which has an extended @code{member} function
  516. (@ref{SRFI-1 Searching}).
  517. @end deffn
  518. @node List Mapping
  519. @subsubsection List Mapping
  520. List processing is very convenient in Scheme because the process of
  521. iterating over the elements of a list can be highly abstracted. The
  522. procedures in this section are the most basic iterating procedures for
  523. lists. They take a procedure and one or more lists as arguments, and
  524. apply the procedure to each element of the list. They differ in their
  525. return value.
  526. @rnindex map
  527. @c begin (texi-doc-string "guile" "map")
  528. @deffn {Scheme Procedure} map proc arg1 arg2 @dots{}
  529. @deffnx {Scheme Procedure} map-in-order proc arg1 arg2 @dots{}
  530. @deffnx {C Function} scm_map (proc, arg1, args)
  531. Apply @var{proc} to each element of the list @var{arg1} (if only two
  532. arguments are given), or to the corresponding elements of the argument
  533. lists (if more than two arguments are given). The result(s) of the
  534. procedure applications are saved and returned in a list. For
  535. @code{map}, the order of procedure applications is not specified,
  536. @code{map-in-order} applies the procedure from left to right to the list
  537. elements.
  538. @end deffn
  539. @rnindex for-each
  540. @c begin (texi-doc-string "guile" "for-each")
  541. @deffn {Scheme Procedure} for-each proc arg1 arg2 @dots{}
  542. Like @code{map}, but the procedure is always applied from left to right,
  543. and the result(s) of the procedure applications are thrown away. The
  544. return value is not specified.
  545. @end deffn
  546. See also SRFI-1 which extends these functions to take lists of unequal
  547. lengths (@ref{SRFI-1 Fold and Map}).
  548. @node Vectors
  549. @subsection Vectors
  550. @tpindex Vectors
  551. Vectors are sequences of Scheme objects. Unlike lists, the length of a
  552. vector, once the vector is created, cannot be changed. The advantage of
  553. vectors over lists is that the time required to access one element of a vector
  554. given its @dfn{position} (synonymous with @dfn{index}), a zero-origin number,
  555. is constant, whereas lists have an access time linear to the position of the
  556. accessed element in the list.
  557. Vectors can contain any kind of Scheme object; it is even possible to
  558. have different types of objects in the same vector. For vectors
  559. containing vectors, you may wish to use arrays, instead. Note, too,
  560. that vectors are the special case of one dimensional non-uniform arrays
  561. and that most array procedures operate happily on vectors
  562. (@pxref{Arrays}).
  563. @menu
  564. * Vector Syntax:: Read syntax for vectors.
  565. * Vector Creation:: Dynamic vector creation and validation.
  566. * Vector Accessors:: Accessing and modifying vector contents.
  567. * Vector Accessing from C:: Ways to work with vectors from C.
  568. @end menu
  569. @node Vector Syntax
  570. @subsubsection Read Syntax for Vectors
  571. Vectors can literally be entered in source code, just like strings,
  572. characters or some of the other data types. The read syntax for vectors
  573. is as follows: A sharp sign (@code{#}), followed by an opening
  574. parentheses, all elements of the vector in their respective read syntax,
  575. and finally a closing parentheses. The following are examples of the
  576. read syntax for vectors; where the first vector only contains numbers
  577. and the second three different object types: a string, a symbol and a
  578. number in hexadecimal notation.
  579. @lisp
  580. #(1 2 3)
  581. #("Hello" foo #xdeadbeef)
  582. @end lisp
  583. Like lists, vectors have to be quoted:
  584. @lisp
  585. '#(a b c) @result{} #(a b c)
  586. @end lisp
  587. @node Vector Creation
  588. @subsubsection Dynamic Vector Creation and Validation
  589. Instead of creating a vector implicitly by using the read syntax just
  590. described, you can create a vector dynamically by calling one of the
  591. @code{vector} and @code{list->vector} primitives with the list of Scheme
  592. values that you want to place into a vector. The size of the vector
  593. thus created is determined implicitly by the number of arguments given.
  594. @rnindex vector
  595. @rnindex list->vector
  596. @deffn {Scheme Procedure} vector . l
  597. @deffnx {Scheme Procedure} list->vector l
  598. @deffnx {C Function} scm_vector (l)
  599. Return a newly allocated vector composed of the
  600. given arguments. Analogous to @code{list}.
  601. @lisp
  602. (vector 'a 'b 'c) @result{} #(a b c)
  603. @end lisp
  604. @end deffn
  605. The inverse operation is @code{vector->list}:
  606. @rnindex vector->list
  607. @deffn {Scheme Procedure} vector->list v
  608. @deffnx {C Function} scm_vector_to_list (v)
  609. Return a newly allocated list composed of the elements of @var{v}.
  610. @lisp
  611. (vector->list '#(dah dah didah)) @result{} (dah dah didah)
  612. (list->vector '(dididit dah)) @result{} #(dididit dah)
  613. @end lisp
  614. @end deffn
  615. To allocate a vector with an explicitly specified size, use
  616. @code{make-vector}. With this primitive you can also specify an initial
  617. value for the vector elements (the same value for all elements, that
  618. is):
  619. @rnindex make-vector
  620. @deffn {Scheme Procedure} make-vector len [fill]
  621. @deffnx {C Function} scm_make_vector (len, fill)
  622. Return a newly allocated vector of @var{len} elements. If a
  623. second argument is given, then each position is initialized to
  624. @var{fill}. Otherwise the initial contents of each position is
  625. unspecified.
  626. @end deffn
  627. @deftypefn {C Function} SCM scm_c_make_vector (size_t k, SCM fill)
  628. Like @code{scm_make_vector}, but the length is given as a @code{size_t}.
  629. @end deftypefn
  630. To check whether an arbitrary Scheme value @emph{is} a vector, use the
  631. @code{vector?} primitive:
  632. @rnindex vector?
  633. @deffn {Scheme Procedure} vector? obj
  634. @deffnx {C Function} scm_vector_p (obj)
  635. Return @code{#t} if @var{obj} is a vector, otherwise return
  636. @code{#f}.
  637. @end deffn
  638. @deftypefn {C Function} int scm_is_vector (SCM obj)
  639. Return non-zero when @var{obj} is a vector, otherwise return
  640. @code{zero}.
  641. @end deftypefn
  642. @node Vector Accessors
  643. @subsubsection Accessing and Modifying Vector Contents
  644. @code{vector-length} and @code{vector-ref} return information about a
  645. given vector, respectively its size and the elements that are contained
  646. in the vector.
  647. @rnindex vector-length
  648. @deffn {Scheme Procedure} vector-length vector
  649. @deffnx {C Function} scm_vector_length vector
  650. Return the number of elements in @var{vector} as an exact integer.
  651. @end deffn
  652. @deftypefn {C Function} size_t scm_c_vector_length (SCM v)
  653. Return the number of elements in @var{vector} as a @code{size_t}.
  654. @end deftypefn
  655. @rnindex vector-ref
  656. @deffn {Scheme Procedure} vector-ref vector k
  657. @deffnx {C Function} scm_vector_ref vector k
  658. Return the contents of position @var{k} of @var{vector}.
  659. @var{k} must be a valid index of @var{vector}.
  660. @lisp
  661. (vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8
  662. (vector-ref '#(1 1 2 3 5 8 13 21)
  663. (let ((i (round (* 2 (acos -1)))))
  664. (if (inexact? i)
  665. (inexact->exact i)
  666. i))) @result{} 13
  667. @end lisp
  668. @end deffn
  669. @deftypefn {C Function} SCM scm_c_vector_ref (SCM v, size_t k)
  670. Return the contents of position @var{k} (a @code{size_t}) of
  671. @var{vector}.
  672. @end deftypefn
  673. A vector created by one of the dynamic vector constructor procedures
  674. (@pxref{Vector Creation}) can be modified using the following
  675. procedures.
  676. @emph{NOTE:} According to R5RS, it is an error to use any of these
  677. procedures on a literally read vector, because such vectors should be
  678. considered as constants. Currently, however, Guile does not detect this
  679. error.
  680. @rnindex vector-set!
  681. @deffn {Scheme Procedure} vector-set! vector k obj
  682. @deffnx {C Function} scm_vector_set_x vector k obj
  683. Store @var{obj} in position @var{k} of @var{vector}.
  684. @var{k} must be a valid index of @var{vector}.
  685. The value returned by @samp{vector-set!} is unspecified.
  686. @lisp
  687. (let ((vec (vector 0 '(2 2 2 2) "Anna")))
  688. (vector-set! vec 1 '("Sue" "Sue"))
  689. vec) @result{} #(0 ("Sue" "Sue") "Anna")
  690. @end lisp
  691. @end deffn
  692. @deftypefn {C Function} void scm_c_vector_set_x (SCM v, size_t k, SCM obj)
  693. Store @var{obj} in position @var{k} (a @code{size_t}) of @var{v}.
  694. @end deftypefn
  695. @rnindex vector-fill!
  696. @deffn {Scheme Procedure} vector-fill! v fill
  697. @deffnx {C Function} scm_vector_fill_x (v, fill)
  698. Store @var{fill} in every position of @var{vector}. The value
  699. returned by @code{vector-fill!} is unspecified.
  700. @end deffn
  701. @deffn {Scheme Procedure} vector-copy vec
  702. @deffnx {C Function} scm_vector_copy (vec)
  703. Return a copy of @var{vec}.
  704. @end deffn
  705. @deffn {Scheme Procedure} vector-move-left! vec1 start1 end1 vec2 start2
  706. @deffnx {C Function} scm_vector_move_left_x (vec1, start1, end1, vec2, start2)
  707. Copy elements from @var{vec1}, positions @var{start1} to @var{end1},
  708. to @var{vec2} starting at position @var{start2}. @var{start1} and
  709. @var{start2} are inclusive indices; @var{end1} is exclusive.
  710. @code{vector-move-left!} copies elements in leftmost order.
  711. Therefore, in the case where @var{vec1} and @var{vec2} refer to the
  712. same vector, @code{vector-move-left!} is usually appropriate when
  713. @var{start1} is greater than @var{start2}.
  714. @end deffn
  715. @deffn {Scheme Procedure} vector-move-right! vec1 start1 end1 vec2 start2
  716. @deffnx {C Function} scm_vector_move_right_x (vec1, start1, end1, vec2, start2)
  717. Copy elements from @var{vec1}, positions @var{start1} to @var{end1},
  718. to @var{vec2} starting at position @var{start2}. @var{start1} and
  719. @var{start2} are inclusive indices; @var{end1} is exclusive.
  720. @code{vector-move-right!} copies elements in rightmost order.
  721. Therefore, in the case where @var{vec1} and @var{vec2} refer to the
  722. same vector, @code{vector-move-right!} is usually appropriate when
  723. @var{start1} is less than @var{start2}.
  724. @end deffn
  725. @node Vector Accessing from C
  726. @subsubsection Vector Accessing from C
  727. A vector can be read and modified from C with the functions
  728. @code{scm_c_vector_ref} and @code{scm_c_vector_set_x}, for example. In
  729. addition to these functions, there are two more ways to access vectors
  730. from C that might be more efficient in certain situations: you can
  731. restrict yourself to @dfn{simple vectors} and then use the very fast
  732. @emph{simple vector macros}; or you can use the very general framework
  733. for accessing all kinds of arrays (@pxref{Accessing Arrays from C}),
  734. which is more verbose, but can deal efficiently with all kinds of
  735. vectors (and arrays). For vectors, you can use the
  736. @code{scm_vector_elements} and @code{scm_vector_writable_elements}
  737. functions as shortcuts.
  738. @deftypefn {C Function} int scm_is_simple_vector (SCM obj)
  739. Return non-zero if @var{obj} is a simple vector, else return zero. A
  740. simple vector is a vector that can be used with the @code{SCM_SIMPLE_*}
  741. macros below.
  742. The following functions are guaranteed to return simple vectors:
  743. @code{scm_make_vector}, @code{scm_c_make_vector}, @code{scm_vector},
  744. @code{scm_list_to_vector}.
  745. @end deftypefn
  746. @deftypefn {C Macro} size_t SCM_SIMPLE_VECTOR_LENGTH (SCM vec)
  747. Evaluates to the length of the simple vector @var{vec}. No type
  748. checking is done.
  749. @end deftypefn
  750. @deftypefn {C Macro} SCM SCM_SIMPLE_VECTOR_REF (SCM vec, size_t idx)
  751. Evaluates to the element at position @var{idx} in the simple vector
  752. @var{vec}. No type or range checking is done.
  753. @end deftypefn
  754. @deftypefn {C Macro} void SCM_SIMPLE_VECTOR_SET (SCM vec, size_t idx, SCM val)
  755. Sets the element at position @var{idx} in the simple vector
  756. @var{vec} to @var{val}. No type or range checking is done.
  757. @end deftypefn
  758. @deftypefn {C Function} {const SCM *} scm_vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  759. Acquire a handle for the vector @var{vec} and return a pointer to the
  760. elements of it. This pointer can only be used to read the elements of
  761. @var{vec}. When @var{vec} is not a vector, an error is signaled. The
  762. handle mustr eventually be released with
  763. @code{scm_array_handle_release}.
  764. The variables pointed to by @var{lenp} and @var{incp} are filled with
  765. the number of elements of the vector and the increment (number of
  766. elements) between successive elements, respectively. Successive
  767. elements of @var{vec} need not be contiguous in their underlying
  768. ``root vector'' returned here; hence the increment is not necessarily
  769. equal to 1 and may well be negative too (@pxref{Shared Arrays}).
  770. The following example shows the typical way to use this function. It
  771. creates a list of all elements of @var{vec} (in reverse order).
  772. @example
  773. scm_t_array_handle handle;
  774. size_t i, len;
  775. ssize_t inc;
  776. const SCM *elt;
  777. SCM list;
  778. elt = scm_vector_elements (vec, &handle, &len, &inc);
  779. list = SCM_EOL;
  780. for (i = 0; i < len; i++, elt += inc)
  781. list = scm_cons (*elt, list);
  782. scm_array_handle_release (&handle);
  783. @end example
  784. @end deftypefn
  785. @deftypefn {C Function} {SCM *} scm_vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  786. Like @code{scm_vector_elements} but the pointer can be used to modify
  787. the vector.
  788. The following example shows the typical way to use this function. It
  789. fills a vector with @code{#t}.
  790. @example
  791. scm_t_array_handle handle;
  792. size_t i, len;
  793. ssize_t inc;
  794. SCM *elt;
  795. elt = scm_vector_writable_elements (vec, &handle, &len, &inc);
  796. for (i = 0; i < len; i++, elt += inc)
  797. *elt = SCM_BOOL_T;
  798. scm_array_handle_release (&handle);
  799. @end example
  800. @end deftypefn
  801. @node Uniform Numeric Vectors
  802. @subsection Uniform Numeric Vectors
  803. A uniform numeric vector is a vector whose elements are all of a single
  804. numeric type. Guile offers uniform numeric vectors for signed and
  805. unsigned 8-bit, 16-bit, 32-bit, and 64-bit integers, two sizes of
  806. floating point values, and complex floating-point numbers of these two
  807. sizes.
  808. Strings could be regarded as uniform vectors of characters,
  809. @xref{Strings}. Likewise, bit vectors could be regarded as uniform
  810. vectors of bits, @xref{Bit Vectors}. Both are sufficiently different
  811. from uniform numeric vectors that the procedures described here do not
  812. apply to these two data types. However, both strings and bit vectors
  813. are generalized vectors, @xref{Generalized Vectors}, and arrays,
  814. @xref{Arrays}.
  815. Uniform numeric vectors are the special case of one dimensional uniform
  816. numeric arrays.
  817. Uniform numeric vectors can be useful since they consume less memory
  818. than the non-uniform, general vectors. Also, since the types they can
  819. store correspond directly to C types, it is easier to work with them
  820. efficiently on a low level. Consider image processing as an example,
  821. where you want to apply a filter to some image. While you could store
  822. the pixels of an image in a general vector and write a general
  823. convolution function, things are much more efficient with uniform
  824. vectors: the convolution function knows that all pixels are unsigned
  825. 8-bit values (say), and can use a very tight inner loop.
  826. That is, when it is written in C. Functions for efficiently working
  827. with uniform numeric vectors from C are listed at the end of this
  828. section.
  829. Procedures similar to the vector procedures (@pxref{Vectors}) are
  830. provided for handling these uniform vectors, but they are distinct
  831. datatypes and the two cannot be inter-mixed. If you want to work
  832. primarily with uniform numeric vectors, but want to offer support for
  833. general vectors as a convenience, you can use one of the
  834. @code{scm_any_to_*} functions. They will coerce lists and vectors to
  835. the given type of uniform vector. Alternatively, you can write two
  836. versions of your code: one that is fast and works only with uniform
  837. numeric vectors, and one that works with any kind of vector but is
  838. slower.
  839. One set of the procedures listed below is a generic one: it works with
  840. all types of uniform numeric vectors. In addition to that, there is a
  841. set of procedures for each type that only works with that type. Unless
  842. you really need to the generality of the first set, it is best to use
  843. the more specific functions. They might not be that much faster, but
  844. their use can serve as a kind of declaration and makes it easier to
  845. optimize later on.
  846. The generic set of procedures uses @code{uniform} in its names, the
  847. specific ones use the tag from the following table.
  848. @table @nicode
  849. @item u8
  850. unsigned 8-bit integers
  851. @item s8
  852. signed 8-bit integers
  853. @item u16
  854. unsigned 16-bit integers
  855. @item s16
  856. signed 16-bit integers
  857. @item u32
  858. unsigned 32-bit integers
  859. @item s32
  860. signed 32-bit integers
  861. @item u64
  862. unsigned 64-bit integers
  863. @item s64
  864. signed 64-bit integers
  865. @item f32
  866. the C type @code{float}
  867. @item f64
  868. the C type @code{double}
  869. @item c32
  870. complex numbers in rectangular form with the real and imaginary part
  871. being a @code{float}
  872. @item c64
  873. complex numbers in rectangular form with the real and imaginary part
  874. being a @code{double}
  875. @end table
  876. The external representation (ie.@: read syntax) for these vectors is
  877. similar to normal Scheme vectors, but with an additional tag from the
  878. table above indiciating the vector's type. For example,
  879. @lisp
  880. #u16(1 2 3)
  881. #f64(3.1415 2.71)
  882. @end lisp
  883. Note that the read syntax for floating-point here conflicts with
  884. @code{#f} for false. In Standard Scheme one can write @code{(1 #f3)}
  885. for a three element list @code{(1 #f 3)}, but for Guile @code{(1 #f3)}
  886. is invalid. @code{(1 #f 3)} is almost certainly what one should write
  887. anyway to make the intention clear, so this is rarely a problem.
  888. @deffn {Scheme Procedure} uniform-vector? obj
  889. @deffnx {Scheme Procedure} u8vector? obj
  890. @deffnx {Scheme Procedure} s8vector? obj
  891. @deffnx {Scheme Procedure} u16vector? obj
  892. @deffnx {Scheme Procedure} s16vector? obj
  893. @deffnx {Scheme Procedure} u32vector? obj
  894. @deffnx {Scheme Procedure} s32vector? obj
  895. @deffnx {Scheme Procedure} u64vector? obj
  896. @deffnx {Scheme Procedure} s64vector? obj
  897. @deffnx {Scheme Procedure} f32vector? obj
  898. @deffnx {Scheme Procedure} f64vector? obj
  899. @deffnx {Scheme Procedure} c32vector? obj
  900. @deffnx {Scheme Procedure} c64vector? obj
  901. @deffnx {C Function} scm_uniform_vector_p (obj)
  902. @deffnx {C Function} scm_u8vector_p (obj)
  903. @deffnx {C Function} scm_s8vector_p (obj)
  904. @deffnx {C Function} scm_u16vector_p (obj)
  905. @deffnx {C Function} scm_s16vector_p (obj)
  906. @deffnx {C Function} scm_u32vector_p (obj)
  907. @deffnx {C Function} scm_s32vector_p (obj)
  908. @deffnx {C Function} scm_u64vector_p (obj)
  909. @deffnx {C Function} scm_s64vector_p (obj)
  910. @deffnx {C Function} scm_f32vector_p (obj)
  911. @deffnx {C Function} scm_f64vector_p (obj)
  912. @deffnx {C Function} scm_c32vector_p (obj)
  913. @deffnx {C Function} scm_c64vector_p (obj)
  914. Return @code{#t} if @var{obj} is a homogeneous numeric vector of the
  915. indicated type.
  916. @end deffn
  917. @deffn {Scheme Procedure} make-u8vector n [value]
  918. @deffnx {Scheme Procedure} make-s8vector n [value]
  919. @deffnx {Scheme Procedure} make-u16vector n [value]
  920. @deffnx {Scheme Procedure} make-s16vector n [value]
  921. @deffnx {Scheme Procedure} make-u32vector n [value]
  922. @deffnx {Scheme Procedure} make-s32vector n [value]
  923. @deffnx {Scheme Procedure} make-u64vector n [value]
  924. @deffnx {Scheme Procedure} make-s64vector n [value]
  925. @deffnx {Scheme Procedure} make-f32vector n [value]
  926. @deffnx {Scheme Procedure} make-f64vector n [value]
  927. @deffnx {Scheme Procedure} make-c32vector n [value]
  928. @deffnx {Scheme Procedure} make-c64vector n [value]
  929. @deffnx {C Function} scm_make_u8vector n [value]
  930. @deffnx {C Function} scm_make_s8vector n [value]
  931. @deffnx {C Function} scm_make_u16vector n [value]
  932. @deffnx {C Function} scm_make_s16vector n [value]
  933. @deffnx {C Function} scm_make_u32vector n [value]
  934. @deffnx {C Function} scm_make_s32vector n [value]
  935. @deffnx {C Function} scm_make_u64vector n [value]
  936. @deffnx {C Function} scm_make_s64vector n [value]
  937. @deffnx {C Function} scm_make_f32vector n [value]
  938. @deffnx {C Function} scm_make_f64vector n [value]
  939. @deffnx {C Function} scm_make_c32vector n [value]
  940. @deffnx {C Function} scm_make_c64vector n [value]
  941. Return a newly allocated homogeneous numeric vector holding @var{n}
  942. elements of the indicated type. If @var{value} is given, the vector
  943. is initialized with that value, otherwise the contents are
  944. unspecified.
  945. @end deffn
  946. @deffn {Scheme Procedure} u8vector value @dots{}
  947. @deffnx {Scheme Procedure} s8vector value @dots{}
  948. @deffnx {Scheme Procedure} u16vector value @dots{}
  949. @deffnx {Scheme Procedure} s16vector value @dots{}
  950. @deffnx {Scheme Procedure} u32vector value @dots{}
  951. @deffnx {Scheme Procedure} s32vector value @dots{}
  952. @deffnx {Scheme Procedure} u64vector value @dots{}
  953. @deffnx {Scheme Procedure} s64vector value @dots{}
  954. @deffnx {Scheme Procedure} f32vector value @dots{}
  955. @deffnx {Scheme Procedure} f64vector value @dots{}
  956. @deffnx {Scheme Procedure} c32vector value @dots{}
  957. @deffnx {Scheme Procedure} c64vector value @dots{}
  958. @deffnx {C Function} scm_u8vector (values)
  959. @deffnx {C Function} scm_s8vector (values)
  960. @deffnx {C Function} scm_u16vector (values)
  961. @deffnx {C Function} scm_s16vector (values)
  962. @deffnx {C Function} scm_u32vector (values)
  963. @deffnx {C Function} scm_s32vector (values)
  964. @deffnx {C Function} scm_u64vector (values)
  965. @deffnx {C Function} scm_s64vector (values)
  966. @deffnx {C Function} scm_f32vector (values)
  967. @deffnx {C Function} scm_f64vector (values)
  968. @deffnx {C Function} scm_c32vector (values)
  969. @deffnx {C Function} scm_c64vector (values)
  970. Return a newly allocated homogeneous numeric vector of the indicated
  971. type, holding the given parameter @var{value}s. The vector length is
  972. the number of parameters given.
  973. @end deffn
  974. @deffn {Scheme Procedure} uniform-vector-length vec
  975. @deffnx {Scheme Procedure} u8vector-length vec
  976. @deffnx {Scheme Procedure} s8vector-length vec
  977. @deffnx {Scheme Procedure} u16vector-length vec
  978. @deffnx {Scheme Procedure} s16vector-length vec
  979. @deffnx {Scheme Procedure} u32vector-length vec
  980. @deffnx {Scheme Procedure} s32vector-length vec
  981. @deffnx {Scheme Procedure} u64vector-length vec
  982. @deffnx {Scheme Procedure} s64vector-length vec
  983. @deffnx {Scheme Procedure} f32vector-length vec
  984. @deffnx {Scheme Procedure} f64vector-length vec
  985. @deffnx {Scheme Procedure} c32vector-length vec
  986. @deffnx {Scheme Procedure} c64vector-length vec
  987. @deffnx {C Function} scm_uniform_vector_length (vec)
  988. @deffnx {C Function} scm_u8vector_length (vec)
  989. @deffnx {C Function} scm_s8vector_length (vec)
  990. @deffnx {C Function} scm_u16vector_length (vec)
  991. @deffnx {C Function} scm_s16vector_length (vec)
  992. @deffnx {C Function} scm_u32vector_length (vec)
  993. @deffnx {C Function} scm_s32vector_length (vec)
  994. @deffnx {C Function} scm_u64vector_length (vec)
  995. @deffnx {C Function} scm_s64vector_length (vec)
  996. @deffnx {C Function} scm_f32vector_length (vec)
  997. @deffnx {C Function} scm_f64vector_length (vec)
  998. @deffnx {C Function} scm_c32vector_length (vec)
  999. @deffnx {C Function} scm_c64vector_length (vec)
  1000. Return the number of elements in @var{vec}.
  1001. @end deffn
  1002. @deffn {Scheme Procedure} uniform-vector-ref vec i
  1003. @deffnx {Scheme Procedure} u8vector-ref vec i
  1004. @deffnx {Scheme Procedure} s8vector-ref vec i
  1005. @deffnx {Scheme Procedure} u16vector-ref vec i
  1006. @deffnx {Scheme Procedure} s16vector-ref vec i
  1007. @deffnx {Scheme Procedure} u32vector-ref vec i
  1008. @deffnx {Scheme Procedure} s32vector-ref vec i
  1009. @deffnx {Scheme Procedure} u64vector-ref vec i
  1010. @deffnx {Scheme Procedure} s64vector-ref vec i
  1011. @deffnx {Scheme Procedure} f32vector-ref vec i
  1012. @deffnx {Scheme Procedure} f64vector-ref vec i
  1013. @deffnx {Scheme Procedure} c32vector-ref vec i
  1014. @deffnx {Scheme Procedure} c64vector-ref vec i
  1015. @deffnx {C Function} scm_uniform_vector_ref (vec i)
  1016. @deffnx {C Function} scm_u8vector_ref (vec i)
  1017. @deffnx {C Function} scm_s8vector_ref (vec i)
  1018. @deffnx {C Function} scm_u16vector_ref (vec i)
  1019. @deffnx {C Function} scm_s16vector_ref (vec i)
  1020. @deffnx {C Function} scm_u32vector_ref (vec i)
  1021. @deffnx {C Function} scm_s32vector_ref (vec i)
  1022. @deffnx {C Function} scm_u64vector_ref (vec i)
  1023. @deffnx {C Function} scm_s64vector_ref (vec i)
  1024. @deffnx {C Function} scm_f32vector_ref (vec i)
  1025. @deffnx {C Function} scm_f64vector_ref (vec i)
  1026. @deffnx {C Function} scm_c32vector_ref (vec i)
  1027. @deffnx {C Function} scm_c64vector_ref (vec i)
  1028. Return the element at index @var{i} in @var{vec}. The first element
  1029. in @var{vec} is index 0.
  1030. @end deffn
  1031. @deffn {Scheme Procedure} uniform-vector-set! vec i value
  1032. @deffnx {Scheme Procedure} u8vector-set! vec i value
  1033. @deffnx {Scheme Procedure} s8vector-set! vec i value
  1034. @deffnx {Scheme Procedure} u16vector-set! vec i value
  1035. @deffnx {Scheme Procedure} s16vector-set! vec i value
  1036. @deffnx {Scheme Procedure} u32vector-set! vec i value
  1037. @deffnx {Scheme Procedure} s32vector-set! vec i value
  1038. @deffnx {Scheme Procedure} u64vector-set! vec i value
  1039. @deffnx {Scheme Procedure} s64vector-set! vec i value
  1040. @deffnx {Scheme Procedure} f32vector-set! vec i value
  1041. @deffnx {Scheme Procedure} f64vector-set! vec i value
  1042. @deffnx {Scheme Procedure} c32vector-set! vec i value
  1043. @deffnx {Scheme Procedure} c64vector-set! vec i value
  1044. @deffnx {C Function} scm_uniform_vector_set_x (vec i value)
  1045. @deffnx {C Function} scm_u8vector_set_x (vec i value)
  1046. @deffnx {C Function} scm_s8vector_set_x (vec i value)
  1047. @deffnx {C Function} scm_u16vector_set_x (vec i value)
  1048. @deffnx {C Function} scm_s16vector_set_x (vec i value)
  1049. @deffnx {C Function} scm_u32vector_set_x (vec i value)
  1050. @deffnx {C Function} scm_s32vector_set_x (vec i value)
  1051. @deffnx {C Function} scm_u64vector_set_x (vec i value)
  1052. @deffnx {C Function} scm_s64vector_set_x (vec i value)
  1053. @deffnx {C Function} scm_f32vector_set_x (vec i value)
  1054. @deffnx {C Function} scm_f64vector_set_x (vec i value)
  1055. @deffnx {C Function} scm_c32vector_set_x (vec i value)
  1056. @deffnx {C Function} scm_c64vector_set_x (vec i value)
  1057. Set the element at index @var{i} in @var{vec} to @var{value}. The
  1058. first element in @var{vec} is index 0. The return value is
  1059. unspecified.
  1060. @end deffn
  1061. @deffn {Scheme Procedure} uniform-vector->list vec
  1062. @deffnx {Scheme Procedure} u8vector->list vec
  1063. @deffnx {Scheme Procedure} s8vector->list vec
  1064. @deffnx {Scheme Procedure} u16vector->list vec
  1065. @deffnx {Scheme Procedure} s16vector->list vec
  1066. @deffnx {Scheme Procedure} u32vector->list vec
  1067. @deffnx {Scheme Procedure} s32vector->list vec
  1068. @deffnx {Scheme Procedure} u64vector->list vec
  1069. @deffnx {Scheme Procedure} s64vector->list vec
  1070. @deffnx {Scheme Procedure} f32vector->list vec
  1071. @deffnx {Scheme Procedure} f64vector->list vec
  1072. @deffnx {Scheme Procedure} c32vector->list vec
  1073. @deffnx {Scheme Procedure} c64vector->list vec
  1074. @deffnx {C Function} scm_uniform_vector_to_list (vec)
  1075. @deffnx {C Function} scm_u8vector_to_list (vec)
  1076. @deffnx {C Function} scm_s8vector_to_list (vec)
  1077. @deffnx {C Function} scm_u16vector_to_list (vec)
  1078. @deffnx {C Function} scm_s16vector_to_list (vec)
  1079. @deffnx {C Function} scm_u32vector_to_list (vec)
  1080. @deffnx {C Function} scm_s32vector_to_list (vec)
  1081. @deffnx {C Function} scm_u64vector_to_list (vec)
  1082. @deffnx {C Function} scm_s64vector_to_list (vec)
  1083. @deffnx {C Function} scm_f32vector_to_list (vec)
  1084. @deffnx {C Function} scm_f64vector_to_list (vec)
  1085. @deffnx {C Function} scm_c32vector_to_list (vec)
  1086. @deffnx {C Function} scm_c64vector_to_list (vec)
  1087. Return a newly allocated list holding all elements of @var{vec}.
  1088. @end deffn
  1089. @deffn {Scheme Procedure} list->u8vector lst
  1090. @deffnx {Scheme Procedure} list->s8vector lst
  1091. @deffnx {Scheme Procedure} list->u16vector lst
  1092. @deffnx {Scheme Procedure} list->s16vector lst
  1093. @deffnx {Scheme Procedure} list->u32vector lst
  1094. @deffnx {Scheme Procedure} list->s32vector lst
  1095. @deffnx {Scheme Procedure} list->u64vector lst
  1096. @deffnx {Scheme Procedure} list->s64vector lst
  1097. @deffnx {Scheme Procedure} list->f32vector lst
  1098. @deffnx {Scheme Procedure} list->f64vector lst
  1099. @deffnx {Scheme Procedure} list->c32vector lst
  1100. @deffnx {Scheme Procedure} list->c64vector lst
  1101. @deffnx {C Function} scm_list_to_u8vector (lst)
  1102. @deffnx {C Function} scm_list_to_s8vector (lst)
  1103. @deffnx {C Function} scm_list_to_u16vector (lst)
  1104. @deffnx {C Function} scm_list_to_s16vector (lst)
  1105. @deffnx {C Function} scm_list_to_u32vector (lst)
  1106. @deffnx {C Function} scm_list_to_s32vector (lst)
  1107. @deffnx {C Function} scm_list_to_u64vector (lst)
  1108. @deffnx {C Function} scm_list_to_s64vector (lst)
  1109. @deffnx {C Function} scm_list_to_f32vector (lst)
  1110. @deffnx {C Function} scm_list_to_f64vector (lst)
  1111. @deffnx {C Function} scm_list_to_c32vector (lst)
  1112. @deffnx {C Function} scm_list_to_c64vector (lst)
  1113. Return a newly allocated homogeneous numeric vector of the indicated type,
  1114. initialized with the elements of the list @var{lst}.
  1115. @end deffn
  1116. @deffn {Scheme Procedure} any->u8vector obj
  1117. @deffnx {Scheme Procedure} any->s8vector obj
  1118. @deffnx {Scheme Procedure} any->u16vector obj
  1119. @deffnx {Scheme Procedure} any->s16vector obj
  1120. @deffnx {Scheme Procedure} any->u32vector obj
  1121. @deffnx {Scheme Procedure} any->s32vector obj
  1122. @deffnx {Scheme Procedure} any->u64vector obj
  1123. @deffnx {Scheme Procedure} any->s64vector obj
  1124. @deffnx {Scheme Procedure} any->f32vector obj
  1125. @deffnx {Scheme Procedure} any->f64vector obj
  1126. @deffnx {Scheme Procedure} any->c32vector obj
  1127. @deffnx {Scheme Procedure} any->c64vector obj
  1128. @deffnx {C Function} scm_any_to_u8vector (obj)
  1129. @deffnx {C Function} scm_any_to_s8vector (obj)
  1130. @deffnx {C Function} scm_any_to_u16vector (obj)
  1131. @deffnx {C Function} scm_any_to_s16vector (obj)
  1132. @deffnx {C Function} scm_any_to_u32vector (obj)
  1133. @deffnx {C Function} scm_any_to_s32vector (obj)
  1134. @deffnx {C Function} scm_any_to_u64vector (obj)
  1135. @deffnx {C Function} scm_any_to_s64vector (obj)
  1136. @deffnx {C Function} scm_any_to_f32vector (obj)
  1137. @deffnx {C Function} scm_any_to_f64vector (obj)
  1138. @deffnx {C Function} scm_any_to_c32vector (obj)
  1139. @deffnx {C Function} scm_any_to_c64vector (obj)
  1140. Return a (maybe newly allocated) uniform numeric vector of the indicated
  1141. type, initialized with the elements of @var{obj}, which must be a list,
  1142. a vector, or a uniform vector. When @var{obj} is already a suitable
  1143. uniform numeric vector, it is returned unchanged.
  1144. @end deffn
  1145. @deftypefn {C Function} int scm_is_uniform_vector (SCM uvec)
  1146. Return non-zero when @var{uvec} is a uniform numeric vector, zero
  1147. otherwise.
  1148. @end deftypefn
  1149. @deftypefn {C Function} SCM scm_take_u8vector (const scm_t_uint8 *data, size_t len)
  1150. @deftypefnx {C Function} SCM scm_take_s8vector (const scm_t_int8 *data, size_t len)
  1151. @deftypefnx {C Function} SCM scm_take_u16vector (const scm_t_uint16 *data, size_t len)
  1152. @deftypefnx {C Function} SCM scm_take_s168vector (const scm_t_int16 *data, size_t len)
  1153. @deftypefnx {C Function} SCM scm_take_u32vector (const scm_t_uint32 *data, size_t len)
  1154. @deftypefnx {C Function} SCM scm_take_s328vector (const scm_t_int32 *data, size_t len)
  1155. @deftypefnx {C Function} SCM scm_take_u64vector (const scm_t_uint64 *data, size_t len)
  1156. @deftypefnx {C Function} SCM scm_take_s64vector (const scm_t_int64 *data, size_t len)
  1157. @deftypefnx {C Function} SCM scm_take_f32vector (const float *data, size_t len)
  1158. @deftypefnx {C Function} SCM scm_take_f64vector (const double *data, size_t len)
  1159. @deftypefnx {C Function} SCM scm_take_c32vector (const float *data, size_t len)
  1160. @deftypefnx {C Function} SCM scm_take_c64vector (const double *data, size_t len)
  1161. Return a new uniform numeric vector of the indicated type and length
  1162. that uses the memory pointed to by @var{data} to store its elements.
  1163. This memory will eventually be freed with @code{free}. The argument
  1164. @var{len} specifies the number of elements in @var{data}, not its size
  1165. in bytes.
  1166. The @code{c32} and @code{c64} variants take a pointer to a C array of
  1167. @code{float}s or @code{double}s. The real parts of the complex numbers
  1168. are at even indices in that array, the corresponding imaginary parts are
  1169. at the following odd index.
  1170. @end deftypefn
  1171. @deftypefn {C Function} size_t scm_c_uniform_vector_length (SCM uvec)
  1172. Return the number of elements of @var{uvec} as a @code{size_t}.
  1173. @end deftypefn
  1174. @deftypefn {C Function} {const void *} scm_uniform_vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1175. @deftypefnx {C Function} {const scm_t_uint8 *} scm_u8vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1176. @deftypefnx {C Function} {const scm_t_int8 *} scm_s8vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1177. @deftypefnx {C Function} {const scm_t_uint16 *} scm_u16vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1178. @deftypefnx {C Function} {const scm_t_int16 *} scm_s16vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1179. @deftypefnx {C Function} {const scm_t_uint32 *} scm_u32vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1180. @deftypefnx {C Function} {const scm_t_int32 *} scm_s32vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1181. @deftypefnx {C Function} {const scm_t_uint64 *} scm_u64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1182. @deftypefnx {C Function} {const scm_t_int64 *} scm_s64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1183. @deftypefnx {C Function} {const float *} scm_f23vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1184. @deftypefnx {C Function} {const double *} scm_f64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1185. @deftypefnx {C Function} {const float *} scm_c32vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1186. @deftypefnx {C Function} {const double *} scm_c64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1187. Like @code{scm_vector_elements} (@pxref{Vector Accessing from C}), but
  1188. returns a pointer to the elements of a uniform numeric vector of the
  1189. indicated kind.
  1190. @end deftypefn
  1191. @deftypefn {C Function} {void *} scm_uniform_vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1192. @deftypefnx {C Function} {scm_t_uint8 *} scm_u8vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1193. @deftypefnx {C Function} {scm_t_int8 *} scm_s8vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1194. @deftypefnx {C Function} {scm_t_uint16 *} scm_u16vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1195. @deftypefnx {C Function} {scm_t_int16 *} scm_s16vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1196. @deftypefnx {C Function} {scm_t_uint32 *} scm_u32vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1197. @deftypefnx {C Function} {scm_t_int32 *} scm_s32vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1198. @deftypefnx {C Function} {scm_t_uint64 *} scm_u64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1199. @deftypefnx {C Function} {scm_t_int64 *} scm_s64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1200. @deftypefnx {C Function} {float *} scm_f23vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1201. @deftypefnx {C Function} {double *} scm_f64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1202. @deftypefnx {C Function} {float *} scm_c32vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1203. @deftypefnx {C Function} {double *} scm_c64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  1204. Like @code{scm_vector_writable_elements} (@pxref{Vector Accessing from
  1205. C}), but returns a pointer to the elements of a uniform numeric vector
  1206. of the indicated kind.
  1207. @end deftypefn
  1208. @deffn {Scheme Procedure} uniform-vector-read! uvec [port_or_fd [start [end]]]
  1209. @deffnx {C Function} scm_uniform_vector_read_x (uvec, port_or_fd, start, end)
  1210. Fill the elements of @var{uvec} by reading
  1211. raw bytes from @var{port-or-fdes}, using host byte order.
  1212. The optional arguments @var{start} (inclusive) and @var{end}
  1213. (exclusive) allow a specified region to be read,
  1214. leaving the remainder of the vector unchanged.
  1215. When @var{port-or-fdes} is a port, all specified elements
  1216. of @var{uvec} are attempted to be read, potentially blocking
  1217. while waiting formore input or end-of-file.
  1218. When @var{port-or-fd} is an integer, a single call to
  1219. read(2) is made.
  1220. An error is signalled when the last element has only
  1221. been partially filled before reaching end-of-file or in
  1222. the single call to read(2).
  1223. @code{uniform-vector-read!} returns the number of elements
  1224. read.
  1225. @var{port-or-fdes} may be omitted, in which case it defaults
  1226. to the value returned by @code{(current-input-port)}.
  1227. @end deffn
  1228. @deffn {Scheme Procedure} uniform-vector-write uvec [port_or_fd [start [end]]]
  1229. @deffnx {C Function} scm_uniform_vector_write (uvec, port_or_fd, start, end)
  1230. Write the elements of @var{uvec} as raw bytes to
  1231. @var{port-or-fdes}, in the host byte order.
  1232. The optional arguments @var{start} (inclusive)
  1233. and @var{end} (exclusive) allow
  1234. a specified region to be written.
  1235. When @var{port-or-fdes} is a port, all specified elements
  1236. of @var{uvec} are attempted to be written, potentially blocking
  1237. while waiting for more room.
  1238. When @var{port-or-fd} is an integer, a single call to
  1239. write(2) is made.
  1240. An error is signalled when the last element has only
  1241. been partially written in the single call to write(2).
  1242. The number of objects actually written is returned.
  1243. @var{port-or-fdes} may be
  1244. omitted, in which case it defaults to the value returned by
  1245. @code{(current-output-port)}.
  1246. @end deffn
  1247. @node Bit Vectors
  1248. @subsection Bit Vectors
  1249. @noindent
  1250. Bit vectors are zero-origin, one-dimensional arrays of booleans. They
  1251. are displayed as a sequence of @code{0}s and @code{1}s prefixed by
  1252. @code{#*}, e.g.,
  1253. @example
  1254. (make-bitvector 8 #f) @result{}
  1255. #*00000000
  1256. @end example
  1257. Bit vectors are are also generalized vectors, @xref{Generalized
  1258. Vectors}, and can thus be used with the array procedures, @xref{Arrays}.
  1259. Bit vectors are the special case of one dimensional bit arrays.
  1260. @deffn {Scheme Procedure} bitvector? obj
  1261. @deffnx {C Function} scm_bitvector_p (obj)
  1262. Return @code{#t} when @var{obj} is a bitvector, else
  1263. return @code{#f}.
  1264. @end deffn
  1265. @deftypefn {C Function} int scm_is_bitvector (SCM obj)
  1266. Return @code{1} when @var{obj} is a bitvector, else return @code{0}.
  1267. @end deftypefn
  1268. @deffn {Scheme Procedure} make-bitvector len [fill]
  1269. @deffnx {C Function} scm_make_bitvector (len, fill)
  1270. Create a new bitvector of length @var{len} and
  1271. optionally initialize all elements to @var{fill}.
  1272. @end deffn
  1273. @deftypefn {C Function} SCM scm_c_make_bitvector (size_t len, SCM fill)
  1274. Like @code{scm_make_bitvector}, but the length is given as a
  1275. @code{size_t}.
  1276. @end deftypefn
  1277. @deffn {Scheme Procedure} bitvector . bits
  1278. @deffnx {C Function} scm_bitvector (bits)
  1279. Create a new bitvector with the arguments as elements.
  1280. @end deffn
  1281. @deffn {Scheme Procedure} bitvector-length vec
  1282. @deffnx {C Function} scm_bitvector_length (vec)
  1283. Return the length of the bitvector @var{vec}.
  1284. @end deffn
  1285. @deftypefn {C Function} size_t scm_c_bitvector_length (SCM vec)
  1286. Like @code{scm_bitvector_length}, but the length is returned as a
  1287. @code{size_t}.
  1288. @end deftypefn
  1289. @deffn {Scheme Procedure} bitvector-ref vec idx
  1290. @deffnx {C Function} scm_bitvector_ref (vec, idx)
  1291. Return the element at index @var{idx} of the bitvector
  1292. @var{vec}.
  1293. @end deffn
  1294. @deftypefn {C Function} SCM scm_c_bitvector_ref (SCM obj, size_t idx)
  1295. Return the element at index @var{idx} of the bitvector
  1296. @var{vec}.
  1297. @end deftypefn
  1298. @deffn {Scheme Procedure} bitvector-set! vec idx val
  1299. @deffnx {C Function} scm_bitvector_set_x (vec, idx, val)
  1300. Set the element at index @var{idx} of the bitvector
  1301. @var{vec} when @var{val} is true, else clear it.
  1302. @end deffn
  1303. @deftypefn {C Function} SCM scm_c_bitvector_set_x (SCM obj, size_t idx, SCM val)
  1304. Set the element at index @var{idx} of the bitvector
  1305. @var{vec} when @var{val} is true, else clear it.
  1306. @end deftypefn
  1307. @deffn {Scheme Procedure} bitvector-fill! vec val
  1308. @deffnx {C Function} scm_bitvector_fill_x (vec, val)
  1309. Set all elements of the bitvector
  1310. @var{vec} when @var{val} is true, else clear them.
  1311. @end deffn
  1312. @deffn {Scheme Procedure} list->bitvector list
  1313. @deffnx {C Function} scm_list_to_bitvector (list)
  1314. Return a new bitvector initialized with the elements
  1315. of @var{list}.
  1316. @end deffn
  1317. @deffn {Scheme Procedure} bitvector->list vec
  1318. @deffnx {C Function} scm_bitvector_to_list (vec)
  1319. Return a new list initialized with the elements
  1320. of the bitvector @var{vec}.
  1321. @end deffn
  1322. @deffn {Scheme Procedure} bit-count bool bitvector
  1323. @deffnx {C Function} scm_bit_count (bool, bitvector)
  1324. Return a count of how many entries in @var{bitvector} are equal to
  1325. @var{bool}. For example,
  1326. @example
  1327. (bit-count #f #*000111000) @result{} 6
  1328. @end example
  1329. @end deffn
  1330. @deffn {Scheme Procedure} bit-position bool bitvector start
  1331. @deffnx {C Function} scm_bit_position (bool, bitvector, start)
  1332. Return the index of the first occurrance of @var{bool} in
  1333. @var{bitvector}, starting from @var{start}. If there is no @var{bool}
  1334. entry between @var{start} and the end of @var{bitvector}, then return
  1335. @code{#f}. For example,
  1336. @example
  1337. (bit-position #t #*000101 0) @result{} 3
  1338. (bit-position #f #*0001111 3) @result{} #f
  1339. @end example
  1340. @end deffn
  1341. @deffn {Scheme Procedure} bit-invert! bitvector
  1342. @deffnx {C Function} scm_bit_invert_x (bitvector)
  1343. Modify @var{bitvector} by replacing each element with its negation.
  1344. @end deffn
  1345. @deffn {Scheme Procedure} bit-set*! bitvector uvec bool
  1346. @deffnx {C Function} scm_bit_set_star_x (bitvector, uvec, bool)
  1347. Set entries of @var{bitvector} to @var{bool}, with @var{uvec}
  1348. selecting the entries to change. The return value is unspecified.
  1349. If @var{uvec} is a bit vector, then those entries where it has
  1350. @code{#t} are the ones in @var{bitvector} which are set to @var{bool}.
  1351. @var{uvec} and @var{bitvector} must be the same length. When
  1352. @var{bool} is @code{#t} it's like @var{uvec} is OR'ed into
  1353. @var{bitvector}. Or when @var{bool} is @code{#f} it can be seen as an
  1354. ANDNOT.
  1355. @example
  1356. (define bv #*01000010)
  1357. (bit-set*! bv #*10010001 #t)
  1358. bv
  1359. @result{} #*11010011
  1360. @end example
  1361. If @var{uvec} is a uniform vector of unsigned long integers, then
  1362. they're indexes into @var{bitvector} which are set to @var{bool}.
  1363. @example
  1364. (define bv #*01000010)
  1365. (bit-set*! bv #u(5 2 7) #t)
  1366. bv
  1367. @result{} #*01100111
  1368. @end example
  1369. @end deffn
  1370. @deffn {Scheme Procedure} bit-count* bitvector uvec bool
  1371. @deffnx {C Function} scm_bit_count_star (bitvector, uvec, bool)
  1372. Return a count of how many entries in @var{bitvector} are equal to
  1373. @var{bool}, with @var{uvec} selecting the entries to consider.
  1374. @var{uvec} is interpreted in the same way as for @code{bit-set*!}
  1375. above. Namely, if @var{uvec} is a bit vector then entries which have
  1376. @code{#t} there are considered in @var{bitvector}. Or if @var{uvec}
  1377. is a uniform vector of unsigned long integers then it's the indexes in
  1378. @var{bitvector} to consider.
  1379. For example,
  1380. @example
  1381. (bit-count* #*01110111 #*11001101 #t) @result{} 3
  1382. (bit-count* #*01110111 #u(7 0 4) #f) @result{} 2
  1383. @end example
  1384. @end deffn
  1385. @deftypefn {C Function} {const scm_t_uint32 *} scm_bitvector_elements (SCM vec, scm_t_array_handle *handle, size_t *offp, size_t *lenp, ssize_t *incp)
  1386. Like @code{scm_vector_elements} (@pxref{Vector Accessing from C}), but
  1387. for bitvectors. The variable pointed to by @var{offp} is set to the
  1388. value returned by @code{scm_array_handle_bit_elements_offset}. See
  1389. @code{scm_array_handle_bit_elements} for how to use the returned
  1390. pointer and the offset.
  1391. @end deftypefn
  1392. @deftypefn {C Function} {scm_t_uint32 *} scm_bitvector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *offp, size_t *lenp, ssize_t *incp)
  1393. Like @code{scm_bitvector_elements}, but the pointer is good for reading
  1394. and writing.
  1395. @end deftypefn
  1396. @node Generalized Vectors
  1397. @subsection Generalized Vectors
  1398. Guile has a number of data types that are generally vector-like:
  1399. strings, uniform numeric vectors, bitvectors, and of course ordinary
  1400. vectors of arbitrary Scheme values. These types are disjoint: a
  1401. Scheme value belongs to at most one of the four types listed above.
  1402. If you want to gloss over this distinction and want to treat all four
  1403. types with common code, you can use the procedures in this section.
  1404. They work with the @emph{generalized vector} type, which is the union
  1405. of the four vector-like types.
  1406. @deffn {Scheme Procedure} generalized-vector? obj
  1407. @deffnx {C Function} scm_generalized_vector_p (obj)
  1408. Return @code{#t} if @var{obj} is a vector, string,
  1409. bitvector, or uniform numeric vector.
  1410. @end deffn
  1411. @deffn {Scheme Procedure} generalized-vector-length v
  1412. @deffnx {C Function} scm_generalized_vector_length (v)
  1413. Return the length of the generalized vector @var{v}.
  1414. @end deffn
  1415. @deffn {Scheme Procedure} generalized-vector-ref v idx
  1416. @deffnx {C Function} scm_generalized_vector_ref (v, idx)
  1417. Return the element at index @var{idx} of the
  1418. generalized vector @var{v}.
  1419. @end deffn
  1420. @deffn {Scheme Procedure} generalized-vector-set! v idx val
  1421. @deffnx {C Function} scm_generalized_vector_set_x (v, idx, val)
  1422. Set the element at index @var{idx} of the
  1423. generalized vector @var{v} to @var{val}.
  1424. @end deffn
  1425. @deffn {Scheme Procedure} generalized-vector->list v
  1426. @deffnx {C Function} scm_generalized_vector_to_list (v)
  1427. Return a new list whose elements are the elements of the
  1428. generalized vector @var{v}.
  1429. @end deffn
  1430. @deftypefn {C Function} int scm_is_generalized_vector (SCM obj)
  1431. Return @code{1} if @var{obj} is a vector, string,
  1432. bitvector, or uniform numeric vector; else return @code{0}.
  1433. @end deftypefn
  1434. @deftypefn {C Function} size_t scm_c_generalized_vector_length (SCM v)
  1435. Return the length of the generalized vector @var{v}.
  1436. @end deftypefn
  1437. @deftypefn {C Function} SCM scm_c_generalized_vector_ref (SCM v, size_t idx)
  1438. Return the element at index @var{idx} of the generalized vector @var{v}.
  1439. @end deftypefn
  1440. @deftypefn {C Function} void scm_c_generalized_vector_set_x (SCM v, size_t idx, SCM val)
  1441. Set the element at index @var{idx} of the generalized vector @var{v}
  1442. to @var{val}.
  1443. @end deftypefn
  1444. @deftypefn {C Function} void scm_generalized_vector_get_handle (SCM v, scm_t_array_handle *handle)
  1445. Like @code{scm_array_get_handle} but an error is signalled when @var{v}
  1446. is not of rank one. You can use @code{scm_array_handle_ref} and
  1447. @code{scm_array_handle_set} to read and write the elements of @var{v},
  1448. or you can use functions like @code{scm_array_handle_<foo>_elements} to
  1449. deal with specific types of vectors.
  1450. @end deftypefn
  1451. @node Arrays
  1452. @subsection Arrays
  1453. @tpindex Arrays
  1454. @dfn{Arrays} are a collection of cells organized into an arbitrary
  1455. number of dimensions. Each cell can be accessed in constant time by
  1456. supplying an index for each dimension.
  1457. In the current implementation, an array uses a generalized vector for
  1458. the actual storage of its elements. Any kind of generalized vector
  1459. will do, so you can have arrays of uniform numeric values, arrays of
  1460. characters, arrays of bits, and of course, arrays of arbitrary Scheme
  1461. values. For example, arrays with an underlying @code{c64vector} might
  1462. be nice for digital signal processing, while arrays made from a
  1463. @code{u8vector} might be used to hold gray-scale images.
  1464. The number of dimensions of an array is called its @dfn{rank}. Thus,
  1465. a matrix is an array of rank 2, while a vector has rank 1. When
  1466. accessing an array element, you have to specify one exact integer for
  1467. each dimension. These integers are called the @dfn{indices} of the
  1468. element. An array specifies the allowed range of indices for each
  1469. dimension via an inclusive lower and upper bound. These bounds can
  1470. well be negative, but the upper bound must be greater than or equal to
  1471. the lower bound minus one. When all lower bounds of an array are
  1472. zero, it is called a @dfn{zero-origin} array.
  1473. Arrays can be of rank 0, which could be interpreted as a scalar.
  1474. Thus, a zero-rank array can store exactly one object and the list of
  1475. indices of this element is the empty list.
  1476. Arrays contain zero elements when one of their dimensions has a zero
  1477. length. These empty arrays maintain information about their shape: a
  1478. matrix with zero columns and 3 rows is different from a matrix with 3
  1479. columns and zero rows, which again is different from a vector of
  1480. length zero.
  1481. Generalized vectors, such as strings, uniform numeric vectors, bit
  1482. vectors and ordinary vectors, are the special case of one dimensional
  1483. arrays.
  1484. @menu
  1485. * Array Syntax::
  1486. * Array Procedures::
  1487. * Shared Arrays::
  1488. * Accessing Arrays from C::
  1489. @end menu
  1490. @node Array Syntax
  1491. @subsubsection Array Syntax
  1492. An array is displayed as @code{#} followed by its rank, followed by a
  1493. tag that describes the underlying vector, optionally followed by
  1494. information about its shape, and finally followed by the cells,
  1495. organized into dimensions using parentheses.
  1496. In more words, the array tag is of the form
  1497. @example
  1498. #<rank><vectag><@@lower><:len><@@lower><:len>...
  1499. @end example
  1500. where @code{<rank>} is a positive integer in decimal giving the rank of
  1501. the array. It is omitted when the rank is 1 and the array is non-shared
  1502. and has zero-origin (see below). For shared arrays and for a non-zero
  1503. origin, the rank is always printed even when it is 1 to dinstinguish
  1504. them from ordinary vectors.
  1505. The @code{<vectag>} part is the tag for a uniform numeric vector, like
  1506. @code{u8}, @code{s16}, etc, @code{b} for bitvectors, or @code{a} for
  1507. strings. It is empty for ordinary vectors.
  1508. The @code{<@@lower>} part is a @samp{@@} character followed by a signed
  1509. integer in decimal giving the lower bound of a dimension. There is one
  1510. @code{<@@lower>} for each dimension. When all lower bounds are zero,
  1511. all @code{<@@lower>} parts are omitted.
  1512. The @code{<:len>} part is a @samp{:} character followed by an unsigned
  1513. integer in decimal giving the length of a dimension. Like for the lower
  1514. bounds, there is one @code{<:len>} for each dimension, and the
  1515. @code{<:len>} part always follows the @code{<@@lower>} part for a
  1516. dimension. Lengths are only then printed when they can't be deduced
  1517. from the nested lists of elements of the array literal, which can happen
  1518. when at least one length is zero.
  1519. As a special case, an array of rank 0 is printed as
  1520. @code{#0<vectag>(<scalar>)}, where @code{<scalar>} is the result of
  1521. printing the single element of the array.
  1522. Thus,
  1523. @table @code
  1524. @item #(1 2 3)
  1525. is an ordinary array of rank 1 with lower bound 0 in dimension 0.
  1526. (I.e., a regular vector.)
  1527. @item #@@2(1 2 3)
  1528. is an ordinary array of rank 1 with lower bound 2 in dimension 0.
  1529. @item #2((1 2 3) (4 5 6))
  1530. is a non-uniform array of rank 2; a 3@cross{}3 matrix with index ranges 0..2
  1531. and 0..2.
  1532. @item #u32(0 1 2)
  1533. is a uniform u8 array of rank 1.
  1534. @item #2u32@@2@@3((1 2) (2 3))
  1535. is a uniform u8 array of rank 2 with index ranges 2..3 and 3..4.
  1536. @item #2()
  1537. is a two-dimensional array with index ranges 0..-1 and 0..-1, i.e. both
  1538. dimensions have length zero.
  1539. @item #2:0:2()
  1540. is a two-dimensional array with index ranges 0..-1 and 0..1, i.e. the
  1541. first dimension has length zero, but the second has length 2.
  1542. @item #0(12)
  1543. is a rank-zero array with contents 12.
  1544. @end table
  1545. @node Array Procedures
  1546. @subsubsection Array Procedures
  1547. When an array is created, the range of each dimension must be
  1548. specified, e.g., to create a 2@cross{}3 array with a zero-based index:
  1549. @example
  1550. (make-array 'ho 2 3) @result{} #2((ho ho ho) (ho ho ho))
  1551. @end example
  1552. The range of each dimension can also be given explicitly, e.g., another
  1553. way to create the same array:
  1554. @example
  1555. (make-array 'ho '(0 1) '(0 2)) @result{} #2((ho ho ho) (ho ho ho))
  1556. @end example
  1557. The following procedures can be used with arrays (or vectors). An
  1558. argument shown as @var{idx}@dots{} means one parameter for each
  1559. dimension in the array. A @var{idxlist} argument means a list of such
  1560. values, one for each dimension.
  1561. @deffn {Scheme Procedure} array? obj
  1562. @deffnx {C Function} scm_array_p (obj, unused)
  1563. Return @code{#t} if the @var{obj} is an array, and @code{#f} if
  1564. not.
  1565. The second argument to scm_array_p is there for historical reasons,
  1566. but it is not used. You should always pass @code{SCM_UNDEFINED} as
  1567. its value.
  1568. @end deffn
  1569. @deffn {Scheme Procedure} typed-array? obj type
  1570. @deffnx {C Function} scm_typed_array_p (obj, type)
  1571. Return @code{#t} if the @var{obj} is an array of type @var{type}, and
  1572. @code{#f} if not.
  1573. @end deffn
  1574. @deftypefn {C Function} int scm_is_array (SCM obj)
  1575. Return @code{1} if the @var{obj} is an array and @code{0} if not.
  1576. @end deftypefn
  1577. @deftypefn {C Function} int scm_is_typed_array (SCM obj, SCM type)
  1578. Return @code{0} if the @var{obj} is an array of type @var{type}, and
  1579. @code{1} if not.
  1580. @end deftypefn
  1581. @deffn {Scheme Procedure} make-array fill bound @dots{}
  1582. @deffnx {C Function} scm_make_array (fill, bounds)
  1583. Equivalent to @code{(make-typed-array #t @var{fill} @var{bound} ...)}.
  1584. @end deffn
  1585. @deffn {Scheme Procedure} make-typed-array type fill bound @dots{}
  1586. @deffnx {C Function} scm_make_typed_array (type, fill, bounds)
  1587. Create and return an array that has as many dimensions as there are
  1588. @var{bound}s and (maybe) fill it with @var{fill}.
  1589. The underlaying storage vector is created according to @var{type},
  1590. which must be a symbol whose name is the `vectag' of the array as
  1591. explained above, or @code{#t} for ordinary, non-specialized arrays.
  1592. For example, using the symbol @code{f64} for @var{type} will create an
  1593. array that uses a @code{f64vector} for storing its elements, and
  1594. @code{a} will use a string.
  1595. When @var{fill} is not the special @emph{unspecified} value, the new
  1596. array is filled with @var{fill}. Otherwise, the initial contents of
  1597. the array is unspecified. The special @emph{unspecified} value is
  1598. stored in the variable @code{*unspecified*} so that for example
  1599. @code{(make-typed-array 'u32 *unspecified* 4)} creates a uninitialized
  1600. @code{u32} vector of length 4.
  1601. Each @var{bound} may be a positive non-zero integer @var{N}, in which
  1602. case the index for that dimension can range from 0 through @var{N-1}; or
  1603. an explicit index range specifier in the form @code{(LOWER UPPER)},
  1604. where both @var{lower} and @var{upper} are integers, possibly less than
  1605. zero, and possibly the same number (however, @var{lower} cannot be
  1606. greater than @var{upper}).
  1607. @end deffn
  1608. @deffn {Scheme Procedure} list->array dimspec list
  1609. Equivalent to @code{(list->typed-array #t @var{dimspec}
  1610. @var{list})}.
  1611. @end deffn
  1612. @deffn {Scheme Procedure} list->typed-array type dimspec list
  1613. @deffnx {C Function} scm_list_to_typed_array (type, dimspec, list)
  1614. Return an array of the type indicated by @var{type} with elements the
  1615. same as those of @var{list}.
  1616. The argument @var{dimspec} determines the number of dimensions of the
  1617. array and their lower bounds. When @var{dimspec} is an exact integer,
  1618. it gives the number of dimensions directly and all lower bounds are
  1619. zero. When it is a list of exact integers, then each element is the
  1620. lower index bound of a dimension, and there will be as many dimensions
  1621. as elements in the list.
  1622. @end deffn
  1623. @deffn {Scheme Procedure} array-type array
  1624. Return the type of @var{array}. This is the `vectag' used for
  1625. printing @var{array} (or @code{#t} for ordinary arrays) and can be
  1626. used with @code{make-typed-array} to create an array of the same kind
  1627. as @var{array}.
  1628. @end deffn
  1629. @deffn {Scheme Procedure} array-ref array idx @dots{}
  1630. Return the element at @code{(idx @dots{})} in @var{array}.
  1631. @example
  1632. (define a (make-array 999 '(1 2) '(3 4)))
  1633. (array-ref a 2 4) @result{} 999
  1634. @end example
  1635. @end deffn
  1636. @deffn {Scheme Procedure} array-in-bounds? array idx @dots{}
  1637. @deffnx {C Function} scm_array_in_bounds_p (array, idxlist)
  1638. Return @code{#t} if the given index would be acceptable to
  1639. @code{array-ref}.
  1640. @example
  1641. (define a (make-array #f '(1 2) '(3 4)))
  1642. (array-in-bounds? a 2 3) @result{} #t
  1643. (array-in-bounds? a 0 0) @result{} #f
  1644. @end example
  1645. @end deffn
  1646. @deffn {Scheme Procedure} array-set! array obj idx @dots{}
  1647. @deffnx {C Function} scm_array_set_x (array, obj, idxlist)
  1648. Set the element at @code{(idx @dots{})} in @var{array} to @var{obj}.
  1649. The return value is unspecified.
  1650. @example
  1651. (define a (make-array #f '(0 1) '(0 1)))
  1652. (array-set! a #t 1 1)
  1653. a @result{} #2((#f #f) (#f #t))
  1654. @end example
  1655. @end deffn
  1656. @deffn {Scheme Procedure} enclose-array array dim1 @dots{}
  1657. @deffnx {C Function} scm_enclose_array (array, dimlist)
  1658. @var{dim1}, @var{dim2} @dots{} should be nonnegative integers less than
  1659. the rank of @var{array}. @code{enclose-array} returns an array
  1660. resembling an array of shared arrays. The dimensions of each shared
  1661. array are the same as the @var{dim}th dimensions of the original array,
  1662. the dimensions of the outer array are the same as those of the original
  1663. array that did not match a @var{dim}.
  1664. An enclosed array is not a general Scheme array. Its elements may not
  1665. be set using @code{array-set!}. Two references to the same element of
  1666. an enclosed array will be @code{equal?} but will not in general be
  1667. @code{eq?}. The value returned by @code{array-prototype} when given an
  1668. enclosed array is unspecified.
  1669. For example,
  1670. @lisp
  1671. (enclose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1)
  1672. @result{}
  1673. #<enclosed-array (#1(a d) #1(b e) #1(c f)) (#1(1 4) #1(2 5) #1(3 6))>
  1674. (enclose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 0)
  1675. @result{}
  1676. #<enclosed-array #2((a 1) (d 4)) #2((b 2) (e 5)) #2((c 3) (f 6))>
  1677. @end lisp
  1678. @end deffn
  1679. @deffn {Scheme Procedure} array-shape array
  1680. @deffnx {Scheme Procedure} array-dimensions array
  1681. @deffnx {C Function} scm_array_dimensions (array)
  1682. Return a list of the bounds for each dimenson of @var{array}.
  1683. @code{array-shape} gives @code{(@var{lower} @var{upper})} for each
  1684. dimension. @code{array-dimensions} instead returns just
  1685. @math{@var{upper}+1} for dimensions with a 0 lower bound. Both are
  1686. suitable as input to @code{make-array}.
  1687. For example,
  1688. @example
  1689. (define a (make-array 'foo '(-1 3) 5))
  1690. (array-shape a) @result{} ((-1 3) (0 4))
  1691. (array-dimensions a) @result{} ((-1 3) 5)
  1692. @end example
  1693. @end deffn
  1694. @deffn {Scheme Procedure} array-rank obj
  1695. @deffnx {C Function} scm_array_rank (obj)
  1696. Return the rank of @var{array}.
  1697. @end deffn
  1698. @deftypefn {C Function} size_t scm_c_array_rank (SCM array)
  1699. Return the rank of @var{array} as a @code{size_t}.
  1700. @end deftypefn
  1701. @deffn {Scheme Procedure} array->list array
  1702. @deffnx {C Function} scm_array_to_list (array)
  1703. Return a list consisting of all the elements, in order, of
  1704. @var{array}.
  1705. @end deffn
  1706. @c FIXME: Describe how the order affects the copying (it matters for
  1707. @c shared arrays with the same underlying root vector, presumably).
  1708. @c
  1709. @deffn {Scheme Procedure} array-copy! src dst
  1710. @deffnx {Scheme Procedure} array-copy-in-order! src dst
  1711. @deffnx {C Function} scm_array_copy_x (src, dst)
  1712. Copy every element from vector or array @var{src} to the corresponding
  1713. element of @var{dst}. @var{dst} must have the same rank as @var{src},
  1714. and be at least as large in each dimension. The return value is
  1715. unspecified.
  1716. @end deffn
  1717. @deffn {Scheme Procedure} array-fill! array fill
  1718. @deffnx {C Function} scm_array_fill_x (array, fill)
  1719. Store @var{fill} in every element of @var{array}. The value returned
  1720. is unspecified.
  1721. @end deffn
  1722. @c begin (texi-doc-string "guile" "array-equal?")
  1723. @deffn {Scheme Procedure} array-equal? array1 array2 @dots{}
  1724. Return @code{#t} if all arguments are arrays with the same shape, the
  1725. same type, and have corresponding elements which are either
  1726. @code{equal?} or @code{array-equal?}. This function differs from
  1727. @code{equal?} (@pxref{Equality}) in that a one dimensional shared
  1728. array may be @code{array-equal?} but not @code{equal?} to a vector or
  1729. uniform vector.
  1730. @end deffn
  1731. @c FIXME: array-map! accepts no source arrays at all, and in that
  1732. @c case makes calls "(proc)". Is that meant to be a documented
  1733. @c feature?
  1734. @c
  1735. @c FIXME: array-for-each doesn't say what happens if the sources have
  1736. @c different index ranges. The code currently iterates over the
  1737. @c indices of the first and expects the others to cover those. That
  1738. @c at least vaguely matches array-map!, but is is meant to be a
  1739. @c documented feature?
  1740. @deffn {Scheme Procedure} array-map! dst proc src1 @dots{} srcN
  1741. @deffnx {Scheme Procedure} array-map-in-order! dst proc src1 @dots{} srcN
  1742. @deffnx {C Function} scm_array_map_x (dst, proc, srclist)
  1743. Set each element of the @var{dst} array to values obtained from calls
  1744. to @var{proc}. The value returned is unspecified.
  1745. Each call is @code{(@var{proc} @var{elem1} @dots{} @var{elemN})},
  1746. where each @var{elem} is from the corresponding @var{src} array, at
  1747. the @var{dst} index. @code{array-map-in-order!} makes the calls in
  1748. row-major order, @code{array-map!} makes them in an unspecified order.
  1749. The @var{src} arrays must have the same number of dimensions as
  1750. @var{dst}, and must have a range for each dimension which covers the
  1751. range in @var{dst}. This ensures all @var{dst} indices are valid in
  1752. each @var{src}.
  1753. @end deffn
  1754. @deffn {Scheme Procedure} array-for-each proc src1 @dots{} srcN
  1755. @deffnx {C Function} scm_array_for_each (proc, src1, srclist)
  1756. Apply @var{proc} to each tuple of elements of @var{src1} @dots{}
  1757. @var{srcN}, in row-major order. The value returned is unspecified.
  1758. @end deffn
  1759. @deffn {Scheme Procedure} array-index-map! dst proc
  1760. @deffnx {C Function} scm_array_index_map_x (dst, proc)
  1761. Set each element of the @var{dst} array to values returned by calls to
  1762. @var{proc}. The value returned is unspecified.
  1763. Each call is @code{(@var{proc} @var{i1} @dots{} @var{iN})}, where
  1764. @var{i1}@dots{}@var{iN} is the destination index, one parameter for
  1765. each dimension. The order in which the calls are made is unspecified.
  1766. For example, to create a @m{4\times4, 4x4} matrix representing a
  1767. cyclic group,
  1768. @tex
  1769. \advance\leftskip by 2\lispnarrowing {
  1770. $\left(\matrix{%
  1771. 0 & 1 & 2 & 3 \cr
  1772. 1 & 2 & 3 & 0 \cr
  1773. 2 & 3 & 0 & 1 \cr
  1774. 3 & 0 & 1 & 2 \cr
  1775. }\right)$} \par
  1776. @end tex
  1777. @ifnottex
  1778. @example
  1779. / 0 1 2 3 \
  1780. | 1 2 3 0 |
  1781. | 2 3 0 1 |
  1782. \ 3 0 1 2 /
  1783. @end example
  1784. @end ifnottex
  1785. @example
  1786. (define a (make-array #f 4 4))
  1787. (array-index-map! a (lambda (i j)
  1788. (modulo (+ i j) 4)))
  1789. @end example
  1790. @end deffn
  1791. @deffn {Scheme Procedure} uniform-array-read! ra [port_or_fd [start [end]]]
  1792. @deffnx {C Function} scm_uniform_array_read_x (ra, port_or_fd, start, end)
  1793. Attempt to read all elements of @var{ura}, in lexicographic order, as
  1794. binary objects from @var{port-or-fdes}.
  1795. If an end of file is encountered,
  1796. the objects up to that point are put into @var{ura}
  1797. (starting at the beginning) and the remainder of the array is
  1798. unchanged.
  1799. The optional arguments @var{start} and @var{end} allow
  1800. a specified region of a vector (or linearized array) to be read,
  1801. leaving the remainder of the vector unchanged.
  1802. @code{uniform-array-read!} returns the number of objects read.
  1803. @var{port-or-fdes} may be omitted, in which case it defaults to the value
  1804. returned by @code{(current-input-port)}.
  1805. @end deffn
  1806. @deffn {Scheme Procedure} uniform-array-write v [port_or_fd [start [end]]]
  1807. @deffnx {C Function} scm_uniform_array_write (v, port_or_fd, start, end)
  1808. Writes all elements of @var{ura} as binary objects to
  1809. @var{port-or-fdes}.
  1810. The optional arguments @var{start}
  1811. and @var{end} allow
  1812. a specified region of a vector (or linearized array) to be written.
  1813. The number of objects actually written is returned.
  1814. @var{port-or-fdes} may be
  1815. omitted, in which case it defaults to the value returned by
  1816. @code{(current-output-port)}.
  1817. @end deffn
  1818. @node Shared Arrays
  1819. @subsubsection Shared Arrays
  1820. @deffn {Scheme Procedure} make-shared-array oldarray mapfunc bound @dots{}
  1821. @deffnx {C Function} scm_make_shared_array (oldarray, mapfunc, boundlist)
  1822. Return a new array which shares the storage of @var{oldarray}.
  1823. Changes made through either affect the same underlying storage. The
  1824. @var{bound@dots{}} arguments are the shape of the new array, the same
  1825. as @code{make-array} (@pxref{Array Procedures}).
  1826. @var{mapfunc} translates coordinates from the new array to the
  1827. @var{oldarray}. It's called as @code{(@var{mapfunc} newidx1 @dots{})}
  1828. with one parameter for each dimension of the new array, and should
  1829. return a list of indices for @var{oldarray}, one for each dimension of
  1830. @var{oldarray}.
  1831. @var{mapfunc} must be affine linear, meaning that each @var{oldarray}
  1832. index must be formed by adding integer multiples (possibly negative)
  1833. of some or all of @var{newidx1} etc, plus a possible integer offset.
  1834. The multiples and offset must be the same in each call.
  1835. @sp 1
  1836. One good use for a shared array is to restrict the range of some
  1837. dimensions, so as to apply say @code{array-for-each} or
  1838. @code{array-fill!} to only part of an array. The plain @code{list}
  1839. function can be used for @var{mapfunc} in this case, making no changes
  1840. to the index values. For example,
  1841. @example
  1842. (make-shared-array #2((a b c) (d e f) (g h i)) list 3 2)
  1843. @result{} #2((a b) (d e) (g h))
  1844. @end example
  1845. The new array can have fewer dimensions than @var{oldarray}, for
  1846. example to take a column from an array.
  1847. @example
  1848. (make-shared-array #2((a b c) (d e f) (g h i))
  1849. (lambda (i) (list i 2))
  1850. '(0 2))
  1851. @result{} #1(c f i)
  1852. @end example
  1853. A diagonal can be taken by using the single new array index for both
  1854. row and column in the old array. For example,
  1855. @example
  1856. (make-shared-array #2((a b c) (d e f) (g h i))
  1857. (lambda (i) (list i i))
  1858. '(0 2))
  1859. @result{} #1(a e i)
  1860. @end example
  1861. Dimensions can be increased by for instance considering portions of a
  1862. one dimensional array as rows in a two dimensional array.
  1863. (@code{array-contents} below can do the opposite, flattening an
  1864. array.)
  1865. @example
  1866. (make-shared-array #1(a b c d e f g h i j k l)
  1867. (lambda (i j) (list (+ (* i 3) j)))
  1868. 4 3)
  1869. @result{} #2((a b c) (d e f) (g h i) (j k l))
  1870. @end example
  1871. By negating an index the order that elements appear can be reversed.
  1872. The following just reverses the column order,
  1873. @example
  1874. (make-shared-array #2((a b c) (d e f) (g h i))
  1875. (lambda (i j) (list i (- 2 j)))
  1876. 3 3)
  1877. @result{} #2((c b a) (f e d) (i h g))
  1878. @end example
  1879. A fixed offset on indexes allows for instance a change from a 0 based
  1880. to a 1 based array,
  1881. @example
  1882. (define x #2((a b c) (d e f) (g h i)))
  1883. (define y (make-shared-array x
  1884. (lambda (i j) (list (1- i) (1- j)))
  1885. '(1 3) '(1 3)))
  1886. (array-ref x 0 0) @result{} a
  1887. (array-ref y 1 1) @result{} a
  1888. @end example
  1889. A multiple on an index allows every Nth element of an array to be
  1890. taken. The following is every third element,
  1891. @example
  1892. (make-shared-array #1(a b c d e f g h i j k l)
  1893. (lambda (i) (list (* i 3)))
  1894. 4)
  1895. @result{} #1(a d g j)
  1896. @end example
  1897. The above examples can be combined to make weird and wonderful
  1898. selections from an array, but it's important to note that because
  1899. @var{mapfunc} must be affine linear, arbitrary permutations are not
  1900. possible.
  1901. In the current implementation, @var{mapfunc} is not called for every
  1902. access to the new array but only on some sample points to establish a
  1903. base and stride for new array indices in @var{oldarray} data. A few
  1904. sample points are enough because @var{mapfunc} is linear.
  1905. @end deffn
  1906. @deffn {Scheme Procedure} shared-array-increments array
  1907. @deffnx {C Function} scm_shared_array_increments (array)
  1908. For each dimension, return the distance between elements in the root vector.
  1909. @end deffn
  1910. @deffn {Scheme Procedure} shared-array-offset array
  1911. @deffnx {C Function} scm_shared_array_offset (array)
  1912. Return the root vector index of the first element in the array.
  1913. @end deffn
  1914. @deffn {Scheme Procedure} shared-array-root array
  1915. @deffnx {C Function} scm_shared_array_root (array)
  1916. Return the root vector of a shared array.
  1917. @end deffn
  1918. @deffn {Scheme Procedure} array-contents array [strict]
  1919. @deffnx {C Function} scm_array_contents (array, strict)
  1920. If @var{array} may be @dfn{unrolled} into a one dimensional shared array
  1921. without changing their order (last subscript changing fastest), then
  1922. @code{array-contents} returns that shared array, otherwise it returns
  1923. @code{#f}. All arrays made by @code{make-array} and
  1924. @code{make-typed-array} may be unrolled, some arrays made by
  1925. @code{make-shared-array} may not be.
  1926. If the optional argument @var{strict} is provided, a shared array will
  1927. be returned only if its elements are stored internally contiguous in
  1928. memory.
  1929. @end deffn
  1930. @deffn {Scheme Procedure} transpose-array array dim1 @dots{}
  1931. @deffnx {C Function} scm_transpose_array (array, dimlist)
  1932. Return an array sharing contents with @var{array}, but with
  1933. dimensions arranged in a different order. There must be one
  1934. @var{dim} argument for each dimension of @var{array}.
  1935. @var{dim1}, @var{dim2}, @dots{} should be integers between 0
  1936. and the rank of the array to be returned. Each integer in that
  1937. range must appear at least once in the argument list.
  1938. The values of @var{dim1}, @var{dim2}, @dots{} correspond to
  1939. dimensions in the array to be returned, and their positions in the
  1940. argument list to dimensions of @var{array}. Several @var{dim}s
  1941. may have the same value, in which case the returned array will
  1942. have smaller rank than @var{array}.
  1943. @lisp
  1944. (transpose-array '#2((a b) (c d)) 1 0) @result{} #2((a c) (b d))
  1945. (transpose-array '#2((a b) (c d)) 0 0) @result{} #1(a d)
  1946. (transpose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 1 0) @result{}
  1947. #2((a 4) (b 5) (c 6))
  1948. @end lisp
  1949. @end deffn
  1950. @node Accessing Arrays from C
  1951. @subsubsection Accessing Arrays from C
  1952. Arrays, especially uniform numeric arrays, are useful to efficiently
  1953. represent large amounts of rectangularily organized information, such as
  1954. matrices, images, or generally blobs of binary data. It is desirable to
  1955. access these blobs in a C like manner so that they can be handed to
  1956. external C code such as linear algebra libraries or image processing
  1957. routines.
  1958. While pointers to the elements of an array are in use, the array itself
  1959. must be protected so that the pointer remains valid. Such a protected
  1960. array is said to be @dfn{reserved}. A reserved array can be read but
  1961. modifications to it that would cause the pointer to its elements to
  1962. become invalid are prevented. When you attempt such a modification, an
  1963. error is signalled.
  1964. (This is similar to locking the array while it is in use, but without
  1965. the danger of a deadlock. In a multi-threaded program, you will need
  1966. additional synchronization to avoid modifying reserved arrays.)
  1967. You must take care to always unreserve an array after reserving it,
  1968. also in the presence of non-local exits. To simplify this, reserving
  1969. and unreserving work like a dynwind context (@pxref{Dynamic Wind}): a
  1970. call to @code{scm_array_get_handle} can be thought of as beginning a
  1971. dynwind context and @code{scm_array_handle_release} as ending it.
  1972. When a non-local exit happens between these two calls, the array is
  1973. implicitely unreserved.
  1974. That is, you need to properly pair reserving and unreserving in your
  1975. code, but you don't need to worry about non-local exits.
  1976. These calls and other pairs of calls that establish dynwind contexts
  1977. need to be properly nested. If you begin a context prior to reserving
  1978. an array, you need to unreserve the array before ending the context.
  1979. Likewise, when reserving two or more arrays in a certain order, you
  1980. need to unreserve them in the opposite order.
  1981. Once you have reserved an array and have retrieved the pointer to its
  1982. elements, you must figure out the layout of the elements in memory.
  1983. Guile allows slices to be taken out of arrays without actually making a
  1984. copy, such as making an alias for the diagonal of a matrix that can be
  1985. treated as a vector. Arrays that result from such an operation are not
  1986. stored contiguously in memory and when working with their elements
  1987. directly, you need to take this into account.
  1988. The layout of array elements in memory can be defined via a
  1989. @emph{mapping function} that computes a scalar position from a vector of
  1990. indices. The scalar position then is the offset of the element with the
  1991. given indices from the start of the storage block of the array.
  1992. In Guile, this mapping function is restricted to be @dfn{affine}: all
  1993. mapping functions of Guile arrays can be written as @code{p = b +
  1994. c[0]*i[0] + c[1]*i[1] + ... + c[n-1]*i[n-1]} where @code{i[k]} is the
  1995. @nicode{k}th index and @code{n} is the rank of the array. For
  1996. example, a matrix of size 3x3 would have @code{b == 0}, @code{c[0] ==
  1997. 3} and @code{c[1] == 1}. When you transpose this matrix (with
  1998. @code{transpose-array}, say), you will get an array whose mapping
  1999. function has @code{b == 0}, @code{c[0] == 1} and @code{c[1] == 3}.
  2000. The function @code{scm_array_handle_dims} gives you (indirect) access to
  2001. the coefficients @code{c[k]}.
  2002. @c XXX
  2003. Note that there are no functions for accessing the elements of a
  2004. character array yet. Once the string implementation of Guile has been
  2005. changed to use Unicode, we will provide them.
  2006. @deftp {C Type} scm_t_array_handle
  2007. This is a structure type that holds all information necessary to manage
  2008. the reservation of arrays as explained above. Structures of this type
  2009. must be allocated on the stack and must only be accessed by the
  2010. functions listed below.
  2011. @end deftp
  2012. @deftypefn {C Function} void scm_array_get_handle (SCM array, scm_t_array_handle *handle)
  2013. Reserve @var{array}, which must be an array, and prepare @var{handle} to
  2014. be used with the functions below. You must eventually call
  2015. @code{scm_array_handle_release} on @var{handle}, and do this in a
  2016. properly nested fashion, as explained above. The structure pointed to
  2017. by @var{handle} does not need to be initialized before calling this
  2018. function.
  2019. @end deftypefn
  2020. @deftypefn {C Function} void scm_array_handle_release (scm_t_array_handle *handle)
  2021. End the array reservation represented by @var{handle}. After a call to
  2022. this function, @var{handle} might be used for another reservation.
  2023. @end deftypefn
  2024. @deftypefn {C Function} size_t scm_array_handle_rank (scm_t_array_handle *handle)
  2025. Return the rank of the array represented by @var{handle}.
  2026. @end deftypefn
  2027. @deftp {C Type} scm_t_array_dim
  2028. This structure type holds information about the layout of one dimension
  2029. of an array. It includes the following fields:
  2030. @table @code
  2031. @item ssize_t lbnd
  2032. @itemx ssize_t ubnd
  2033. The lower and upper bounds (both inclusive) of the permissible index
  2034. range for the given dimension. Both values can be negative, but
  2035. @var{lbnd} is always less than or equal to @var{ubnd}.
  2036. @item ssize_t inc
  2037. The distance from one element of this dimension to the next. Note, too,
  2038. that this can be negative.
  2039. @end table
  2040. @end deftp
  2041. @deftypefn {C Function} {const scm_t_array_dim *} scm_array_handle_dims (scm_t_array_handle *handle)
  2042. Return a pointer to a C vector of information about the dimensions of
  2043. the array represented by @var{handle}. This pointer is valid as long as
  2044. the array remains reserved. As explained above, the
  2045. @code{scm_t_array_dim} structures returned by this function can be used
  2046. calculate the position of an element in the storage block of the array
  2047. from its indices.
  2048. This position can then be used as an index into the C array pointer
  2049. returned by the various @code{scm_array_handle_<foo>_elements}
  2050. functions, or with @code{scm_array_handle_ref} and
  2051. @code{scm_array_handle_set}.
  2052. Here is how one can compute the position @var{pos} of an element given
  2053. its indices in the vector @var{indices}:
  2054. @example
  2055. ssize_t indices[RANK];
  2056. scm_t_array_dim *dims;
  2057. ssize_t pos;
  2058. size_t i;
  2059. pos = 0;
  2060. for (i = 0; i < RANK; i++)
  2061. @{
  2062. if (indices[i] < dims[i].lbnd || indices[i] > dims[i].ubnd)
  2063. out_of_range ();
  2064. pos += (indices[i] - dims[i].lbnd) * dims[i].inc;
  2065. @}
  2066. @end example
  2067. @end deftypefn
  2068. @deftypefn {C Function} ssize_t scm_array_handle_pos (scm_t_array_handle *handle, SCM indices)
  2069. Compute the position corresponding to @var{indices}, a list of
  2070. indices. The position is computed as described above for
  2071. @code{scm_array_handle_dims}. The number of the indices and their
  2072. range is checked and an approrpiate error is signalled for invalid
  2073. indices.
  2074. @end deftypefn
  2075. @deftypefn {C Function} SCM scm_array_handle_ref (scm_t_array_handle *handle, ssize_t pos)
  2076. Return the element at position @var{pos} in the storage block of the
  2077. array represented by @var{handle}. Any kind of array is acceptable. No
  2078. range checking is done on @var{pos}.
  2079. @end deftypefn
  2080. @deftypefn {C Function} void scm_array_handle_set (scm_t_array_handle *handle, ssize_t pos, SCM val)
  2081. Set the element at position @var{pos} in the storage block of the array
  2082. represented by @var{handle} to @var{val}. Any kind of array is
  2083. acceptable. No range checking is done on @var{pos}. An error is
  2084. signalled when the array can not store @var{val}.
  2085. @end deftypefn
  2086. @deftypefn {C Function} {const SCM *} scm_array_handle_elements (scm_t_array_handle *handle)
  2087. Return a pointer to the elements of a ordinary array of general Scheme
  2088. values (i.e., a non-uniform array) for reading. This pointer is valid
  2089. as long as the array remains reserved.
  2090. @end deftypefn
  2091. @deftypefn {C Function} {SCM *} scm_array_handle_writable_elements (scm_t_array_handle *handle)
  2092. Like @code{scm_array_handle_elements}, but the pointer is good for
  2093. reading and writing.
  2094. @end deftypefn
  2095. @deftypefn {C Function} {const void *} scm_array_handle_uniform_elements (scm_t_array_handle *handle)
  2096. Return a pointer to the elements of a uniform numeric array for reading.
  2097. This pointer is valid as long as the array remains reserved. The size
  2098. of each element is given by @code{scm_array_handle_uniform_element_size}.
  2099. @end deftypefn
  2100. @deftypefn {C Function} {void *} scm_array_handle_uniform_writable_elements (scm_t_array_handle *handle)
  2101. Like @code{scm_array_handle_uniform_elements}, but the pointer is good
  2102. reading and writing.
  2103. @end deftypefn
  2104. @deftypefn {C Function} size_t scm_array_handle_uniform_element_size (scm_t_array_handle *handle)
  2105. Return the size of one element of the uniform numeric array represented
  2106. by @var{handle}.
  2107. @end deftypefn
  2108. @deftypefn {C Function} {const scm_t_uint8 *} scm_array_handle_u8_elements (scm_t_array_handle *handle)
  2109. @deftypefnx {C Function} {const scm_t_int8 *} scm_array_handle_s8_elements (scm_t_array_handle *handle)
  2110. @deftypefnx {C Function} {const scm_t_uint16 *} scm_array_handle_u16_elements (scm_t_array_handle *handle)
  2111. @deftypefnx {C Function} {const scm_t_int16 *} scm_array_handle_s16_elements (scm_t_array_handle *handle)
  2112. @deftypefnx {C Function} {const scm_t_uint32 *} scm_array_handle_u32_elements (scm_t_array_handle *handle)
  2113. @deftypefnx {C Function} {const scm_t_int32 *} scm_array_handle_s32_elements (scm_t_array_handle *handle)
  2114. @deftypefnx {C Function} {const scm_t_uint64 *} scm_array_handle_u64_elements (scm_t_array_handle *handle)
  2115. @deftypefnx {C Function} {const scm_t_int64 *} scm_array_handle_s64_elements (scm_t_array_handle *handle)
  2116. @deftypefnx {C Function} {const float *} scm_array_handle_f32_elements (scm_t_array_handle *handle)
  2117. @deftypefnx {C Function} {const double *} scm_array_handle_f64_elements (scm_t_array_handle *handle)
  2118. @deftypefnx {C Function} {const float *} scm_array_handle_c32_elements (scm_t_array_handle *handle)
  2119. @deftypefnx {C Function} {const double *} scm_array_handle_c64_elements (scm_t_array_handle *handle)
  2120. Return a pointer to the elements of a uniform numeric array of the
  2121. indicated kind for reading. This pointer is valid as long as the array
  2122. remains reserved.
  2123. The pointers for @code{c32} and @code{c64} uniform numeric arrays point
  2124. to pairs of floating point numbers. The even index holds the real part,
  2125. the odd index the imaginary part of the complex number.
  2126. @end deftypefn
  2127. @deftypefn {C Function} {scm_t_uint8 *} scm_array_handle_u8_writable_elements (scm_t_array_handle *handle)
  2128. @deftypefnx {C Function} {scm_t_int8 *} scm_array_handle_s8_writable_elements (scm_t_array_handle *handle)
  2129. @deftypefnx {C Function} {scm_t_uint16 *} scm_array_handle_u16_writable_elements (scm_t_array_handle *handle)
  2130. @deftypefnx {C Function} {scm_t_int16 *} scm_array_handle_s16_writable_elements (scm_t_array_handle *handle)
  2131. @deftypefnx {C Function} {scm_t_uint32 *} scm_array_handle_u32_writable_elements (scm_t_array_handle *handle)
  2132. @deftypefnx {C Function} {scm_t_int32 *} scm_array_handle_s32_writable_elements (scm_t_array_handle *handle)
  2133. @deftypefnx {C Function} {scm_t_uint64 *} scm_array_handle_u64_writable_elements (scm_t_array_handle *handle)
  2134. @deftypefnx {C Function} {scm_t_int64 *} scm_array_handle_s64_writable_elements (scm_t_array_handle *handle)
  2135. @deftypefnx {C Function} {float *} scm_array_handle_f32_writable_elements (scm_t_array_handle *handle)
  2136. @deftypefnx {C Function} {double *} scm_array_handle_f64_writable_elements (scm_t_array_handle *handle)
  2137. @deftypefnx {C Function} {float *} scm_array_handle_c32_writable_elements (scm_t_array_handle *handle)
  2138. @deftypefnx {C Function} {double *} scm_array_handle_c64_writable_elements (scm_t_array_handle *handle)
  2139. Like @code{scm_array_handle_<kind>_elements}, but the pointer is good
  2140. for reading and writing.
  2141. @end deftypefn
  2142. @deftypefn {C Function} {const scm_t_uint32 *} scm_array_handle_bit_elements (scm_t_array_handle *handle)
  2143. Return a pointer to the words that store the bits of the represented
  2144. array, which must be a bit array.
  2145. Unlike other arrays, bit arrays have an additional offset that must be
  2146. figured into index calculations. That offset is returned by
  2147. @code{scm_array_handle_bit_elements_offset}.
  2148. To find a certain bit you first need to calculate its position as
  2149. explained above for @code{scm_array_handle_dims} and then add the
  2150. offset. This gives the absolute position of the bit, which is always a
  2151. non-negative integer.
  2152. Each word of the bit array storage block contains exactly 32 bits, with
  2153. the least significant bit in that word having the lowest absolute
  2154. position number. The next word contains the next 32 bits.
  2155. Thus, the following code can be used to access a bit whose position
  2156. according to @code{scm_array_handle_dims} is given in @var{pos}:
  2157. @example
  2158. SCM bit_array;
  2159. scm_t_array_handle handle;
  2160. scm_t_uint32 *bits;
  2161. ssize_t pos;
  2162. size_t abs_pos;
  2163. size_t word_pos, mask;
  2164. scm_array_get_handle (&bit_array, &handle);
  2165. bits = scm_array_handle_bit_elements (&handle);
  2166. pos = ...
  2167. abs_pos = pos + scm_array_handle_bit_elements_offset (&handle);
  2168. word_pos = abs_pos / 32;
  2169. mask = 1L << (abs_pos % 32);
  2170. if (bits[word_pos] & mask)
  2171. /* bit is set. */
  2172. scm_array_handle_release (&handle);
  2173. @end example
  2174. @end deftypefn
  2175. @deftypefn {C Function} {scm_t_uint32 *} scm_array_handle_bit_writable_elements (scm_t_array_handle *handle)
  2176. Like @code{scm_array_handle_bit_elements} but the pointer is good for
  2177. reading and writing. You must take care not to modify bits outside of
  2178. the allowed index range of the array, even for contiguous arrays.
  2179. @end deftypefn
  2180. @node Records
  2181. @subsection Records
  2182. A @dfn{record type} is a first class object representing a user-defined
  2183. data type. A @dfn{record} is an instance of a record type.
  2184. @deffn {Scheme Procedure} record? obj
  2185. Return @code{#t} if @var{obj} is a record of any type and @code{#f}
  2186. otherwise.
  2187. Note that @code{record?} may be true of any Scheme value; there is no
  2188. promise that records are disjoint with other Scheme types.
  2189. @end deffn
  2190. @deffn {Scheme Procedure} make-record-type type-name field-names [print]
  2191. Create and return a new @dfn{record-type descriptor}.
  2192. @var{type-name} is a string naming the type. Currently it's only used
  2193. in the printed representation of records, and in diagnostics.
  2194. @var{field-names} is a list of symbols naming the fields of a record
  2195. of the type. Duplicates are not allowed among these symbols.
  2196. @example
  2197. (make-record-type "employee" '(name age salary))
  2198. @end example
  2199. The optional @var{print} argument is a function used by
  2200. @code{display}, @code{write}, etc, for printing a record of the new
  2201. type. It's called as @code{(@var{print} record port)} and should look
  2202. at @var{record} and write to @var{port}.
  2203. @end deffn
  2204. @deffn {Scheme Procedure} record-constructor rtd [field-names]
  2205. Return a procedure for constructing new members of the type represented
  2206. by @var{rtd}. The returned procedure accepts exactly as many arguments
  2207. as there are symbols in the given list, @var{field-names}; these are
  2208. used, in order, as the initial values of those fields in a new record,
  2209. which is returned by the constructor procedure. The values of any
  2210. fields not named in that list are unspecified. The @var{field-names}
  2211. argument defaults to the list of field names in the call to
  2212. @code{make-record-type} that created the type represented by @var{rtd};
  2213. if the @var{field-names} argument is provided, it is an error if it
  2214. contains any duplicates or any symbols not in the default list.
  2215. @end deffn
  2216. @deffn {Scheme Procedure} record-predicate rtd
  2217. Return a procedure for testing membership in the type represented by
  2218. @var{rtd}. The returned procedure accepts exactly one argument and
  2219. returns a true value if the argument is a member of the indicated record
  2220. type; it returns a false value otherwise.
  2221. @end deffn
  2222. @deffn {Scheme Procedure} record-accessor rtd field-name
  2223. Return a procedure for reading the value of a particular field of a
  2224. member of the type represented by @var{rtd}. The returned procedure
  2225. accepts exactly one argument which must be a record of the appropriate
  2226. type; it returns the current value of the field named by the symbol
  2227. @var{field-name} in that record. The symbol @var{field-name} must be a
  2228. member of the list of field-names in the call to @code{make-record-type}
  2229. that created the type represented by @var{rtd}.
  2230. @end deffn
  2231. @deffn {Scheme Procedure} record-modifier rtd field-name
  2232. Return a procedure for writing the value of a particular field of a
  2233. member of the type represented by @var{rtd}. The returned procedure
  2234. accepts exactly two arguments: first, a record of the appropriate type,
  2235. and second, an arbitrary Scheme value; it modifies the field named by
  2236. the symbol @var{field-name} in that record to contain the given value.
  2237. The returned value of the modifier procedure is unspecified. The symbol
  2238. @var{field-name} must be a member of the list of field-names in the call
  2239. to @code{make-record-type} that created the type represented by
  2240. @var{rtd}.
  2241. @end deffn
  2242. @deffn {Scheme Procedure} record-type-descriptor record
  2243. Return a record-type descriptor representing the type of the given
  2244. record. That is, for example, if the returned descriptor were passed to
  2245. @code{record-predicate}, the resulting predicate would return a true
  2246. value when passed the given record. Note that it is not necessarily the
  2247. case that the returned descriptor is the one that was passed to
  2248. @code{record-constructor} in the call that created the constructor
  2249. procedure that created the given record.
  2250. @end deffn
  2251. @deffn {Scheme Procedure} record-type-name rtd
  2252. Return the type-name associated with the type represented by rtd. The
  2253. returned value is @code{eqv?} to the @var{type-name} argument given in
  2254. the call to @code{make-record-type} that created the type represented by
  2255. @var{rtd}.
  2256. @end deffn
  2257. @deffn {Scheme Procedure} record-type-fields rtd
  2258. Return a list of the symbols naming the fields in members of the type
  2259. represented by @var{rtd}. The returned value is @code{equal?} to the
  2260. field-names argument given in the call to @code{make-record-type} that
  2261. created the type represented by @var{rtd}.
  2262. @end deffn
  2263. @node Structures
  2264. @subsection Structures
  2265. @tpindex Structures
  2266. A @dfn{structure} is a first class data type which holds Scheme values
  2267. or C words in fields numbered 0 upwards. A @dfn{vtable} represents a
  2268. structure type, giving field types and permissions, and an optional
  2269. print function for @code{write} etc.
  2270. Structures are lower level than records (@pxref{Records}) but have
  2271. some extra features. The vtable system allows sets of types be
  2272. constructed, with class data. The uninterpreted words can
  2273. inter-operate with C code, allowing arbitrary pointers or other values
  2274. to be stored along side usual Scheme @code{SCM} values.
  2275. @menu
  2276. * Vtables::
  2277. * Structure Basics::
  2278. * Vtable Contents::
  2279. * Vtable Vtables::
  2280. @end menu
  2281. @node Vtables, Structure Basics, Structures, Structures
  2282. @subsubsection Vtables
  2283. A vtable is a structure type, specifying its layout, and other
  2284. information. A vtable is actually itself a structure, but there's no
  2285. need to worray about that initially (@pxref{Vtable Contents}.)
  2286. @deffn {Scheme Procedure} make-vtable fields [print]
  2287. Create a new vtable.
  2288. @var{fields} is a string describing the fields in the structures to be
  2289. created. Each field is represented by two characters, a type letter
  2290. and a permissions letter, for example @code{"pw"}. The types are as
  2291. follows.
  2292. @itemize @bullet{}
  2293. @item
  2294. @code{p} -- a Scheme value. ``p'' stands for ``protected'' meaning
  2295. it's protected against garbage collection.
  2296. @item
  2297. @code{u} -- an arbitrary word of data (an @code{scm_t_bits}). At the
  2298. Scheme level it's read and written as an unsigned integer. ``u''
  2299. stands for ``uninterpreted'' (it's not treated as a Scheme value), or
  2300. ``unprotected'' (it's not marked during GC), or ``unsigned long'' (its
  2301. size), or all of these things.
  2302. @item
  2303. @code{s} -- a self-reference. Such a field holds the @code{SCM} value
  2304. of the structure itself (a circular reference). This can be useful in
  2305. C code where you might have a pointer to the data array, and want to
  2306. get the Scheme @code{SCM} handle for the structure. In Scheme code it
  2307. has no use.
  2308. @end itemize
  2309. The second letter for each field is a permission code,
  2310. @itemize @bullet{}
  2311. @item
  2312. @code{w} -- writable, the field can be read and written.
  2313. @item
  2314. @code{r} -- read-only, the field can be read but not written.
  2315. @item
  2316. @code{o} -- opaque, the field can be neither read nor written at the
  2317. Scheme level. This can be used for fields which should only be used
  2318. from C code.
  2319. @item
  2320. @code{W},@code{R},@code{O} -- a tail array, with permissions for the
  2321. array fields as per @code{w},@code{r},@code{o}.
  2322. @end itemize
  2323. A tail array is further fields at the end of a structure. The last
  2324. field in the layout string might be for instance @samp{pW} to have a
  2325. tail of writable Scheme-valued fields. The @samp{pW} field itself
  2326. holds the tail size, and the tail fields come after it.
  2327. Here are some examples.
  2328. @example
  2329. (make-vtable "pw") ;; one writable field
  2330. (make-vtable "prpw") ;; one read-only and one writable
  2331. (make-vtable "pwuwuw") ;; one scheme and two uninterpreted
  2332. (make-vtable "prpW") ;; one fixed then a tail array
  2333. @end example
  2334. The optional @var{print} argument is a function called by
  2335. @code{display} and @code{write} (etc) to give a printed representation
  2336. of a structure created from this vtable. It's called
  2337. @code{(@var{print} struct port)} and should look at @var{struct} and
  2338. write to @var{port}. The default print merely gives a form like
  2339. @samp{#<struct ADDR:ADDR>} with a pair of machine addresses.
  2340. The following print function for example shows the two fields of its
  2341. structure.
  2342. @example
  2343. (make-vtable "prpw"
  2344. (lambda (struct port)
  2345. (display "#<")
  2346. (display (struct-ref 0))
  2347. (display " and ")
  2348. (display (struct-ref 1))
  2349. (display ">")))
  2350. @end example
  2351. @end deffn
  2352. @node Structure Basics, Vtable Contents, Vtables, Structures
  2353. @subsubsection Structure Basics
  2354. This section describes the basic procedures for working with
  2355. structures. @code{make-struct} creates a structure, and
  2356. @code{struct-ref} and @code{struct-set!} access write fields.
  2357. @deffn {Scheme Procedure} make-struct vtable tail-size [init...]
  2358. @deffnx {C Function} scm_make_struct (vtable, tail_size, init_list)
  2359. Create a new structure, with layout per the given @var{vtable}
  2360. (@pxref{Vtables}).
  2361. @var{tail-size} is the size of the tail array if @var{vtable}
  2362. specifies a tail array. @var{tail-size} should be 0 when @var{vtable}
  2363. doesn't specify a tail array.
  2364. The optional @var{init}@dots{} arguments are initial values for the
  2365. fields of the structure (and the tail array). This is the only way to
  2366. put values in read-only fields. If there are fewer @var{init}
  2367. arguments than fields then the defaults are @code{#f} for a Scheme
  2368. field (type @code{p}) or 0 for an uninterpreted field (type @code{u}).
  2369. Type @code{s} self-reference fields, permission @code{o} opaque
  2370. fields, and the count field of a tail array are all ignored for the
  2371. @var{init} arguments, ie.@: an argument is not consumed by such a
  2372. field. An @code{s} is always set to the structure itself, an @code{o}
  2373. is always set to @code{#f} or 0 (with the intention that C code will
  2374. do something to it later), and the tail count is always the given
  2375. @var{tail-size}.
  2376. For example,
  2377. @example
  2378. (define v (make-vtable "prpwpw"))
  2379. (define s (make-struct v 0 123 "abc" 456))
  2380. (struct-ref s 0) @result{} 123
  2381. (struct-ref s 1) @result{} "abc"
  2382. @end example
  2383. @example
  2384. (define v (make-vtable "prpW"))
  2385. (define s (make-struct v 6 "fixed field" 'x 'y))
  2386. (struct-ref s 0) @result{} "fixed field"
  2387. (struct-ref s 1) @result{} 2 ;; tail size
  2388. (struct-ref s 2) @result{} x ;; tail array ...
  2389. (struct-ref s 3) @result{} y
  2390. (struct-ref s 4) @result{} #f
  2391. @end example
  2392. @end deffn
  2393. @deffn {Scheme Procedure} struct? obj
  2394. @deffnx {C Function} scm_struct_p (obj)
  2395. Return @code{#t} if @var{obj} is a structure, or @code{#f} if not.
  2396. @end deffn
  2397. @deffn {Scheme Procedure} struct-ref struct n
  2398. @deffnx {C Function} scm_struct_ref (struct, n)
  2399. Return the contents of field number @var{n} in @var{struct}. The
  2400. first field is number 0.
  2401. An error is thrown if @var{n} is out of range, or if the field cannot
  2402. be read because it's @code{o} opaque.
  2403. @end deffn
  2404. @deffn {Scheme Procedure} struct-set! struct n value
  2405. @deffnx {C Function} scm_struct_set_x (struct, n, value)
  2406. Set field number @var{n} in @var{struct} to @var{value}. The first
  2407. field is number 0.
  2408. An error is thrown if @var{n} is out of range, or if the field cannot
  2409. be written because it's @code{r} read-only or @code{o} opaque.
  2410. @end deffn
  2411. @deffn {Scheme Procedure} struct-vtable struct
  2412. @deffnx {C Function} scm_struct_vtable (struct)
  2413. Return the vtable used by @var{struct}.
  2414. This can be used to examine the layout of an unknown structure, see
  2415. @ref{Vtable Contents}.
  2416. @end deffn
  2417. @node Vtable Contents, Vtable Vtables, Structure Basics, Structures
  2418. @subsubsection Vtable Contents
  2419. A vtable is itself a structure, with particular fields that hold
  2420. information about the structures to be created. These include the
  2421. fields of those structures, and the print function for them. The
  2422. variables below allow access to those fields.
  2423. @deffn {Scheme Procedure} struct-vtable? obj
  2424. @deffnx {C Function} scm_struct_vtable_p (obj)
  2425. Return @code{#t} if @var{obj} is a vtable structure.
  2426. Note that because vtables are simply structures with a particular
  2427. layout, @code{struct-vtable?} can potentially return true on an
  2428. application structure which merely happens to look like a vtable.
  2429. @end deffn
  2430. @defvr {Scheme Variable} vtable-index-layout
  2431. @defvrx {C Macro} scm_vtable_index_layout
  2432. The field number of the layout specification in a vtable. The layout
  2433. specification is a symbol like @code{pwpw} formed from the fields
  2434. string passed to @code{make-vtable}, or created by
  2435. @code{make-struct-layout} (@pxref{Vtable Vtables}).
  2436. @example
  2437. (define v (make-vtable "pwpw" 0))
  2438. (struct-ref v vtable-index-layout) @result{} pwpw
  2439. @end example
  2440. This field is read-only, since the layout of structures using a vtable
  2441. cannot be changed.
  2442. @end defvr
  2443. @defvr {Scheme Variable} vtable-index-vtable
  2444. @defvrx {C Macro} scm_vtable_index_vtable
  2445. A self-reference to the vtable, ie.@: a type @code{s} field. This is
  2446. used by C code within Guile and has no use at the Scheme level.
  2447. @end defvr
  2448. @defvr {Scheme Variable} vtable-index-printer
  2449. @defvrx {C Macro} scm_vtable_index_printer
  2450. The field number of the printer function. This field contains @code{#f}
  2451. if the default print function should be used.
  2452. @example
  2453. (define (my-print-func struct port)
  2454. ...)
  2455. (define v (make-vtable "pwpw" my-print-func))
  2456. (struct-ref v vtable-index-printer) @result{} my-print-func
  2457. @end example
  2458. This field is writable, allowing the print function to be changed
  2459. dynamically.
  2460. @end defvr
  2461. @deffn {Scheme Procedure} struct-vtable-name vtable
  2462. @deffnx {Scheme Procedure} set-struct-vtable-name! vtable name
  2463. @deffnx {C Function} scm_struct_vtable_name (vtable)
  2464. @deffnx {C Function} scm_set_struct_vtable_name_x (vtable, name)
  2465. Get or set the name of @var{vtable}. @var{name} is a symbol and is
  2466. used in the default print function when printing structures created
  2467. from @var{vtable}.
  2468. @example
  2469. (define v (make-vtable "pw"))
  2470. (set-struct-vtable-name! v 'my-name)
  2471. (define s (make-struct v 0))
  2472. (display s) @print{} #<my-name b7ab3ae0:b7ab3730>
  2473. @end example
  2474. @end deffn
  2475. @deffn {Scheme Procedure} struct-vtable-tag vtable
  2476. @deffnx {C Function} scm_struct_vtable_tag (vtable)
  2477. Return the tag of the given @var{vtable}.
  2478. @c
  2479. @c FIXME: what can be said about what this means?
  2480. @c
  2481. @end deffn
  2482. @node Vtable Vtables, , Vtable Contents, Structures
  2483. @subsubsection Vtable Vtables
  2484. As noted above, a vtable is a structure and that structure is itself
  2485. described by a vtable. Such a ``vtable of a vtable'' can be created
  2486. with @code{make-vtable-vtable} below. This can be used to build sets
  2487. of related vtables, possibly with extra application fields.
  2488. This second level of vtable can be a little confusing. The ball
  2489. example below is a typical use, adding a ``class data'' field to the
  2490. vtables, from which instance structures are created. The current
  2491. implementation of Guile's own records (@pxref{Records}) does something
  2492. similar, a record type descriptor is a vtable with room to hold the
  2493. field names of the records to be created from it.
  2494. @deffn {Scheme Procedure} make-vtable-vtable user-fields tail-size [print]
  2495. @deffnx {C Function} scm_make_vtable_vtable (user_fields, tail_size, print_and_init_list)
  2496. Create a ``vtable-vtable'' which can be used to create vtables. This
  2497. vtable-vtable is also a vtable, and is self-describing, meaning its
  2498. vtable is itself. The following is a simple usage.
  2499. @example
  2500. (define vt-vt (make-vtable-vtable "" 0))
  2501. (define vt (make-struct vt-vt 0
  2502. (make-struct-layout "pwpw"))
  2503. (define s (make-struct vt 0 123 456))
  2504. (struct-ref s 0) @result{} 123
  2505. @end example
  2506. @code{make-struct} is used to create a vtable from the vtable-vtable.
  2507. The first initializer is a layout object (field
  2508. @code{vtable-index-layout}), usually obtained from
  2509. @code{make-struct-layout} (below). An optional second initializer is
  2510. a printer function (field @code{vtable-index-printer}), used as
  2511. described under @code{make-vtable} (@pxref{Vtables}).
  2512. @sp 1
  2513. @var{user-fields} is a layout string giving extra fields to have in
  2514. the vtables. A vtable starts with some base fields as per @ref{Vtable
  2515. Contents}, and @var{user-fields} is appended. The @var{user-fields}
  2516. start at field number @code{vtable-offset-user} (below), and exist in
  2517. both the vtable-vtable and in the vtables created from it. Such
  2518. fields provide space for ``class data''. For example,
  2519. @example
  2520. (define vt-of-vt (make-vtable-vtable "pw" 0))
  2521. (define vt (make-struct vt-of-vt 0))
  2522. (struct-set! vt vtable-offset-user "my class data")
  2523. @end example
  2524. @var{tail-size} is the size of the tail array in the vtable-vtable
  2525. itself, if @var{user-fields} specifies a tail array. This should be 0
  2526. if nothing extra is required or the format has no tail array. The
  2527. tail array field such as @samp{pW} holds the tail array size, as
  2528. usual, and is followed by the extra space.
  2529. @example
  2530. (define vt-vt (make-vtable-vtable "pW" 20))
  2531. (define my-vt-tail-start (1+ vtable-offset-user))
  2532. (struct-set! vt-vt (+ 3 my-vt-tail-start) "data in tail")
  2533. @end example
  2534. The optional @var{print} argument is used by @code{display} and
  2535. @code{write} (etc) to print the vtable-vtable and any vtables created
  2536. from it. It's called as @code{(@var{print} vtable port)} and should
  2537. look at @var{vtable} and write to @var{port}. The default is the
  2538. usual structure print function, which just gives machine addresses.
  2539. @end deffn
  2540. @deffn {Scheme Procedure} make-struct-layout fields
  2541. @deffnx {C Function} scm_make_struct_layout (fields)
  2542. Return a structure layout symbol, from a @var{fields} string.
  2543. @var{fields} is as described under @code{make-vtable}
  2544. (@pxref{Vtables}). An invalid @var{fields} string is an error.
  2545. @example
  2546. (make-struct-layout "prpW") @result{} prpW
  2547. (make-struct-layout "blah") @result{} ERROR
  2548. @end example
  2549. @end deffn
  2550. @defvr {Scheme Variable} vtable-offset-user
  2551. @defvrx {C Macro} scm_vtable_offset_user
  2552. The first field in a vtable which is available for application use.
  2553. Such fields only exist when specified by @var{user-fields} in
  2554. @code{make-vtable-vtable} above.
  2555. @end defvr
  2556. @sp 1
  2557. Here's an extended vtable-vtable example, creating classes of
  2558. ``balls''. Each class has a ``colour'', which is fixed. Instances of
  2559. those classes are created, and such each such ball has an ``owner'',
  2560. which can be changed.
  2561. @lisp
  2562. (define ball-root (make-vtable-vtable "pr" 0))
  2563. (define (make-ball-type ball-color)
  2564. (make-struct ball-root 0
  2565. (make-struct-layout "pw")
  2566. (lambda (ball port)
  2567. (format port "#<a ~A ball owned by ~A>"
  2568. (color ball)
  2569. (owner ball)))
  2570. ball-color))
  2571. (define (color ball) (struct-ref (struct-vtable ball) vtable-offset-user))
  2572. (define (owner ball) (struct-ref ball 0))
  2573. (define red (make-ball-type 'red))
  2574. (define green (make-ball-type 'green))
  2575. (define (make-ball type owner) (make-struct type 0 owner))
  2576. (define ball (make-ball green 'Nisse))
  2577. ball @result{} #<a green ball owned by Nisse>
  2578. @end lisp
  2579. @node Dictionary Types
  2580. @subsection Dictionary Types
  2581. A @dfn{dictionary} object is a data structure used to index
  2582. information in a user-defined way. In standard Scheme, the main
  2583. aggregate data types are lists and vectors. Lists are not really
  2584. indexed at all, and vectors are indexed only by number
  2585. (e.g. @code{(vector-ref foo 5)}). Often you will find it useful
  2586. to index your data on some other type; for example, in a library
  2587. catalog you might want to look up a book by the name of its
  2588. author. Dictionaries are used to help you organize information in
  2589. such a way.
  2590. An @dfn{association list} (or @dfn{alist} for short) is a list of
  2591. key-value pairs. Each pair represents a single quantity or
  2592. object; the @code{car} of the pair is a key which is used to
  2593. identify the object, and the @code{cdr} is the object's value.
  2594. A @dfn{hash table} also permits you to index objects with
  2595. arbitrary keys, but in a way that makes looking up any one object
  2596. extremely fast. A well-designed hash system makes hash table
  2597. lookups almost as fast as conventional array or vector references.
  2598. Alists are popular among Lisp programmers because they use only
  2599. the language's primitive operations (lists, @dfn{car}, @dfn{cdr}
  2600. and the equality primitives). No changes to the language core are
  2601. necessary. Therefore, with Scheme's built-in list manipulation
  2602. facilities, it is very convenient to handle data stored in an
  2603. association list. Also, alists are highly portable and can be
  2604. easily implemented on even the most minimal Lisp systems.
  2605. However, alists are inefficient, especially for storing large
  2606. quantities of data. Because we want Guile to be useful for large
  2607. software systems as well as small ones, Guile provides a rich set
  2608. of tools for using either association lists or hash tables.
  2609. @node Association Lists
  2610. @subsection Association Lists
  2611. @tpindex Association Lists
  2612. @tpindex Alist
  2613. @cindex association List
  2614. @cindex alist
  2615. @cindex aatabase
  2616. An association list is a conventional data structure that is often used
  2617. to implement simple key-value databases. It consists of a list of
  2618. entries in which each entry is a pair. The @dfn{key} of each entry is
  2619. the @code{car} of the pair and the @dfn{value} of each entry is the
  2620. @code{cdr}.
  2621. @example
  2622. ASSOCIATION LIST ::= '( (KEY1 . VALUE1)
  2623. (KEY2 . VALUE2)
  2624. (KEY3 . VALUE3)
  2625. @dots{}
  2626. )
  2627. @end example
  2628. @noindent
  2629. Association lists are also known, for short, as @dfn{alists}.
  2630. The structure of an association list is just one example of the infinite
  2631. number of possible structures that can be built using pairs and lists.
  2632. As such, the keys and values in an association list can be manipulated
  2633. using the general list structure procedures @code{cons}, @code{car},
  2634. @code{cdr}, @code{set-car!}, @code{set-cdr!} and so on. However,
  2635. because association lists are so useful, Guile also provides specific
  2636. procedures for manipulating them.
  2637. @menu
  2638. * Alist Key Equality::
  2639. * Adding or Setting Alist Entries::
  2640. * Retrieving Alist Entries::
  2641. * Removing Alist Entries::
  2642. * Sloppy Alist Functions::
  2643. * Alist Example::
  2644. @end menu
  2645. @node Alist Key Equality
  2646. @subsubsection Alist Key Equality
  2647. All of Guile's dedicated association list procedures, apart from
  2648. @code{acons}, come in three flavours, depending on the level of equality
  2649. that is required to decide whether an existing key in the association
  2650. list is the same as the key that the procedure call uses to identify the
  2651. required entry.
  2652. @itemize @bullet
  2653. @item
  2654. Procedures with @dfn{assq} in their name use @code{eq?} to determine key
  2655. equality.
  2656. @item
  2657. Procedures with @dfn{assv} in their name use @code{eqv?} to determine
  2658. key equality.
  2659. @item
  2660. Procedures with @dfn{assoc} in their name use @code{equal?} to
  2661. determine key equality.
  2662. @end itemize
  2663. @code{acons} is an exception because it is used to build association
  2664. lists which do not require their entries' keys to be unique.
  2665. @node Adding or Setting Alist Entries
  2666. @subsubsection Adding or Setting Alist Entries
  2667. @code{acons} adds a new entry to an association list and returns the
  2668. combined association list. The combined alist is formed by consing the
  2669. new entry onto the head of the alist specified in the @code{acons}
  2670. procedure call. So the specified alist is not modified, but its
  2671. contents become shared with the tail of the combined alist that
  2672. @code{acons} returns.
  2673. In the most common usage of @code{acons}, a variable holding the
  2674. original association list is updated with the combined alist:
  2675. @example
  2676. (set! address-list (acons name address address-list))
  2677. @end example
  2678. In such cases, it doesn't matter that the old and new values of
  2679. @code{address-list} share some of their contents, since the old value is
  2680. usually no longer independently accessible.
  2681. Note that @code{acons} adds the specified new entry regardless of
  2682. whether the alist may already contain entries with keys that are, in
  2683. some sense, the same as that of the new entry. Thus @code{acons} is
  2684. ideal for building alists where there is no concept of key uniqueness.
  2685. @example
  2686. (set! task-list (acons 3 "pay gas bill" '()))
  2687. task-list
  2688. @result{}
  2689. ((3 . "pay gas bill"))
  2690. (set! task-list (acons 3 "tidy bedroom" task-list))
  2691. task-list
  2692. @result{}
  2693. ((3 . "tidy bedroom") (3 . "pay gas bill"))
  2694. @end example
  2695. @code{assq-set!}, @code{assv-set!} and @code{assoc-set!} are used to add
  2696. or replace an entry in an association list where there @emph{is} a
  2697. concept of key uniqueness. If the specified association list already
  2698. contains an entry whose key is the same as that specified in the
  2699. procedure call, the existing entry is replaced by the new one.
  2700. Otherwise, the new entry is consed onto the head of the old association
  2701. list to create the combined alist. In all cases, these procedures
  2702. return the combined alist.
  2703. @code{assq-set!} and friends @emph{may} destructively modify the
  2704. structure of the old association list in such a way that an existing
  2705. variable is correctly updated without having to @code{set!} it to the
  2706. value returned:
  2707. @example
  2708. address-list
  2709. @result{}
  2710. (("mary" . "34 Elm Road") ("james" . "16 Bow Street"))
  2711. (assoc-set! address-list "james" "1a London Road")
  2712. @result{}
  2713. (("mary" . "34 Elm Road") ("james" . "1a London Road"))
  2714. address-list
  2715. @result{}
  2716. (("mary" . "34 Elm Road") ("james" . "1a London Road"))
  2717. @end example
  2718. Or they may not:
  2719. @example
  2720. (assoc-set! address-list "bob" "11 Newington Avenue")
  2721. @result{}
  2722. (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road")
  2723. ("james" . "1a London Road"))
  2724. address-list
  2725. @result{}
  2726. (("mary" . "34 Elm Road") ("james" . "1a London Road"))
  2727. @end example
  2728. The only safe way to update an association list variable when adding or
  2729. replacing an entry like this is to @code{set!} the variable to the
  2730. returned value:
  2731. @example
  2732. (set! address-list
  2733. (assoc-set! address-list "bob" "11 Newington Avenue"))
  2734. address-list
  2735. @result{}
  2736. (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road")
  2737. ("james" . "1a London Road"))
  2738. @end example
  2739. Because of this slight inconvenience, you may find it more convenient to
  2740. use hash tables to store dictionary data. If your application will not
  2741. be modifying the contents of an alist very often, this may not make much
  2742. difference to you.
  2743. If you need to keep the old value of an association list in a form
  2744. independent from the list that results from modification by
  2745. @code{acons}, @code{assq-set!}, @code{assv-set!} or @code{assoc-set!},
  2746. use @code{list-copy} to copy the old association list before modifying
  2747. it.
  2748. @deffn {Scheme Procedure} acons key value alist
  2749. @deffnx {C Function} scm_acons (key, value, alist)
  2750. Add a new key-value pair to @var{alist}. A new pair is
  2751. created whose car is @var{key} and whose cdr is @var{value}, and the
  2752. pair is consed onto @var{alist}, and the new list is returned. This
  2753. function is @emph{not} destructive; @var{alist} is not modified.
  2754. @end deffn
  2755. @deffn {Scheme Procedure} assq-set! alist key val
  2756. @deffnx {Scheme Procedure} assv-set! alist key value
  2757. @deffnx {Scheme Procedure} assoc-set! alist key value
  2758. @deffnx {C Function} scm_assq_set_x (alist, key, val)
  2759. @deffnx {C Function} scm_assv_set_x (alist, key, val)
  2760. @deffnx {C Function} scm_assoc_set_x (alist, key, val)
  2761. Reassociate @var{key} in @var{alist} with @var{value}: find any existing
  2762. @var{alist} entry for @var{key} and associate it with the new
  2763. @var{value}. If @var{alist} does not contain an entry for @var{key},
  2764. add a new one. Return the (possibly new) alist.
  2765. These functions do not attempt to verify the structure of @var{alist},
  2766. and so may cause unusual results if passed an object that is not an
  2767. association list.
  2768. @end deffn
  2769. @node Retrieving Alist Entries
  2770. @subsubsection Retrieving Alist Entries
  2771. @rnindex assq
  2772. @rnindex assv
  2773. @rnindex assoc
  2774. @code{assq}, @code{assv} and @code{assoc} find the entry in an alist
  2775. for a given key, and return the @code{(@var{key} . @var{value})} pair.
  2776. @code{assq-ref}, @code{assv-ref} and @code{assoc-ref} do a similar
  2777. lookup, but return just the @var{value}.
  2778. @deffn {Scheme Procedure} assq key alist
  2779. @deffnx {Scheme Procedure} assv key alist
  2780. @deffnx {Scheme Procedure} assoc key alist
  2781. @deffnx {C Function} scm_assq (key, alist)
  2782. @deffnx {C Function} scm_assv (key, alist)
  2783. @deffnx {C Function} scm_assoc (key, alist)
  2784. Return the first entry in @var{alist} with the given @var{key}. The
  2785. return is the pair @code{(KEY . VALUE)} from @var{alist}. If there's
  2786. no matching entry the return is @code{#f}.
  2787. @code{assq} compares keys with @code{eq?}, @code{assv} uses
  2788. @code{eqv?} and @code{assoc} uses @code{equal?}. See also SRFI-1
  2789. which has an extended @code{assoc} (@ref{SRFI-1 Association Lists}).
  2790. @end deffn
  2791. @deffn {Scheme Procedure} assq-ref alist key
  2792. @deffnx {Scheme Procedure} assv-ref alist key
  2793. @deffnx {Scheme Procedure} assoc-ref alist key
  2794. @deffnx {C Function} scm_assq_ref (alist, key)
  2795. @deffnx {C Function} scm_assv_ref (alist, key)
  2796. @deffnx {C Function} scm_assoc_ref (alist, key)
  2797. Return the value from the first entry in @var{alist} with the given
  2798. @var{key}, or @code{#f} if there's no such entry.
  2799. @code{assq-ref} compares keys with @code{eq?}, @code{assv-ref} uses
  2800. @code{eqv?} and @code{assoc-ref} uses @code{equal?}.
  2801. Notice these functions have the @var{key} argument last, like other
  2802. @code{-ref} functions, but this is opposite to what what @code{assq}
  2803. etc above use.
  2804. When the return is @code{#f} it can be either @var{key} not found, or
  2805. an entry which happens to have value @code{#f} in the @code{cdr}. Use
  2806. @code{assq} etc above if you need to differentiate these cases.
  2807. @end deffn
  2808. @node Removing Alist Entries
  2809. @subsubsection Removing Alist Entries
  2810. To remove the element from an association list whose key matches a
  2811. specified key, use @code{assq-remove!}, @code{assv-remove!} or
  2812. @code{assoc-remove!} (depending, as usual, on the level of equality
  2813. required between the key that you specify and the keys in the
  2814. association list).
  2815. As with @code{assq-set!} and friends, the specified alist may or may not
  2816. be modified destructively, and the only safe way to update a variable
  2817. containing the alist is to @code{set!} it to the value that
  2818. @code{assq-remove!} and friends return.
  2819. @example
  2820. address-list
  2821. @result{}
  2822. (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road")
  2823. ("james" . "1a London Road"))
  2824. (set! address-list (assoc-remove! address-list "mary"))
  2825. address-list
  2826. @result{}
  2827. (("bob" . "11 Newington Avenue") ("james" . "1a London Road"))
  2828. @end example
  2829. Note that, when @code{assq/v/oc-remove!} is used to modify an
  2830. association list that has been constructed only using the corresponding
  2831. @code{assq/v/oc-set!}, there can be at most one matching entry in the
  2832. alist, so the question of multiple entries being removed in one go does
  2833. not arise. If @code{assq/v/oc-remove!} is applied to an association
  2834. list that has been constructed using @code{acons}, or an
  2835. @code{assq/v/oc-set!} with a different level of equality, or any mixture
  2836. of these, it removes only the first matching entry from the alist, even
  2837. if the alist might contain further matching entries. For example:
  2838. @example
  2839. (define address-list '())
  2840. (set! address-list (assq-set! address-list "mary" "11 Elm Street"))
  2841. (set! address-list (assq-set! address-list "mary" "57 Pine Drive"))
  2842. address-list
  2843. @result{}
  2844. (("mary" . "57 Pine Drive") ("mary" . "11 Elm Street"))
  2845. (set! address-list (assoc-remove! address-list "mary"))
  2846. address-list
  2847. @result{}
  2848. (("mary" . "11 Elm Street"))
  2849. @end example
  2850. In this example, the two instances of the string "mary" are not the same
  2851. when compared using @code{eq?}, so the two @code{assq-set!} calls add
  2852. two distinct entries to @code{address-list}. When compared using
  2853. @code{equal?}, both "mary"s in @code{address-list} are the same as the
  2854. "mary" in the @code{assoc-remove!} call, but @code{assoc-remove!} stops
  2855. after removing the first matching entry that it finds, and so one of the
  2856. "mary" entries is left in place.
  2857. @deffn {Scheme Procedure} assq-remove! alist key
  2858. @deffnx {Scheme Procedure} assv-remove! alist key
  2859. @deffnx {Scheme Procedure} assoc-remove! alist key
  2860. @deffnx {C Function} scm_assq_remove_x (alist, key)
  2861. @deffnx {C Function} scm_assv_remove_x (alist, key)
  2862. @deffnx {C Function} scm_assoc_remove_x (alist, key)
  2863. Delete the first entry in @var{alist} associated with @var{key}, and return
  2864. the resulting alist.
  2865. @end deffn
  2866. @node Sloppy Alist Functions
  2867. @subsubsection Sloppy Alist Functions
  2868. @code{sloppy-assq}, @code{sloppy-assv} and @code{sloppy-assoc} behave
  2869. like the corresponding non-@code{sloppy-} procedures, except that they
  2870. return @code{#f} when the specified association list is not well-formed,
  2871. where the non-@code{sloppy-} versions would signal an error.
  2872. Specifically, there are two conditions for which the non-@code{sloppy-}
  2873. procedures signal an error, which the @code{sloppy-} procedures handle
  2874. instead by returning @code{#f}. Firstly, if the specified alist as a
  2875. whole is not a proper list:
  2876. @example
  2877. (assoc "mary" '((1 . 2) ("key" . "door") . "open sesame"))
  2878. @result{}
  2879. ERROR: In procedure assoc in expression (assoc "mary" (quote #)):
  2880. ERROR: Wrong type argument in position 2 (expecting association list): ((1 . 2) ("key" . "door") . "open sesame")
  2881. (sloppy-assoc "mary" '((1 . 2) ("key" . "door") . "open sesame"))
  2882. @result{}
  2883. #f
  2884. @end example
  2885. @noindent
  2886. Secondly, if one of the entries in the specified alist is not a pair:
  2887. @example
  2888. (assoc 2 '((1 . 1) 2 (3 . 9)))
  2889. @result{}
  2890. ERROR: In procedure assoc in expression (assoc 2 (quote #)):
  2891. ERROR: Wrong type argument in position 2 (expecting association list): ((1 . 1) 2 (3 . 9))
  2892. (sloppy-assoc 2 '((1 . 1) 2 (3 . 9)))
  2893. @result{}
  2894. #f
  2895. @end example
  2896. Unless you are explicitly working with badly formed association lists,
  2897. it is much safer to use the non-@code{sloppy-} procedures, because they
  2898. help to highlight coding and data errors that the @code{sloppy-}
  2899. versions would silently cover up.
  2900. @deffn {Scheme Procedure} sloppy-assq key alist
  2901. @deffnx {C Function} scm_sloppy_assq (key, alist)
  2902. Behaves like @code{assq} but does not do any error checking.
  2903. Recommended only for use in Guile internals.
  2904. @end deffn
  2905. @deffn {Scheme Procedure} sloppy-assv key alist
  2906. @deffnx {C Function} scm_sloppy_assv (key, alist)
  2907. Behaves like @code{assv} but does not do any error checking.
  2908. Recommended only for use in Guile internals.
  2909. @end deffn
  2910. @deffn {Scheme Procedure} sloppy-assoc key alist
  2911. @deffnx {C Function} scm_sloppy_assoc (key, alist)
  2912. Behaves like @code{assoc} but does not do any error checking.
  2913. Recommended only for use in Guile internals.
  2914. @end deffn
  2915. @node Alist Example
  2916. @subsubsection Alist Example
  2917. Here is a longer example of how alists may be used in practice.
  2918. @lisp
  2919. (define capitals '(("New York" . "Albany")
  2920. ("Oregon" . "Salem")
  2921. ("Florida" . "Miami")))
  2922. ;; What's the capital of Oregon?
  2923. (assoc "Oregon" capitals) @result{} ("Oregon" . "Salem")
  2924. (assoc-ref capitals "Oregon") @result{} "Salem"
  2925. ;; We left out South Dakota.
  2926. (set! capitals
  2927. (assoc-set! capitals "South Dakota" "Pierre"))
  2928. capitals
  2929. @result{} (("South Dakota" . "Pierre")
  2930. ("New York" . "Albany")
  2931. ("Oregon" . "Salem")
  2932. ("Florida" . "Miami"))
  2933. ;; And we got Florida wrong.
  2934. (set! capitals
  2935. (assoc-set! capitals "Florida" "Tallahassee"))
  2936. capitals
  2937. @result{} (("South Dakota" . "Pierre")
  2938. ("New York" . "Albany")
  2939. ("Oregon" . "Salem")
  2940. ("Florida" . "Tallahassee"))
  2941. ;; After Oregon secedes, we can remove it.
  2942. (set! capitals
  2943. (assoc-remove! capitals "Oregon"))
  2944. capitals
  2945. @result{} (("South Dakota" . "Pierre")
  2946. ("New York" . "Albany")
  2947. ("Florida" . "Tallahassee"))
  2948. @end lisp
  2949. @node Hash Tables
  2950. @subsection Hash Tables
  2951. @tpindex Hash Tables
  2952. Hash tables are dictionaries which offer similar functionality as
  2953. association lists: They provide a mapping from keys to values. The
  2954. difference is that association lists need time linear in the size of
  2955. elements when searching for entries, whereas hash tables can normally
  2956. search in constant time. The drawback is that hash tables require a
  2957. little bit more memory, and that you can not use the normal list
  2958. procedures (@pxref{Lists}) for working with them.
  2959. Guile provides two types of hashtables. One is an abstract data type
  2960. that can only be manipulated with the functions in this section. The
  2961. other type is concrete: it uses a normal vector with alists as
  2962. elements. The advantage of the abstract hash tables is that they will
  2963. be automatically resized when they become too full or too empty.
  2964. @menu
  2965. * Hash Table Examples:: Demonstration of hash table usage.
  2966. * Hash Table Reference:: Hash table procedure descriptions.
  2967. @end menu
  2968. @node Hash Table Examples
  2969. @subsubsection Hash Table Examples
  2970. For demonstration purposes, this section gives a few usage examples of
  2971. some hash table procedures, together with some explanation what they do.
  2972. First we start by creating a new hash table with 31 slots, and
  2973. populate it with two key/value pairs.
  2974. @lisp
  2975. (define h (make-hash-table 31))
  2976. ;; This is an opaque object
  2977. h
  2978. @result{}
  2979. #<hash-table 0/31>
  2980. ;; We can also use a vector of alists.
  2981. (define h (make-vector 7 '()))
  2982. h
  2983. @result{}
  2984. #(() () () () () () ())
  2985. ;; Inserting into a hash table can be done with hashq-set!
  2986. (hashq-set! h 'foo "bar")
  2987. @result{}
  2988. "bar"
  2989. (hashq-set! h 'braz "zonk")
  2990. @result{}
  2991. "zonk"
  2992. ;; Or with hash-create-handle!
  2993. (hashq-create-handle! h 'frob #f)
  2994. @result{}
  2995. (frob . #f)
  2996. ;; The vector now contains three elements in the alists and the frob
  2997. ;; entry is at index (hashq 'frob).
  2998. h
  2999. @result{}
  3000. #(() () () () ((frob . #f) (braz . "zonk")) () ((foo . "bar")))
  3001. (hashq 'frob)
  3002. @result{}
  3003. 4
  3004. @end lisp
  3005. You can get the value for a given key with the procedure
  3006. @code{hashq-ref}, but the problem with this procedure is that you
  3007. cannot reliably determine whether a key does exists in the table. The
  3008. reason is that the procedure returns @code{#f} if the key is not in
  3009. the table, but it will return the same value if the key is in the
  3010. table and just happens to have the value @code{#f}, as you can see in
  3011. the following examples.
  3012. @lisp
  3013. (hashq-ref h 'foo)
  3014. @result{}
  3015. "bar"
  3016. (hashq-ref h 'frob)
  3017. @result{}
  3018. #f
  3019. (hashq-ref h 'not-there)
  3020. @result{}
  3021. #f
  3022. @end lisp
  3023. Better is to use the procedure @code{hashq-get-handle}, which makes a
  3024. distinction between the two cases. Just like @code{assq}, this
  3025. procedure returns a key/value-pair on success, and @code{#f} if the
  3026. key is not found.
  3027. @lisp
  3028. (hashq-get-handle h 'foo)
  3029. @result{}
  3030. (foo . "bar")
  3031. (hashq-get-handle h 'not-there)
  3032. @result{}
  3033. #f
  3034. @end lisp
  3035. There is no procedure for calculating the number of key/value-pairs in
  3036. a hash table, but @code{hash-fold} can be used for doing exactly that.
  3037. @lisp
  3038. (hash-fold (lambda (key value seed) (+ 1 seed)) 0 h)
  3039. @result{}
  3040. 3
  3041. @end lisp
  3042. @node Hash Table Reference
  3043. @subsubsection Hash Table Reference
  3044. @c FIXME: Describe in broad terms what happens for resizing, and what
  3045. @c the initial size means for this.
  3046. Like the association list functions, the hash table functions come in
  3047. several varieties, according to the equality test used for the keys.
  3048. Plain @code{hash-} functions use @code{equal?}, @code{hashq-}
  3049. functions use @code{eq?}, @code{hashv-} functions use @code{eqv?}, and
  3050. the @code{hashx-} functions use an application supplied test.
  3051. A single @code{make-hash-table} creates a hash table suitable for use
  3052. with any set of functions, but it's imperative that just one set is
  3053. then used consistently, or results will be unpredictable.
  3054. Hash tables are implemented as a vector indexed by a hash value formed
  3055. from the key, with an association list of key/value pairs for each
  3056. bucket in case distinct keys hash together. Direct access to the
  3057. pairs in those lists is provided by the @code{-handle-} functions.
  3058. The abstract kind of hash tables hide the vector in an opaque object
  3059. that represents the hash table, while for the concrete kind the vector
  3060. @emph{is} the hashtable.
  3061. When the number of table entries in an abstract hash table goes above
  3062. a threshold, the vector is made larger and the entries are rehashed,
  3063. to prevent the bucket lists from becoming too long and slowing down
  3064. accesses. When the number of entries goes below a threshold, the
  3065. vector is shrunk to save space.
  3066. A abstract hash table is created with @code{make-hash-table}. To
  3067. create a vector that is suitable as a hash table, use
  3068. @code{(make-vector @var{size} '())}, for example.
  3069. For the @code{hashx-} ``extended'' routines, an application supplies a
  3070. @var{hash} function producing an integer index like @code{hashq} etc
  3071. below, and an @var{assoc} alist search function like @code{assq} etc
  3072. (@pxref{Retrieving Alist Entries}). Here's an example of such
  3073. functions implementing case-insensitive hashing of string keys,
  3074. @example
  3075. (use-modules (srfi srfi-1)
  3076. (srfi srfi-13))
  3077. (define (my-hash str size)
  3078. (remainder (string-hash-ci str) size))
  3079. (define (my-assoc str alist)
  3080. (find (lambda (pair) (string-ci=? str (car pair))) alist))
  3081. (define my-table (make-hash-table))
  3082. (hashx-set! my-hash my-assoc my-table "foo" 123)
  3083. (hashx-ref my-hash my-assoc my-table "FOO")
  3084. @result{} 123
  3085. @end example
  3086. In a @code{hashx-} @var{hash} function the aim is to spread keys
  3087. across the vector, so bucket lists don't become long. But the actual
  3088. values are arbitrary as long as they're in the range 0 to
  3089. @math{@var{size}-1}. Helpful functions for forming a hash value, in
  3090. addition to @code{hashq} etc below, include @code{symbol-hash}
  3091. (@pxref{Symbol Keys}), @code{string-hash} and @code{string-hash-ci}
  3092. (@pxref{String Comparison}), and @code{char-set-hash}
  3093. (@pxref{Character Set Predicates/Comparison}).
  3094. @sp 1
  3095. @deffn {Scheme Procedure} make-hash-table [size]
  3096. Create a new abstract hash table object, with an optional minimum
  3097. vector @var{size}.
  3098. When @var{size} is given, the table vector will still grow and shrink
  3099. automatically, as described above, but with @var{size} as a minimum.
  3100. If an application knows roughly how many entries the table will hold
  3101. then it can use @var{size} to avoid rehashing when initial entries are
  3102. added.
  3103. @end deffn
  3104. @deffn {Scheme Procedure} hash-table? obj
  3105. @deffnx {C Function} scm_hash_table_p (obj)
  3106. Return @code{#t} if @var{obj} is a abstract hash table object.
  3107. @end deffn
  3108. @deffn {Scheme Procedure} hash-clear! table
  3109. @deffnx {C Function} scm_hash_clear_x (table)
  3110. Remove all items from @var{table} (without triggering a resize).
  3111. @end deffn
  3112. @deffn {Scheme Procedure} hash-ref table key [dflt]
  3113. @deffnx {Scheme Procedure} hashq-ref table key [dflt]
  3114. @deffnx {Scheme Procedure} hashv-ref table key [dflt]
  3115. @deffnx {Scheme Procedure} hashx-ref hash assoc table key [dflt]
  3116. @deffnx {C Function} scm_hash_ref (table, key, dflt)
  3117. @deffnx {C Function} scm_hashq_ref (table, key, dflt)
  3118. @deffnx {C Function} scm_hashv_ref (table, key, dflt)
  3119. @deffnx {C Function} scm_hashx_ref (hash, assoc, table, key, dflt)
  3120. Lookup @var{key} in the given hash @var{table}, and return the
  3121. associated value. If @var{key} is not found, return @var{dflt}, or
  3122. @code{#f} if @var{dflt} is not given.
  3123. @end deffn
  3124. @deffn {Scheme Procedure} hash-set! table key val
  3125. @deffnx {Scheme Procedure} hashq-set! table key val
  3126. @deffnx {Scheme Procedure} hashv-set! table key val
  3127. @deffnx {Scheme Procedure} hashx-set! hash assoc table key val
  3128. @deffnx {C Function} scm_hash_set_x (table, key, val)
  3129. @deffnx {C Function} scm_hashq_set_x (table, key, val)
  3130. @deffnx {C Function} scm_hashv_set_x (table, key, val)
  3131. @deffnx {C Function} scm_hashx_set_x (hash, assoc, table, key, val)
  3132. Associate @var{val} with @var{key} in the given hash @var{table}. If
  3133. @var{key} is already present then it's associated value is changed.
  3134. If it's not present then a new entry is created.
  3135. @end deffn
  3136. @deffn {Scheme Procedure} hash-remove! table key
  3137. @deffnx {Scheme Procedure} hashq-remove! table key
  3138. @deffnx {Scheme Procedure} hashv-remove! table key
  3139. @deffnx {Scheme Procedure} hashx-remove! hash assoc table key
  3140. @deffnx {C Function} scm_hash_remove_x (table, key)
  3141. @deffnx {C Function} scm_hashq_remove_x (table, key)
  3142. @deffnx {C Function} scm_hashv_remove_x (table, key)
  3143. @deffnx {C Function} scm_hashx_remove_x (hash, assoc, table, key)
  3144. Remove any association for @var{key} in the given hash @var{table}.
  3145. If @var{key} is not in @var{table} then nothing is done.
  3146. @end deffn
  3147. @deffn {Scheme Procedure} hash key size
  3148. @deffnx {Scheme Procedure} hashq key size
  3149. @deffnx {Scheme Procedure} hashv key size
  3150. @deffnx {C Function} scm_hash (key, size)
  3151. @deffnx {C Function} scm_hashq (key, size)
  3152. @deffnx {C Function} scm_hashv (key, size)
  3153. Return a hash value for @var{key}. This is a number in the range
  3154. @math{0} to @math{@var{size}-1}, which is suitable for use in a hash
  3155. table of the given @var{size}.
  3156. Note that @code{hashq} and @code{hashv} may use internal addresses of
  3157. objects, so if an object is garbage collected and re-created it can
  3158. have a different hash value, even when the two are notionally
  3159. @code{eq?}. For instance with symbols,
  3160. @example
  3161. (hashq 'something 123) @result{} 19
  3162. (gc)
  3163. (hashq 'something 123) @result{} 62
  3164. @end example
  3165. In normal use this is not a problem, since an object entered into a
  3166. hash table won't be garbage collected until removed. It's only if
  3167. hashing calculations are somehow separated from normal references that
  3168. its lifetime needs to be considered.
  3169. @end deffn
  3170. @deffn {Scheme Procedure} hash-get-handle table key
  3171. @deffnx {Scheme Procedure} hashq-get-handle table key
  3172. @deffnx {Scheme Procedure} hashv-get-handle table key
  3173. @deffnx {Scheme Procedure} hashx-get-handle hash assoc table key
  3174. @deffnx {C Function} scm_hash_get_handle (table, key)
  3175. @deffnx {C Function} scm_hashq_get_handle (table, key)
  3176. @deffnx {C Function} scm_hashv_get_handle (table, key)
  3177. @deffnx {C Function} scm_hashx_get_handle (hash, assoc, table, key)
  3178. Return the @code{(@var{key} . @var{value})} pair for @var{key} in the
  3179. given hash @var{table}, or @code{#f} if @var{key} is not in
  3180. @var{table}.
  3181. @end deffn
  3182. @deffn {Scheme Procedure} hash-create-handle! table key init
  3183. @deffnx {Scheme Procedure} hashq-create-handle! table key init
  3184. @deffnx {Scheme Procedure} hashv-create-handle! table key init
  3185. @deffnx {Scheme Procedure} hashx-create-handle! hash assoc table key init
  3186. @deffnx {C Function} scm_hash_create_handle_x (table, key, init)
  3187. @deffnx {C Function} scm_hashq_create_handle_x (table, key, init)
  3188. @deffnx {C Function} scm_hashv_create_handle_x (table, key, init)
  3189. @deffnx {C Function} scm_hashx_create_handle_x (hash, assoc, table, key, init)
  3190. Return the @code{(@var{key} . @var{value})} pair for @var{key} in the
  3191. given hash @var{table}. If @var{key} is not in @var{table} then
  3192. create an entry for it with @var{init} as the value, and return that
  3193. pair.
  3194. @end deffn
  3195. @deffn {Scheme Procedure} hash-map->list proc table
  3196. @deffnx {Scheme Procedure} hash-for-each proc table
  3197. @deffnx {C Function} scm_hash_map_to_list (proc, table)
  3198. @deffnx {C Function} scm_hash_for_each (proc, table)
  3199. Apply @var{proc} to the entries in the given hash @var{table}. Each
  3200. call is @code{(@var{proc} @var{key} @var{value})}. @code{hash-map->list}
  3201. returns a list of the results from these calls, @code{hash-for-each}
  3202. discards the results and returns an unspecified value.
  3203. Calls are made over the table entries in an unspecified order, and for
  3204. @code{hash-map->list} the order of the values in the returned list is
  3205. unspecified. Results will be unpredictable if @var{table} is modified
  3206. while iterating.
  3207. For example the following returns a new alist comprising all the
  3208. entries from @code{mytable}, in no particular order.
  3209. @example
  3210. (hash-map->list cons mytable)
  3211. @end example
  3212. @end deffn
  3213. @deffn {Scheme Procedure} hash-for-each-handle proc table
  3214. @deffnx {C Function} scm_hash_for_each_handle (proc, table)
  3215. Apply @var{proc} to the entries in the given hash @var{table}. Each
  3216. call is @code{(@var{proc} @var{handle})}, where @var{handle} is a
  3217. @code{(@var{key} . @var{value})} pair. Return an unspecified value.
  3218. @code{hash-for-each-handle} differs from @code{hash-for-each} only in
  3219. the argument list of @var{proc}.
  3220. @end deffn
  3221. @deffn {Scheme Procedure} hash-fold proc init table
  3222. @deffnx {C Function} scm_hash_fold (proc, init, table)
  3223. Accumulate a result by applying @var{proc} to the elements of the
  3224. given hash @var{table}. Each call is @code{(@var{proc} @var{key}
  3225. @var{value} @var{prior-result})}, where @var{key} and @var{value} are
  3226. from the @var{table} and @var{prior-result} is the return from the
  3227. previous @var{proc} call. For the first call, @var{prior-result} is
  3228. the given @var{init} value.
  3229. Calls are made over the table entries in an unspecified order.
  3230. Results will be unpredictable if @var{table} is modified while
  3231. @code{hash-fold} is running.
  3232. For example, the following returns a count of how many keys in
  3233. @code{mytable} are strings.
  3234. @example
  3235. (hash-fold (lambda (key value prior)
  3236. (if (string? key) (1+ prior) prior))
  3237. 0 mytable)
  3238. @end example
  3239. @end deffn
  3240. @c Local Variables:
  3241. @c TeX-master: "guile.texi"
  3242. @c End: