srfi-modules.texi 189 KB

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