api-data.texi 213 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988
  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,
  4. @c 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Simple Data Types
  7. @section Simple Generic Data Types
  8. This chapter describes those of Guile's simple data types which are
  9. primarily used for their role as items of generic data. By
  10. @dfn{simple} we mean data types that are not primarily used as
  11. containers to hold other data --- i.e.@: pairs, lists, vectors and so on.
  12. For the documentation of such @dfn{compound} data types, see
  13. @ref{Compound Data Types}.
  14. @c One of the great strengths of Scheme is that there is no straightforward
  15. @c distinction between ``data'' and ``functionality''. For example,
  16. @c Guile's support for dynamic linking could be described:
  17. @c @itemize @bullet
  18. @c @item
  19. @c either in a ``data-centric'' way, as the behaviour and properties of the
  20. @c ``dynamically linked object'' data type, and the operations that may be
  21. @c applied to instances of this type
  22. @c @item
  23. @c or in a ``functionality-centric'' way, as the set of procedures that
  24. @c constitute Guile's support for dynamic linking, in the context of the
  25. @c module system.
  26. @c @end itemize
  27. @c The contents of this chapter are, therefore, a matter of judgment. By
  28. @c @dfn{generic}, we mean to select those data types whose typical use as
  29. @c @emph{data} in a wide variety of programming contexts is more important
  30. @c than their use in the implementation of a particular piece of
  31. @c @emph{functionality}. The last section of this chapter provides
  32. @c references for all the data types that are documented not here but in a
  33. @c ``functionality-centric'' way elsewhere in the manual.
  34. @menu
  35. * Booleans:: True/false values.
  36. * Numbers:: Numerical data types.
  37. * Characters:: Single characters.
  38. * Character Sets:: Sets of characters.
  39. * Strings:: Sequences of characters.
  40. * Bytevectors:: Sequences of bytes.
  41. * Symbols:: Symbols.
  42. * Keywords:: Self-quoting, customizable display keywords.
  43. * Other Types:: "Functionality-centric" data types.
  44. @end menu
  45. @node Booleans
  46. @subsection Booleans
  47. @tpindex Booleans
  48. The two boolean values are @code{#t} for true and @code{#f} for false.
  49. Boolean values are returned by predicate procedures, such as the general
  50. equality predicates @code{eq?}, @code{eqv?} and @code{equal?}
  51. (@pxref{Equality}) and numerical and string comparison operators like
  52. @code{string=?} (@pxref{String Comparison}) and @code{<=}
  53. (@pxref{Comparison}).
  54. @lisp
  55. (<= 3 8)
  56. @result{} #t
  57. (<= 3 -3)
  58. @result{} #f
  59. (equal? "house" "houses")
  60. @result{} #f
  61. (eq? #f #f)
  62. @result{}
  63. #t
  64. @end lisp
  65. In test condition contexts like @code{if} and @code{cond}
  66. (@pxref{Conditionals}), where a group of subexpressions will be
  67. evaluated only if a @var{condition} expression evaluates to ``true'',
  68. ``true'' means any value at all except @code{#f}.
  69. @lisp
  70. (if #t "yes" "no")
  71. @result{} "yes"
  72. (if 0 "yes" "no")
  73. @result{} "yes"
  74. (if #f "yes" "no")
  75. @result{} "no"
  76. @end lisp
  77. A result of this asymmetry is that typical Scheme source code more often
  78. uses @code{#f} explicitly than @code{#t}: @code{#f} is necessary to
  79. represent an @code{if} or @code{cond} false value, whereas @code{#t} is
  80. not necessary to represent an @code{if} or @code{cond} true value.
  81. It is important to note that @code{#f} is @strong{not} equivalent to any
  82. other Scheme value. In particular, @code{#f} is not the same as the
  83. number 0 (like in C and C++), and not the same as the ``empty list''
  84. (like in some Lisp dialects).
  85. In C, the two Scheme boolean values are available as the two constants
  86. @code{SCM_BOOL_T} for @code{#t} and @code{SCM_BOOL_F} for @code{#f}.
  87. Care must be taken with the false value @code{SCM_BOOL_F}: it is not
  88. false when used in C conditionals. In order to test for it, use
  89. @code{scm_is_false} or @code{scm_is_true}.
  90. @rnindex not
  91. @deffn {Scheme Procedure} not x
  92. @deffnx {C Function} scm_not (x)
  93. Return @code{#t} if @var{x} is @code{#f}, else return @code{#f}.
  94. @end deffn
  95. @rnindex boolean?
  96. @deffn {Scheme Procedure} boolean? obj
  97. @deffnx {C Function} scm_boolean_p (obj)
  98. Return @code{#t} if @var{obj} is either @code{#t} or @code{#f}, else
  99. return @code{#f}.
  100. @end deffn
  101. @deftypevr {C Macro} SCM SCM_BOOL_T
  102. The @code{SCM} representation of the Scheme object @code{#t}.
  103. @end deftypevr
  104. @deftypevr {C Macro} SCM SCM_BOOL_F
  105. The @code{SCM} representation of the Scheme object @code{#f}.
  106. @end deftypevr
  107. @deftypefn {C Function} int scm_is_true (SCM obj)
  108. Return @code{0} if @var{obj} is @code{#f}, else return @code{1}.
  109. @end deftypefn
  110. @deftypefn {C Function} int scm_is_false (SCM obj)
  111. Return @code{1} if @var{obj} is @code{#f}, else return @code{0}.
  112. @end deftypefn
  113. @deftypefn {C Function} int scm_is_bool (SCM obj)
  114. Return @code{1} if @var{obj} is either @code{#t} or @code{#f}, else
  115. return @code{0}.
  116. @end deftypefn
  117. @deftypefn {C Function} SCM scm_from_bool (int val)
  118. Return @code{#f} if @var{val} is @code{0}, else return @code{#t}.
  119. @end deftypefn
  120. @deftypefn {C Function} int scm_to_bool (SCM val)
  121. Return @code{1} if @var{val} is @code{SCM_BOOL_T}, return @code{0}
  122. when @var{val} is @code{SCM_BOOL_F}, else signal a `wrong type' error.
  123. You should probably use @code{scm_is_true} instead of this function
  124. when you just want to test a @code{SCM} value for trueness.
  125. @end deftypefn
  126. @node Numbers
  127. @subsection Numerical data types
  128. @tpindex Numbers
  129. Guile supports a rich ``tower'' of numerical types --- integer,
  130. rational, real and complex --- and provides an extensive set of
  131. mathematical and scientific functions for operating on numerical
  132. data. This section of the manual documents those types and functions.
  133. You may also find it illuminating to read R5RS's presentation of numbers
  134. in Scheme, which is particularly clear and accessible: see
  135. @ref{Numbers,,,r5rs,R5RS}.
  136. @menu
  137. * Numerical Tower:: Scheme's numerical "tower".
  138. * Integers:: Whole numbers.
  139. * Reals and Rationals:: Real and rational numbers.
  140. * Complex Numbers:: Complex numbers.
  141. * Exactness:: Exactness and inexactness.
  142. * Number Syntax:: Read syntax for numerical data.
  143. * Integer Operations:: Operations on integer values.
  144. * Comparison:: Comparison predicates.
  145. * Conversion:: Converting numbers to and from strings.
  146. * Complex:: Complex number operations.
  147. * Arithmetic:: Arithmetic functions.
  148. * Scientific:: Scientific functions.
  149. * Bitwise Operations:: Logical AND, OR, NOT, and so on.
  150. * Random:: Random number generation.
  151. @end menu
  152. @node Numerical Tower
  153. @subsubsection Scheme's Numerical ``Tower''
  154. @rnindex number?
  155. Scheme's numerical ``tower'' consists of the following categories of
  156. numbers:
  157. @table @dfn
  158. @item integers
  159. Whole numbers, positive or negative; e.g.@: --5, 0, 18.
  160. @item rationals
  161. The set of numbers that can be expressed as @math{@var{p}/@var{q}}
  162. where @var{p} and @var{q} are integers; e.g.@: @math{9/16} works, but
  163. pi (an irrational number) doesn't. These include integers
  164. (@math{@var{n}/1}).
  165. @item real numbers
  166. The set of numbers that describes all possible positions along a
  167. one-dimensional line. This includes rationals as well as irrational
  168. numbers.
  169. @item complex numbers
  170. The set of numbers that describes all possible positions in a two
  171. dimensional space. This includes real as well as imaginary numbers
  172. (@math{@var{a}+@var{b}i}, where @var{a} is the @dfn{real part},
  173. @var{b} is the @dfn{imaginary part}, and @math{i} is the square root of
  174. @minus{}1.)
  175. @end table
  176. It is called a tower because each category ``sits on'' the one that
  177. follows it, in the sense that every integer is also a rational, every
  178. rational is also real, and every real number is also a complex number
  179. (but with zero imaginary part).
  180. In addition to the classification into integers, rationals, reals and
  181. complex numbers, Scheme also distinguishes between whether a number is
  182. represented exactly or not. For example, the result of
  183. @m{2\sin(\pi/4),2*sin(pi/4)} is exactly @m{\sqrt{2},2^(1/2)}, but Guile
  184. can represent neither @m{\pi/4,pi/4} nor @m{\sqrt{2},2^(1/2)} exactly.
  185. Instead, it stores an inexact approximation, using the C type
  186. @code{double}.
  187. Guile can represent exact rationals of any magnitude, inexact
  188. rationals that fit into a C @code{double}, and inexact complex numbers
  189. with @code{double} real and imaginary parts.
  190. The @code{number?} predicate may be applied to any Scheme value to
  191. discover whether the value is any of the supported numerical types.
  192. @deffn {Scheme Procedure} number? obj
  193. @deffnx {C Function} scm_number_p (obj)
  194. Return @code{#t} if @var{obj} is any kind of number, else @code{#f}.
  195. @end deffn
  196. For example:
  197. @lisp
  198. (number? 3)
  199. @result{} #t
  200. (number? "hello there!")
  201. @result{} #f
  202. (define pi 3.141592654)
  203. (number? pi)
  204. @result{} #t
  205. @end lisp
  206. @deftypefn {C Function} int scm_is_number (SCM obj)
  207. This is equivalent to @code{scm_is_true (scm_number_p (obj))}.
  208. @end deftypefn
  209. The next few subsections document each of Guile's numerical data types
  210. in detail.
  211. @node Integers
  212. @subsubsection Integers
  213. @tpindex Integer numbers
  214. @rnindex integer?
  215. Integers are whole numbers, that is numbers with no fractional part,
  216. such as 2, 83, and @minus{}3789.
  217. Integers in Guile can be arbitrarily big, as shown by the following
  218. example.
  219. @lisp
  220. (define (factorial n)
  221. (let loop ((n n) (product 1))
  222. (if (= n 0)
  223. product
  224. (loop (- n 1) (* product n)))))
  225. (factorial 3)
  226. @result{} 6
  227. (factorial 20)
  228. @result{} 2432902008176640000
  229. (- (factorial 45))
  230. @result{} -119622220865480194561963161495657715064383733760000000000
  231. @end lisp
  232. Readers whose background is in programming languages where integers are
  233. limited by the need to fit into just 4 or 8 bytes of memory may find
  234. this surprising, or suspect that Guile's representation of integers is
  235. inefficient. In fact, Guile achieves a near optimal balance of
  236. convenience and efficiency by using the host computer's native
  237. representation of integers where possible, and a more general
  238. representation where the required number does not fit in the native
  239. form. Conversion between these two representations is automatic and
  240. completely invisible to the Scheme level programmer.
  241. C has a host of different integer types, and Guile offers a host of
  242. functions to convert between them and the @code{SCM} representation.
  243. For example, a C @code{int} can be handled with @code{scm_to_int} and
  244. @code{scm_from_int}. Guile also defines a few C integer types of its
  245. own, to help with differences between systems.
  246. C integer types that are not covered can be handled with the generic
  247. @code{scm_to_signed_integer} and @code{scm_from_signed_integer} for
  248. signed types, or with @code{scm_to_unsigned_integer} and
  249. @code{scm_from_unsigned_integer} for unsigned types.
  250. Scheme integers can be exact and inexact. For example, a number
  251. written as @code{3.0} with an explicit decimal-point is inexact, but
  252. it is also an integer. The functions @code{integer?} and
  253. @code{scm_is_integer} report true for such a number, but the functions
  254. @code{scm_is_signed_integer} and @code{scm_is_unsigned_integer} only
  255. allow exact integers and thus report false. Likewise, the conversion
  256. functions like @code{scm_to_signed_integer} only accept exact
  257. integers.
  258. The motivation for this behavior is that the inexactness of a number
  259. should not be lost silently. If you want to allow inexact integers,
  260. you can explicitly insert a call to @code{inexact->exact} or to its C
  261. equivalent @code{scm_inexact_to_exact}. (Only inexact integers will
  262. be converted by this call into exact integers; inexact non-integers
  263. will become exact fractions.)
  264. @deffn {Scheme Procedure} integer? x
  265. @deffnx {C Function} scm_integer_p (x)
  266. Return @code{#t} if @var{x} is an exact or inexact integer number, else
  267. @code{#f}.
  268. @lisp
  269. (integer? 487)
  270. @result{} #t
  271. (integer? 3.0)
  272. @result{} #t
  273. (integer? -3.4)
  274. @result{} #f
  275. (integer? +inf.0)
  276. @result{} #t
  277. @end lisp
  278. @end deffn
  279. @deftypefn {C Function} int scm_is_integer (SCM x)
  280. This is equivalent to @code{scm_is_true (scm_integer_p (x))}.
  281. @end deftypefn
  282. @defvr {C Type} scm_t_int8
  283. @defvrx {C Type} scm_t_uint8
  284. @defvrx {C Type} scm_t_int16
  285. @defvrx {C Type} scm_t_uint16
  286. @defvrx {C Type} scm_t_int32
  287. @defvrx {C Type} scm_t_uint32
  288. @defvrx {C Type} scm_t_int64
  289. @defvrx {C Type} scm_t_uint64
  290. @defvrx {C Type} scm_t_intmax
  291. @defvrx {C Type} scm_t_uintmax
  292. The C types are equivalent to the corresponding ISO C types but are
  293. defined on all platforms, with the exception of @code{scm_t_int64} and
  294. @code{scm_t_uint64}, which are only defined when a 64-bit type is
  295. available. For example, @code{scm_t_int8} is equivalent to
  296. @code{int8_t}.
  297. You can regard these definitions as a stop-gap measure until all
  298. platforms provide these types. If you know that all the platforms
  299. that you are interested in already provide these types, it is better
  300. to use them directly instead of the types provided by Guile.
  301. @end defvr
  302. @deftypefn {C Function} int scm_is_signed_integer (SCM x, scm_t_intmax min, scm_t_intmax max)
  303. @deftypefnx {C Function} int scm_is_unsigned_integer (SCM x, scm_t_uintmax min, scm_t_uintmax max)
  304. Return @code{1} when @var{x} represents an exact integer that is
  305. between @var{min} and @var{max}, inclusive.
  306. These functions can be used to check whether a @code{SCM} value will
  307. fit into a given range, such as the range of a given C integer type.
  308. If you just want to convert a @code{SCM} value to a given C integer
  309. type, use one of the conversion functions directly.
  310. @end deftypefn
  311. @deftypefn {C Function} scm_t_intmax scm_to_signed_integer (SCM x, scm_t_intmax min, scm_t_intmax max)
  312. @deftypefnx {C Function} scm_t_uintmax scm_to_unsigned_integer (SCM x, scm_t_uintmax min, scm_t_uintmax max)
  313. When @var{x} represents an exact integer that is between @var{min} and
  314. @var{max} inclusive, return that integer. Else signal an error,
  315. either a `wrong-type' error when @var{x} is not an exact integer, or
  316. an `out-of-range' error when it doesn't fit the given range.
  317. @end deftypefn
  318. @deftypefn {C Function} SCM scm_from_signed_integer (scm_t_intmax x)
  319. @deftypefnx {C Function} SCM scm_from_unsigned_integer (scm_t_uintmax x)
  320. Return the @code{SCM} value that represents the integer @var{x}. This
  321. function will always succeed and will always return an exact number.
  322. @end deftypefn
  323. @deftypefn {C Function} char scm_to_char (SCM x)
  324. @deftypefnx {C Function} {signed char} scm_to_schar (SCM x)
  325. @deftypefnx {C Function} {unsigned char} scm_to_uchar (SCM x)
  326. @deftypefnx {C Function} short scm_to_short (SCM x)
  327. @deftypefnx {C Function} {unsigned short} scm_to_ushort (SCM x)
  328. @deftypefnx {C Function} int scm_to_int (SCM x)
  329. @deftypefnx {C Function} {unsigned int} scm_to_uint (SCM x)
  330. @deftypefnx {C Function} long scm_to_long (SCM x)
  331. @deftypefnx {C Function} {unsigned long} scm_to_ulong (SCM x)
  332. @deftypefnx {C Function} {long long} scm_to_long_long (SCM x)
  333. @deftypefnx {C Function} {unsigned long long} scm_to_ulong_long (SCM x)
  334. @deftypefnx {C Function} size_t scm_to_size_t (SCM x)
  335. @deftypefnx {C Function} ssize_t scm_to_ssize_t (SCM x)
  336. @deftypefnx {C Function} scm_t_ptrdiff scm_to_ptrdiff_t (SCM x)
  337. @deftypefnx {C Function} scm_t_int8 scm_to_int8 (SCM x)
  338. @deftypefnx {C Function} scm_t_uint8 scm_to_uint8 (SCM x)
  339. @deftypefnx {C Function} scm_t_int16 scm_to_int16 (SCM x)
  340. @deftypefnx {C Function} scm_t_uint16 scm_to_uint16 (SCM x)
  341. @deftypefnx {C Function} scm_t_int32 scm_to_int32 (SCM x)
  342. @deftypefnx {C Function} scm_t_uint32 scm_to_uint32 (SCM x)
  343. @deftypefnx {C Function} scm_t_int64 scm_to_int64 (SCM x)
  344. @deftypefnx {C Function} scm_t_uint64 scm_to_uint64 (SCM x)
  345. @deftypefnx {C Function} scm_t_intmax scm_to_intmax (SCM x)
  346. @deftypefnx {C Function} scm_t_uintmax scm_to_uintmax (SCM x)
  347. When @var{x} represents an exact integer that fits into the indicated
  348. C type, return that integer. Else signal an error, either a
  349. `wrong-type' error when @var{x} is not an exact integer, or an
  350. `out-of-range' error when it doesn't fit the given range.
  351. The functions @code{scm_to_long_long}, @code{scm_to_ulong_long},
  352. @code{scm_to_int64}, and @code{scm_to_uint64} are only available when
  353. the corresponding types are.
  354. @end deftypefn
  355. @deftypefn {C Function} SCM scm_from_char (char x)
  356. @deftypefnx {C Function} SCM scm_from_schar (signed char x)
  357. @deftypefnx {C Function} SCM scm_from_uchar (unsigned char x)
  358. @deftypefnx {C Function} SCM scm_from_short (short x)
  359. @deftypefnx {C Function} SCM scm_from_ushort (unsigned short x)
  360. @deftypefnx {C Function} SCM scm_from_int (int x)
  361. @deftypefnx {C Function} SCM scm_from_uint (unsigned int x)
  362. @deftypefnx {C Function} SCM scm_from_long (long x)
  363. @deftypefnx {C Function} SCM scm_from_ulong (unsigned long x)
  364. @deftypefnx {C Function} SCM scm_from_long_long (long long x)
  365. @deftypefnx {C Function} SCM scm_from_ulong_long (unsigned long long x)
  366. @deftypefnx {C Function} SCM scm_from_size_t (size_t x)
  367. @deftypefnx {C Function} SCM scm_from_ssize_t (ssize_t x)
  368. @deftypefnx {C Function} SCM scm_from_ptrdiff_t (scm_t_ptrdiff x)
  369. @deftypefnx {C Function} SCM scm_from_int8 (scm_t_int8 x)
  370. @deftypefnx {C Function} SCM scm_from_uint8 (scm_t_uint8 x)
  371. @deftypefnx {C Function} SCM scm_from_int16 (scm_t_int16 x)
  372. @deftypefnx {C Function} SCM scm_from_uint16 (scm_t_uint16 x)
  373. @deftypefnx {C Function} SCM scm_from_int32 (scm_t_int32 x)
  374. @deftypefnx {C Function} SCM scm_from_uint32 (scm_t_uint32 x)
  375. @deftypefnx {C Function} SCM scm_from_int64 (scm_t_int64 x)
  376. @deftypefnx {C Function} SCM scm_from_uint64 (scm_t_uint64 x)
  377. @deftypefnx {C Function} SCM scm_from_intmax (scm_t_intmax x)
  378. @deftypefnx {C Function} SCM scm_from_uintmax (scm_t_uintmax x)
  379. Return the @code{SCM} value that represents the integer @var{x}.
  380. These functions will always succeed and will always return an exact
  381. number.
  382. @end deftypefn
  383. @deftypefn {C Function} void scm_to_mpz (SCM val, mpz_t rop)
  384. Assign @var{val} to the multiple precision integer @var{rop}.
  385. @var{val} must be an exact integer, otherwise an error will be
  386. signalled. @var{rop} must have been initialized with @code{mpz_init}
  387. before this function is called. When @var{rop} is no longer needed
  388. the occupied space must be freed with @code{mpz_clear}.
  389. @xref{Initializing Integers,,, gmp, GNU MP Manual}, for details.
  390. @end deftypefn
  391. @deftypefn {C Function} SCM scm_from_mpz (mpz_t val)
  392. Return the @code{SCM} value that represents @var{val}.
  393. @end deftypefn
  394. @node Reals and Rationals
  395. @subsubsection Real and Rational Numbers
  396. @tpindex Real numbers
  397. @tpindex Rational numbers
  398. @rnindex real?
  399. @rnindex rational?
  400. Mathematically, the real numbers are the set of numbers that describe
  401. all possible points along a continuous, infinite, one-dimensional line.
  402. The rational numbers are the set of all numbers that can be written as
  403. fractions @var{p}/@var{q}, where @var{p} and @var{q} are integers.
  404. All rational numbers are also real, but there are real numbers that
  405. are not rational, for example @m{\sqrt{2}, the square root of 2}, and
  406. @m{\pi,pi}.
  407. Guile can represent both exact and inexact rational numbers, but it
  408. cannot represent precise finite irrational numbers. Exact rationals are
  409. represented by storing the numerator and denominator as two exact
  410. integers. Inexact rationals are stored as floating point numbers using
  411. the C type @code{double}.
  412. Exact rationals are written as a fraction of integers. There must be
  413. no whitespace around the slash:
  414. @lisp
  415. 1/2
  416. -22/7
  417. @end lisp
  418. Even though the actual encoding of inexact rationals is in binary, it
  419. may be helpful to think of it as a decimal number with a limited
  420. number of significant figures and a decimal point somewhere, since
  421. this corresponds to the standard notation for non-whole numbers. For
  422. example:
  423. @lisp
  424. 0.34
  425. -0.00000142857931198
  426. -5648394822220000000000.0
  427. 4.0
  428. @end lisp
  429. The limited precision of Guile's encoding means that any finite ``real''
  430. number in Guile can be written in a rational form, by multiplying and
  431. then dividing by sufficient powers of 10 (or in fact, 2). For example,
  432. @samp{-0.00000142857931198} is the same as @minus{}142857931198 divided
  433. by 100000000000000000. In Guile's current incarnation, therefore, the
  434. @code{rational?} and @code{real?} predicates are equivalent for finite
  435. numbers.
  436. Dividing by an exact zero leads to a error message, as one might expect.
  437. However, dividing by an inexact zero does not produce an error.
  438. Instead, the result of the division is either plus or minus infinity,
  439. depending on the sign of the divided number and the sign of the zero
  440. divisor (some platforms support signed zeroes @samp{-0.0} and
  441. @samp{+0.0}; @samp{0.0} is the same as @samp{+0.0}).
  442. Dividing zero by an inexact zero yields a @acronym{NaN} (`not a number')
  443. value, although they are actually considered numbers by Scheme.
  444. Attempts to compare a @acronym{NaN} value with any number (including
  445. itself) using @code{=}, @code{<}, @code{>}, @code{<=} or @code{>=}
  446. always returns @code{#f}. Although a @acronym{NaN} value is not
  447. @code{=} to itself, it is both @code{eqv?} and @code{equal?} to itself
  448. and other @acronym{NaN} values. However, the preferred way to test for
  449. them is by using @code{nan?}.
  450. The real @acronym{NaN} values and infinities are written @samp{+nan.0},
  451. @samp{+inf.0} and @samp{-inf.0}. This syntax is also recognized by
  452. @code{read} as an extension to the usual Scheme syntax. These special
  453. values are considered by Scheme to be inexact real numbers but not
  454. rational. Note that non-real complex numbers may also contain
  455. infinities or @acronym{NaN} values in their real or imaginary parts. To
  456. test a real number to see if it is infinite, a @acronym{NaN} value, or
  457. neither, use @code{inf?}, @code{nan?}, or @code{finite?}, respectively.
  458. Every real number in Scheme belongs to precisely one of those three
  459. classes.
  460. On platforms that follow @acronym{IEEE} 754 for their floating point
  461. arithmetic, the @samp{+inf.0}, @samp{-inf.0}, and @samp{+nan.0} values
  462. are implemented using the corresponding @acronym{IEEE} 754 values.
  463. They behave in arithmetic operations like @acronym{IEEE} 754 describes
  464. it, i.e., @code{(= +nan.0 +nan.0)} @result{} @code{#f}.
  465. @deffn {Scheme Procedure} real? obj
  466. @deffnx {C Function} scm_real_p (obj)
  467. Return @code{#t} if @var{obj} is a real number, else @code{#f}. Note
  468. that the sets of integer and rational values form subsets of the set
  469. of real numbers, so the predicate will also be fulfilled if @var{obj}
  470. is an integer number or a rational number.
  471. @end deffn
  472. @deffn {Scheme Procedure} rational? x
  473. @deffnx {C Function} scm_rational_p (x)
  474. Return @code{#t} if @var{x} is a rational number, @code{#f} otherwise.
  475. Note that the set of integer values forms a subset of the set of
  476. rational numbers, i.e.@: the predicate will also be fulfilled if
  477. @var{x} is an integer number.
  478. @end deffn
  479. @deffn {Scheme Procedure} rationalize x eps
  480. @deffnx {C Function} scm_rationalize (x, eps)
  481. Returns the @emph{simplest} rational number differing
  482. from @var{x} by no more than @var{eps}.
  483. As required by @acronym{R5RS}, @code{rationalize} only returns an
  484. exact result when both its arguments are exact. Thus, you might need
  485. to use @code{inexact->exact} on the arguments.
  486. @lisp
  487. (rationalize (inexact->exact 1.2) 1/100)
  488. @result{} 6/5
  489. @end lisp
  490. @end deffn
  491. @deffn {Scheme Procedure} inf? x
  492. @deffnx {C Function} scm_inf_p (x)
  493. Return @code{#t} if the real number @var{x} is @samp{+inf.0} or
  494. @samp{-inf.0}. Otherwise return @code{#f}.
  495. @end deffn
  496. @deffn {Scheme Procedure} nan? x
  497. @deffnx {C Function} scm_nan_p (x)
  498. Return @code{#t} if the real number @var{x} is @samp{+nan.0}, or
  499. @code{#f} otherwise.
  500. @end deffn
  501. @deffn {Scheme Procedure} finite? x
  502. @deffnx {C Function} scm_finite_p (x)
  503. Return @code{#t} if the real number @var{x} is neither infinite nor a
  504. NaN, @code{#f} otherwise.
  505. @end deffn
  506. @deffn {Scheme Procedure} nan
  507. @deffnx {C Function} scm_nan ()
  508. Return @samp{+nan.0}, a @acronym{NaN} value.
  509. @end deffn
  510. @deffn {Scheme Procedure} inf
  511. @deffnx {C Function} scm_inf ()
  512. Return @samp{+inf.0}, positive infinity.
  513. @end deffn
  514. @deffn {Scheme Procedure} numerator x
  515. @deffnx {C Function} scm_numerator (x)
  516. Return the numerator of the rational number @var{x}.
  517. @end deffn
  518. @deffn {Scheme Procedure} denominator x
  519. @deffnx {C Function} scm_denominator (x)
  520. Return the denominator of the rational number @var{x}.
  521. @end deffn
  522. @deftypefn {C Function} int scm_is_real (SCM val)
  523. @deftypefnx {C Function} int scm_is_rational (SCM val)
  524. Equivalent to @code{scm_is_true (scm_real_p (val))} and
  525. @code{scm_is_true (scm_rational_p (val))}, respectively.
  526. @end deftypefn
  527. @deftypefn {C Function} double scm_to_double (SCM val)
  528. Returns the number closest to @var{val} that is representable as a
  529. @code{double}. Returns infinity for a @var{val} that is too large in
  530. magnitude. The argument @var{val} must be a real number.
  531. @end deftypefn
  532. @deftypefn {C Function} SCM scm_from_double (double val)
  533. Return the @code{SCM} value that represents @var{val}. The returned
  534. value is inexact according to the predicate @code{inexact?}, but it
  535. will be exactly equal to @var{val}.
  536. @end deftypefn
  537. @node Complex Numbers
  538. @subsubsection Complex Numbers
  539. @tpindex Complex numbers
  540. @rnindex complex?
  541. Complex numbers are the set of numbers that describe all possible points
  542. in a two-dimensional space. The two coordinates of a particular point
  543. in this space are known as the @dfn{real} and @dfn{imaginary} parts of
  544. the complex number that describes that point.
  545. In Guile, complex numbers are written in rectangular form as the sum of
  546. their real and imaginary parts, using the symbol @code{i} to indicate
  547. the imaginary part.
  548. @lisp
  549. 3+4i
  550. @result{}
  551. 3.0+4.0i
  552. (* 3-8i 2.3+0.3i)
  553. @result{}
  554. 9.3-17.5i
  555. @end lisp
  556. @cindex polar form
  557. @noindent
  558. Polar form can also be used, with an @samp{@@} between magnitude and
  559. angle,
  560. @lisp
  561. 1@@3.141592 @result{} -1.0 (approx)
  562. -1@@1.57079 @result{} 0.0-1.0i (approx)
  563. @end lisp
  564. Guile represents a complex number as a pair of inexact reals, so the
  565. real and imaginary parts of a complex number have the same properties of
  566. inexactness and limited precision as single inexact real numbers.
  567. Note that each part of a complex number may contain any inexact real
  568. value, including the special values @samp{+nan.0}, @samp{+inf.0} and
  569. @samp{-inf.0}, as well as either of the signed zeroes @samp{0.0} or
  570. @samp{-0.0}.
  571. @deffn {Scheme Procedure} complex? z
  572. @deffnx {C Function} scm_complex_p (z)
  573. Return @code{#t} if @var{z} is a complex number, @code{#f}
  574. otherwise. Note that the sets of real, rational and integer
  575. values form subsets of the set of complex numbers, i.e.@: the
  576. predicate will also be fulfilled if @var{z} is a real,
  577. rational or integer number.
  578. @end deffn
  579. @deftypefn {C Function} int scm_is_complex (SCM val)
  580. Equivalent to @code{scm_is_true (scm_complex_p (val))}.
  581. @end deftypefn
  582. @node Exactness
  583. @subsubsection Exact and Inexact Numbers
  584. @tpindex Exact numbers
  585. @tpindex Inexact numbers
  586. @rnindex exact?
  587. @rnindex inexact?
  588. @rnindex exact->inexact
  589. @rnindex inexact->exact
  590. R5RS requires that, with few exceptions, a calculation involving inexact
  591. numbers always produces an inexact result. To meet this requirement,
  592. Guile distinguishes between an exact integer value such as @samp{5} and
  593. the corresponding inexact integer value which, to the limited precision
  594. available, has no fractional part, and is printed as @samp{5.0}. Guile
  595. will only convert the latter value to the former when forced to do so by
  596. an invocation of the @code{inexact->exact} procedure.
  597. The only exception to the above requirement is when the values of the
  598. inexact numbers do not affect the result. For example @code{(expt n 0)}
  599. is @samp{1} for any value of @code{n}, therefore @code{(expt 5.0 0)} is
  600. permitted to return an exact @samp{1}.
  601. @deffn {Scheme Procedure} exact? z
  602. @deffnx {C Function} scm_exact_p (z)
  603. Return @code{#t} if the number @var{z} is exact, @code{#f}
  604. otherwise.
  605. @lisp
  606. (exact? 2)
  607. @result{} #t
  608. (exact? 0.5)
  609. @result{} #f
  610. (exact? (/ 2))
  611. @result{} #t
  612. @end lisp
  613. @end deffn
  614. @deftypefn {C Function} int scm_is_exact (SCM z)
  615. Return a @code{1} if the number @var{z} is exact, and @code{0}
  616. otherwise. This is equivalent to @code{scm_is_true (scm_exact_p (z))}.
  617. An alternate approch to testing the exactness of a number is to
  618. use @code{scm_is_signed_integer} or @code{scm_is_unsigned_integer}.
  619. @end deftypefn
  620. @deffn {Scheme Procedure} inexact? z
  621. @deffnx {C Function} scm_inexact_p (z)
  622. Return @code{#t} if the number @var{z} is inexact, @code{#f}
  623. else.
  624. @end deffn
  625. @deftypefn {C Function} int scm_is_inexact (SCM z)
  626. Return a @code{1} if the number @var{z} is inexact, and @code{0}
  627. otherwise. This is equivalent to @code{scm_is_true (scm_inexact_p (z))}.
  628. @end deftypefn
  629. @deffn {Scheme Procedure} inexact->exact z
  630. @deffnx {C Function} scm_inexact_to_exact (z)
  631. Return an exact number that is numerically closest to @var{z}, when
  632. there is one. For inexact rationals, Guile returns the exact rational
  633. that is numerically equal to the inexact rational. Inexact complex
  634. numbers with a non-zero imaginary part can not be made exact.
  635. @lisp
  636. (inexact->exact 0.5)
  637. @result{} 1/2
  638. @end lisp
  639. The following happens because 12/10 is not exactly representable as a
  640. @code{double} (on most platforms). However, when reading a decimal
  641. number that has been marked exact with the ``#e'' prefix, Guile is
  642. able to represent it correctly.
  643. @lisp
  644. (inexact->exact 1.2)
  645. @result{} 5404319552844595/4503599627370496
  646. #e1.2
  647. @result{} 6/5
  648. @end lisp
  649. @end deffn
  650. @c begin (texi-doc-string "guile" "exact->inexact")
  651. @deffn {Scheme Procedure} exact->inexact z
  652. @deffnx {C Function} scm_exact_to_inexact (z)
  653. Convert the number @var{z} to its inexact representation.
  654. @end deffn
  655. @node Number Syntax
  656. @subsubsection Read Syntax for Numerical Data
  657. The read syntax for integers is a string of digits, optionally
  658. preceded by a minus or plus character, a code indicating the
  659. base in which the integer is encoded, and a code indicating whether
  660. the number is exact or inexact. The supported base codes are:
  661. @table @code
  662. @item #b
  663. @itemx #B
  664. the integer is written in binary (base 2)
  665. @item #o
  666. @itemx #O
  667. the integer is written in octal (base 8)
  668. @item #d
  669. @itemx #D
  670. the integer is written in decimal (base 10)
  671. @item #x
  672. @itemx #X
  673. the integer is written in hexadecimal (base 16)
  674. @end table
  675. If the base code is omitted, the integer is assumed to be decimal. The
  676. following examples show how these base codes are used.
  677. @lisp
  678. -13
  679. @result{} -13
  680. #d-13
  681. @result{} -13
  682. #x-13
  683. @result{} -19
  684. #b+1101
  685. @result{} 13
  686. #o377
  687. @result{} 255
  688. @end lisp
  689. The codes for indicating exactness (which can, incidentally, be applied
  690. to all numerical values) are:
  691. @table @code
  692. @item #e
  693. @itemx #E
  694. the number is exact
  695. @item #i
  696. @itemx #I
  697. the number is inexact.
  698. @end table
  699. If the exactness indicator is omitted, the number is exact unless it
  700. contains a radix point. Since Guile can not represent exact complex
  701. numbers, an error is signalled when asking for them.
  702. @lisp
  703. (exact? 1.2)
  704. @result{} #f
  705. (exact? #e1.2)
  706. @result{} #t
  707. (exact? #e+1i)
  708. ERROR: Wrong type argument
  709. @end lisp
  710. Guile also understands the syntax @samp{+inf.0} and @samp{-inf.0} for
  711. plus and minus infinity, respectively. The value must be written
  712. exactly as shown, that is, they always must have a sign and exactly
  713. one zero digit after the decimal point. It also understands
  714. @samp{+nan.0} and @samp{-nan.0} for the special `not-a-number' value.
  715. The sign is ignored for `not-a-number' and the value is always printed
  716. as @samp{+nan.0}.
  717. @node Integer Operations
  718. @subsubsection Operations on Integer Values
  719. @rnindex odd?
  720. @rnindex even?
  721. @rnindex quotient
  722. @rnindex remainder
  723. @rnindex modulo
  724. @rnindex gcd
  725. @rnindex lcm
  726. @deffn {Scheme Procedure} odd? n
  727. @deffnx {C Function} scm_odd_p (n)
  728. Return @code{#t} if @var{n} is an odd number, @code{#f}
  729. otherwise.
  730. @end deffn
  731. @deffn {Scheme Procedure} even? n
  732. @deffnx {C Function} scm_even_p (n)
  733. Return @code{#t} if @var{n} is an even number, @code{#f}
  734. otherwise.
  735. @end deffn
  736. @c begin (texi-doc-string "guile" "quotient")
  737. @c begin (texi-doc-string "guile" "remainder")
  738. @deffn {Scheme Procedure} quotient n d
  739. @deffnx {Scheme Procedure} remainder n d
  740. @deffnx {C Function} scm_quotient (n, d)
  741. @deffnx {C Function} scm_remainder (n, d)
  742. Return the quotient or remainder from @var{n} divided by @var{d}. The
  743. quotient is rounded towards zero, and the remainder will have the same
  744. sign as @var{n}. In all cases quotient and remainder satisfy
  745. @math{@var{n} = @var{q}*@var{d} + @var{r}}.
  746. @lisp
  747. (remainder 13 4) @result{} 1
  748. (remainder -13 4) @result{} -1
  749. @end lisp
  750. See also @code{truncate-quotient}, @code{truncate-remainder} and
  751. related operations in @ref{Arithmetic}.
  752. @end deffn
  753. @c begin (texi-doc-string "guile" "modulo")
  754. @deffn {Scheme Procedure} modulo n d
  755. @deffnx {C Function} scm_modulo (n, d)
  756. Return the remainder from @var{n} divided by @var{d}, with the same
  757. sign as @var{d}.
  758. @lisp
  759. (modulo 13 4) @result{} 1
  760. (modulo -13 4) @result{} 3
  761. (modulo 13 -4) @result{} -3
  762. (modulo -13 -4) @result{} -1
  763. @end lisp
  764. See also @code{floor-quotient}, @code{floor-remainder} and
  765. related operations in @ref{Arithmetic}.
  766. @end deffn
  767. @c begin (texi-doc-string "guile" "gcd")
  768. @deffn {Scheme Procedure} gcd x@dots{}
  769. @deffnx {C Function} scm_gcd (x, y)
  770. Return the greatest common divisor of all arguments.
  771. If called without arguments, 0 is returned.
  772. The C function @code{scm_gcd} always takes two arguments, while the
  773. Scheme function can take an arbitrary number.
  774. @end deffn
  775. @c begin (texi-doc-string "guile" "lcm")
  776. @deffn {Scheme Procedure} lcm x@dots{}
  777. @deffnx {C Function} scm_lcm (x, y)
  778. Return the least common multiple of the arguments.
  779. If called without arguments, 1 is returned.
  780. The C function @code{scm_lcm} always takes two arguments, while the
  781. Scheme function can take an arbitrary number.
  782. @end deffn
  783. @deffn {Scheme Procedure} modulo-expt n k m
  784. @deffnx {C Function} scm_modulo_expt (n, k, m)
  785. Return @var{n} raised to the integer exponent
  786. @var{k}, modulo @var{m}.
  787. @lisp
  788. (modulo-expt 2 3 5)
  789. @result{} 3
  790. @end lisp
  791. @end deffn
  792. @deftypefn {Scheme Procedure} {} exact-integer-sqrt @var{k}
  793. @deftypefnx {C Function} void scm_exact_integer_sqrt (SCM @var{k}, SCM *@var{s}, SCM *@var{r})
  794. Return two exact non-negative integers @var{s} and @var{r}
  795. such that @math{@var{k} = @var{s}^2 + @var{r}} and
  796. @math{@var{s}^2 <= @var{k} < (@var{s} + 1)^2}.
  797. An error is raised if @var{k} is not an exact non-negative integer.
  798. @lisp
  799. (exact-integer-sqrt 10) @result{} 3 and 1
  800. @end lisp
  801. @end deftypefn
  802. @node Comparison
  803. @subsubsection Comparison Predicates
  804. @rnindex zero?
  805. @rnindex positive?
  806. @rnindex negative?
  807. The C comparison functions below always takes two arguments, while the
  808. Scheme functions can take an arbitrary number. Also keep in mind that
  809. the C functions return one of the Scheme boolean values
  810. @code{SCM_BOOL_T} or @code{SCM_BOOL_F} which are both true as far as C
  811. is concerned. Thus, always write @code{scm_is_true (scm_num_eq_p (x,
  812. y))} when testing the two Scheme numbers @code{x} and @code{y} for
  813. equality, for example.
  814. @c begin (texi-doc-string "guile" "=")
  815. @deffn {Scheme Procedure} =
  816. @deffnx {C Function} scm_num_eq_p (x, y)
  817. Return @code{#t} if all parameters are numerically equal.
  818. @end deffn
  819. @c begin (texi-doc-string "guile" "<")
  820. @deffn {Scheme Procedure} <
  821. @deffnx {C Function} scm_less_p (x, y)
  822. Return @code{#t} if the list of parameters is monotonically
  823. increasing.
  824. @end deffn
  825. @c begin (texi-doc-string "guile" ">")
  826. @deffn {Scheme Procedure} >
  827. @deffnx {C Function} scm_gr_p (x, y)
  828. Return @code{#t} if the list of parameters is monotonically
  829. decreasing.
  830. @end deffn
  831. @c begin (texi-doc-string "guile" "<=")
  832. @deffn {Scheme Procedure} <=
  833. @deffnx {C Function} scm_leq_p (x, y)
  834. Return @code{#t} if the list of parameters is monotonically
  835. non-decreasing.
  836. @end deffn
  837. @c begin (texi-doc-string "guile" ">=")
  838. @deffn {Scheme Procedure} >=
  839. @deffnx {C Function} scm_geq_p (x, y)
  840. Return @code{#t} if the list of parameters is monotonically
  841. non-increasing.
  842. @end deffn
  843. @c begin (texi-doc-string "guile" "zero?")
  844. @deffn {Scheme Procedure} zero? z
  845. @deffnx {C Function} scm_zero_p (z)
  846. Return @code{#t} if @var{z} is an exact or inexact number equal to
  847. zero.
  848. @end deffn
  849. @c begin (texi-doc-string "guile" "positive?")
  850. @deffn {Scheme Procedure} positive? x
  851. @deffnx {C Function} scm_positive_p (x)
  852. Return @code{#t} if @var{x} is an exact or inexact number greater than
  853. zero.
  854. @end deffn
  855. @c begin (texi-doc-string "guile" "negative?")
  856. @deffn {Scheme Procedure} negative? x
  857. @deffnx {C Function} scm_negative_p (x)
  858. Return @code{#t} if @var{x} is an exact or inexact number less than
  859. zero.
  860. @end deffn
  861. @node Conversion
  862. @subsubsection Converting Numbers To and From Strings
  863. @rnindex number->string
  864. @rnindex string->number
  865. The following procedures read and write numbers according to their
  866. external representation as defined by R5RS (@pxref{Lexical structure,
  867. R5RS Lexical Structure,, r5rs, The Revised^5 Report on the Algorithmic
  868. Language Scheme}). @xref{Number Input and Output, the @code{(ice-9
  869. i18n)} module}, for locale-dependent number parsing.
  870. @deffn {Scheme Procedure} number->string n [radix]
  871. @deffnx {C Function} scm_number_to_string (n, radix)
  872. Return a string holding the external representation of the
  873. number @var{n} in the given @var{radix}. If @var{n} is
  874. inexact, a radix of 10 will be used.
  875. @end deffn
  876. @deffn {Scheme Procedure} string->number string [radix]
  877. @deffnx {C Function} scm_string_to_number (string, radix)
  878. Return a number of the maximally precise representation
  879. expressed by the given @var{string}. @var{radix} must be an
  880. exact integer, either 2, 8, 10, or 16. If supplied, @var{radix}
  881. is a default radix that may be overridden by an explicit radix
  882. prefix in @var{string} (e.g.@: "#o177"). If @var{radix} is not
  883. supplied, then the default radix is 10. If string is not a
  884. syntactically valid notation for a number, then
  885. @code{string->number} returns @code{#f}.
  886. @end deffn
  887. @deftypefn {C Function} SCM scm_c_locale_stringn_to_number (const char *string, size_t len, unsigned radix)
  888. As per @code{string->number} above, but taking a C string, as pointer
  889. and length. The string characters should be in the current locale
  890. encoding (@code{locale} in the name refers only to that, there's no
  891. locale-dependent parsing).
  892. @end deftypefn
  893. @node Complex
  894. @subsubsection Complex Number Operations
  895. @rnindex make-rectangular
  896. @rnindex make-polar
  897. @rnindex real-part
  898. @rnindex imag-part
  899. @rnindex magnitude
  900. @rnindex angle
  901. @deffn {Scheme Procedure} make-rectangular real_part imaginary_part
  902. @deffnx {C Function} scm_make_rectangular (real_part, imaginary_part)
  903. Return a complex number constructed of the given @var{real-part} and @var{imaginary-part} parts.
  904. @end deffn
  905. @deffn {Scheme Procedure} make-polar mag ang
  906. @deffnx {C Function} scm_make_polar (mag, ang)
  907. @cindex polar form
  908. Return the complex number @var{mag} * e^(i * @var{ang}).
  909. @end deffn
  910. @c begin (texi-doc-string "guile" "real-part")
  911. @deffn {Scheme Procedure} real-part z
  912. @deffnx {C Function} scm_real_part (z)
  913. Return the real part of the number @var{z}.
  914. @end deffn
  915. @c begin (texi-doc-string "guile" "imag-part")
  916. @deffn {Scheme Procedure} imag-part z
  917. @deffnx {C Function} scm_imag_part (z)
  918. Return the imaginary part of the number @var{z}.
  919. @end deffn
  920. @c begin (texi-doc-string "guile" "magnitude")
  921. @deffn {Scheme Procedure} magnitude z
  922. @deffnx {C Function} scm_magnitude (z)
  923. Return the magnitude of the number @var{z}. This is the same as
  924. @code{abs} for real arguments, but also allows complex numbers.
  925. @end deffn
  926. @c begin (texi-doc-string "guile" "angle")
  927. @deffn {Scheme Procedure} angle z
  928. @deffnx {C Function} scm_angle (z)
  929. Return the angle of the complex number @var{z}.
  930. @end deffn
  931. @deftypefn {C Function} SCM scm_c_make_rectangular (double re, double im)
  932. @deftypefnx {C Function} SCM scm_c_make_polar (double x, double y)
  933. Like @code{scm_make_rectangular} or @code{scm_make_polar},
  934. respectively, but these functions take @code{double}s as their
  935. arguments.
  936. @end deftypefn
  937. @deftypefn {C Function} double scm_c_real_part (z)
  938. @deftypefnx {C Function} double scm_c_imag_part (z)
  939. Returns the real or imaginary part of @var{z} as a @code{double}.
  940. @end deftypefn
  941. @deftypefn {C Function} double scm_c_magnitude (z)
  942. @deftypefnx {C Function} double scm_c_angle (z)
  943. Returns the magnitude or angle of @var{z} as a @code{double}.
  944. @end deftypefn
  945. @node Arithmetic
  946. @subsubsection Arithmetic Functions
  947. @rnindex max
  948. @rnindex min
  949. @rnindex +
  950. @rnindex *
  951. @rnindex -
  952. @rnindex /
  953. @findex 1+
  954. @findex 1-
  955. @rnindex abs
  956. @rnindex floor
  957. @rnindex ceiling
  958. @rnindex truncate
  959. @rnindex round
  960. @rnindex euclidean/
  961. @rnindex euclidean-quotient
  962. @rnindex euclidean-remainder
  963. @rnindex floor/
  964. @rnindex floor-quotient
  965. @rnindex floor-remainder
  966. @rnindex ceiling/
  967. @rnindex ceiling-quotient
  968. @rnindex ceiling-remainder
  969. @rnindex truncate/
  970. @rnindex truncate-quotient
  971. @rnindex truncate-remainder
  972. @rnindex centered/
  973. @rnindex centered-quotient
  974. @rnindex centered-remainder
  975. @rnindex round/
  976. @rnindex round-quotient
  977. @rnindex round-remainder
  978. The C arithmetic functions below always takes two arguments, while the
  979. Scheme functions can take an arbitrary number. When you need to
  980. invoke them with just one argument, for example to compute the
  981. equivalent of @code{(- x)}, pass @code{SCM_UNDEFINED} as the second
  982. one: @code{scm_difference (x, SCM_UNDEFINED)}.
  983. @c begin (texi-doc-string "guile" "+")
  984. @deffn {Scheme Procedure} + z1 @dots{}
  985. @deffnx {C Function} scm_sum (z1, z2)
  986. Return the sum of all parameter values. Return 0 if called without any
  987. parameters.
  988. @end deffn
  989. @c begin (texi-doc-string "guile" "-")
  990. @deffn {Scheme Procedure} - z1 z2 @dots{}
  991. @deffnx {C Function} scm_difference (z1, z2)
  992. If called with one argument @var{z1}, -@var{z1} is returned. Otherwise
  993. the sum of all but the first argument are subtracted from the first
  994. argument.
  995. @end deffn
  996. @c begin (texi-doc-string "guile" "*")
  997. @deffn {Scheme Procedure} * z1 @dots{}
  998. @deffnx {C Function} scm_product (z1, z2)
  999. Return the product of all arguments. If called without arguments, 1 is
  1000. returned.
  1001. @end deffn
  1002. @c begin (texi-doc-string "guile" "/")
  1003. @deffn {Scheme Procedure} / z1 z2 @dots{}
  1004. @deffnx {C Function} scm_divide (z1, z2)
  1005. Divide the first argument by the product of the remaining arguments. If
  1006. called with one argument @var{z1}, 1/@var{z1} is returned.
  1007. @end deffn
  1008. @deffn {Scheme Procedure} 1+ z
  1009. @deffnx {C Function} scm_oneplus (z)
  1010. Return @math{@var{z} + 1}.
  1011. @end deffn
  1012. @deffn {Scheme Procedure} 1- z
  1013. @deffnx {C function} scm_oneminus (z)
  1014. Return @math{@var{z} - 1}.
  1015. @end deffn
  1016. @c begin (texi-doc-string "guile" "abs")
  1017. @deffn {Scheme Procedure} abs x
  1018. @deffnx {C Function} scm_abs (x)
  1019. Return the absolute value of @var{x}.
  1020. @var{x} must be a number with zero imaginary part. To calculate the
  1021. magnitude of a complex number, use @code{magnitude} instead.
  1022. @end deffn
  1023. @c begin (texi-doc-string "guile" "max")
  1024. @deffn {Scheme Procedure} max x1 x2 @dots{}
  1025. @deffnx {C Function} scm_max (x1, x2)
  1026. Return the maximum of all parameter values.
  1027. @end deffn
  1028. @c begin (texi-doc-string "guile" "min")
  1029. @deffn {Scheme Procedure} min x1 x2 @dots{}
  1030. @deffnx {C Function} scm_min (x1, x2)
  1031. Return the minimum of all parameter values.
  1032. @end deffn
  1033. @c begin (texi-doc-string "guile" "truncate")
  1034. @deffn {Scheme Procedure} truncate x
  1035. @deffnx {C Function} scm_truncate_number (x)
  1036. Round the inexact number @var{x} towards zero.
  1037. @end deffn
  1038. @c begin (texi-doc-string "guile" "round")
  1039. @deffn {Scheme Procedure} round x
  1040. @deffnx {C Function} scm_round_number (x)
  1041. Round the inexact number @var{x} to the nearest integer. When exactly
  1042. halfway between two integers, round to the even one.
  1043. @end deffn
  1044. @c begin (texi-doc-string "guile" "floor")
  1045. @deffn {Scheme Procedure} floor x
  1046. @deffnx {C Function} scm_floor (x)
  1047. Round the number @var{x} towards minus infinity.
  1048. @end deffn
  1049. @c begin (texi-doc-string "guile" "ceiling")
  1050. @deffn {Scheme Procedure} ceiling x
  1051. @deffnx {C Function} scm_ceiling (x)
  1052. Round the number @var{x} towards infinity.
  1053. @end deffn
  1054. @deftypefn {C Function} double scm_c_truncate (double x)
  1055. @deftypefnx {C Function} double scm_c_round (double x)
  1056. Like @code{scm_truncate_number} or @code{scm_round_number},
  1057. respectively, but these functions take and return @code{double}
  1058. values.
  1059. @end deftypefn
  1060. @deftypefn {Scheme Procedure} {} euclidean/ @var{x} @var{y}
  1061. @deftypefnx {Scheme Procedure} {} euclidean-quotient @var{x} @var{y}
  1062. @deftypefnx {Scheme Procedure} {} euclidean-remainder @var{x} @var{y}
  1063. @deftypefnx {C Function} void scm_euclidean_divide (SCM @var{x}, SCM @var{y}, SCM *@var{q}, SCM *@var{r})
  1064. @deftypefnx {C Function} SCM scm_euclidean_quotient (SCM @var{x}, SCM @var{y})
  1065. @deftypefnx {C Function} SCM scm_euclidean_remainder (SCM @var{x}, SCM @var{y})
  1066. These procedures accept two real numbers @var{x} and @var{y}, where the
  1067. divisor @var{y} must be non-zero. @code{euclidean-quotient} returns the
  1068. integer @var{q} and @code{euclidean-remainder} returns the real number
  1069. @var{r} such that @math{@var{x} = @var{q}*@var{y} + @var{r}} and
  1070. @math{0 <= @var{r} < |@var{y}|}. @code{euclidean/} returns both @var{q} and
  1071. @var{r}, and is more efficient than computing each separately. Note
  1072. that when @math{@var{y} > 0}, @code{euclidean-quotient} returns
  1073. @math{floor(@var{x}/@var{y})}, otherwise it returns
  1074. @math{ceiling(@var{x}/@var{y})}.
  1075. Note that these operators are equivalent to the R6RS operators
  1076. @code{div}, @code{mod}, and @code{div-and-mod}.
  1077. @lisp
  1078. (euclidean-quotient 123 10) @result{} 12
  1079. (euclidean-remainder 123 10) @result{} 3
  1080. (euclidean/ 123 10) @result{} 12 and 3
  1081. (euclidean/ 123 -10) @result{} -12 and 3
  1082. (euclidean/ -123 10) @result{} -13 and 7
  1083. (euclidean/ -123 -10) @result{} 13 and 7
  1084. (euclidean/ -123.2 -63.5) @result{} 2.0 and 3.8
  1085. (euclidean/ 16/3 -10/7) @result{} -3 and 22/21
  1086. @end lisp
  1087. @end deftypefn
  1088. @deftypefn {Scheme Procedure} {} floor/ @var{x} @var{y}
  1089. @deftypefnx {Scheme Procedure} {} floor-quotient @var{x} @var{y}
  1090. @deftypefnx {Scheme Procedure} {} floor-remainder @var{x} @var{y}
  1091. @deftypefnx {C Function} void scm_floor_divide (SCM @var{x}, SCM @var{y}, SCM *@var{q}, SCM *@var{r})
  1092. @deftypefnx {C Function} SCM scm_floor_quotient (@var{x}, @var{y})
  1093. @deftypefnx {C Function} SCM scm_floor_remainder (@var{x}, @var{y})
  1094. These procedures accept two real numbers @var{x} and @var{y}, where the
  1095. divisor @var{y} must be non-zero. @code{floor-quotient} returns the
  1096. integer @var{q} and @code{floor-remainder} returns the real number
  1097. @var{r} such that @math{@var{q} = floor(@var{x}/@var{y})} and
  1098. @math{@var{x} = @var{q}*@var{y} + @var{r}}. @code{floor/} returns
  1099. both @var{q} and @var{r}, and is more efficient than computing each
  1100. separately. Note that @var{r}, if non-zero, will have the same sign
  1101. as @var{y}.
  1102. When @var{x} and @var{y} are integers, @code{floor-remainder} is
  1103. equivalent to the R5RS integer-only operator @code{modulo}.
  1104. @lisp
  1105. (floor-quotient 123 10) @result{} 12
  1106. (floor-remainder 123 10) @result{} 3
  1107. (floor/ 123 10) @result{} 12 and 3
  1108. (floor/ 123 -10) @result{} -13 and -7
  1109. (floor/ -123 10) @result{} -13 and 7
  1110. (floor/ -123 -10) @result{} 12 and -3
  1111. (floor/ -123.2 -63.5) @result{} 1.0 and -59.7
  1112. (floor/ 16/3 -10/7) @result{} -4 and -8/21
  1113. @end lisp
  1114. @end deftypefn
  1115. @deftypefn {Scheme Procedure} {} ceiling/ @var{x} @var{y}
  1116. @deftypefnx {Scheme Procedure} {} ceiling-quotient @var{x} @var{y}
  1117. @deftypefnx {Scheme Procedure} {} ceiling-remainder @var{x} @var{y}
  1118. @deftypefnx {C Function} void scm_ceiling_divide (SCM @var{x}, SCM @var{y}, SCM *@var{q}, SCM *@var{r})
  1119. @deftypefnx {C Function} SCM scm_ceiling_quotient (@var{x}, @var{y})
  1120. @deftypefnx {C Function} SCM scm_ceiling_remainder (@var{x}, @var{y})
  1121. These procedures accept two real numbers @var{x} and @var{y}, where the
  1122. divisor @var{y} must be non-zero. @code{ceiling-quotient} returns the
  1123. integer @var{q} and @code{ceiling-remainder} returns the real number
  1124. @var{r} such that @math{@var{q} = ceiling(@var{x}/@var{y})} and
  1125. @math{@var{x} = @var{q}*@var{y} + @var{r}}. @code{ceiling/} returns
  1126. both @var{q} and @var{r}, and is more efficient than computing each
  1127. separately. Note that @var{r}, if non-zero, will have the opposite sign
  1128. of @var{y}.
  1129. @lisp
  1130. (ceiling-quotient 123 10) @result{} 13
  1131. (ceiling-remainder 123 10) @result{} -7
  1132. (ceiling/ 123 10) @result{} 13 and -7
  1133. (ceiling/ 123 -10) @result{} -12 and 3
  1134. (ceiling/ -123 10) @result{} -12 and -3
  1135. (ceiling/ -123 -10) @result{} 13 and 7
  1136. (ceiling/ -123.2 -63.5) @result{} 2.0 and 3.8
  1137. (ceiling/ 16/3 -10/7) @result{} -3 and 22/21
  1138. @end lisp
  1139. @end deftypefn
  1140. @deftypefn {Scheme Procedure} {} truncate/ @var{x} @var{y}
  1141. @deftypefnx {Scheme Procedure} {} truncate-quotient @var{x} @var{y}
  1142. @deftypefnx {Scheme Procedure} {} truncate-remainder @var{x} @var{y}
  1143. @deftypefnx {C Function} void scm_truncate_divide (SCM @var{x}, SCM @var{y}, SCM *@var{q}, SCM *@var{r})
  1144. @deftypefnx {C Function} SCM scm_truncate_quotient (@var{x}, @var{y})
  1145. @deftypefnx {C Function} SCM scm_truncate_remainder (@var{x}, @var{y})
  1146. These procedures accept two real numbers @var{x} and @var{y}, where the
  1147. divisor @var{y} must be non-zero. @code{truncate-quotient} returns the
  1148. integer @var{q} and @code{truncate-remainder} returns the real number
  1149. @var{r} such that @var{q} is @math{@var{x}/@var{y}} rounded toward zero,
  1150. and @math{@var{x} = @var{q}*@var{y} + @var{r}}. @code{truncate/} returns
  1151. both @var{q} and @var{r}, and is more efficient than computing each
  1152. separately. Note that @var{r}, if non-zero, will have the same sign
  1153. as @var{x}.
  1154. When @var{x} and @var{y} are integers, these operators are
  1155. equivalent to the R5RS integer-only operators @code{quotient} and
  1156. @code{remainder}.
  1157. @lisp
  1158. (truncate-quotient 123 10) @result{} 12
  1159. (truncate-remainder 123 10) @result{} 3
  1160. (truncate/ 123 10) @result{} 12 and 3
  1161. (truncate/ 123 -10) @result{} -12 and 3
  1162. (truncate/ -123 10) @result{} -12 and -3
  1163. (truncate/ -123 -10) @result{} 12 and -3
  1164. (truncate/ -123.2 -63.5) @result{} 1.0 and -59.7
  1165. (truncate/ 16/3 -10/7) @result{} -3 and 22/21
  1166. @end lisp
  1167. @end deftypefn
  1168. @deftypefn {Scheme Procedure} {} centered/ @var{x} @var{y}
  1169. @deftypefnx {Scheme Procedure} {} centered-quotient @var{x} @var{y}
  1170. @deftypefnx {Scheme Procedure} {} centered-remainder @var{x} @var{y}
  1171. @deftypefnx {C Function} void scm_centered_divide (SCM @var{x}, SCM @var{y}, SCM *@var{q}, SCM *@var{r})
  1172. @deftypefnx {C Function} SCM scm_centered_quotient (SCM @var{x}, SCM @var{y})
  1173. @deftypefnx {C Function} SCM scm_centered_remainder (SCM @var{x}, SCM @var{y})
  1174. These procedures accept two real numbers @var{x} and @var{y}, where the
  1175. divisor @var{y} must be non-zero. @code{centered-quotient} returns the
  1176. integer @var{q} and @code{centered-remainder} returns the real number
  1177. @var{r} such that @math{@var{x} = @var{q}*@var{y} + @var{r}} and
  1178. @math{-|@var{y}/2| <= @var{r} < |@var{y}/2|}. @code{centered/}
  1179. returns both @var{q} and @var{r}, and is more efficient than computing
  1180. each separately.
  1181. Note that @code{centered-quotient} returns @math{@var{x}/@var{y}}
  1182. rounded to the nearest integer. When @math{@var{x}/@var{y}} lies
  1183. exactly half-way between two integers, the tie is broken according to
  1184. the sign of @var{y}. If @math{@var{y} > 0}, ties are rounded toward
  1185. positive infinity, otherwise they are rounded toward negative infinity.
  1186. This is a consequence of the requirement that
  1187. @math{-|@var{y}/2| <= @var{r} < |@var{y}/2|}.
  1188. Note that these operators are equivalent to the R6RS operators
  1189. @code{div0}, @code{mod0}, and @code{div0-and-mod0}.
  1190. @lisp
  1191. (centered-quotient 123 10) @result{} 12
  1192. (centered-remainder 123 10) @result{} 3
  1193. (centered/ 123 10) @result{} 12 and 3
  1194. (centered/ 123 -10) @result{} -12 and 3
  1195. (centered/ -123 10) @result{} -12 and -3
  1196. (centered/ -123 -10) @result{} 12 and -3
  1197. (centered/ 125 10) @result{} 13 and -5
  1198. (centered/ 127 10) @result{} 13 and -3
  1199. (centered/ 135 10) @result{} 14 and -5
  1200. (centered/ -123.2 -63.5) @result{} 2.0 and 3.8
  1201. (centered/ 16/3 -10/7) @result{} -4 and -8/21
  1202. @end lisp
  1203. @end deftypefn
  1204. @deftypefn {Scheme Procedure} {} round/ @var{x} @var{y}
  1205. @deftypefnx {Scheme Procedure} {} round-quotient @var{x} @var{y}
  1206. @deftypefnx {Scheme Procedure} {} round-remainder @var{x} @var{y}
  1207. @deftypefnx {C Function} void scm_round_divide (SCM @var{x}, SCM @var{y}, SCM *@var{q}, SCM *@var{r})
  1208. @deftypefnx {C Function} SCM scm_round_quotient (@var{x}, @var{y})
  1209. @deftypefnx {C Function} SCM scm_round_remainder (@var{x}, @var{y})
  1210. These procedures accept two real numbers @var{x} and @var{y}, where the
  1211. divisor @var{y} must be non-zero. @code{round-quotient} returns the
  1212. integer @var{q} and @code{round-remainder} returns the real number
  1213. @var{r} such that @math{@var{x} = @var{q}*@var{y} + @var{r}} and
  1214. @var{q} is @math{@var{x}/@var{y}} rounded to the nearest integer,
  1215. with ties going to the nearest even integer. @code{round/}
  1216. returns both @var{q} and @var{r}, and is more efficient than computing
  1217. each separately.
  1218. Note that @code{round/} and @code{centered/} are almost equivalent, but
  1219. their behavior differs when @math{@var{x}/@var{y}} lies exactly half-way
  1220. between two integers. In this case, @code{round/} chooses the nearest
  1221. even integer, whereas @code{centered/} chooses in such a way to satisfy
  1222. the constraint @math{-|@var{y}/2| <= @var{r} < |@var{y}/2|}, which
  1223. is stronger than the corresponding constraint for @code{round/},
  1224. @math{-|@var{y}/2| <= @var{r} <= |@var{y}/2|}. In particular,
  1225. when @var{x} and @var{y} are integers, the number of possible remainders
  1226. returned by @code{centered/} is @math{|@var{y}|}, whereas the number of
  1227. possible remainders returned by @code{round/} is @math{|@var{y}|+1} when
  1228. @var{y} is even.
  1229. @lisp
  1230. (round-quotient 123 10) @result{} 12
  1231. (round-remainder 123 10) @result{} 3
  1232. (round/ 123 10) @result{} 12 and 3
  1233. (round/ 123 -10) @result{} -12 and 3
  1234. (round/ -123 10) @result{} -12 and -3
  1235. (round/ -123 -10) @result{} 12 and -3
  1236. (round/ 125 10) @result{} 12 and 5
  1237. (round/ 127 10) @result{} 13 and -3
  1238. (round/ 135 10) @result{} 14 and -5
  1239. (round/ -123.2 -63.5) @result{} 2.0 and 3.8
  1240. (round/ 16/3 -10/7) @result{} -4 and -8/21
  1241. @end lisp
  1242. @end deftypefn
  1243. @node Scientific
  1244. @subsubsection Scientific Functions
  1245. The following procedures accept any kind of number as arguments,
  1246. including complex numbers.
  1247. @rnindex sqrt
  1248. @c begin (texi-doc-string "guile" "sqrt")
  1249. @deffn {Scheme Procedure} sqrt z
  1250. Return the square root of @var{z}. Of the two possible roots
  1251. (positive and negative), the one with a positive real part is
  1252. returned, or if that's zero then a positive imaginary part. Thus,
  1253. @example
  1254. (sqrt 9.0) @result{} 3.0
  1255. (sqrt -9.0) @result{} 0.0+3.0i
  1256. (sqrt 1.0+1.0i) @result{} 1.09868411346781+0.455089860562227i
  1257. (sqrt -1.0-1.0i) @result{} 0.455089860562227-1.09868411346781i
  1258. @end example
  1259. @end deffn
  1260. @rnindex expt
  1261. @c begin (texi-doc-string "guile" "expt")
  1262. @deffn {Scheme Procedure} expt z1 z2
  1263. Return @var{z1} raised to the power of @var{z2}.
  1264. @end deffn
  1265. @rnindex sin
  1266. @c begin (texi-doc-string "guile" "sin")
  1267. @deffn {Scheme Procedure} sin z
  1268. Return the sine of @var{z}.
  1269. @end deffn
  1270. @rnindex cos
  1271. @c begin (texi-doc-string "guile" "cos")
  1272. @deffn {Scheme Procedure} cos z
  1273. Return the cosine of @var{z}.
  1274. @end deffn
  1275. @rnindex tan
  1276. @c begin (texi-doc-string "guile" "tan")
  1277. @deffn {Scheme Procedure} tan z
  1278. Return the tangent of @var{z}.
  1279. @end deffn
  1280. @rnindex asin
  1281. @c begin (texi-doc-string "guile" "asin")
  1282. @deffn {Scheme Procedure} asin z
  1283. Return the arcsine of @var{z}.
  1284. @end deffn
  1285. @rnindex acos
  1286. @c begin (texi-doc-string "guile" "acos")
  1287. @deffn {Scheme Procedure} acos z
  1288. Return the arccosine of @var{z}.
  1289. @end deffn
  1290. @rnindex atan
  1291. @c begin (texi-doc-string "guile" "atan")
  1292. @deffn {Scheme Procedure} atan z
  1293. @deffnx {Scheme Procedure} atan y x
  1294. Return the arctangent of @var{z}, or of @math{@var{y}/@var{x}}.
  1295. @end deffn
  1296. @rnindex exp
  1297. @c begin (texi-doc-string "guile" "exp")
  1298. @deffn {Scheme Procedure} exp z
  1299. Return e to the power of @var{z}, where e is the base of natural
  1300. logarithms (2.71828@dots{}).
  1301. @end deffn
  1302. @rnindex log
  1303. @c begin (texi-doc-string "guile" "log")
  1304. @deffn {Scheme Procedure} log z
  1305. Return the natural logarithm of @var{z}.
  1306. @end deffn
  1307. @c begin (texi-doc-string "guile" "log10")
  1308. @deffn {Scheme Procedure} log10 z
  1309. Return the base 10 logarithm of @var{z}.
  1310. @end deffn
  1311. @c begin (texi-doc-string "guile" "sinh")
  1312. @deffn {Scheme Procedure} sinh z
  1313. Return the hyperbolic sine of @var{z}.
  1314. @end deffn
  1315. @c begin (texi-doc-string "guile" "cosh")
  1316. @deffn {Scheme Procedure} cosh z
  1317. Return the hyperbolic cosine of @var{z}.
  1318. @end deffn
  1319. @c begin (texi-doc-string "guile" "tanh")
  1320. @deffn {Scheme Procedure} tanh z
  1321. Return the hyperbolic tangent of @var{z}.
  1322. @end deffn
  1323. @c begin (texi-doc-string "guile" "asinh")
  1324. @deffn {Scheme Procedure} asinh z
  1325. Return the hyperbolic arcsine of @var{z}.
  1326. @end deffn
  1327. @c begin (texi-doc-string "guile" "acosh")
  1328. @deffn {Scheme Procedure} acosh z
  1329. Return the hyperbolic arccosine of @var{z}.
  1330. @end deffn
  1331. @c begin (texi-doc-string "guile" "atanh")
  1332. @deffn {Scheme Procedure} atanh z
  1333. Return the hyperbolic arctangent of @var{z}.
  1334. @end deffn
  1335. @node Bitwise Operations
  1336. @subsubsection Bitwise Operations
  1337. For the following bitwise functions, negative numbers are treated as
  1338. infinite precision twos-complements. For instance @math{-6} is bits
  1339. @math{@dots{}111010}, with infinitely many ones on the left. It can
  1340. be seen that adding 6 (binary 110) to such a bit pattern gives all
  1341. zeros.
  1342. @deffn {Scheme Procedure} logand n1 n2 @dots{}
  1343. @deffnx {C Function} scm_logand (n1, n2)
  1344. Return the bitwise @sc{and} of the integer arguments.
  1345. @lisp
  1346. (logand) @result{} -1
  1347. (logand 7) @result{} 7
  1348. (logand #b111 #b011 #b001) @result{} 1
  1349. @end lisp
  1350. @end deffn
  1351. @deffn {Scheme Procedure} logior n1 n2 @dots{}
  1352. @deffnx {C Function} scm_logior (n1, n2)
  1353. Return the bitwise @sc{or} of the integer arguments.
  1354. @lisp
  1355. (logior) @result{} 0
  1356. (logior 7) @result{} 7
  1357. (logior #b000 #b001 #b011) @result{} 3
  1358. @end lisp
  1359. @end deffn
  1360. @deffn {Scheme Procedure} logxor n1 n2 @dots{}
  1361. @deffnx {C Function} scm_loxor (n1, n2)
  1362. Return the bitwise @sc{xor} of the integer arguments. A bit is
  1363. set in the result if it is set in an odd number of arguments.
  1364. @lisp
  1365. (logxor) @result{} 0
  1366. (logxor 7) @result{} 7
  1367. (logxor #b000 #b001 #b011) @result{} 2
  1368. (logxor #b000 #b001 #b011 #b011) @result{} 1
  1369. @end lisp
  1370. @end deffn
  1371. @deffn {Scheme Procedure} lognot n
  1372. @deffnx {C Function} scm_lognot (n)
  1373. Return the integer which is the ones-complement of the integer
  1374. argument, ie.@: each 0 bit is changed to 1 and each 1 bit to 0.
  1375. @lisp
  1376. (number->string (lognot #b10000000) 2)
  1377. @result{} "-10000001"
  1378. (number->string (lognot #b0) 2)
  1379. @result{} "-1"
  1380. @end lisp
  1381. @end deffn
  1382. @deffn {Scheme Procedure} logtest j k
  1383. @deffnx {C Function} scm_logtest (j, k)
  1384. Test whether @var{j} and @var{k} have any 1 bits in common. This is
  1385. equivalent to @code{(not (zero? (logand j k)))}, but without actually
  1386. calculating the @code{logand}, just testing for non-zero.
  1387. @lisp
  1388. (logtest #b0100 #b1011) @result{} #f
  1389. (logtest #b0100 #b0111) @result{} #t
  1390. @end lisp
  1391. @end deffn
  1392. @deffn {Scheme Procedure} logbit? index j
  1393. @deffnx {C Function} scm_logbit_p (index, j)
  1394. Test whether bit number @var{index} in @var{j} is set. @var{index}
  1395. starts from 0 for the least significant bit.
  1396. @lisp
  1397. (logbit? 0 #b1101) @result{} #t
  1398. (logbit? 1 #b1101) @result{} #f
  1399. (logbit? 2 #b1101) @result{} #t
  1400. (logbit? 3 #b1101) @result{} #t
  1401. (logbit? 4 #b1101) @result{} #f
  1402. @end lisp
  1403. @end deffn
  1404. @deffn {Scheme Procedure} ash n count
  1405. @deffnx {C Function} scm_ash (n, count)
  1406. Return @math{floor(n * 2^count)}.
  1407. @var{n} and @var{count} must be exact integers.
  1408. With @var{n} viewed as an infinite-precision twos-complement
  1409. integer, @code{ash} means a left shift introducing zero bits
  1410. when @var{count} is positive, or a right shift dropping bits
  1411. when @var{count} is negative. This is an ``arithmetic'' shift.
  1412. @lisp
  1413. (number->string (ash #b1 3) 2) @result{} "1000"
  1414. (number->string (ash #b1010 -1) 2) @result{} "101"
  1415. ;; -23 is bits ...11101001, -6 is bits ...111010
  1416. (ash -23 -2) @result{} -6
  1417. @end lisp
  1418. @end deffn
  1419. @deffn {Scheme Procedure} round-ash n count
  1420. @deffnx {C Function} scm_round_ash (n, count)
  1421. Return @math{round(n * 2^count)}.
  1422. @var{n} and @var{count} must be exact integers.
  1423. With @var{n} viewed as an infinite-precision twos-complement
  1424. integer, @code{round-ash} means a left shift introducing zero
  1425. bits when @var{count} is positive, or a right shift rounding
  1426. to the nearest integer (with ties going to the nearest even
  1427. integer) when @var{count} is negative. This is a rounded
  1428. ``arithmetic'' shift.
  1429. @lisp
  1430. (number->string (round-ash #b1 3) 2) @result{} \"1000\"
  1431. (number->string (round-ash #b1010 -1) 2) @result{} \"101\"
  1432. (number->string (round-ash #b1010 -2) 2) @result{} \"10\"
  1433. (number->string (round-ash #b1011 -2) 2) @result{} \"11\"
  1434. (number->string (round-ash #b1101 -2) 2) @result{} \"11\"
  1435. (number->string (round-ash #b1110 -2) 2) @result{} \"100\"
  1436. @end lisp
  1437. @end deffn
  1438. @deffn {Scheme Procedure} logcount n
  1439. @deffnx {C Function} scm_logcount (n)
  1440. Return the number of bits in integer @var{n}. If @var{n} is
  1441. positive, the 1-bits in its binary representation are counted.
  1442. If negative, the 0-bits in its two's-complement binary
  1443. representation are counted. If zero, 0 is returned.
  1444. @lisp
  1445. (logcount #b10101010)
  1446. @result{} 4
  1447. (logcount 0)
  1448. @result{} 0
  1449. (logcount -2)
  1450. @result{} 1
  1451. @end lisp
  1452. @end deffn
  1453. @deffn {Scheme Procedure} integer-length n
  1454. @deffnx {C Function} scm_integer_length (n)
  1455. Return the number of bits necessary to represent @var{n}.
  1456. For positive @var{n} this is how many bits to the most significant one
  1457. bit. For negative @var{n} it's how many bits to the most significant
  1458. zero bit in twos complement form.
  1459. @lisp
  1460. (integer-length #b10101010) @result{} 8
  1461. (integer-length #b1111) @result{} 4
  1462. (integer-length 0) @result{} 0
  1463. (integer-length -1) @result{} 0
  1464. (integer-length -256) @result{} 8
  1465. (integer-length -257) @result{} 9
  1466. @end lisp
  1467. @end deffn
  1468. @deffn {Scheme Procedure} integer-expt n k
  1469. @deffnx {C Function} scm_integer_expt (n, k)
  1470. Return @var{n} raised to the power @var{k}. @var{k} must be an exact
  1471. integer, @var{n} can be any number.
  1472. Negative @var{k} is supported, and results in @m{1/n^|k|, 1/n^abs(k)}
  1473. in the usual way. @math{@var{n}^0} is 1, as usual, and that includes
  1474. @math{0^0} is 1.
  1475. @lisp
  1476. (integer-expt 2 5) @result{} 32
  1477. (integer-expt -3 3) @result{} -27
  1478. (integer-expt 5 -3) @result{} 1/125
  1479. (integer-expt 0 0) @result{} 1
  1480. @end lisp
  1481. @end deffn
  1482. @deffn {Scheme Procedure} bit-extract n start end
  1483. @deffnx {C Function} scm_bit_extract (n, start, end)
  1484. Return the integer composed of the @var{start} (inclusive)
  1485. through @var{end} (exclusive) bits of @var{n}. The
  1486. @var{start}th bit becomes the 0-th bit in the result.
  1487. @lisp
  1488. (number->string (bit-extract #b1101101010 0 4) 2)
  1489. @result{} "1010"
  1490. (number->string (bit-extract #b1101101010 4 9) 2)
  1491. @result{} "10110"
  1492. @end lisp
  1493. @end deffn
  1494. @node Random
  1495. @subsubsection Random Number Generation
  1496. Pseudo-random numbers are generated from a random state object, which
  1497. can be created with @code{seed->random-state} or
  1498. @code{datum->random-state}. An external representation (i.e.@: one
  1499. which can written with @code{write} and read with @code{read}) of a
  1500. random state object can be obtained via
  1501. @code{random-state->datum}. The @var{state} parameter to the
  1502. various functions below is optional, it defaults to the state object
  1503. in the @code{*random-state*} variable.
  1504. @deffn {Scheme Procedure} copy-random-state [state]
  1505. @deffnx {C Function} scm_copy_random_state (state)
  1506. Return a copy of the random state @var{state}.
  1507. @end deffn
  1508. @deffn {Scheme Procedure} random n [state]
  1509. @deffnx {C Function} scm_random (n, state)
  1510. Return a number in [0, @var{n}).
  1511. Accepts a positive integer or real n and returns a
  1512. number of the same type between zero (inclusive) and
  1513. @var{n} (exclusive). The values returned have a uniform
  1514. distribution.
  1515. @end deffn
  1516. @deffn {Scheme Procedure} random:exp [state]
  1517. @deffnx {C Function} scm_random_exp (state)
  1518. Return an inexact real in an exponential distribution with mean
  1519. 1. For an exponential distribution with mean @var{u} use @code{(*
  1520. @var{u} (random:exp))}.
  1521. @end deffn
  1522. @deffn {Scheme Procedure} random:hollow-sphere! vect [state]
  1523. @deffnx {C Function} scm_random_hollow_sphere_x (vect, state)
  1524. Fills @var{vect} with inexact real random numbers the sum of whose
  1525. squares is equal to 1.0. Thinking of @var{vect} as coordinates in
  1526. space of dimension @var{n} @math{=} @code{(vector-length @var{vect})},
  1527. the coordinates are uniformly distributed over the surface of the unit
  1528. n-sphere.
  1529. @end deffn
  1530. @deffn {Scheme Procedure} random:normal [state]
  1531. @deffnx {C Function} scm_random_normal (state)
  1532. Return an inexact real in a normal distribution. The distribution
  1533. used has mean 0 and standard deviation 1. For a normal distribution
  1534. with mean @var{m} and standard deviation @var{d} use @code{(+ @var{m}
  1535. (* @var{d} (random:normal)))}.
  1536. @end deffn
  1537. @deffn {Scheme Procedure} random:normal-vector! vect [state]
  1538. @deffnx {C Function} scm_random_normal_vector_x (vect, state)
  1539. Fills @var{vect} with inexact real random numbers that are
  1540. independent and standard normally distributed
  1541. (i.e., with mean 0 and variance 1).
  1542. @end deffn
  1543. @deffn {Scheme Procedure} random:solid-sphere! vect [state]
  1544. @deffnx {C Function} scm_random_solid_sphere_x (vect, state)
  1545. Fills @var{vect} with inexact real random numbers the sum of whose
  1546. squares is less than 1.0. Thinking of @var{vect} as coordinates in
  1547. space of dimension @var{n} @math{=} @code{(vector-length @var{vect})},
  1548. the coordinates are uniformly distributed within the unit
  1549. @var{n}-sphere.
  1550. @c FIXME: What does this mean, particularly the n-sphere part?
  1551. @end deffn
  1552. @deffn {Scheme Procedure} random:uniform [state]
  1553. @deffnx {C Function} scm_random_uniform (state)
  1554. Return a uniformly distributed inexact real random number in
  1555. [0,1).
  1556. @end deffn
  1557. @deffn {Scheme Procedure} seed->random-state seed
  1558. @deffnx {C Function} scm_seed_to_random_state (seed)
  1559. Return a new random state using @var{seed}.
  1560. @end deffn
  1561. @deffn {Scheme Procedure} datum->random-state datum
  1562. @deffnx {C Function} scm_datum_to_random_state (datum)
  1563. Return a new random state from @var{datum}, which should have been
  1564. obtained by @code{random-state->datum}.
  1565. @end deffn
  1566. @deffn {Scheme Procedure} random-state->datum state
  1567. @deffnx {C Function} scm_random_state_to_datum (state)
  1568. Return a datum representation of @var{state} that may be written out and
  1569. read back with the Scheme reader.
  1570. @end deffn
  1571. @deffn {Scheme Procedure} random-state-from-platform
  1572. @deffnx {C Function} scm_random_state_from_platform ()
  1573. Construct a new random state seeded from a platform-specific source of
  1574. entropy, appropriate for use in non-security-critical applications.
  1575. Currently @file{/dev/urandom} is tried first, or else the seed is based
  1576. on the time, date, process ID, an address from a freshly allocated heap
  1577. cell, an address from the local stack frame, and a high-resolution timer
  1578. if available.
  1579. @end deffn
  1580. @defvar *random-state*
  1581. The global random state used by the above functions when the
  1582. @var{state} parameter is not given.
  1583. @end defvar
  1584. Note that the initial value of @code{*random-state*} is the same every
  1585. time Guile starts up. Therefore, if you don't pass a @var{state}
  1586. parameter to the above procedures, and you don't set
  1587. @code{*random-state*} to @code{(seed->random-state your-seed)}, where
  1588. @code{your-seed} is something that @emph{isn't} the same every time,
  1589. you'll get the same sequence of ``random'' numbers on every run.
  1590. For example, unless the relevant source code has changed, @code{(map
  1591. random (cdr (iota 30)))}, if the first use of random numbers since
  1592. Guile started up, will always give:
  1593. @lisp
  1594. (map random (cdr (iota 19)))
  1595. @result{}
  1596. (0 1 1 2 2 2 1 2 6 7 10 0 5 3 12 5 5 12)
  1597. @end lisp
  1598. To seed the random state in a sensible way for non-security-critical
  1599. applications, do this during initialization of your program:
  1600. @lisp
  1601. (set! *random-state* (random-state-from-platform))
  1602. @end lisp
  1603. @node Characters
  1604. @subsection Characters
  1605. @tpindex Characters
  1606. In Scheme, there is a data type to describe a single character.
  1607. Defining what exactly a character @emph{is} can be more complicated
  1608. than it seems. Guile follows the advice of R6RS and uses The Unicode
  1609. Standard to help define what a character is. So, for Guile, a
  1610. character is anything in the Unicode Character Database.
  1611. @cindex code point
  1612. @cindex Unicode code point
  1613. The Unicode Character Database is basically a table of characters
  1614. indexed using integers called 'code points'. Valid code points are in
  1615. the ranges 0 to @code{#xD7FF} inclusive or @code{#xE000} to
  1616. @code{#x10FFFF} inclusive, which is about 1.1 million code points.
  1617. @cindex designated code point
  1618. @cindex code point, designated
  1619. Any code point that has been assigned to a character or that has
  1620. otherwise been given a meaning by Unicode is called a 'designated code
  1621. point'. Most of the designated code points, about 200,000 of them,
  1622. indicate characters, accents or other combining marks that modify
  1623. other characters, symbols, whitespace, and control characters. Some
  1624. are not characters but indicators that suggest how to format or
  1625. display neighboring characters.
  1626. @cindex reserved code point
  1627. @cindex code point, reserved
  1628. If a code point is not a designated code point -- if it has not been
  1629. assigned to a character by The Unicode Standard -- it is a 'reserved
  1630. code point', meaning that they are reserved for future use. Most of
  1631. the code points, about 800,000, are 'reserved code points'.
  1632. By convention, a Unicode code point is written as
  1633. ``U+XXXX'' where ``XXXX'' is a hexadecimal number. Please note that
  1634. this convenient notation is not valid code. Guile does not interpret
  1635. ``U+XXXX'' as a character.
  1636. In Scheme, a character literal is written as @code{#\@var{name}} where
  1637. @var{name} is the name of the character that you want. Printable
  1638. characters have their usual single character name; for example,
  1639. @code{#\a} is a lower case @code{a}.
  1640. Some of the code points are 'combining characters' that are not meant
  1641. to be printed by themselves but are instead meant to modify the
  1642. appearance of the previous character. For combining characters, an
  1643. alternate form of the character literal is @code{#\} followed by
  1644. U+25CC (a small, dotted circle), followed by the combining character.
  1645. This allows the combining character to be drawn on the circle, not on
  1646. the backslash of @code{#\}.
  1647. Many of the non-printing characters, such as whitespace characters and
  1648. control characters, also have names.
  1649. The most commonly used non-printing characters have long character
  1650. names, described in the table below.
  1651. @multitable {@code{#\backspace}} {Preferred}
  1652. @item Character Name @tab Codepoint
  1653. @item @code{#\nul} @tab U+0000
  1654. @item @code{#\alarm} @tab u+0007
  1655. @item @code{#\backspace} @tab U+0008
  1656. @item @code{#\tab} @tab U+0009
  1657. @item @code{#\linefeed} @tab U+000A
  1658. @item @code{#\newline} @tab U+000A
  1659. @item @code{#\vtab} @tab U+000B
  1660. @item @code{#\page} @tab U+000C
  1661. @item @code{#\return} @tab U+000D
  1662. @item @code{#\esc} @tab U+001B
  1663. @item @code{#\space} @tab U+0020
  1664. @item @code{#\delete} @tab U+007F
  1665. @end multitable
  1666. There are also short names for all of the ``C0 control characters''
  1667. (those with code points below 32). The following table lists the short
  1668. name for each character.
  1669. @multitable @columnfractions .25 .25 .25 .25
  1670. @item 0 = @code{#\nul}
  1671. @tab 1 = @code{#\soh}
  1672. @tab 2 = @code{#\stx}
  1673. @tab 3 = @code{#\etx}
  1674. @item 4 = @code{#\eot}
  1675. @tab 5 = @code{#\enq}
  1676. @tab 6 = @code{#\ack}
  1677. @tab 7 = @code{#\bel}
  1678. @item 8 = @code{#\bs}
  1679. @tab 9 = @code{#\ht}
  1680. @tab 10 = @code{#\lf}
  1681. @tab 11 = @code{#\vt}
  1682. @item 12 = @code{#\ff}
  1683. @tab 13 = @code{#\cr}
  1684. @tab 14 = @code{#\so}
  1685. @tab 15 = @code{#\si}
  1686. @item 16 = @code{#\dle}
  1687. @tab 17 = @code{#\dc1}
  1688. @tab 18 = @code{#\dc2}
  1689. @tab 19 = @code{#\dc3}
  1690. @item 20 = @code{#\dc4}
  1691. @tab 21 = @code{#\nak}
  1692. @tab 22 = @code{#\syn}
  1693. @tab 23 = @code{#\etb}
  1694. @item 24 = @code{#\can}
  1695. @tab 25 = @code{#\em}
  1696. @tab 26 = @code{#\sub}
  1697. @tab 27 = @code{#\esc}
  1698. @item 28 = @code{#\fs}
  1699. @tab 29 = @code{#\gs}
  1700. @tab 30 = @code{#\rs}
  1701. @tab 31 = @code{#\us}
  1702. @item 32 = @code{#\sp}
  1703. @end multitable
  1704. The short name for the ``delete'' character (code point U+007F) is
  1705. @code{#\del}.
  1706. There are also a few alternative names left over for compatibility with
  1707. previous versions of Guile.
  1708. @multitable {@code{#\backspace}} {Preferred}
  1709. @item Alternate @tab Standard
  1710. @item @code{#\nl} @tab @code{#\newline}
  1711. @item @code{#\np} @tab @code{#\page}
  1712. @item @code{#\null} @tab @code{#\nul}
  1713. @end multitable
  1714. Characters may also be written using their code point values. They can
  1715. be written with as an octal number, such as @code{#\10} for
  1716. @code{#\bs} or @code{#\177} for @code{#\del}.
  1717. If one prefers hex to octal, there is an additional syntax for character
  1718. escapes: @code{#\xHHHH} -- the letter 'x' followed by a hexadecimal
  1719. number of one to eight digits.
  1720. @rnindex char?
  1721. @deffn {Scheme Procedure} char? x
  1722. @deffnx {C Function} scm_char_p (x)
  1723. Return @code{#t} if @var{x} is a character, else @code{#f}.
  1724. @end deffn
  1725. Fundamentally, the character comparison operations below are
  1726. numeric comparisons of the character's code points.
  1727. @rnindex char=?
  1728. @deffn {Scheme Procedure} char=? x y
  1729. Return @code{#t} if code point of @var{x} is equal to the code point
  1730. of @var{y}, else @code{#f}.
  1731. @end deffn
  1732. @rnindex char<?
  1733. @deffn {Scheme Procedure} char<? x y
  1734. Return @code{#t} if the code point of @var{x} is less than the code
  1735. point of @var{y}, else @code{#f}.
  1736. @end deffn
  1737. @rnindex char<=?
  1738. @deffn {Scheme Procedure} char<=? x y
  1739. Return @code{#t} if the code point of @var{x} is less than or equal
  1740. to the code point of @var{y}, else @code{#f}.
  1741. @end deffn
  1742. @rnindex char>?
  1743. @deffn {Scheme Procedure} char>? x y
  1744. Return @code{#t} if the code point of @var{x} is greater than the
  1745. code point of @var{y}, else @code{#f}.
  1746. @end deffn
  1747. @rnindex char>=?
  1748. @deffn {Scheme Procedure} char>=? x y
  1749. Return @code{#t} if the code point of @var{x} is greater than or
  1750. equal to the code point of @var{y}, else @code{#f}.
  1751. @end deffn
  1752. @cindex case folding
  1753. Case-insensitive character comparisons use @emph{Unicode case
  1754. folding}. In case folding comparisons, if a character is lowercase
  1755. and has an uppercase form that can be expressed as a single character,
  1756. it is converted to uppercase before comparison. All other characters
  1757. undergo no conversion before the comparison occurs. This includes the
  1758. German sharp S (Eszett) which is not uppercased before conversion
  1759. because its uppercase form has two characters. Unicode case folding
  1760. is language independent: it uses rules that are generally true, but,
  1761. it cannot cover all cases for all languages.
  1762. @rnindex char-ci=?
  1763. @deffn {Scheme Procedure} char-ci=? x y
  1764. Return @code{#t} if the case-folded code point of @var{x} is the same
  1765. as the case-folded code point of @var{y}, else @code{#f}.
  1766. @end deffn
  1767. @rnindex char-ci<?
  1768. @deffn {Scheme Procedure} char-ci<? x y
  1769. Return @code{#t} if the case-folded code point of @var{x} is less
  1770. than the case-folded code point of @var{y}, else @code{#f}.
  1771. @end deffn
  1772. @rnindex char-ci<=?
  1773. @deffn {Scheme Procedure} char-ci<=? x y
  1774. Return @code{#t} if the case-folded code point of @var{x} is less
  1775. than or equal to the case-folded code point of @var{y}, else
  1776. @code{#f}.
  1777. @end deffn
  1778. @rnindex char-ci>?
  1779. @deffn {Scheme Procedure} char-ci>? x y
  1780. Return @code{#t} if the case-folded code point of @var{x} is greater
  1781. than the case-folded code point of @var{y}, else @code{#f}.
  1782. @end deffn
  1783. @rnindex char-ci>=?
  1784. @deffn {Scheme Procedure} char-ci>=? x y
  1785. Return @code{#t} if the case-folded code point of @var{x} is greater
  1786. than or equal to the case-folded code point of @var{y}, else
  1787. @code{#f}.
  1788. @end deffn
  1789. @rnindex char-alphabetic?
  1790. @deffn {Scheme Procedure} char-alphabetic? chr
  1791. @deffnx {C Function} scm_char_alphabetic_p (chr)
  1792. Return @code{#t} if @var{chr} is alphabetic, else @code{#f}.
  1793. @end deffn
  1794. @rnindex char-numeric?
  1795. @deffn {Scheme Procedure} char-numeric? chr
  1796. @deffnx {C Function} scm_char_numeric_p (chr)
  1797. Return @code{#t} if @var{chr} is numeric, else @code{#f}.
  1798. @end deffn
  1799. @rnindex char-whitespace?
  1800. @deffn {Scheme Procedure} char-whitespace? chr
  1801. @deffnx {C Function} scm_char_whitespace_p (chr)
  1802. Return @code{#t} if @var{chr} is whitespace, else @code{#f}.
  1803. @end deffn
  1804. @rnindex char-upper-case?
  1805. @deffn {Scheme Procedure} char-upper-case? chr
  1806. @deffnx {C Function} scm_char_upper_case_p (chr)
  1807. Return @code{#t} if @var{chr} is uppercase, else @code{#f}.
  1808. @end deffn
  1809. @rnindex char-lower-case?
  1810. @deffn {Scheme Procedure} char-lower-case? chr
  1811. @deffnx {C Function} scm_char_lower_case_p (chr)
  1812. Return @code{#t} if @var{chr} is lowercase, else @code{#f}.
  1813. @end deffn
  1814. @deffn {Scheme Procedure} char-is-both? chr
  1815. @deffnx {C Function} scm_char_is_both_p (chr)
  1816. Return @code{#t} if @var{chr} is either uppercase or lowercase, else
  1817. @code{#f}.
  1818. @end deffn
  1819. @deffn {Scheme Procedure} char-general-category chr
  1820. @deffnx {C Function} scm_char_general_category (chr)
  1821. Return a symbol giving the two-letter name of the Unicode general
  1822. category assigned to @var{chr} or @code{#f} if no named category is
  1823. assigned. The following table provides a list of category names along
  1824. with their meanings.
  1825. @multitable @columnfractions .1 .4 .1 .4
  1826. @item Lu
  1827. @tab Uppercase letter
  1828. @tab Pf
  1829. @tab Final quote punctuation
  1830. @item Ll
  1831. @tab Lowercase letter
  1832. @tab Po
  1833. @tab Other punctuation
  1834. @item Lt
  1835. @tab Titlecase letter
  1836. @tab Sm
  1837. @tab Math symbol
  1838. @item Lm
  1839. @tab Modifier letter
  1840. @tab Sc
  1841. @tab Currency symbol
  1842. @item Lo
  1843. @tab Other letter
  1844. @tab Sk
  1845. @tab Modifier symbol
  1846. @item Mn
  1847. @tab Non-spacing mark
  1848. @tab So
  1849. @tab Other symbol
  1850. @item Mc
  1851. @tab Combining spacing mark
  1852. @tab Zs
  1853. @tab Space separator
  1854. @item Me
  1855. @tab Enclosing mark
  1856. @tab Zl
  1857. @tab Line separator
  1858. @item Nd
  1859. @tab Decimal digit number
  1860. @tab Zp
  1861. @tab Paragraph separator
  1862. @item Nl
  1863. @tab Letter number
  1864. @tab Cc
  1865. @tab Control
  1866. @item No
  1867. @tab Other number
  1868. @tab Cf
  1869. @tab Format
  1870. @item Pc
  1871. @tab Connector punctuation
  1872. @tab Cs
  1873. @tab Surrogate
  1874. @item Pd
  1875. @tab Dash punctuation
  1876. @tab Co
  1877. @tab Private use
  1878. @item Ps
  1879. @tab Open punctuation
  1880. @tab Cn
  1881. @tab Unassigned
  1882. @item Pe
  1883. @tab Close punctuation
  1884. @tab
  1885. @tab
  1886. @item Pi
  1887. @tab Initial quote punctuation
  1888. @tab
  1889. @tab
  1890. @end multitable
  1891. @end deffn
  1892. @rnindex char->integer
  1893. @deffn {Scheme Procedure} char->integer chr
  1894. @deffnx {C Function} scm_char_to_integer (chr)
  1895. Return the code point of @var{chr}.
  1896. @end deffn
  1897. @rnindex integer->char
  1898. @deffn {Scheme Procedure} integer->char n
  1899. @deffnx {C Function} scm_integer_to_char (n)
  1900. Return the character that has code point @var{n}. The integer @var{n}
  1901. must be a valid code point. Valid code points are in the ranges 0 to
  1902. @code{#xD7FF} inclusive or @code{#xE000} to @code{#x10FFFF} inclusive.
  1903. @end deffn
  1904. @rnindex char-upcase
  1905. @deffn {Scheme Procedure} char-upcase chr
  1906. @deffnx {C Function} scm_char_upcase (chr)
  1907. Return the uppercase character version of @var{chr}.
  1908. @end deffn
  1909. @rnindex char-downcase
  1910. @deffn {Scheme Procedure} char-downcase chr
  1911. @deffnx {C Function} scm_char_downcase (chr)
  1912. Return the lowercase character version of @var{chr}.
  1913. @end deffn
  1914. @rnindex char-titlecase
  1915. @deffn {Scheme Procedure} char-titlecase chr
  1916. @deffnx {C Function} scm_char_titlecase (chr)
  1917. Return the titlecase character version of @var{chr} if one exists;
  1918. otherwise return the uppercase version.
  1919. For most characters these will be the same, but the Unicode Standard
  1920. includes certain digraph compatibility characters, such as @code{U+01F3}
  1921. ``dz'', for which the uppercase and titlecase characters are different
  1922. (@code{U+01F1} ``DZ'' and @code{U+01F2} ``Dz'' in this case,
  1923. respectively).
  1924. @end deffn
  1925. @tindex scm_t_wchar
  1926. @deftypefn {C Function} scm_t_wchar scm_c_upcase (scm_t_wchar @var{c})
  1927. @deftypefnx {C Function} scm_t_wchar scm_c_downcase (scm_t_wchar @var{c})
  1928. @deftypefnx {C Function} scm_t_wchar scm_c_titlecase (scm_t_wchar @var{c})
  1929. These C functions take an integer representation of a Unicode
  1930. codepoint and return the codepoint corresponding to its uppercase,
  1931. lowercase, and titlecase forms respectively. The type
  1932. @code{scm_t_wchar} is a signed, 32-bit integer.
  1933. @end deftypefn
  1934. @node Character Sets
  1935. @subsection Character Sets
  1936. The features described in this section correspond directly to SRFI-14.
  1937. The data type @dfn{charset} implements sets of characters
  1938. (@pxref{Characters}). Because the internal representation of
  1939. character sets is not visible to the user, a lot of procedures for
  1940. handling them are provided.
  1941. Character sets can be created, extended, tested for the membership of a
  1942. characters and be compared to other character sets.
  1943. @menu
  1944. * Character Set Predicates/Comparison::
  1945. * Iterating Over Character Sets:: Enumerate charset elements.
  1946. * Creating Character Sets:: Making new charsets.
  1947. * Querying Character Sets:: Test charsets for membership etc.
  1948. * Character-Set Algebra:: Calculating new charsets.
  1949. * Standard Character Sets:: Variables containing predefined charsets.
  1950. @end menu
  1951. @node Character Set Predicates/Comparison
  1952. @subsubsection Character Set Predicates/Comparison
  1953. Use these procedures for testing whether an object is a character set,
  1954. or whether several character sets are equal or subsets of each other.
  1955. @code{char-set-hash} can be used for calculating a hash value, maybe for
  1956. usage in fast lookup procedures.
  1957. @deffn {Scheme Procedure} char-set? obj
  1958. @deffnx {C Function} scm_char_set_p (obj)
  1959. Return @code{#t} if @var{obj} is a character set, @code{#f}
  1960. otherwise.
  1961. @end deffn
  1962. @deffn {Scheme Procedure} char-set= char_set @dots{}
  1963. @deffnx {C Function} scm_char_set_eq (char_sets)
  1964. Return @code{#t} if all given character sets are equal.
  1965. @end deffn
  1966. @deffn {Scheme Procedure} char-set<= char_set @dots{}
  1967. @deffnx {C Function} scm_char_set_leq (char_sets)
  1968. Return @code{#t} if every character set @var{char_set}i is a subset
  1969. of character set @var{char_set}i+1.
  1970. @end deffn
  1971. @deffn {Scheme Procedure} char-set-hash cs [bound]
  1972. @deffnx {C Function} scm_char_set_hash (cs, bound)
  1973. Compute a hash value for the character set @var{cs}. If
  1974. @var{bound} is given and non-zero, it restricts the
  1975. returned value to the range 0 @dots{} @var{bound} - 1.
  1976. @end deffn
  1977. @c ===================================================================
  1978. @node Iterating Over Character Sets
  1979. @subsubsection Iterating Over Character Sets
  1980. Character set cursors are a means for iterating over the members of a
  1981. character sets. After creating a character set cursor with
  1982. @code{char-set-cursor}, a cursor can be dereferenced with
  1983. @code{char-set-ref}, advanced to the next member with
  1984. @code{char-set-cursor-next}. Whether a cursor has passed past the last
  1985. element of the set can be checked with @code{end-of-char-set?}.
  1986. Additionally, mapping and (un-)folding procedures for character sets are
  1987. provided.
  1988. @deffn {Scheme Procedure} char-set-cursor cs
  1989. @deffnx {C Function} scm_char_set_cursor (cs)
  1990. Return a cursor into the character set @var{cs}.
  1991. @end deffn
  1992. @deffn {Scheme Procedure} char-set-ref cs cursor
  1993. @deffnx {C Function} scm_char_set_ref (cs, cursor)
  1994. Return the character at the current cursor position
  1995. @var{cursor} in the character set @var{cs}. It is an error to
  1996. pass a cursor for which @code{end-of-char-set?} returns true.
  1997. @end deffn
  1998. @deffn {Scheme Procedure} char-set-cursor-next cs cursor
  1999. @deffnx {C Function} scm_char_set_cursor_next (cs, cursor)
  2000. Advance the character set cursor @var{cursor} to the next
  2001. character in the character set @var{cs}. It is an error if the
  2002. cursor given satisfies @code{end-of-char-set?}.
  2003. @end deffn
  2004. @deffn {Scheme Procedure} end-of-char-set? cursor
  2005. @deffnx {C Function} scm_end_of_char_set_p (cursor)
  2006. Return @code{#t} if @var{cursor} has reached the end of a
  2007. character set, @code{#f} otherwise.
  2008. @end deffn
  2009. @deffn {Scheme Procedure} char-set-fold kons knil cs
  2010. @deffnx {C Function} scm_char_set_fold (kons, knil, cs)
  2011. Fold the procedure @var{kons} over the character set @var{cs},
  2012. initializing it with @var{knil}.
  2013. @end deffn
  2014. @deffn {Scheme Procedure} char-set-unfold p f g seed [base_cs]
  2015. @deffnx {C Function} scm_char_set_unfold (p, f, g, seed, base_cs)
  2016. This is a fundamental constructor for character sets.
  2017. @itemize @bullet
  2018. @item @var{g} is used to generate a series of ``seed'' values
  2019. from the initial seed: @var{seed}, (@var{g} @var{seed}),
  2020. (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}), @dots{}
  2021. @item @var{p} tells us when to stop -- when it returns true
  2022. when applied to one of the seed values.
  2023. @item @var{f} maps each seed value to a character. These
  2024. characters are added to the base character set @var{base_cs} to
  2025. form the result; @var{base_cs} defaults to the empty set.
  2026. @end itemize
  2027. @end deffn
  2028. @deffn {Scheme Procedure} char-set-unfold! p f g seed base_cs
  2029. @deffnx {C Function} scm_char_set_unfold_x (p, f, g, seed, base_cs)
  2030. This is a fundamental constructor for character sets.
  2031. @itemize @bullet
  2032. @item @var{g} is used to generate a series of ``seed'' values
  2033. from the initial seed: @var{seed}, (@var{g} @var{seed}),
  2034. (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}), @dots{}
  2035. @item @var{p} tells us when to stop -- when it returns true
  2036. when applied to one of the seed values.
  2037. @item @var{f} maps each seed value to a character. These
  2038. characters are added to the base character set @var{base_cs} to
  2039. form the result; @var{base_cs} defaults to the empty set.
  2040. @end itemize
  2041. @end deffn
  2042. @deffn {Scheme Procedure} char-set-for-each proc cs
  2043. @deffnx {C Function} scm_char_set_for_each (proc, cs)
  2044. Apply @var{proc} to every character in the character set
  2045. @var{cs}. The return value is not specified.
  2046. @end deffn
  2047. @deffn {Scheme Procedure} char-set-map proc cs
  2048. @deffnx {C Function} scm_char_set_map (proc, cs)
  2049. Map the procedure @var{proc} over every character in @var{cs}.
  2050. @var{proc} must be a character -> character procedure.
  2051. @end deffn
  2052. @c ===================================================================
  2053. @node Creating Character Sets
  2054. @subsubsection Creating Character Sets
  2055. New character sets are produced with these procedures.
  2056. @deffn {Scheme Procedure} char-set-copy cs
  2057. @deffnx {C Function} scm_char_set_copy (cs)
  2058. Return a newly allocated character set containing all
  2059. characters in @var{cs}.
  2060. @end deffn
  2061. @deffn {Scheme Procedure} char-set chr @dots{}
  2062. @deffnx {C Function} scm_char_set (chrs)
  2063. Return a character set containing all given characters.
  2064. @end deffn
  2065. @deffn {Scheme Procedure} list->char-set list [base_cs]
  2066. @deffnx {C Function} scm_list_to_char_set (list, base_cs)
  2067. Convert the character list @var{list} to a character set. If
  2068. the character set @var{base_cs} is given, the character in this
  2069. set are also included in the result.
  2070. @end deffn
  2071. @deffn {Scheme Procedure} list->char-set! list base_cs
  2072. @deffnx {C Function} scm_list_to_char_set_x (list, base_cs)
  2073. Convert the character list @var{list} to a character set. The
  2074. characters are added to @var{base_cs} and @var{base_cs} is
  2075. returned.
  2076. @end deffn
  2077. @deffn {Scheme Procedure} string->char-set str [base_cs]
  2078. @deffnx {C Function} scm_string_to_char_set (str, base_cs)
  2079. Convert the string @var{str} to a character set. If the
  2080. character set @var{base_cs} is given, the characters in this
  2081. set are also included in the result.
  2082. @end deffn
  2083. @deffn {Scheme Procedure} string->char-set! str base_cs
  2084. @deffnx {C Function} scm_string_to_char_set_x (str, base_cs)
  2085. Convert the string @var{str} to a character set. The
  2086. characters from the string are added to @var{base_cs}, and
  2087. @var{base_cs} is returned.
  2088. @end deffn
  2089. @deffn {Scheme Procedure} char-set-filter pred cs [base_cs]
  2090. @deffnx {C Function} scm_char_set_filter (pred, cs, base_cs)
  2091. Return a character set containing every character from @var{cs}
  2092. so that it satisfies @var{pred}. If provided, the characters
  2093. from @var{base_cs} are added to the result.
  2094. @end deffn
  2095. @deffn {Scheme Procedure} char-set-filter! pred cs base_cs
  2096. @deffnx {C Function} scm_char_set_filter_x (pred, cs, base_cs)
  2097. Return a character set containing every character from @var{cs}
  2098. so that it satisfies @var{pred}. The characters are added to
  2099. @var{base_cs} and @var{base_cs} is returned.
  2100. @end deffn
  2101. @deffn {Scheme Procedure} ucs-range->char-set lower upper [error [base_cs]]
  2102. @deffnx {C Function} scm_ucs_range_to_char_set (lower, upper, error, base_cs)
  2103. Return a character set containing all characters whose
  2104. character codes lie in the half-open range
  2105. [@var{lower},@var{upper}).
  2106. If @var{error} is a true value, an error is signalled if the
  2107. specified range contains characters which are not contained in
  2108. the implemented character range. If @var{error} is @code{#f},
  2109. these characters are silently left out of the resulting
  2110. character set.
  2111. The characters in @var{base_cs} are added to the result, if
  2112. given.
  2113. @end deffn
  2114. @deffn {Scheme Procedure} ucs-range->char-set! lower upper error base_cs
  2115. @deffnx {C Function} scm_ucs_range_to_char_set_x (lower, upper, error, base_cs)
  2116. Return a character set containing all characters whose
  2117. character codes lie in the half-open range
  2118. [@var{lower},@var{upper}).
  2119. If @var{error} is a true value, an error is signalled if the
  2120. specified range contains characters which are not contained in
  2121. the implemented character range. If @var{error} is @code{#f},
  2122. these characters are silently left out of the resulting
  2123. character set.
  2124. The characters are added to @var{base_cs} and @var{base_cs} is
  2125. returned.
  2126. @end deffn
  2127. @deffn {Scheme Procedure} ->char-set x
  2128. @deffnx {C Function} scm_to_char_set (x)
  2129. Coerces x into a char-set. @var{x} may be a string, character or
  2130. char-set. A string is converted to the set of its constituent
  2131. characters; a character is converted to a singleton set; a char-set is
  2132. returned as-is.
  2133. @end deffn
  2134. @c ===================================================================
  2135. @node Querying Character Sets
  2136. @subsubsection Querying Character Sets
  2137. Access the elements and other information of a character set with these
  2138. procedures.
  2139. @deffn {Scheme Procedure} %char-set-dump cs
  2140. Returns an association list containing debugging information
  2141. for @var{cs}. The association list has the following entries.
  2142. @table @code
  2143. @item char-set
  2144. The char-set itself
  2145. @item len
  2146. The number of groups of contiguous code points the char-set
  2147. contains
  2148. @item ranges
  2149. A list of lists where each sublist is a range of code points
  2150. and their associated characters
  2151. @end table
  2152. The return value of this function cannot be relied upon to be
  2153. consistent between versions of Guile and should not be used in code.
  2154. @end deffn
  2155. @deffn {Scheme Procedure} char-set-size cs
  2156. @deffnx {C Function} scm_char_set_size (cs)
  2157. Return the number of elements in character set @var{cs}.
  2158. @end deffn
  2159. @deffn {Scheme Procedure} char-set-count pred cs
  2160. @deffnx {C Function} scm_char_set_count (pred, cs)
  2161. Return the number of the elements int the character set
  2162. @var{cs} which satisfy the predicate @var{pred}.
  2163. @end deffn
  2164. @deffn {Scheme Procedure} char-set->list cs
  2165. @deffnx {C Function} scm_char_set_to_list (cs)
  2166. Return a list containing the elements of the character set
  2167. @var{cs}.
  2168. @end deffn
  2169. @deffn {Scheme Procedure} char-set->string cs
  2170. @deffnx {C Function} scm_char_set_to_string (cs)
  2171. Return a string containing the elements of the character set
  2172. @var{cs}. The order in which the characters are placed in the
  2173. string is not defined.
  2174. @end deffn
  2175. @deffn {Scheme Procedure} char-set-contains? cs ch
  2176. @deffnx {C Function} scm_char_set_contains_p (cs, ch)
  2177. Return @code{#t} if the character @var{ch} is contained in the
  2178. character set @var{cs}, or @code{#f} otherwise.
  2179. @end deffn
  2180. @deffn {Scheme Procedure} char-set-every pred cs
  2181. @deffnx {C Function} scm_char_set_every (pred, cs)
  2182. Return a true value if every character in the character set
  2183. @var{cs} satisfies the predicate @var{pred}.
  2184. @end deffn
  2185. @deffn {Scheme Procedure} char-set-any pred cs
  2186. @deffnx {C Function} scm_char_set_any (pred, cs)
  2187. Return a true value if any character in the character set
  2188. @var{cs} satisfies the predicate @var{pred}.
  2189. @end deffn
  2190. @c ===================================================================
  2191. @node Character-Set Algebra
  2192. @subsubsection Character-Set Algebra
  2193. Character sets can be manipulated with the common set algebra operation,
  2194. such as union, complement, intersection etc. All of these procedures
  2195. provide side-effecting variants, which modify their character set
  2196. argument(s).
  2197. @deffn {Scheme Procedure} char-set-adjoin cs chr @dots{}
  2198. @deffnx {C Function} scm_char_set_adjoin (cs, chrs)
  2199. Add all character arguments to the first argument, which must
  2200. be a character set.
  2201. @end deffn
  2202. @deffn {Scheme Procedure} char-set-delete cs chr @dots{}
  2203. @deffnx {C Function} scm_char_set_delete (cs, chrs)
  2204. Delete all character arguments from the first argument, which
  2205. must be a character set.
  2206. @end deffn
  2207. @deffn {Scheme Procedure} char-set-adjoin! cs chr @dots{}
  2208. @deffnx {C Function} scm_char_set_adjoin_x (cs, chrs)
  2209. Add all character arguments to the first argument, which must
  2210. be a character set.
  2211. @end deffn
  2212. @deffn {Scheme Procedure} char-set-delete! cs chr @dots{}
  2213. @deffnx {C Function} scm_char_set_delete_x (cs, chrs)
  2214. Delete all character arguments from the first argument, which
  2215. must be a character set.
  2216. @end deffn
  2217. @deffn {Scheme Procedure} char-set-complement cs
  2218. @deffnx {C Function} scm_char_set_complement (cs)
  2219. Return the complement of the character set @var{cs}.
  2220. @end deffn
  2221. Note that the complement of a character set is likely to contain many
  2222. reserved code points (code points that are not associated with
  2223. characters). It may be helpful to modify the output of
  2224. @code{char-set-complement} by computing its intersection with the set
  2225. of designated code points, @code{char-set:designated}.
  2226. @deffn {Scheme Procedure} char-set-union cs @dots{}
  2227. @deffnx {C Function} scm_char_set_union (char_sets)
  2228. Return the union of all argument character sets.
  2229. @end deffn
  2230. @deffn {Scheme Procedure} char-set-intersection cs @dots{}
  2231. @deffnx {C Function} scm_char_set_intersection (char_sets)
  2232. Return the intersection of all argument character sets.
  2233. @end deffn
  2234. @deffn {Scheme Procedure} char-set-difference cs1 cs @dots{}
  2235. @deffnx {C Function} scm_char_set_difference (cs1, char_sets)
  2236. Return the difference of all argument character sets.
  2237. @end deffn
  2238. @deffn {Scheme Procedure} char-set-xor cs @dots{}
  2239. @deffnx {C Function} scm_char_set_xor (char_sets)
  2240. Return the exclusive-or of all argument character sets.
  2241. @end deffn
  2242. @deffn {Scheme Procedure} char-set-diff+intersection cs1 cs @dots{}
  2243. @deffnx {C Function} scm_char_set_diff_plus_intersection (cs1, char_sets)
  2244. Return the difference and the intersection of all argument
  2245. character sets.
  2246. @end deffn
  2247. @deffn {Scheme Procedure} char-set-complement! cs
  2248. @deffnx {C Function} scm_char_set_complement_x (cs)
  2249. Return the complement of the character set @var{cs}.
  2250. @end deffn
  2251. @deffn {Scheme Procedure} char-set-union! cs1 cs @dots{}
  2252. @deffnx {C Function} scm_char_set_union_x (cs1, char_sets)
  2253. Return the union of all argument character sets.
  2254. @end deffn
  2255. @deffn {Scheme Procedure} char-set-intersection! cs1 cs @dots{}
  2256. @deffnx {C Function} scm_char_set_intersection_x (cs1, char_sets)
  2257. Return the intersection of all argument character sets.
  2258. @end deffn
  2259. @deffn {Scheme Procedure} char-set-difference! cs1 cs @dots{}
  2260. @deffnx {C Function} scm_char_set_difference_x (cs1, char_sets)
  2261. Return the difference of all argument character sets.
  2262. @end deffn
  2263. @deffn {Scheme Procedure} char-set-xor! cs1 cs @dots{}
  2264. @deffnx {C Function} scm_char_set_xor_x (cs1, char_sets)
  2265. Return the exclusive-or of all argument character sets.
  2266. @end deffn
  2267. @deffn {Scheme Procedure} char-set-diff+intersection! cs1 cs2 cs @dots{}
  2268. @deffnx {C Function} scm_char_set_diff_plus_intersection_x (cs1, cs2, char_sets)
  2269. Return the difference and the intersection of all argument
  2270. character sets.
  2271. @end deffn
  2272. @c ===================================================================
  2273. @node Standard Character Sets
  2274. @subsubsection Standard Character Sets
  2275. In order to make the use of the character set data type and procedures
  2276. useful, several predefined character set variables exist.
  2277. @cindex codeset
  2278. @cindex charset
  2279. @cindex locale
  2280. These character sets are locale independent and are not recomputed
  2281. upon a @code{setlocale} call. They contain characters from the whole
  2282. range of Unicode code points. For instance, @code{char-set:letter}
  2283. contains about 100,000 characters.
  2284. @defvr {Scheme Variable} char-set:lower-case
  2285. @defvrx {C Variable} scm_char_set_lower_case
  2286. All lower-case characters.
  2287. @end defvr
  2288. @defvr {Scheme Variable} char-set:upper-case
  2289. @defvrx {C Variable} scm_char_set_upper_case
  2290. All upper-case characters.
  2291. @end defvr
  2292. @defvr {Scheme Variable} char-set:title-case
  2293. @defvrx {C Variable} scm_char_set_title_case
  2294. All single characters that function as if they were an upper-case
  2295. letter followed by a lower-case letter.
  2296. @end defvr
  2297. @defvr {Scheme Variable} char-set:letter
  2298. @defvrx {C Variable} scm_char_set_letter
  2299. All letters. This includes @code{char-set:lower-case},
  2300. @code{char-set:upper-case}, @code{char-set:title-case}, and many
  2301. letters that have no case at all. For example, Chinese and Japanese
  2302. characters typically have no concept of case.
  2303. @end defvr
  2304. @defvr {Scheme Variable} char-set:digit
  2305. @defvrx {C Variable} scm_char_set_digit
  2306. All digits.
  2307. @end defvr
  2308. @defvr {Scheme Variable} char-set:letter+digit
  2309. @defvrx {C Variable} scm_char_set_letter_and_digit
  2310. The union of @code{char-set:letter} and @code{char-set:digit}.
  2311. @end defvr
  2312. @defvr {Scheme Variable} char-set:graphic
  2313. @defvrx {C Variable} scm_char_set_graphic
  2314. All characters which would put ink on the paper.
  2315. @end defvr
  2316. @defvr {Scheme Variable} char-set:printing
  2317. @defvrx {C Variable} scm_char_set_printing
  2318. The union of @code{char-set:graphic} and @code{char-set:whitespace}.
  2319. @end defvr
  2320. @defvr {Scheme Variable} char-set:whitespace
  2321. @defvrx {C Variable} scm_char_set_whitespace
  2322. All whitespace characters.
  2323. @end defvr
  2324. @defvr {Scheme Variable} char-set:blank
  2325. @defvrx {C Variable} scm_char_set_blank
  2326. All horizontal whitespace characters, which notably includes
  2327. @code{#\space} and @code{#\tab}.
  2328. @end defvr
  2329. @defvr {Scheme Variable} char-set:iso-control
  2330. @defvrx {C Variable} scm_char_set_iso_control
  2331. The ISO control characters are the C0 control characters (U+0000 to
  2332. U+001F), delete (U+007F), and the C1 control characters (U+0080 to
  2333. U+009F).
  2334. @end defvr
  2335. @defvr {Scheme Variable} char-set:punctuation
  2336. @defvrx {C Variable} scm_char_set_punctuation
  2337. All punctuation characters, such as the characters
  2338. @code{!"#%&'()*,-./:;?@@[\\]_@{@}}
  2339. @end defvr
  2340. @defvr {Scheme Variable} char-set:symbol
  2341. @defvrx {C Variable} scm_char_set_symbol
  2342. All symbol characters, such as the characters @code{$+<=>^`|~}.
  2343. @end defvr
  2344. @defvr {Scheme Variable} char-set:hex-digit
  2345. @defvrx {C Variable} scm_char_set_hex_digit
  2346. The hexadecimal digits @code{0123456789abcdefABCDEF}.
  2347. @end defvr
  2348. @defvr {Scheme Variable} char-set:ascii
  2349. @defvrx {C Variable} scm_char_set_ascii
  2350. All ASCII characters.
  2351. @end defvr
  2352. @defvr {Scheme Variable} char-set:empty
  2353. @defvrx {C Variable} scm_char_set_empty
  2354. The empty character set.
  2355. @end defvr
  2356. @defvr {Scheme Variable} char-set:designated
  2357. @defvrx {C Variable} scm_char_set_designated
  2358. This character set contains all designated code points. This includes
  2359. all the code points to which Unicode has assigned a character or other
  2360. meaning.
  2361. @end defvr
  2362. @defvr {Scheme Variable} char-set:full
  2363. @defvrx {C Variable} scm_char_set_full
  2364. This character set contains all possible code points. This includes
  2365. both designated and reserved code points.
  2366. @end defvr
  2367. @node Strings
  2368. @subsection Strings
  2369. @tpindex Strings
  2370. Strings are fixed-length sequences of characters. They can be created
  2371. by calling constructor procedures, but they can also literally get
  2372. entered at the @acronym{REPL} or in Scheme source files.
  2373. @c Guile provides a rich set of string processing procedures, because text
  2374. @c handling is very important when Guile is used as a scripting language.
  2375. Strings always carry the information about how many characters they are
  2376. composed of with them, so there is no special end-of-string character,
  2377. like in C. That means that Scheme strings can contain any character,
  2378. even the @samp{#\nul} character @samp{\0}.
  2379. To use strings efficiently, you need to know a bit about how Guile
  2380. implements them. In Guile, a string consists of two parts, a head and
  2381. the actual memory where the characters are stored. When a string (or
  2382. a substring of it) is copied, only a new head gets created, the memory
  2383. is usually not copied. The two heads start out pointing to the same
  2384. memory.
  2385. When one of these two strings is modified, as with @code{string-set!},
  2386. their common memory does get copied so that each string has its own
  2387. memory and modifying one does not accidentally modify the other as well.
  2388. Thus, Guile's strings are `copy on write'; the actual copying of their
  2389. memory is delayed until one string is written to.
  2390. This implementation makes functions like @code{substring} very
  2391. efficient in the common case that no modifications are done to the
  2392. involved strings.
  2393. If you do know that your strings are getting modified right away, you
  2394. can use @code{substring/copy} instead of @code{substring}. This
  2395. function performs the copy immediately at the time of creation. This
  2396. is more efficient, especially in a multi-threaded program. Also,
  2397. @code{substring/copy} can avoid the problem that a short substring
  2398. holds on to the memory of a very large original string that could
  2399. otherwise be recycled.
  2400. If you want to avoid the copy altogether, so that modifications of one
  2401. string show up in the other, you can use @code{substring/shared}. The
  2402. strings created by this procedure are called @dfn{mutation sharing
  2403. substrings} since the substring and the original string share
  2404. modifications to each other.
  2405. If you want to prevent modifications, use @code{substring/read-only}.
  2406. Guile provides all procedures of SRFI-13 and a few more.
  2407. @menu
  2408. * String Syntax:: Read syntax for strings.
  2409. * String Predicates:: Testing strings for certain properties.
  2410. * String Constructors:: Creating new string objects.
  2411. * List/String Conversion:: Converting from/to lists of characters.
  2412. * String Selection:: Select portions from strings.
  2413. * String Modification:: Modify parts or whole strings.
  2414. * String Comparison:: Lexicographic ordering predicates.
  2415. * String Searching:: Searching in strings.
  2416. * Alphabetic Case Mapping:: Convert the alphabetic case of strings.
  2417. * Reversing and Appending Strings:: Appending strings to form a new string.
  2418. * Mapping Folding and Unfolding:: Iterating over strings.
  2419. * Miscellaneous String Operations:: Replicating, insertion, parsing, ...
  2420. * Representing Strings as Bytes:: Encoding and decoding strings.
  2421. * Conversion to/from C::
  2422. * String Internals:: The storage strategy for strings.
  2423. @end menu
  2424. @node String Syntax
  2425. @subsubsection String Read Syntax
  2426. @c In the following @code is used to get a good font in TeX etc, but
  2427. @c is omitted for Info format, so as not to risk any confusion over
  2428. @c whether surrounding ` ' quotes are part of the escape or are
  2429. @c special in a string (they're not).
  2430. The read syntax for strings is an arbitrarily long sequence of
  2431. characters enclosed in double quotes (@nicode{"}).
  2432. Backslash is an escape character and can be used to insert the following
  2433. special characters. @nicode{\"} and @nicode{\\} are R5RS standard, the
  2434. next seven are R6RS standard --- notice they follow C syntax --- and the
  2435. remaining four are Guile extensions.
  2436. @table @asis
  2437. @item @nicode{\\}
  2438. Backslash character.
  2439. @item @nicode{\"}
  2440. Double quote character (an unescaped @nicode{"} is otherwise the end
  2441. of the string).
  2442. @item @nicode{\a}
  2443. Bell character (ASCII 7).
  2444. @item @nicode{\f}
  2445. Formfeed character (ASCII 12).
  2446. @item @nicode{\n}
  2447. Newline character (ASCII 10).
  2448. @item @nicode{\r}
  2449. Carriage return character (ASCII 13).
  2450. @item @nicode{\t}
  2451. Tab character (ASCII 9).
  2452. @item @nicode{\v}
  2453. Vertical tab character (ASCII 11).
  2454. @item @nicode{\b}
  2455. Backspace character (ASCII 8).
  2456. @item @nicode{\0}
  2457. NUL character (ASCII 0).
  2458. @item @nicode{\} followed by newline (ASCII 10)
  2459. Nothing. This way if @nicode{\} is the last character in a line, the
  2460. string will continue with the first character from the next line,
  2461. without a line break.
  2462. If the @code{hungry-eol-escapes} reader option is enabled, which is not
  2463. the case by default, leading whitespace on the next line is discarded.
  2464. @lisp
  2465. "foo\
  2466. bar"
  2467. @result{} "foo bar"
  2468. (read-enable 'hungry-eol-escapes)
  2469. "foo\
  2470. bar"
  2471. @result{} "foobar"
  2472. @end lisp
  2473. @item @nicode{\xHH}
  2474. Character code given by two hexadecimal digits. For example
  2475. @nicode{\x7f} for an ASCII DEL (127).
  2476. @item @nicode{\uHHHH}
  2477. Character code given by four hexadecimal digits. For example
  2478. @nicode{\u0100} for a capital A with macron (U+0100).
  2479. @item @nicode{\UHHHHHH}
  2480. Character code given by six hexadecimal digits. For example
  2481. @nicode{\U010402}.
  2482. @end table
  2483. @noindent
  2484. The following are examples of string literals:
  2485. @lisp
  2486. "foo"
  2487. "bar plonk"
  2488. "Hello World"
  2489. "\"Hi\", he said."
  2490. @end lisp
  2491. The three escape sequences @code{\xHH}, @code{\uHHHH} and @code{\UHHHHHH} were
  2492. chosen to not break compatibility with code written for previous versions of
  2493. Guile. The R6RS specification suggests a different, incompatible syntax for hex
  2494. escapes: @code{\xHHHH;} -- a character code followed by one to eight hexadecimal
  2495. digits terminated with a semicolon. If this escape format is desired instead,
  2496. it can be enabled with the reader option @code{r6rs-hex-escapes}.
  2497. @lisp
  2498. (read-enable 'r6rs-hex-escapes)
  2499. @end lisp
  2500. For more on reader options, @xref{Scheme Read}.
  2501. @node String Predicates
  2502. @subsubsection String Predicates
  2503. The following procedures can be used to check whether a given string
  2504. fulfills some specified property.
  2505. @rnindex string?
  2506. @deffn {Scheme Procedure} string? obj
  2507. @deffnx {C Function} scm_string_p (obj)
  2508. Return @code{#t} if @var{obj} is a string, else @code{#f}.
  2509. @end deffn
  2510. @deftypefn {C Function} int scm_is_string (SCM obj)
  2511. Returns @code{1} if @var{obj} is a string, @code{0} otherwise.
  2512. @end deftypefn
  2513. @deffn {Scheme Procedure} string-null? str
  2514. @deffnx {C Function} scm_string_null_p (str)
  2515. Return @code{#t} if @var{str}'s length is zero, and
  2516. @code{#f} otherwise.
  2517. @lisp
  2518. (string-null? "") @result{} #t
  2519. y @result{} "foo"
  2520. (string-null? y) @result{} #f
  2521. @end lisp
  2522. @end deffn
  2523. @deffn {Scheme Procedure} string-any char_pred s [start [end]]
  2524. @deffnx {C Function} scm_string_any (char_pred, s, start, end)
  2525. Check if @var{char_pred} is true for any character in string @var{s}.
  2526. @var{char_pred} can be a character to check for any equal to that, or
  2527. a character set (@pxref{Character Sets}) to check for any in that set,
  2528. or a predicate procedure to call.
  2529. For a procedure, calls @code{(@var{char_pred} c)} are made
  2530. successively on the characters from @var{start} to @var{end}. If
  2531. @var{char_pred} returns true (ie.@: non-@code{#f}), @code{string-any}
  2532. stops and that return value is the return from @code{string-any}. The
  2533. call on the last character (ie.@: at @math{@var{end}-1}), if that
  2534. point is reached, is a tail call.
  2535. If there are no characters in @var{s} (ie.@: @var{start} equals
  2536. @var{end}) then the return is @code{#f}.
  2537. @end deffn
  2538. @deffn {Scheme Procedure} string-every char_pred s [start [end]]
  2539. @deffnx {C Function} scm_string_every (char_pred, s, start, end)
  2540. Check if @var{char_pred} is true for every character in string
  2541. @var{s}.
  2542. @var{char_pred} can be a character to check for every character equal
  2543. to that, or a character set (@pxref{Character Sets}) to check for
  2544. every character being in that set, or a predicate procedure to call.
  2545. For a procedure, calls @code{(@var{char_pred} c)} are made
  2546. successively on the characters from @var{start} to @var{end}. If
  2547. @var{char_pred} returns @code{#f}, @code{string-every} stops and
  2548. returns @code{#f}. The call on the last character (ie.@: at
  2549. @math{@var{end}-1}), if that point is reached, is a tail call and the
  2550. return from that call is the return from @code{string-every}.
  2551. If there are no characters in @var{s} (ie.@: @var{start} equals
  2552. @var{end}) then the return is @code{#t}.
  2553. @end deffn
  2554. @node String Constructors
  2555. @subsubsection String Constructors
  2556. The string constructor procedures create new string objects, possibly
  2557. initializing them with some specified character data. See also
  2558. @xref{String Selection}, for ways to create strings from existing
  2559. strings.
  2560. @c FIXME::martin: list->string belongs into `List/String Conversion'
  2561. @deffn {Scheme Procedure} string char@dots{}
  2562. @rnindex string
  2563. Return a newly allocated string made from the given character
  2564. arguments.
  2565. @example
  2566. (string #\x #\y #\z) @result{} "xyz"
  2567. (string) @result{} ""
  2568. @end example
  2569. @end deffn
  2570. @deffn {Scheme Procedure} list->string lst
  2571. @deffnx {C Function} scm_string (lst)
  2572. @rnindex list->string
  2573. Return a newly allocated string made from a list of characters.
  2574. @example
  2575. (list->string '(#\a #\b #\c)) @result{} "abc"
  2576. @end example
  2577. @end deffn
  2578. @deffn {Scheme Procedure} reverse-list->string lst
  2579. @deffnx {C Function} scm_reverse_list_to_string (lst)
  2580. Return a newly allocated string made from a list of characters, in
  2581. reverse order.
  2582. @example
  2583. (reverse-list->string '(#\a #\B #\c)) @result{} "cBa"
  2584. @end example
  2585. @end deffn
  2586. @rnindex make-string
  2587. @deffn {Scheme Procedure} make-string k [chr]
  2588. @deffnx {C Function} scm_make_string (k, chr)
  2589. Return a newly allocated string of
  2590. length @var{k}. If @var{chr} is given, then all elements of
  2591. the string are initialized to @var{chr}, otherwise the contents
  2592. of the string are unspecified.
  2593. @end deffn
  2594. @deftypefn {C Function} SCM scm_c_make_string (size_t len, SCM chr)
  2595. Like @code{scm_make_string}, but expects the length as a
  2596. @code{size_t}.
  2597. @end deftypefn
  2598. @deffn {Scheme Procedure} string-tabulate proc len
  2599. @deffnx {C Function} scm_string_tabulate (proc, len)
  2600. @var{proc} is an integer->char procedure. Construct a string
  2601. of size @var{len} by applying @var{proc} to each index to
  2602. produce the corresponding string element. The order in which
  2603. @var{proc} is applied to the indices is not specified.
  2604. @end deffn
  2605. @deffn {Scheme Procedure} string-join ls [delimiter [grammar]]
  2606. @deffnx {C Function} scm_string_join (ls, delimiter, grammar)
  2607. Append the string in the string list @var{ls}, using the string
  2608. @var{delimiter} as a delimiter between the elements of @var{ls}.
  2609. @var{grammar} is a symbol which specifies how the delimiter is
  2610. placed between the strings, and defaults to the symbol
  2611. @code{infix}.
  2612. @table @code
  2613. @item infix
  2614. Insert the separator between list elements. An empty string
  2615. will produce an empty list.
  2616. @item strict-infix
  2617. Like @code{infix}, but will raise an error if given the empty
  2618. list.
  2619. @item suffix
  2620. Insert the separator after every list element.
  2621. @item prefix
  2622. Insert the separator before each list element.
  2623. @end table
  2624. @end deffn
  2625. @node List/String Conversion
  2626. @subsubsection List/String conversion
  2627. When processing strings, it is often convenient to first convert them
  2628. into a list representation by using the procedure @code{string->list},
  2629. work with the resulting list, and then convert it back into a string.
  2630. These procedures are useful for similar tasks.
  2631. @rnindex string->list
  2632. @deffn {Scheme Procedure} string->list str [start [end]]
  2633. @deffnx {C Function} scm_substring_to_list (str, start, end)
  2634. @deffnx {C Function} scm_string_to_list (str)
  2635. Convert the string @var{str} into a list of characters.
  2636. @end deffn
  2637. @deffn {Scheme Procedure} string-split str char_pred
  2638. @deffnx {C Function} scm_string_split (str, char_pred)
  2639. Split the string @var{str} into a list of substrings delimited
  2640. by appearances of characters that
  2641. @itemize @bullet
  2642. @item
  2643. equal @var{char_pred}, if it is a character,
  2644. @item
  2645. satisfy the predicate @var{char_pred}, if it is a procedure,
  2646. @item
  2647. are in the set @var{char_pred}, if it is a character set.
  2648. @end itemize
  2649. Note that an empty substring between separator characters will result in
  2650. an empty string in the result list.
  2651. @lisp
  2652. (string-split "root:x:0:0:root:/root:/bin/bash" #\:)
  2653. @result{}
  2654. ("root" "x" "0" "0" "root" "/root" "/bin/bash")
  2655. (string-split "::" #\:)
  2656. @result{}
  2657. ("" "" "")
  2658. (string-split "" #\:)
  2659. @result{}
  2660. ("")
  2661. @end lisp
  2662. @end deffn
  2663. @node String Selection
  2664. @subsubsection String Selection
  2665. Portions of strings can be extracted by these procedures.
  2666. @code{string-ref} delivers individual characters whereas
  2667. @code{substring} can be used to extract substrings from longer strings.
  2668. @rnindex string-length
  2669. @deffn {Scheme Procedure} string-length string
  2670. @deffnx {C Function} scm_string_length (string)
  2671. Return the number of characters in @var{string}.
  2672. @end deffn
  2673. @deftypefn {C Function} size_t scm_c_string_length (SCM str)
  2674. Return the number of characters in @var{str} as a @code{size_t}.
  2675. @end deftypefn
  2676. @rnindex string-ref
  2677. @deffn {Scheme Procedure} string-ref str k
  2678. @deffnx {C Function} scm_string_ref (str, k)
  2679. Return character @var{k} of @var{str} using zero-origin
  2680. indexing. @var{k} must be a valid index of @var{str}.
  2681. @end deffn
  2682. @deftypefn {C Function} SCM scm_c_string_ref (SCM str, size_t k)
  2683. Return character @var{k} of @var{str} using zero-origin
  2684. indexing. @var{k} must be a valid index of @var{str}.
  2685. @end deftypefn
  2686. @rnindex string-copy
  2687. @deffn {Scheme Procedure} string-copy str [start [end]]
  2688. @deffnx {C Function} scm_substring_copy (str, start, end)
  2689. @deffnx {C Function} scm_string_copy (str)
  2690. Return a copy of the given string @var{str}.
  2691. The returned string shares storage with @var{str} initially, but it is
  2692. copied as soon as one of the two strings is modified.
  2693. @end deffn
  2694. @rnindex substring
  2695. @deffn {Scheme Procedure} substring str start [end]
  2696. @deffnx {C Function} scm_substring (str, start, end)
  2697. Return a new string formed from the characters
  2698. of @var{str} beginning with index @var{start} (inclusive) and
  2699. ending with index @var{end} (exclusive).
  2700. @var{str} must be a string, @var{start} and @var{end} must be
  2701. exact integers satisfying:
  2702. 0 <= @var{start} <= @var{end} <= @code{(string-length @var{str})}.
  2703. The returned string shares storage with @var{str} initially, but it is
  2704. copied as soon as one of the two strings is modified.
  2705. @end deffn
  2706. @deffn {Scheme Procedure} substring/shared str start [end]
  2707. @deffnx {C Function} scm_substring_shared (str, start, end)
  2708. Like @code{substring}, but the strings continue to share their storage
  2709. even if they are modified. Thus, modifications to @var{str} show up
  2710. in the new string, and vice versa.
  2711. @end deffn
  2712. @deffn {Scheme Procedure} substring/copy str start [end]
  2713. @deffnx {C Function} scm_substring_copy (str, start, end)
  2714. Like @code{substring}, but the storage for the new string is copied
  2715. immediately.
  2716. @end deffn
  2717. @deffn {Scheme Procedure} substring/read-only str start [end]
  2718. @deffnx {C Function} scm_substring_read_only (str, start, end)
  2719. Like @code{substring}, but the resulting string can not be modified.
  2720. @end deffn
  2721. @deftypefn {C Function} SCM scm_c_substring (SCM str, size_t start, size_t end)
  2722. @deftypefnx {C Function} SCM scm_c_substring_shared (SCM str, size_t start, size_t end)
  2723. @deftypefnx {C Function} SCM scm_c_substring_copy (SCM str, size_t start, size_t end)
  2724. @deftypefnx {C Function} SCM scm_c_substring_read_only (SCM str, size_t start, size_t end)
  2725. Like @code{scm_substring}, etc. but the bounds are given as a @code{size_t}.
  2726. @end deftypefn
  2727. @deffn {Scheme Procedure} string-take s n
  2728. @deffnx {C Function} scm_string_take (s, n)
  2729. Return the @var{n} first characters of @var{s}.
  2730. @end deffn
  2731. @deffn {Scheme Procedure} string-drop s n
  2732. @deffnx {C Function} scm_string_drop (s, n)
  2733. Return all but the first @var{n} characters of @var{s}.
  2734. @end deffn
  2735. @deffn {Scheme Procedure} string-take-right s n
  2736. @deffnx {C Function} scm_string_take_right (s, n)
  2737. Return the @var{n} last characters of @var{s}.
  2738. @end deffn
  2739. @deffn {Scheme Procedure} string-drop-right s n
  2740. @deffnx {C Function} scm_string_drop_right (s, n)
  2741. Return all but the last @var{n} characters of @var{s}.
  2742. @end deffn
  2743. @deffn {Scheme Procedure} string-pad s len [chr [start [end]]]
  2744. @deffnx {Scheme Procedure} string-pad-right s len [chr [start [end]]]
  2745. @deffnx {C Function} scm_string_pad (s, len, chr, start, end)
  2746. @deffnx {C Function} scm_string_pad_right (s, len, chr, start, end)
  2747. Take characters @var{start} to @var{end} from the string @var{s} and
  2748. either pad with @var{chr} or truncate them to give @var{len}
  2749. characters.
  2750. @code{string-pad} pads or truncates on the left, so for example
  2751. @example
  2752. (string-pad "x" 3) @result{} " x"
  2753. (string-pad "abcde" 3) @result{} "cde"
  2754. @end example
  2755. @code{string-pad-right} pads or truncates on the right, so for example
  2756. @example
  2757. (string-pad-right "x" 3) @result{} "x "
  2758. (string-pad-right "abcde" 3) @result{} "abc"
  2759. @end example
  2760. @end deffn
  2761. @deffn {Scheme Procedure} string-trim s [char_pred [start [end]]]
  2762. @deffnx {Scheme Procedure} string-trim-right s [char_pred [start [end]]]
  2763. @deffnx {Scheme Procedure} string-trim-both s [char_pred [start [end]]]
  2764. @deffnx {C Function} scm_string_trim (s, char_pred, start, end)
  2765. @deffnx {C Function} scm_string_trim_right (s, char_pred, start, end)
  2766. @deffnx {C Function} scm_string_trim_both (s, char_pred, start, end)
  2767. Trim occurrences of @var{char_pred} from the ends of @var{s}.
  2768. @code{string-trim} trims @var{char_pred} characters from the left
  2769. (start) of the string, @code{string-trim-right} trims them from the
  2770. right (end) of the string, @code{string-trim-both} trims from both
  2771. ends.
  2772. @var{char_pred} can be a character, a character set, or a predicate
  2773. procedure to call on each character. If @var{char_pred} is not given
  2774. the default is whitespace as per @code{char-set:whitespace}
  2775. (@pxref{Standard Character Sets}).
  2776. @example
  2777. (string-trim " x ") @result{} "x "
  2778. (string-trim-right "banana" #\a) @result{} "banan"
  2779. (string-trim-both ".,xy:;" char-set:punctuation)
  2780. @result{} "xy"
  2781. (string-trim-both "xyzzy" (lambda (c)
  2782. (or (eqv? c #\x)
  2783. (eqv? c #\y))))
  2784. @result{} "zz"
  2785. @end example
  2786. @end deffn
  2787. @node String Modification
  2788. @subsubsection String Modification
  2789. These procedures are for modifying strings in-place. This means that the
  2790. result of the operation is not a new string; instead, the original string's
  2791. memory representation is modified.
  2792. @rnindex string-set!
  2793. @deffn {Scheme Procedure} string-set! str k chr
  2794. @deffnx {C Function} scm_string_set_x (str, k, chr)
  2795. Store @var{chr} in element @var{k} of @var{str} and return
  2796. an unspecified value. @var{k} must be a valid index of
  2797. @var{str}.
  2798. @end deffn
  2799. @deftypefn {C Function} void scm_c_string_set_x (SCM str, size_t k, SCM chr)
  2800. Like @code{scm_string_set_x}, but the index is given as a @code{size_t}.
  2801. @end deftypefn
  2802. @rnindex string-fill!
  2803. @deffn {Scheme Procedure} string-fill! str chr [start [end]]
  2804. @deffnx {C Function} scm_substring_fill_x (str, chr, start, end)
  2805. @deffnx {C Function} scm_string_fill_x (str, chr)
  2806. Stores @var{chr} in every element of the given @var{str} and
  2807. returns an unspecified value.
  2808. @end deffn
  2809. @deffn {Scheme Procedure} substring-fill! str start end fill
  2810. @deffnx {C Function} scm_substring_fill_x (str, start, end, fill)
  2811. Change every character in @var{str} between @var{start} and
  2812. @var{end} to @var{fill}.
  2813. @lisp
  2814. (define y (string-copy "abcdefg"))
  2815. (substring-fill! y 1 3 #\r)
  2816. y
  2817. @result{} "arrdefg"
  2818. @end lisp
  2819. @end deffn
  2820. @deffn {Scheme Procedure} substring-move! str1 start1 end1 str2 start2
  2821. @deffnx {C Function} scm_substring_move_x (str1, start1, end1, str2, start2)
  2822. Copy the substring of @var{str1} bounded by @var{start1} and @var{end1}
  2823. into @var{str2} beginning at position @var{start2}.
  2824. @var{str1} and @var{str2} can be the same string.
  2825. @end deffn
  2826. @deffn {Scheme Procedure} string-copy! target tstart s [start [end]]
  2827. @deffnx {C Function} scm_string_copy_x (target, tstart, s, start, end)
  2828. Copy the sequence of characters from index range [@var{start},
  2829. @var{end}) in string @var{s} to string @var{target}, beginning
  2830. at index @var{tstart}. The characters are copied left-to-right
  2831. or right-to-left as needed -- the copy is guaranteed to work,
  2832. even if @var{target} and @var{s} are the same string. It is an
  2833. error if the copy operation runs off the end of the target
  2834. string.
  2835. @end deffn
  2836. @node String Comparison
  2837. @subsubsection String Comparison
  2838. The procedures in this section are similar to the character ordering
  2839. predicates (@pxref{Characters}), but are defined on character sequences.
  2840. The first set is specified in R5RS and has names that end in @code{?}.
  2841. The second set is specified in SRFI-13 and the names have not ending
  2842. @code{?}.
  2843. The predicates ending in @code{-ci} ignore the character case
  2844. when comparing strings. For now, case-insensitive comparison is done
  2845. using the R5RS rules, where every lower-case character that has a
  2846. single character upper-case form is converted to uppercase before
  2847. comparison. See @xref{Text Collation, the @code{(ice-9
  2848. i18n)} module}, for locale-dependent string comparison.
  2849. @rnindex string=?
  2850. @deffn {Scheme Procedure} string=? s1 s2 s3 @dots{}
  2851. Lexicographic equality predicate; return @code{#t} if all strings are
  2852. the same length and contain the same characters in the same positions,
  2853. otherwise return @code{#f}.
  2854. The procedure @code{string-ci=?} treats upper and lower case
  2855. letters as though they were the same character, but
  2856. @code{string=?} treats upper and lower case as distinct
  2857. characters.
  2858. @end deffn
  2859. @rnindex string<?
  2860. @deffn {Scheme Procedure} string<? s1 s2 s3 @dots{}
  2861. Lexicographic ordering predicate; return @code{#t} if, for every pair of
  2862. consecutive string arguments @var{str_i} and @var{str_i+1}, @var{str_i} is
  2863. lexicographically less than @var{str_i+1}.
  2864. @end deffn
  2865. @rnindex string<=?
  2866. @deffn {Scheme Procedure} string<=? s1 s2 s3 @dots{}
  2867. Lexicographic ordering predicate; return @code{#t} if, for every pair of
  2868. consecutive string arguments @var{str_i} and @var{str_i+1}, @var{str_i} is
  2869. lexicographically less than or equal to @var{str_i+1}.
  2870. @end deffn
  2871. @rnindex string>?
  2872. @deffn {Scheme Procedure} string>? s1 s2 s3 @dots{}
  2873. Lexicographic ordering predicate; return @code{#t} if, for every pair of
  2874. consecutive string arguments @var{str_i} and @var{str_i+1}, @var{str_i} is
  2875. lexicographically greater than @var{str_i+1}.
  2876. @end deffn
  2877. @rnindex string>=?
  2878. @deffn {Scheme Procedure} string>=? s1 s2 s3 @dots{}
  2879. Lexicographic ordering predicate; return @code{#t} if, for every pair of
  2880. consecutive string arguments @var{str_i} and @var{str_i+1}, @var{str_i} is
  2881. lexicographically greater than or equal to @var{str_i+1}.
  2882. @end deffn
  2883. @rnindex string-ci=?
  2884. @deffn {Scheme Procedure} string-ci=? s1 s2 s3 @dots{}
  2885. Case-insensitive string equality predicate; return @code{#t} if
  2886. all strings are the same length and their component
  2887. characters match (ignoring case) at each position; otherwise
  2888. return @code{#f}.
  2889. @end deffn
  2890. @rnindex string-ci<?
  2891. @deffn {Scheme Procedure} string-ci<? s1 s2 s3 @dots{}
  2892. Case insensitive lexicographic ordering predicate; return @code{#t} if,
  2893. for every pair of consecutive string arguments @var{str_i} and
  2894. @var{str_i+1}, @var{str_i} is lexicographically less than @var{str_i+1}
  2895. regardless of case.
  2896. @end deffn
  2897. @rnindex string<=?
  2898. @deffn {Scheme Procedure} string-ci<=? s1 s2 s3 @dots{}
  2899. Case insensitive lexicographic ordering predicate; return @code{#t} if,
  2900. for every pair of consecutive string arguments @var{str_i} and
  2901. @var{str_i+1}, @var{str_i} is lexicographically less than or equal to
  2902. @var{str_i+1} regardless of case.
  2903. @end deffn
  2904. @rnindex string-ci>?
  2905. @deffn {Scheme Procedure} string-ci>? s1 s2 s3 @dots{}
  2906. Case insensitive lexicographic ordering predicate; return @code{#t} if,
  2907. for every pair of consecutive string arguments @var{str_i} and
  2908. @var{str_i+1}, @var{str_i} is lexicographically greater than
  2909. @var{str_i+1} regardless of case.
  2910. @end deffn
  2911. @rnindex string-ci>=?
  2912. @deffn {Scheme Procedure} string-ci>=? s1 s2 s3 @dots{}
  2913. Case insensitive lexicographic ordering predicate; return @code{#t} if,
  2914. for every pair of consecutive string arguments @var{str_i} and
  2915. @var{str_i+1}, @var{str_i} is lexicographically greater than or equal to
  2916. @var{str_i+1} regardless of case.
  2917. @end deffn
  2918. @deffn {Scheme Procedure} string-compare s1 s2 proc_lt proc_eq proc_gt [start1 [end1 [start2 [end2]]]]
  2919. @deffnx {C Function} scm_string_compare (s1, s2, proc_lt, proc_eq, proc_gt, start1, end1, start2, end2)
  2920. Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the
  2921. mismatch index, depending upon whether @var{s1} is less than,
  2922. equal to, or greater than @var{s2}. The mismatch index is the
  2923. largest index @var{i} such that for every 0 <= @var{j} <
  2924. @var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] -- that is,
  2925. @var{i} is the first position that does not match.
  2926. @end deffn
  2927. @deffn {Scheme Procedure} string-compare-ci s1 s2 proc_lt proc_eq proc_gt [start1 [end1 [start2 [end2]]]]
  2928. @deffnx {C Function} scm_string_compare_ci (s1, s2, proc_lt, proc_eq, proc_gt, start1, end1, start2, end2)
  2929. Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the
  2930. mismatch index, depending upon whether @var{s1} is less than,
  2931. equal to, or greater than @var{s2}. The mismatch index is the
  2932. largest index @var{i} such that for every 0 <= @var{j} <
  2933. @var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] -- that is,
  2934. @var{i} is the first position where the lowercased letters
  2935. do not match.
  2936. @end deffn
  2937. @deffn {Scheme Procedure} string= s1 s2 [start1 [end1 [start2 [end2]]]]
  2938. @deffnx {C Function} scm_string_eq (s1, s2, start1, end1, start2, end2)
  2939. Return @code{#f} if @var{s1} and @var{s2} are not equal, a true
  2940. value otherwise.
  2941. @end deffn
  2942. @deffn {Scheme Procedure} string<> s1 s2 [start1 [end1 [start2 [end2]]]]
  2943. @deffnx {C Function} scm_string_neq (s1, s2, start1, end1, start2, end2)
  2944. Return @code{#f} if @var{s1} and @var{s2} are equal, a true
  2945. value otherwise.
  2946. @end deffn
  2947. @deffn {Scheme Procedure} string< s1 s2 [start1 [end1 [start2 [end2]]]]
  2948. @deffnx {C Function} scm_string_lt (s1, s2, start1, end1, start2, end2)
  2949. Return @code{#f} if @var{s1} is greater or equal to @var{s2}, a
  2950. true value otherwise.
  2951. @end deffn
  2952. @deffn {Scheme Procedure} string> s1 s2 [start1 [end1 [start2 [end2]]]]
  2953. @deffnx {C Function} scm_string_gt (s1, s2, start1, end1, start2, end2)
  2954. Return @code{#f} if @var{s1} is less or equal to @var{s2}, a
  2955. true value otherwise.
  2956. @end deffn
  2957. @deffn {Scheme Procedure} string<= s1 s2 [start1 [end1 [start2 [end2]]]]
  2958. @deffnx {C Function} scm_string_le (s1, s2, start1, end1, start2, end2)
  2959. Return @code{#f} if @var{s1} is greater to @var{s2}, a true
  2960. value otherwise.
  2961. @end deffn
  2962. @deffn {Scheme Procedure} string>= s1 s2 [start1 [end1 [start2 [end2]]]]
  2963. @deffnx {C Function} scm_string_ge (s1, s2, start1, end1, start2, end2)
  2964. Return @code{#f} if @var{s1} is less to @var{s2}, a true value
  2965. otherwise.
  2966. @end deffn
  2967. @deffn {Scheme Procedure} string-ci= s1 s2 [start1 [end1 [start2 [end2]]]]
  2968. @deffnx {C Function} scm_string_ci_eq (s1, s2, start1, end1, start2, end2)
  2969. Return @code{#f} if @var{s1} and @var{s2} are not equal, a true
  2970. value otherwise. The character comparison is done
  2971. case-insensitively.
  2972. @end deffn
  2973. @deffn {Scheme Procedure} string-ci<> s1 s2 [start1 [end1 [start2 [end2]]]]
  2974. @deffnx {C Function} scm_string_ci_neq (s1, s2, start1, end1, start2, end2)
  2975. Return @code{#f} if @var{s1} and @var{s2} are equal, a true
  2976. value otherwise. The character comparison is done
  2977. case-insensitively.
  2978. @end deffn
  2979. @deffn {Scheme Procedure} string-ci< s1 s2 [start1 [end1 [start2 [end2]]]]
  2980. @deffnx {C Function} scm_string_ci_lt (s1, s2, start1, end1, start2, end2)
  2981. Return @code{#f} if @var{s1} is greater or equal to @var{s2}, a
  2982. true value otherwise. The character comparison is done
  2983. case-insensitively.
  2984. @end deffn
  2985. @deffn {Scheme Procedure} string-ci> s1 s2 [start1 [end1 [start2 [end2]]]]
  2986. @deffnx {C Function} scm_string_ci_gt (s1, s2, start1, end1, start2, end2)
  2987. Return @code{#f} if @var{s1} is less or equal to @var{s2}, a
  2988. true value otherwise. The character comparison is done
  2989. case-insensitively.
  2990. @end deffn
  2991. @deffn {Scheme Procedure} string-ci<= s1 s2 [start1 [end1 [start2 [end2]]]]
  2992. @deffnx {C Function} scm_string_ci_le (s1, s2, start1, end1, start2, end2)
  2993. Return @code{#f} if @var{s1} is greater to @var{s2}, a true
  2994. value otherwise. The character comparison is done
  2995. case-insensitively.
  2996. @end deffn
  2997. @deffn {Scheme Procedure} string-ci>= s1 s2 [start1 [end1 [start2 [end2]]]]
  2998. @deffnx {C Function} scm_string_ci_ge (s1, s2, start1, end1, start2, end2)
  2999. Return @code{#f} if @var{s1} is less to @var{s2}, a true value
  3000. otherwise. The character comparison is done
  3001. case-insensitively.
  3002. @end deffn
  3003. @deffn {Scheme Procedure} string-hash s [bound [start [end]]]
  3004. @deffnx {C Function} scm_substring_hash (s, bound, start, end)
  3005. Compute a hash value for @var{s}. The optional argument @var{bound} is a non-negative exact integer specifying the range of the hash function. A positive value restricts the return value to the range [0,bound).
  3006. @end deffn
  3007. @deffn {Scheme Procedure} string-hash-ci s [bound [start [end]]]
  3008. @deffnx {C Function} scm_substring_hash_ci (s, bound, start, end)
  3009. Compute a hash value for @var{s}. The optional argument @var{bound} is a non-negative exact integer specifying the range of the hash function. A positive value restricts the return value to the range [0,bound).
  3010. @end deffn
  3011. Because the same visual appearance of an abstract Unicode character can
  3012. be obtained via multiple sequences of Unicode characters, even the
  3013. case-insensitive string comparison functions described above may return
  3014. @code{#f} when presented with strings containing different
  3015. representations of the same character. For example, the Unicode
  3016. character ``LATIN SMALL LETTER S WITH DOT BELOW AND DOT ABOVE'' can be
  3017. represented with a single character (U+1E69) or by the character ``LATIN
  3018. SMALL LETTER S'' (U+0073) followed by the combining marks ``COMBINING
  3019. DOT BELOW'' (U+0323) and ``COMBINING DOT ABOVE'' (U+0307).
  3020. For this reason, it is often desirable to ensure that the strings
  3021. to be compared are using a mutually consistent representation for every
  3022. character. The Unicode standard defines two methods of normalizing the
  3023. contents of strings: Decomposition, which breaks composite characters
  3024. into a set of constituent characters with an ordering defined by the
  3025. Unicode Standard; and composition, which performs the converse.
  3026. There are two decomposition operations. ``Canonical decomposition''
  3027. produces character sequences that share the same visual appearance as
  3028. the original characters, while ``compatibility decomposition'' produces
  3029. ones whose visual appearances may differ from the originals but which
  3030. represent the same abstract character.
  3031. These operations are encapsulated in the following set of normalization
  3032. forms:
  3033. @table @dfn
  3034. @item NFD
  3035. Characters are decomposed to their canonical forms.
  3036. @item NFKD
  3037. Characters are decomposed to their compatibility forms.
  3038. @item NFC
  3039. Characters are decomposed to their canonical forms, then composed.
  3040. @item NFKC
  3041. Characters are decomposed to their compatibility forms, then composed.
  3042. @end table
  3043. The functions below put their arguments into one of the forms described
  3044. above.
  3045. @deffn {Scheme Procedure} string-normalize-nfd s
  3046. @deffnx {C Function} scm_string_normalize_nfd (s)
  3047. Return the @code{NFD} normalized form of @var{s}.
  3048. @end deffn
  3049. @deffn {Scheme Procedure} string-normalize-nfkd s
  3050. @deffnx {C Function} scm_string_normalize_nfkd (s)
  3051. Return the @code{NFKD} normalized form of @var{s}.
  3052. @end deffn
  3053. @deffn {Scheme Procedure} string-normalize-nfc s
  3054. @deffnx {C Function} scm_string_normalize_nfc (s)
  3055. Return the @code{NFC} normalized form of @var{s}.
  3056. @end deffn
  3057. @deffn {Scheme Procedure} string-normalize-nfkc s
  3058. @deffnx {C Function} scm_string_normalize_nfkc (s)
  3059. Return the @code{NFKC} normalized form of @var{s}.
  3060. @end deffn
  3061. @node String Searching
  3062. @subsubsection String Searching
  3063. @deffn {Scheme Procedure} string-index s char_pred [start [end]]
  3064. @deffnx {C Function} scm_string_index (s, char_pred, start, end)
  3065. Search through the string @var{s} from left to right, returning
  3066. the index of the first occurrence of a character which
  3067. @itemize @bullet
  3068. @item
  3069. equals @var{char_pred}, if it is character,
  3070. @item
  3071. satisfies the predicate @var{char_pred}, if it is a procedure,
  3072. @item
  3073. is in the set @var{char_pred}, if it is a character set.
  3074. @end itemize
  3075. Return @code{#f} if no match is found.
  3076. @end deffn
  3077. @deffn {Scheme Procedure} string-rindex s char_pred [start [end]]
  3078. @deffnx {C Function} scm_string_rindex (s, char_pred, start, end)
  3079. Search through the string @var{s} from right to left, returning
  3080. the index of the last occurrence of a character which
  3081. @itemize @bullet
  3082. @item
  3083. equals @var{char_pred}, if it is character,
  3084. @item
  3085. satisfies the predicate @var{char_pred}, if it is a procedure,
  3086. @item
  3087. is in the set if @var{char_pred} is a character set.
  3088. @end itemize
  3089. Return @code{#f} if no match is found.
  3090. @end deffn
  3091. @deffn {Scheme Procedure} string-prefix-length s1 s2 [start1 [end1 [start2 [end2]]]]
  3092. @deffnx {C Function} scm_string_prefix_length (s1, s2, start1, end1, start2, end2)
  3093. Return the length of the longest common prefix of the two
  3094. strings.
  3095. @end deffn
  3096. @deffn {Scheme Procedure} string-prefix-length-ci s1 s2 [start1 [end1 [start2 [end2]]]]
  3097. @deffnx {C Function} scm_string_prefix_length_ci (s1, s2, start1, end1, start2, end2)
  3098. Return the length of the longest common prefix of the two
  3099. strings, ignoring character case.
  3100. @end deffn
  3101. @deffn {Scheme Procedure} string-suffix-length s1 s2 [start1 [end1 [start2 [end2]]]]
  3102. @deffnx {C Function} scm_string_suffix_length (s1, s2, start1, end1, start2, end2)
  3103. Return the length of the longest common suffix of the two
  3104. strings.
  3105. @end deffn
  3106. @deffn {Scheme Procedure} string-suffix-length-ci s1 s2 [start1 [end1 [start2 [end2]]]]
  3107. @deffnx {C Function} scm_string_suffix_length_ci (s1, s2, start1, end1, start2, end2)
  3108. Return the length of the longest common suffix of the two
  3109. strings, ignoring character case.
  3110. @end deffn
  3111. @deffn {Scheme Procedure} string-prefix? s1 s2 [start1 [end1 [start2 [end2]]]]
  3112. @deffnx {C Function} scm_string_prefix_p (s1, s2, start1, end1, start2, end2)
  3113. Is @var{s1} a prefix of @var{s2}?
  3114. @end deffn
  3115. @deffn {Scheme Procedure} string-prefix-ci? s1 s2 [start1 [end1 [start2 [end2]]]]
  3116. @deffnx {C Function} scm_string_prefix_ci_p (s1, s2, start1, end1, start2, end2)
  3117. Is @var{s1} a prefix of @var{s2}, ignoring character case?
  3118. @end deffn
  3119. @deffn {Scheme Procedure} string-suffix? s1 s2 [start1 [end1 [start2 [end2]]]]
  3120. @deffnx {C Function} scm_string_suffix_p (s1, s2, start1, end1, start2, end2)
  3121. Is @var{s1} a suffix of @var{s2}?
  3122. @end deffn
  3123. @deffn {Scheme Procedure} string-suffix-ci? s1 s2 [start1 [end1 [start2 [end2]]]]
  3124. @deffnx {C Function} scm_string_suffix_ci_p (s1, s2, start1, end1, start2, end2)
  3125. Is @var{s1} a suffix of @var{s2}, ignoring character case?
  3126. @end deffn
  3127. @deffn {Scheme Procedure} string-index-right s char_pred [start [end]]
  3128. @deffnx {C Function} scm_string_index_right (s, char_pred, start, end)
  3129. Search through the string @var{s} from right to left, returning
  3130. the index of the last occurrence of a character which
  3131. @itemize @bullet
  3132. @item
  3133. equals @var{char_pred}, if it is character,
  3134. @item
  3135. satisfies the predicate @var{char_pred}, if it is a procedure,
  3136. @item
  3137. is in the set if @var{char_pred} is a character set.
  3138. @end itemize
  3139. Return @code{#f} if no match is found.
  3140. @end deffn
  3141. @deffn {Scheme Procedure} string-skip s char_pred [start [end]]
  3142. @deffnx {C Function} scm_string_skip (s, char_pred, start, end)
  3143. Search through the string @var{s} from left to right, returning
  3144. the index of the first occurrence of a character which
  3145. @itemize @bullet
  3146. @item
  3147. does not equal @var{char_pred}, if it is character,
  3148. @item
  3149. does not satisfy the predicate @var{char_pred}, if it is a
  3150. procedure,
  3151. @item
  3152. is not in the set if @var{char_pred} is a character set.
  3153. @end itemize
  3154. @end deffn
  3155. @deffn {Scheme Procedure} string-skip-right s char_pred [start [end]]
  3156. @deffnx {C Function} scm_string_skip_right (s, char_pred, start, end)
  3157. Search through the string @var{s} from right to left, returning
  3158. the index of the last occurrence of a character which
  3159. @itemize @bullet
  3160. @item
  3161. does not equal @var{char_pred}, if it is character,
  3162. @item
  3163. does not satisfy the predicate @var{char_pred}, if it is a
  3164. procedure,
  3165. @item
  3166. is not in the set if @var{char_pred} is a character set.
  3167. @end itemize
  3168. @end deffn
  3169. @deffn {Scheme Procedure} string-count s char_pred [start [end]]
  3170. @deffnx {C Function} scm_string_count (s, char_pred, start, end)
  3171. Return the count of the number of characters in the string
  3172. @var{s} which
  3173. @itemize @bullet
  3174. @item
  3175. equals @var{char_pred}, if it is character,
  3176. @item
  3177. satisfies the predicate @var{char_pred}, if it is a procedure.
  3178. @item
  3179. is in the set @var{char_pred}, if it is a character set.
  3180. @end itemize
  3181. @end deffn
  3182. @deffn {Scheme Procedure} string-contains s1 s2 [start1 [end1 [start2 [end2]]]]
  3183. @deffnx {C Function} scm_string_contains (s1, s2, start1, end1, start2, end2)
  3184. Does string @var{s1} contain string @var{s2}? Return the index
  3185. in @var{s1} where @var{s2} occurs as a substring, or false.
  3186. The optional start/end indices restrict the operation to the
  3187. indicated substrings.
  3188. @end deffn
  3189. @deffn {Scheme Procedure} string-contains-ci s1 s2 [start1 [end1 [start2 [end2]]]]
  3190. @deffnx {C Function} scm_string_contains_ci (s1, s2, start1, end1, start2, end2)
  3191. Does string @var{s1} contain string @var{s2}? Return the index
  3192. in @var{s1} where @var{s2} occurs as a substring, or false.
  3193. The optional start/end indices restrict the operation to the
  3194. indicated substrings. Character comparison is done
  3195. case-insensitively.
  3196. @end deffn
  3197. @node Alphabetic Case Mapping
  3198. @subsubsection Alphabetic Case Mapping
  3199. These are procedures for mapping strings to their upper- or lower-case
  3200. equivalents, respectively, or for capitalizing strings.
  3201. They use the basic case mapping rules for Unicode characters. No
  3202. special language or context rules are considered. The resulting strings
  3203. are guaranteed to be the same length as the input strings.
  3204. @xref{Character Case Mapping, the @code{(ice-9
  3205. i18n)} module}, for locale-dependent case conversions.
  3206. @deffn {Scheme Procedure} string-upcase str [start [end]]
  3207. @deffnx {C Function} scm_substring_upcase (str, start, end)
  3208. @deffnx {C Function} scm_string_upcase (str)
  3209. Upcase every character in @code{str}.
  3210. @end deffn
  3211. @deffn {Scheme Procedure} string-upcase! str [start [end]]
  3212. @deffnx {C Function} scm_substring_upcase_x (str, start, end)
  3213. @deffnx {C Function} scm_string_upcase_x (str)
  3214. Destructively upcase every character in @code{str}.
  3215. @lisp
  3216. (string-upcase! y)
  3217. @result{} "ARRDEFG"
  3218. y
  3219. @result{} "ARRDEFG"
  3220. @end lisp
  3221. @end deffn
  3222. @deffn {Scheme Procedure} string-downcase str [start [end]]
  3223. @deffnx {C Function} scm_substring_downcase (str, start, end)
  3224. @deffnx {C Function} scm_string_downcase (str)
  3225. Downcase every character in @var{str}.
  3226. @end deffn
  3227. @deffn {Scheme Procedure} string-downcase! str [start [end]]
  3228. @deffnx {C Function} scm_substring_downcase_x (str, start, end)
  3229. @deffnx {C Function} scm_string_downcase_x (str)
  3230. Destructively downcase every character in @var{str}.
  3231. @lisp
  3232. y
  3233. @result{} "ARRDEFG"
  3234. (string-downcase! y)
  3235. @result{} "arrdefg"
  3236. y
  3237. @result{} "arrdefg"
  3238. @end lisp
  3239. @end deffn
  3240. @deffn {Scheme Procedure} string-capitalize str
  3241. @deffnx {C Function} scm_string_capitalize (str)
  3242. Return a freshly allocated string with the characters in
  3243. @var{str}, where the first character of every word is
  3244. capitalized.
  3245. @end deffn
  3246. @deffn {Scheme Procedure} string-capitalize! str
  3247. @deffnx {C Function} scm_string_capitalize_x (str)
  3248. Upcase the first character of every word in @var{str}
  3249. destructively and return @var{str}.
  3250. @lisp
  3251. y @result{} "hello world"
  3252. (string-capitalize! y) @result{} "Hello World"
  3253. y @result{} "Hello World"
  3254. @end lisp
  3255. @end deffn
  3256. @deffn {Scheme Procedure} string-titlecase str [start [end]]
  3257. @deffnx {C Function} scm_string_titlecase (str, start, end)
  3258. Titlecase every first character in a word in @var{str}.
  3259. @end deffn
  3260. @deffn {Scheme Procedure} string-titlecase! str [start [end]]
  3261. @deffnx {C Function} scm_string_titlecase_x (str, start, end)
  3262. Destructively titlecase every first character in a word in
  3263. @var{str}.
  3264. @end deffn
  3265. @node Reversing and Appending Strings
  3266. @subsubsection Reversing and Appending Strings
  3267. @deffn {Scheme Procedure} string-reverse str [start [end]]
  3268. @deffnx {C Function} scm_string_reverse (str, start, end)
  3269. Reverse the string @var{str}. The optional arguments
  3270. @var{start} and @var{end} delimit the region of @var{str} to
  3271. operate on.
  3272. @end deffn
  3273. @deffn {Scheme Procedure} string-reverse! str [start [end]]
  3274. @deffnx {C Function} scm_string_reverse_x (str, start, end)
  3275. Reverse the string @var{str} in-place. The optional arguments
  3276. @var{start} and @var{end} delimit the region of @var{str} to
  3277. operate on. The return value is unspecified.
  3278. @end deffn
  3279. @rnindex string-append
  3280. @deffn {Scheme Procedure} string-append arg @dots{}
  3281. @deffnx {C Function} scm_string_append (args)
  3282. Return a newly allocated string whose characters form the
  3283. concatenation of the given strings, @var{arg} @enddots{}.
  3284. @example
  3285. (let ((h "hello "))
  3286. (string-append h "world"))
  3287. @result{} "hello world"
  3288. @end example
  3289. @end deffn
  3290. @deffn {Scheme Procedure} string-append/shared arg @dots{}
  3291. @deffnx {C Function} scm_string_append_shared (args)
  3292. Like @code{string-append}, but the result may share memory
  3293. with the argument strings.
  3294. @end deffn
  3295. @deffn {Scheme Procedure} string-concatenate ls
  3296. @deffnx {C Function} scm_string_concatenate (ls)
  3297. Append the elements (which must be strings) of @var{ls} together into a
  3298. single string. Guaranteed to return a freshly allocated string.
  3299. @end deffn
  3300. @deffn {Scheme Procedure} string-concatenate-reverse ls [final_string [end]]
  3301. @deffnx {C Function} scm_string_concatenate_reverse (ls, final_string, end)
  3302. Without optional arguments, this procedure is equivalent to
  3303. @lisp
  3304. (string-concatenate (reverse ls))
  3305. @end lisp
  3306. If the optional argument @var{final_string} is specified, it is
  3307. consed onto the beginning to @var{ls} before performing the
  3308. list-reverse and string-concatenate operations. If @var{end}
  3309. is given, only the characters of @var{final_string} up to index
  3310. @var{end} are used.
  3311. Guaranteed to return a freshly allocated string.
  3312. @end deffn
  3313. @deffn {Scheme Procedure} string-concatenate/shared ls
  3314. @deffnx {C Function} scm_string_concatenate_shared (ls)
  3315. Like @code{string-concatenate}, but the result may share memory
  3316. with the strings in the list @var{ls}.
  3317. @end deffn
  3318. @deffn {Scheme Procedure} string-concatenate-reverse/shared ls [final_string [end]]
  3319. @deffnx {C Function} scm_string_concatenate_reverse_shared (ls, final_string, end)
  3320. Like @code{string-concatenate-reverse}, but the result may
  3321. share memory with the strings in the @var{ls} arguments.
  3322. @end deffn
  3323. @node Mapping Folding and Unfolding
  3324. @subsubsection Mapping, Folding, and Unfolding
  3325. @deffn {Scheme Procedure} string-map proc s [start [end]]
  3326. @deffnx {C Function} scm_string_map (proc, s, start, end)
  3327. @var{proc} is a char->char procedure, it is mapped over
  3328. @var{s}. The order in which the procedure is applied to the
  3329. string elements is not specified.
  3330. @end deffn
  3331. @deffn {Scheme Procedure} string-map! proc s [start [end]]
  3332. @deffnx {C Function} scm_string_map_x (proc, s, start, end)
  3333. @var{proc} is a char->char procedure, it is mapped over
  3334. @var{s}. The order in which the procedure is applied to the
  3335. string elements is not specified. The string @var{s} is
  3336. modified in-place, the return value is not specified.
  3337. @end deffn
  3338. @deffn {Scheme Procedure} string-for-each proc s [start [end]]
  3339. @deffnx {C Function} scm_string_for_each (proc, s, start, end)
  3340. @var{proc} is mapped over @var{s} in left-to-right order. The
  3341. return value is not specified.
  3342. @end deffn
  3343. @deffn {Scheme Procedure} string-for-each-index proc s [start [end]]
  3344. @deffnx {C Function} scm_string_for_each_index (proc, s, start, end)
  3345. Call @code{(@var{proc} i)} for each index i in @var{s}, from left to
  3346. right.
  3347. For example, to change characters to alternately upper and lower case,
  3348. @example
  3349. (define str (string-copy "studly"))
  3350. (string-for-each-index
  3351. (lambda (i)
  3352. (string-set! str i
  3353. ((if (even? i) char-upcase char-downcase)
  3354. (string-ref str i))))
  3355. str)
  3356. str @result{} "StUdLy"
  3357. @end example
  3358. @end deffn
  3359. @deffn {Scheme Procedure} string-fold kons knil s [start [end]]
  3360. @deffnx {C Function} scm_string_fold (kons, knil, s, start, end)
  3361. Fold @var{kons} over the characters of @var{s}, with @var{knil}
  3362. as the terminating element, from left to right. @var{kons}
  3363. must expect two arguments: The actual character and the last
  3364. result of @var{kons}' application.
  3365. @end deffn
  3366. @deffn {Scheme Procedure} string-fold-right kons knil s [start [end]]
  3367. @deffnx {C Function} scm_string_fold_right (kons, knil, s, start, end)
  3368. Fold @var{kons} over the characters of @var{s}, with @var{knil}
  3369. as the terminating element, from right to left. @var{kons}
  3370. must expect two arguments: The actual character and the last
  3371. result of @var{kons}' application.
  3372. @end deffn
  3373. @deffn {Scheme Procedure} string-unfold p f g seed [base [make_final]]
  3374. @deffnx {C Function} scm_string_unfold (p, f, g, seed, base, make_final)
  3375. @itemize @bullet
  3376. @item @var{g} is used to generate a series of @emph{seed}
  3377. values from the initial @var{seed}: @var{seed}, (@var{g}
  3378. @var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),
  3379. @dots{}
  3380. @item @var{p} tells us when to stop -- when it returns true
  3381. when applied to one of these seed values.
  3382. @item @var{f} maps each seed value to the corresponding
  3383. character in the result string. These chars are assembled
  3384. into the string in a left-to-right order.
  3385. @item @var{base} is the optional initial/leftmost portion
  3386. of the constructed string; it default to the empty
  3387. string.
  3388. @item @var{make_final} is applied to the terminal seed
  3389. value (on which @var{p} returns true) to produce
  3390. the final/rightmost portion of the constructed string.
  3391. The default is nothing extra.
  3392. @end itemize
  3393. @end deffn
  3394. @deffn {Scheme Procedure} string-unfold-right p f g seed [base [make_final]]
  3395. @deffnx {C Function} scm_string_unfold_right (p, f, g, seed, base, make_final)
  3396. @itemize @bullet
  3397. @item @var{g} is used to generate a series of @emph{seed}
  3398. values from the initial @var{seed}: @var{seed}, (@var{g}
  3399. @var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),
  3400. @dots{}
  3401. @item @var{p} tells us when to stop -- when it returns true
  3402. when applied to one of these seed values.
  3403. @item @var{f} maps each seed value to the corresponding
  3404. character in the result string. These chars are assembled
  3405. into the string in a right-to-left order.
  3406. @item @var{base} is the optional initial/rightmost portion
  3407. of the constructed string; it default to the empty
  3408. string.
  3409. @item @var{make_final} is applied to the terminal seed
  3410. value (on which @var{p} returns true) to produce
  3411. the final/leftmost portion of the constructed string.
  3412. It defaults to @code{(lambda (x) )}.
  3413. @end itemize
  3414. @end deffn
  3415. @node Miscellaneous String Operations
  3416. @subsubsection Miscellaneous String Operations
  3417. @deffn {Scheme Procedure} xsubstring s from [to [start [end]]]
  3418. @deffnx {C Function} scm_xsubstring (s, from, to, start, end)
  3419. This is the @emph{extended substring} procedure that implements
  3420. replicated copying of a substring of some string.
  3421. @var{s} is a string, @var{start} and @var{end} are optional
  3422. arguments that demarcate a substring of @var{s}, defaulting to
  3423. 0 and the length of @var{s}. Replicate this substring up and
  3424. down index space, in both the positive and negative directions.
  3425. @code{xsubstring} returns the substring of this string
  3426. beginning at index @var{from}, and ending at @var{to}, which
  3427. defaults to @var{from} + (@var{end} - @var{start}).
  3428. @end deffn
  3429. @deffn {Scheme Procedure} string-xcopy! target tstart s sfrom [sto [start [end]]]
  3430. @deffnx {C Function} scm_string_xcopy_x (target, tstart, s, sfrom, sto, start, end)
  3431. Exactly the same as @code{xsubstring}, but the extracted text
  3432. is written into the string @var{target} starting at index
  3433. @var{tstart}. The operation is not defined if @code{(eq?
  3434. @var{target} @var{s})} or these arguments share storage -- you
  3435. cannot copy a string on top of itself.
  3436. @end deffn
  3437. @deffn {Scheme Procedure} string-replace s1 s2 [start1 [end1 [start2 [end2]]]]
  3438. @deffnx {C Function} scm_string_replace (s1, s2, start1, end1, start2, end2)
  3439. Return the string @var{s1}, but with the characters
  3440. @var{start1} @dots{} @var{end1} replaced by the characters
  3441. @var{start2} @dots{} @var{end2} from @var{s2}.
  3442. @end deffn
  3443. @deffn {Scheme Procedure} string-tokenize s [token_set [start [end]]]
  3444. @deffnx {C Function} scm_string_tokenize (s, token_set, start, end)
  3445. Split the string @var{s} into a list of substrings, where each
  3446. substring is a maximal non-empty contiguous sequence of
  3447. characters from the character set @var{token_set}, which
  3448. defaults to @code{char-set:graphic}.
  3449. If @var{start} or @var{end} indices are provided, they restrict
  3450. @code{string-tokenize} to operating on the indicated substring
  3451. of @var{s}.
  3452. @end deffn
  3453. @deffn {Scheme Procedure} string-filter char_pred s [start [end]]
  3454. @deffnx {C Function} scm_string_filter (char_pred, s, start, end)
  3455. Filter the string @var{s}, retaining only those characters which
  3456. satisfy @var{char_pred}.
  3457. If @var{char_pred} is a procedure, it is applied to each character as
  3458. a predicate, if it is a character, it is tested for equality and if it
  3459. is a character set, it is tested for membership.
  3460. @end deffn
  3461. @deffn {Scheme Procedure} string-delete char_pred s [start [end]]
  3462. @deffnx {C Function} scm_string_delete (char_pred, s, start, end)
  3463. Delete characters satisfying @var{char_pred} from @var{s}.
  3464. If @var{char_pred} is a procedure, it is applied to each character as
  3465. a predicate, if it is a character, it is tested for equality and if it
  3466. is a character set, it is tested for membership.
  3467. @end deffn
  3468. @node Representing Strings as Bytes
  3469. @subsubsection Representing Strings as Bytes
  3470. Out in the cold world outside of Guile, not all strings are treated in
  3471. the same way. Out there there are only bytes, and there are many ways
  3472. of representing a strings (sequences of characters) as binary data
  3473. (sequences of bytes).
  3474. As a user, usually you don't have to think about this very much. When
  3475. you type on your keyboard, your system encodes your keystrokes as bytes
  3476. according to the locale that you have configured on your computer.
  3477. Guile uses the locale to decode those bytes back into characters --
  3478. hopefully the same characters that you typed in.
  3479. All is not so clear when dealing with a system with multiple users, such
  3480. as a web server. Your web server might get a request from one user for
  3481. data encoded in the ISO-8859-1 character set, and then another request
  3482. from a different user for UTF-8 data.
  3483. @cindex iconv
  3484. @cindex character encoding
  3485. Guile provides an @dfn{iconv} module for converting between strings and
  3486. sequences of bytes. @xref{Bytevectors}, for more on how Guile
  3487. represents raw byte sequences. This module gets its name from the
  3488. common @sc{unix} command of the same name.
  3489. Note that often it is sufficient to just read and write strings from
  3490. ports instead of using these functions. To do this, specify the port
  3491. encoding using @code{set-port-encoding!}. @xref{Ports}, for more on
  3492. ports and character encodings.
  3493. Unlike the rest of the procedures in this section, you have to load the
  3494. @code{iconv} module before having access to these procedures:
  3495. @example
  3496. (use-modules (ice-9 iconv))
  3497. @end example
  3498. @deffn {Scheme Procedure} string->bytevector string encoding [conversion-strategy]
  3499. Encode @var{string} as a sequence of bytes.
  3500. The string will be encoded in the character set specified by the
  3501. @var{encoding} string. If the string has characters that cannot be
  3502. represented in the encoding, by default this procedure raises an
  3503. @code{encoding-error}. Pass a @var{conversion-strategy} argument to
  3504. specify other behaviors.
  3505. The return value is a bytevector. @xref{Bytevectors}, for more on
  3506. bytevectors. @xref{Ports}, for more on character encodings and
  3507. conversion strategies.
  3508. @end deffn
  3509. @deffn {Scheme Procedure} bytevector->string bytevector encoding [conversion-strategy]
  3510. Decode @var{bytevector} into a string.
  3511. The bytes will be decoded from the character set by the @var{encoding}
  3512. string. If the bytes do not form a valid encoding, by default this
  3513. procedure raises an @code{decoding-error}. As with
  3514. @code{string->bytevector}, pass the optional @var{conversion-strategy}
  3515. argument to modify this behavior. @xref{Ports}, for more on character
  3516. encodings and conversion strategies.
  3517. @end deffn
  3518. @deffn {Scheme Procedure} call-with-output-encoded-string encoding proc [conversion-strategy]
  3519. Like @code{call-with-output-string}, but instead of returning a string,
  3520. returns a encoding of the string according to @var{encoding}, as a
  3521. bytevector. This procedure can be more efficient than collecting a
  3522. string and then converting it via @code{string->bytevector}.
  3523. @end deffn
  3524. @node Conversion to/from C
  3525. @subsubsection Conversion to/from C
  3526. When creating a Scheme string from a C string or when converting a
  3527. Scheme string to a C string, the concept of character encoding becomes
  3528. important.
  3529. In C, a string is just a sequence of bytes, and the character encoding
  3530. describes the relation between these bytes and the actual characters
  3531. that make up the string. For Scheme strings, character encoding is not
  3532. an issue (most of the time), since in Scheme you usually treat strings
  3533. as character sequences, not byte sequences.
  3534. Converting to C and converting from C each have their own challenges.
  3535. When converting from C to Scheme, it is important that the sequence of
  3536. bytes in the C string be valid with respect to its encoding. ASCII
  3537. strings, for example, can't have any bytes greater than 127. An ASCII
  3538. byte greater than 127 is considered @emph{ill-formed} and cannot be
  3539. converted into a Scheme character.
  3540. Problems can occur in the reverse operation as well. Not all character
  3541. encodings can hold all possible Scheme characters. Some encodings, like
  3542. ASCII for example, can only describe a small subset of all possible
  3543. characters. So, when converting to C, one must first decide what to do
  3544. with Scheme characters that can't be represented in the C string.
  3545. Converting a Scheme string to a C string will often allocate fresh
  3546. memory to hold the result. You must take care that this memory is
  3547. properly freed eventually. In many cases, this can be achieved by
  3548. using @code{scm_dynwind_free} inside an appropriate dynwind context,
  3549. @xref{Dynamic Wind}.
  3550. @deftypefn {C Function} SCM scm_from_locale_string (const char *str)
  3551. @deftypefnx {C Function} SCM scm_from_locale_stringn (const char *str, size_t len)
  3552. Creates a new Scheme string that has the same contents as @var{str} when
  3553. interpreted in the character encoding of the current locale.
  3554. For @code{scm_from_locale_string}, @var{str} must be null-terminated.
  3555. For @code{scm_from_locale_stringn}, @var{len} specifies the length of
  3556. @var{str} in bytes, and @var{str} does not need to be null-terminated.
  3557. If @var{len} is @code{(size_t)-1}, then @var{str} does need to be
  3558. null-terminated and the real length will be found with @code{strlen}.
  3559. If the C string is ill-formed, an error will be raised.
  3560. Note that these functions should @emph{not} be used to convert C string
  3561. constants, because there is no guarantee that the current locale will
  3562. match that of the execution character set, used for string and character
  3563. constants. Most modern C compilers use UTF-8 by default, so to convert
  3564. C string constants we recommend @code{scm_from_utf8_string}.
  3565. @end deftypefn
  3566. @deftypefn {C Function} SCM scm_take_locale_string (char *str)
  3567. @deftypefnx {C Function} SCM scm_take_locale_stringn (char *str, size_t len)
  3568. Like @code{scm_from_locale_string} and @code{scm_from_locale_stringn},
  3569. respectively, but also frees @var{str} with @code{free} eventually.
  3570. Thus, you can use this function when you would free @var{str} anyway
  3571. immediately after creating the Scheme string. In certain cases, Guile
  3572. can then use @var{str} directly as its internal representation.
  3573. @end deftypefn
  3574. @deftypefn {C Function} {char *} scm_to_locale_string (SCM str)
  3575. @deftypefnx {C Function} {char *} scm_to_locale_stringn (SCM str, size_t *lenp)
  3576. Returns a C string with the same contents as @var{str} in the character
  3577. encoding of the current locale. The C string must be freed with
  3578. @code{free} eventually, maybe by using @code{scm_dynwind_free},
  3579. @xref{Dynamic Wind}.
  3580. For @code{scm_to_locale_string}, the returned string is
  3581. null-terminated and an error is signalled when @var{str} contains
  3582. @code{#\nul} characters.
  3583. For @code{scm_to_locale_stringn} and @var{lenp} not @code{NULL},
  3584. @var{str} might contain @code{#\nul} characters and the length of the
  3585. returned string in bytes is stored in @code{*@var{lenp}}. The
  3586. returned string will not be null-terminated in this case. If
  3587. @var{lenp} is @code{NULL}, @code{scm_to_locale_stringn} behaves like
  3588. @code{scm_to_locale_string}.
  3589. If a character in @var{str} cannot be represented in the character
  3590. encoding of the current locale, the default port conversion strategy is
  3591. used. @xref{Ports}, for more on conversion strategies.
  3592. If the conversion strategy is @code{error}, an error will be raised. If
  3593. it is @code{substitute}, a replacement character, such as a question
  3594. mark, will be inserted in its place. If it is @code{escape}, a hex
  3595. escape will be inserted in its place.
  3596. @end deftypefn
  3597. @deftypefn {C Function} size_t scm_to_locale_stringbuf (SCM str, char *buf, size_t max_len)
  3598. Puts @var{str} as a C string in the current locale encoding into the
  3599. memory pointed to by @var{buf}. The buffer at @var{buf} has room for
  3600. @var{max_len} bytes and @code{scm_to_local_stringbuf} will never store
  3601. more than that. No terminating @code{'\0'} will be stored.
  3602. The return value of @code{scm_to_locale_stringbuf} is the number of
  3603. bytes that are needed for all of @var{str}, regardless of whether
  3604. @var{buf} was large enough to hold them. Thus, when the return value
  3605. is larger than @var{max_len}, only @var{max_len} bytes have been
  3606. stored and you probably need to try again with a larger buffer.
  3607. @end deftypefn
  3608. For most situations, string conversion should occur using the current
  3609. locale, such as with the functions above. But there may be cases where
  3610. one wants to convert strings from a character encoding other than the
  3611. locale's character encoding. For these cases, the lower-level functions
  3612. @code{scm_to_stringn} and @code{scm_from_stringn} are provided. These
  3613. functions should seldom be necessary if one is properly using locales.
  3614. @deftp {C Type} scm_t_string_failed_conversion_handler
  3615. This is an enumerated type that can take one of three values:
  3616. @code{SCM_FAILED_CONVERSION_ERROR},
  3617. @code{SCM_FAILED_CONVERSION_QUESTION_MARK}, and
  3618. @code{SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE}. They are used to indicate
  3619. a strategy for handling characters that cannot be converted to or from a
  3620. given character encoding. @code{SCM_FAILED_CONVERSION_ERROR} indicates
  3621. that a conversion should throw an error if some characters cannot be
  3622. converted. @code{SCM_FAILED_CONVERSION_QUESTION_MARK} indicates that a
  3623. conversion should replace unconvertable characters with the question
  3624. mark character. And, @code{SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE}
  3625. requests that a conversion should replace an unconvertable character
  3626. with an escape sequence.
  3627. While all three strategies apply when converting Scheme strings to C,
  3628. only @code{SCM_FAILED_CONVERSION_ERROR} and
  3629. @code{SCM_FAILED_CONVERSION_QUESTION_MARK} can be used when converting C
  3630. strings to Scheme.
  3631. @end deftp
  3632. @deftypefn {C Function} char *scm_to_stringn (SCM str, size_t *lenp, const char *encoding, scm_t_string_failed_conversion_handler handler)
  3633. This function returns a newly allocated C string from the Guile string
  3634. @var{str}. The length of the returned string in bytes will be returned in
  3635. @var{lenp}. The character encoding of the C string is passed as the ASCII,
  3636. null-terminated C string @var{encoding}. The @var{handler} parameter
  3637. gives a strategy for dealing with characters that cannot be converted
  3638. into @var{encoding}.
  3639. If @var{lenp} is @code{NULL}, this function will return a null-terminated C
  3640. string. It will throw an error if the string contains a null
  3641. character.
  3642. The Scheme interface to this function is @code{string->bytevector}, from the
  3643. @code{ice-9 iconv} module. @xref{Representing Strings as Bytes}.
  3644. @end deftypefn
  3645. @deftypefn {C Function} SCM scm_from_stringn (const char *str, size_t len, const char *encoding, scm_t_string_failed_conversion_handler handler)
  3646. This function returns a scheme string from the C string @var{str}. The
  3647. length in bytes of the C string is input as @var{len}. The encoding of the C
  3648. string is passed as the ASCII, null-terminated C string @code{encoding}.
  3649. The @var{handler} parameters suggests a strategy for dealing with
  3650. unconvertable characters.
  3651. The Scheme interface to this function is @code{bytevector->string}.
  3652. @xref{Representing Strings as Bytes}.
  3653. @end deftypefn
  3654. The following conversion functions are provided as a convenience for the
  3655. most commonly used encodings.
  3656. @deftypefn {C Function} SCM scm_from_latin1_string (const char *str)
  3657. @deftypefnx {C Function} SCM scm_from_utf8_string (const char *str)
  3658. @deftypefnx {C Function} SCM scm_from_utf32_string (const scm_t_wchar *str)
  3659. Return a scheme string from the null-terminated C string @var{str},
  3660. which is ISO-8859-1-, UTF-8-, or UTF-32-encoded. These functions should
  3661. be used to convert hard-coded C string constants into Scheme strings.
  3662. @end deftypefn
  3663. @deftypefn {C Function} SCM scm_from_latin1_stringn (const char *str, size_t len)
  3664. @deftypefnx {C Function} SCM scm_from_utf8_stringn (const char *str, size_t len)
  3665. @deftypefnx {C Function} SCM scm_from_utf32_stringn (const scm_t_wchar *str, size_t len)
  3666. Return a scheme string from C string @var{str}, which is ISO-8859-1-,
  3667. UTF-8-, or UTF-32-encoded, of length @var{len}. @var{len} is the number
  3668. of bytes pointed to by @var{str} for @code{scm_from_latin1_stringn} and
  3669. @code{scm_from_utf8_stringn}; it is the number of elements (code points)
  3670. in @var{str} in the case of @code{scm_from_utf32_stringn}.
  3671. @end deftypefn
  3672. @deftypefn {C function} char *scm_to_latin1_stringn (SCM str, size_t *lenp)
  3673. @deftypefnx {C function} char *scm_to_utf8_stringn (SCM str, size_t *lenp)
  3674. @deftypefnx {C function} scm_t_wchar *scm_to_utf32_stringn (SCM str, size_t *lenp)
  3675. Return a newly allocated, ISO-8859-1-, UTF-8-, or UTF-32-encoded C string
  3676. from Scheme string @var{str}. An error is thrown when @var{str}
  3677. cannot be converted to the specified encoding. If @var{lenp} is
  3678. @code{NULL}, the returned C string will be null terminated, and an error
  3679. will be thrown if the C string would otherwise contain null
  3680. characters. If @var{lenp} is not @code{NULL}, the string is not null terminated,
  3681. and the length of the returned string is returned in @var{lenp}. The length
  3682. returned is the number of bytes for @code{scm_to_latin1_stringn} and
  3683. @code{scm_to_utf8_stringn}; it is the number of elements (code points)
  3684. for @code{scm_to_utf32_stringn}.
  3685. @end deftypefn
  3686. It is not often the case, but sometimes when you are dealing with the
  3687. implementation details of a port, you need to encode and decode strings
  3688. according to the encoding and conversion strategy of the port. There
  3689. are some convenience functions for that purpose as well.
  3690. @deftypefn {C Function} SCM scm_from_port_string (const char *str, SCM port)
  3691. @deftypefnx {C Function} SCM scm_from_port_stringn (const char *str, size_t len, SCM port)
  3692. @deftypefnx {C Function} char* scm_to_port_string (SCM str, SCM port)
  3693. @deftypefnx {C Function} char* scm_to_port_stringn (SCM str, size_t *lenp, SCM port)
  3694. Like @code{scm_from_stringn} and friends, except they take their
  3695. encoding and conversion strategy from a given port object.
  3696. @end deftypefn
  3697. @node String Internals
  3698. @subsubsection String Internals
  3699. Guile stores each string in memory as a contiguous array of Unicode code
  3700. points along with an associated set of attributes. If all of the code
  3701. points of a string have an integer range between 0 and 255 inclusive,
  3702. the code point array is stored as one byte per code point: it is stored
  3703. as an ISO-8859-1 (aka Latin-1) string. If any of the code points of the
  3704. string has an integer value greater that 255, the code point array is
  3705. stored as four bytes per code point: it is stored as a UTF-32 string.
  3706. Conversion between the one-byte-per-code-point and
  3707. four-bytes-per-code-point representations happens automatically as
  3708. necessary.
  3709. No API is provided to set the internal representation of strings;
  3710. however, there are pair of procedures available to query it. These are
  3711. debugging procedures. Using them in production code is discouraged,
  3712. since the details of Guile's internal representation of strings may
  3713. change from release to release.
  3714. @deffn {Scheme Procedure} string-bytes-per-char str
  3715. @deffnx {C Function} scm_string_bytes_per_char (str)
  3716. Return the number of bytes used to encode a Unicode code point in string
  3717. @var{str}. The result is one or four.
  3718. @end deffn
  3719. @deffn {Scheme Procedure} %string-dump str
  3720. @deffnx {C Function} scm_sys_string_dump (str)
  3721. Returns an association list containing debugging information for
  3722. @var{str}. The association list has the following entries.
  3723. @table @code
  3724. @item string
  3725. The string itself.
  3726. @item start
  3727. The start index of the string into its stringbuf
  3728. @item length
  3729. The length of the string
  3730. @item shared
  3731. If this string is a substring, it returns its
  3732. parent string. Otherwise, it returns @code{#f}
  3733. @item read-only
  3734. @code{#t} if the string is read-only
  3735. @item stringbuf-chars
  3736. A new string containing this string's stringbuf's characters
  3737. @item stringbuf-length
  3738. The number of characters in this stringbuf
  3739. @item stringbuf-shared
  3740. @code{#t} if this stringbuf is shared
  3741. @item stringbuf-wide
  3742. @code{#t} if this stringbuf's characters are stored in a 32-bit buffer,
  3743. or @code{#f} if they are stored in an 8-bit buffer
  3744. @end table
  3745. @end deffn
  3746. @node Bytevectors
  3747. @subsection Bytevectors
  3748. @cindex bytevector
  3749. @cindex R6RS
  3750. A @dfn{bytevector} is a raw bit string. The @code{(rnrs bytevectors)}
  3751. module provides the programming interface specified by the
  3752. @uref{http://www.r6rs.org/, Revised^6 Report on the Algorithmic Language
  3753. Scheme (R6RS)}. It contains procedures to manipulate bytevectors and
  3754. interpret their contents in a number of ways: bytevector contents can be
  3755. accessed as signed or unsigned integer of various sizes and endianness,
  3756. as IEEE-754 floating point numbers, or as strings. It is a useful tool
  3757. to encode and decode binary data.
  3758. The R6RS (Section 4.3.4) specifies an external representation for
  3759. bytevectors, whereby the octets (integers in the range 0--255) contained
  3760. in the bytevector are represented as a list prefixed by @code{#vu8}:
  3761. @lisp
  3762. #vu8(1 53 204)
  3763. @end lisp
  3764. denotes a 3-byte bytevector containing the octets 1, 53, and 204. Like
  3765. string literals, booleans, etc., bytevectors are ``self-quoting'', i.e.,
  3766. they do not need to be quoted:
  3767. @lisp
  3768. #vu8(1 53 204)
  3769. @result{} #vu8(1 53 204)
  3770. @end lisp
  3771. Bytevectors can be used with the binary input/output primitives of the
  3772. R6RS (@pxref{R6RS I/O Ports}).
  3773. @menu
  3774. * Bytevector Endianness:: Dealing with byte order.
  3775. * Bytevector Manipulation:: Creating, copying, manipulating bytevectors.
  3776. * Bytevectors as Integers:: Interpreting bytes as integers.
  3777. * Bytevectors and Integer Lists:: Converting to/from an integer list.
  3778. * Bytevectors as Floats:: Interpreting bytes as real numbers.
  3779. * Bytevectors as Strings:: Interpreting bytes as Unicode strings.
  3780. * Bytevectors as Arrays:: Guile extension to the bytevector API.
  3781. * Bytevectors as Uniform Vectors:: Bytevectors and SRFI-4.
  3782. @end menu
  3783. @node Bytevector Endianness
  3784. @subsubsection Endianness
  3785. @cindex endianness
  3786. @cindex byte order
  3787. @cindex word order
  3788. Some of the following procedures take an @var{endianness} parameter.
  3789. The @dfn{endianness} is defined as the order of bytes in multi-byte
  3790. numbers: numbers encoded in @dfn{big endian} have their most
  3791. significant bytes written first, whereas numbers encoded in
  3792. @dfn{little endian} have their least significant bytes
  3793. first@footnote{Big-endian and little-endian are the most common
  3794. ``endiannesses'', but others do exist. For instance, the GNU MP
  3795. library allows @dfn{word order} to be specified independently of
  3796. @dfn{byte order} (@pxref{Integer Import and Export,,, gmp, The GNU
  3797. Multiple Precision Arithmetic Library Manual}).}.
  3798. Little-endian is the native endianness of the IA32 architecture and
  3799. its derivatives, while big-endian is native to SPARC and PowerPC,
  3800. among others. The @code{native-endianness} procedure returns the
  3801. native endianness of the machine it runs on.
  3802. @deffn {Scheme Procedure} native-endianness
  3803. @deffnx {C Function} scm_native_endianness ()
  3804. Return a value denoting the native endianness of the host machine.
  3805. @end deffn
  3806. @deffn {Scheme Macro} endianness symbol
  3807. Return an object denoting the endianness specified by @var{symbol}. If
  3808. @var{symbol} is neither @code{big} nor @code{little} then an error is
  3809. raised at expand-time.
  3810. @end deffn
  3811. @defvr {C Variable} scm_endianness_big
  3812. @defvrx {C Variable} scm_endianness_little
  3813. The objects denoting big- and little-endianness, respectively.
  3814. @end defvr
  3815. @node Bytevector Manipulation
  3816. @subsubsection Manipulating Bytevectors
  3817. Bytevectors can be created, copied, and analyzed with the following
  3818. procedures and C functions.
  3819. @deffn {Scheme Procedure} make-bytevector len [fill]
  3820. @deffnx {C Function} scm_make_bytevector (len, fill)
  3821. @deffnx {C Function} scm_c_make_bytevector (size_t len)
  3822. Return a new bytevector of @var{len} bytes. Optionally, if @var{fill}
  3823. is given, fill it with @var{fill}; @var{fill} must be in the range
  3824. [-128,255].
  3825. @end deffn
  3826. @deffn {Scheme Procedure} bytevector? obj
  3827. @deffnx {C Function} scm_bytevector_p (obj)
  3828. Return true if @var{obj} is a bytevector.
  3829. @end deffn
  3830. @deftypefn {C Function} int scm_is_bytevector (SCM obj)
  3831. Equivalent to @code{scm_is_true (scm_bytevector_p (obj))}.
  3832. @end deftypefn
  3833. @deffn {Scheme Procedure} bytevector-length bv
  3834. @deffnx {C Function} scm_bytevector_length (bv)
  3835. Return the length in bytes of bytevector @var{bv}.
  3836. @end deffn
  3837. @deftypefn {C Function} size_t scm_c_bytevector_length (SCM bv)
  3838. Likewise, return the length in bytes of bytevector @var{bv}.
  3839. @end deftypefn
  3840. @deffn {Scheme Procedure} bytevector=? bv1 bv2
  3841. @deffnx {C Function} scm_bytevector_eq_p (bv1, bv2)
  3842. Return is @var{bv1} equals to @var{bv2}---i.e., if they have the same
  3843. length and contents.
  3844. @end deffn
  3845. @deffn {Scheme Procedure} bytevector-fill! bv fill
  3846. @deffnx {C Function} scm_bytevector_fill_x (bv, fill)
  3847. Fill bytevector @var{bv} with @var{fill}, a byte.
  3848. @end deffn
  3849. @deffn {Scheme Procedure} bytevector-copy! source source-start target target-start len
  3850. @deffnx {C Function} scm_bytevector_copy_x (source, source_start, target, target_start, len)
  3851. Copy @var{len} bytes from @var{source} into @var{target}, starting
  3852. reading from @var{source-start} (a positive index within @var{source})
  3853. and start writing at @var{target-start}. It is permitted for the
  3854. @var{source} and @var{target} regions to overlap.
  3855. @end deffn
  3856. @deffn {Scheme Procedure} bytevector-copy bv
  3857. @deffnx {C Function} scm_bytevector_copy (bv)
  3858. Return a newly allocated copy of @var{bv}.
  3859. @end deffn
  3860. @deftypefn {C Function} scm_t_uint8 scm_c_bytevector_ref (SCM bv, size_t index)
  3861. Return the byte at @var{index} in bytevector @var{bv}.
  3862. @end deftypefn
  3863. @deftypefn {C Function} void scm_c_bytevector_set_x (SCM bv, size_t index, scm_t_uint8 value)
  3864. Set the byte at @var{index} in @var{bv} to @var{value}.
  3865. @end deftypefn
  3866. Low-level C macros are available. They do not perform any
  3867. type-checking; as such they should be used with care.
  3868. @deftypefn {C Macro} size_t SCM_BYTEVECTOR_LENGTH (bv)
  3869. Return the length in bytes of bytevector @var{bv}.
  3870. @end deftypefn
  3871. @deftypefn {C Macro} {signed char *} SCM_BYTEVECTOR_CONTENTS (bv)
  3872. Return a pointer to the contents of bytevector @var{bv}.
  3873. @end deftypefn
  3874. @node Bytevectors as Integers
  3875. @subsubsection Interpreting Bytevector Contents as Integers
  3876. The contents of a bytevector can be interpreted as a sequence of
  3877. integers of any given size, sign, and endianness.
  3878. @lisp
  3879. (let ((bv (make-bytevector 4)))
  3880. (bytevector-u8-set! bv 0 #x12)
  3881. (bytevector-u8-set! bv 1 #x34)
  3882. (bytevector-u8-set! bv 2 #x56)
  3883. (bytevector-u8-set! bv 3 #x78)
  3884. (map (lambda (number)
  3885. (number->string number 16))
  3886. (list (bytevector-u8-ref bv 0)
  3887. (bytevector-u16-ref bv 0 (endianness big))
  3888. (bytevector-u32-ref bv 0 (endianness little)))))
  3889. @result{} ("12" "1234" "78563412")
  3890. @end lisp
  3891. The most generic procedures to interpret bytevector contents as integers
  3892. are described below.
  3893. @deffn {Scheme Procedure} bytevector-uint-ref bv index endianness size
  3894. @deffnx {C Function} scm_bytevector_uint_ref (bv, index, endianness, size)
  3895. Return the @var{size}-byte long unsigned integer at index @var{index} in
  3896. @var{bv}, decoded according to @var{endianness}.
  3897. @end deffn
  3898. @deffn {Scheme Procedure} bytevector-sint-ref bv index endianness size
  3899. @deffnx {C Function} scm_bytevector_sint_ref (bv, index, endianness, size)
  3900. Return the @var{size}-byte long signed integer at index @var{index} in
  3901. @var{bv}, decoded according to @var{endianness}.
  3902. @end deffn
  3903. @deffn {Scheme Procedure} bytevector-uint-set! bv index value endianness size
  3904. @deffnx {C Function} scm_bytevector_uint_set_x (bv, index, value, endianness, size)
  3905. Set the @var{size}-byte long unsigned integer at @var{index} to
  3906. @var{value}, encoded according to @var{endianness}.
  3907. @end deffn
  3908. @deffn {Scheme Procedure} bytevector-sint-set! bv index value endianness size
  3909. @deffnx {C Function} scm_bytevector_sint_set_x (bv, index, value, endianness, size)
  3910. Set the @var{size}-byte long signed integer at @var{index} to
  3911. @var{value}, encoded according to @var{endianness}.
  3912. @end deffn
  3913. The following procedures are similar to the ones above, but specialized
  3914. to a given integer size:
  3915. @deffn {Scheme Procedure} bytevector-u8-ref bv index
  3916. @deffnx {Scheme Procedure} bytevector-s8-ref bv index
  3917. @deffnx {Scheme Procedure} bytevector-u16-ref bv index endianness
  3918. @deffnx {Scheme Procedure} bytevector-s16-ref bv index endianness
  3919. @deffnx {Scheme Procedure} bytevector-u32-ref bv index endianness
  3920. @deffnx {Scheme Procedure} bytevector-s32-ref bv index endianness
  3921. @deffnx {Scheme Procedure} bytevector-u64-ref bv index endianness
  3922. @deffnx {Scheme Procedure} bytevector-s64-ref bv index endianness
  3923. @deffnx {C Function} scm_bytevector_u8_ref (bv, index)
  3924. @deffnx {C Function} scm_bytevector_s8_ref (bv, index)
  3925. @deffnx {C Function} scm_bytevector_u16_ref (bv, index, endianness)
  3926. @deffnx {C Function} scm_bytevector_s16_ref (bv, index, endianness)
  3927. @deffnx {C Function} scm_bytevector_u32_ref (bv, index, endianness)
  3928. @deffnx {C Function} scm_bytevector_s32_ref (bv, index, endianness)
  3929. @deffnx {C Function} scm_bytevector_u64_ref (bv, index, endianness)
  3930. @deffnx {C Function} scm_bytevector_s64_ref (bv, index, endianness)
  3931. Return the unsigned @var{n}-bit (signed) integer (where @var{n} is 8,
  3932. 16, 32 or 64) from @var{bv} at @var{index}, decoded according to
  3933. @var{endianness}.
  3934. @end deffn
  3935. @deffn {Scheme Procedure} bytevector-u8-set! bv index value
  3936. @deffnx {Scheme Procedure} bytevector-s8-set! bv index value
  3937. @deffnx {Scheme Procedure} bytevector-u16-set! bv index value endianness
  3938. @deffnx {Scheme Procedure} bytevector-s16-set! bv index value endianness
  3939. @deffnx {Scheme Procedure} bytevector-u32-set! bv index value endianness
  3940. @deffnx {Scheme Procedure} bytevector-s32-set! bv index value endianness
  3941. @deffnx {Scheme Procedure} bytevector-u64-set! bv index value endianness
  3942. @deffnx {Scheme Procedure} bytevector-s64-set! bv index value endianness
  3943. @deffnx {C Function} scm_bytevector_u8_set_x (bv, index, value)
  3944. @deffnx {C Function} scm_bytevector_s8_set_x (bv, index, value)
  3945. @deffnx {C Function} scm_bytevector_u16_set_x (bv, index, value, endianness)
  3946. @deffnx {C Function} scm_bytevector_s16_set_x (bv, index, value, endianness)
  3947. @deffnx {C Function} scm_bytevector_u32_set_x (bv, index, value, endianness)
  3948. @deffnx {C Function} scm_bytevector_s32_set_x (bv, index, value, endianness)
  3949. @deffnx {C Function} scm_bytevector_u64_set_x (bv, index, value, endianness)
  3950. @deffnx {C Function} scm_bytevector_s64_set_x (bv, index, value, endianness)
  3951. Store @var{value} as an @var{n}-bit (signed) integer (where @var{n} is
  3952. 8, 16, 32 or 64) in @var{bv} at @var{index}, encoded according to
  3953. @var{endianness}.
  3954. @end deffn
  3955. Finally, a variant specialized for the host's endianness is available
  3956. for each of these functions (with the exception of the @code{u8}
  3957. accessors, for obvious reasons):
  3958. @deffn {Scheme Procedure} bytevector-u16-native-ref bv index
  3959. @deffnx {Scheme Procedure} bytevector-s16-native-ref bv index
  3960. @deffnx {Scheme Procedure} bytevector-u32-native-ref bv index
  3961. @deffnx {Scheme Procedure} bytevector-s32-native-ref bv index
  3962. @deffnx {Scheme Procedure} bytevector-u64-native-ref bv index
  3963. @deffnx {Scheme Procedure} bytevector-s64-native-ref bv index
  3964. @deffnx {C Function} scm_bytevector_u16_native_ref (bv, index)
  3965. @deffnx {C Function} scm_bytevector_s16_native_ref (bv, index)
  3966. @deffnx {C Function} scm_bytevector_u32_native_ref (bv, index)
  3967. @deffnx {C Function} scm_bytevector_s32_native_ref (bv, index)
  3968. @deffnx {C Function} scm_bytevector_u64_native_ref (bv, index)
  3969. @deffnx {C Function} scm_bytevector_s64_native_ref (bv, index)
  3970. Return the unsigned @var{n}-bit (signed) integer (where @var{n} is 8,
  3971. 16, 32 or 64) from @var{bv} at @var{index}, decoded according to the
  3972. host's native endianness.
  3973. @end deffn
  3974. @deffn {Scheme Procedure} bytevector-u16-native-set! bv index value
  3975. @deffnx {Scheme Procedure} bytevector-s16-native-set! bv index value
  3976. @deffnx {Scheme Procedure} bytevector-u32-native-set! bv index value
  3977. @deffnx {Scheme Procedure} bytevector-s32-native-set! bv index value
  3978. @deffnx {Scheme Procedure} bytevector-u64-native-set! bv index value
  3979. @deffnx {Scheme Procedure} bytevector-s64-native-set! bv index value
  3980. @deffnx {C Function} scm_bytevector_u16_native_set_x (bv, index, value)
  3981. @deffnx {C Function} scm_bytevector_s16_native_set_x (bv, index, value)
  3982. @deffnx {C Function} scm_bytevector_u32_native_set_x (bv, index, value)
  3983. @deffnx {C Function} scm_bytevector_s32_native_set_x (bv, index, value)
  3984. @deffnx {C Function} scm_bytevector_u64_native_set_x (bv, index, value)
  3985. @deffnx {C Function} scm_bytevector_s64_native_set_x (bv, index, value)
  3986. Store @var{value} as an @var{n}-bit (signed) integer (where @var{n} is
  3987. 8, 16, 32 or 64) in @var{bv} at @var{index}, encoded according to the
  3988. host's native endianness.
  3989. @end deffn
  3990. @node Bytevectors and Integer Lists
  3991. @subsubsection Converting Bytevectors to/from Integer Lists
  3992. Bytevector contents can readily be converted to/from lists of signed or
  3993. unsigned integers:
  3994. @lisp
  3995. (bytevector->sint-list (u8-list->bytevector (make-list 4 255))
  3996. (endianness little) 2)
  3997. @result{} (-1 -1)
  3998. @end lisp
  3999. @deffn {Scheme Procedure} bytevector->u8-list bv
  4000. @deffnx {C Function} scm_bytevector_to_u8_list (bv)
  4001. Return a newly allocated list of unsigned 8-bit integers from the
  4002. contents of @var{bv}.
  4003. @end deffn
  4004. @deffn {Scheme Procedure} u8-list->bytevector lst
  4005. @deffnx {C Function} scm_u8_list_to_bytevector (lst)
  4006. Return a newly allocated bytevector consisting of the unsigned 8-bit
  4007. integers listed in @var{lst}.
  4008. @end deffn
  4009. @deffn {Scheme Procedure} bytevector->uint-list bv endianness size
  4010. @deffnx {C Function} scm_bytevector_to_uint_list (bv, endianness, size)
  4011. Return a list of unsigned integers of @var{size} bytes representing the
  4012. contents of @var{bv}, decoded according to @var{endianness}.
  4013. @end deffn
  4014. @deffn {Scheme Procedure} bytevector->sint-list bv endianness size
  4015. @deffnx {C Function} scm_bytevector_to_sint_list (bv, endianness, size)
  4016. Return a list of signed integers of @var{size} bytes representing the
  4017. contents of @var{bv}, decoded according to @var{endianness}.
  4018. @end deffn
  4019. @deffn {Scheme Procedure} uint-list->bytevector lst endianness size
  4020. @deffnx {C Function} scm_uint_list_to_bytevector (lst, endianness, size)
  4021. Return a new bytevector containing the unsigned integers listed in
  4022. @var{lst} and encoded on @var{size} bytes according to @var{endianness}.
  4023. @end deffn
  4024. @deffn {Scheme Procedure} sint-list->bytevector lst endianness size
  4025. @deffnx {C Function} scm_sint_list_to_bytevector (lst, endianness, size)
  4026. Return a new bytevector containing the signed integers listed in
  4027. @var{lst} and encoded on @var{size} bytes according to @var{endianness}.
  4028. @end deffn
  4029. @node Bytevectors as Floats
  4030. @subsubsection Interpreting Bytevector Contents as Floating Point Numbers
  4031. @cindex IEEE-754 floating point numbers
  4032. Bytevector contents can also be accessed as IEEE-754 single- or
  4033. double-precision floating point numbers (respectively 32 and 64-bit
  4034. long) using the procedures described here.
  4035. @deffn {Scheme Procedure} bytevector-ieee-single-ref bv index endianness
  4036. @deffnx {Scheme Procedure} bytevector-ieee-double-ref bv index endianness
  4037. @deffnx {C Function} scm_bytevector_ieee_single_ref (bv, index, endianness)
  4038. @deffnx {C Function} scm_bytevector_ieee_double_ref (bv, index, endianness)
  4039. Return the IEEE-754 single-precision floating point number from @var{bv}
  4040. at @var{index} according to @var{endianness}.
  4041. @end deffn
  4042. @deffn {Scheme Procedure} bytevector-ieee-single-set! bv index value endianness
  4043. @deffnx {Scheme Procedure} bytevector-ieee-double-set! bv index value endianness
  4044. @deffnx {C Function} scm_bytevector_ieee_single_set_x (bv, index, value, endianness)
  4045. @deffnx {C Function} scm_bytevector_ieee_double_set_x (bv, index, value, endianness)
  4046. Store real number @var{value} in @var{bv} at @var{index} according to
  4047. @var{endianness}.
  4048. @end deffn
  4049. Specialized procedures are also available:
  4050. @deffn {Scheme Procedure} bytevector-ieee-single-native-ref bv index
  4051. @deffnx {Scheme Procedure} bytevector-ieee-double-native-ref bv index
  4052. @deffnx {C Function} scm_bytevector_ieee_single_native_ref (bv, index)
  4053. @deffnx {C Function} scm_bytevector_ieee_double_native_ref (bv, index)
  4054. Return the IEEE-754 single-precision floating point number from @var{bv}
  4055. at @var{index} according to the host's native endianness.
  4056. @end deffn
  4057. @deffn {Scheme Procedure} bytevector-ieee-single-native-set! bv index value
  4058. @deffnx {Scheme Procedure} bytevector-ieee-double-native-set! bv index value
  4059. @deffnx {C Function} scm_bytevector_ieee_single_native_set_x (bv, index, value)
  4060. @deffnx {C Function} scm_bytevector_ieee_double_native_set_x (bv, index, value)
  4061. Store real number @var{value} in @var{bv} at @var{index} according to
  4062. the host's native endianness.
  4063. @end deffn
  4064. @node Bytevectors as Strings
  4065. @subsubsection Interpreting Bytevector Contents as Unicode Strings
  4066. @cindex Unicode string encoding
  4067. Bytevector contents can also be interpreted as Unicode strings encoded
  4068. in one of the most commonly available encoding formats.
  4069. @xref{Representing Strings as Bytes}, for a more generic interface.
  4070. @lisp
  4071. (utf8->string (u8-list->bytevector '(99 97 102 101)))
  4072. @result{} "cafe"
  4073. (string->utf8 "caf@'e") ;; SMALL LATIN LETTER E WITH ACUTE ACCENT
  4074. @result{} #vu8(99 97 102 195 169)
  4075. @end lisp
  4076. @deffn {Scheme Procedure} string->utf8 str
  4077. @deffnx {Scheme Procedure} string->utf16 str [endianness]
  4078. @deffnx {Scheme Procedure} string->utf32 str [endianness]
  4079. @deffnx {C Function} scm_string_to_utf8 (str)
  4080. @deffnx {C Function} scm_string_to_utf16 (str, endianness)
  4081. @deffnx {C Function} scm_string_to_utf32 (str, endianness)
  4082. Return a newly allocated bytevector that contains the UTF-8, UTF-16, or
  4083. UTF-32 (aka. UCS-4) encoding of @var{str}. For UTF-16 and UTF-32,
  4084. @var{endianness} should be the symbol @code{big} or @code{little}; when omitted,
  4085. it defaults to big endian.
  4086. @end deffn
  4087. @deffn {Scheme Procedure} utf8->string utf
  4088. @deffnx {Scheme Procedure} utf16->string utf [endianness]
  4089. @deffnx {Scheme Procedure} utf32->string utf [endianness]
  4090. @deffnx {C Function} scm_utf8_to_string (utf)
  4091. @deffnx {C Function} scm_utf16_to_string (utf, endianness)
  4092. @deffnx {C Function} scm_utf32_to_string (utf, endianness)
  4093. Return a newly allocated string that contains from the UTF-8-, UTF-16-,
  4094. or UTF-32-decoded contents of bytevector @var{utf}. For UTF-16 and UTF-32,
  4095. @var{endianness} should be the symbol @code{big} or @code{little}; when omitted,
  4096. it defaults to big endian.
  4097. @end deffn
  4098. @node Bytevectors as Arrays
  4099. @subsubsection Accessing Bytevectors with the Array API
  4100. As an extension to the R6RS, Guile allows bytevectors to be manipulated
  4101. with the @dfn{array} procedures (@pxref{Arrays}). When using these
  4102. APIs, bytes are accessed one at a time as 8-bit unsigned integers:
  4103. @example
  4104. (define bv #vu8(0 1 2 3))
  4105. (array? bv)
  4106. @result{} #t
  4107. (array-rank bv)
  4108. @result{} 1
  4109. (array-ref bv 2)
  4110. @result{} 2
  4111. ;; Note the different argument order on array-set!.
  4112. (array-set! bv 77 2)
  4113. (array-ref bv 2)
  4114. @result{} 77
  4115. (array-type bv)
  4116. @result{} vu8
  4117. @end example
  4118. @node Bytevectors as Uniform Vectors
  4119. @subsubsection Accessing Bytevectors with the SRFI-4 API
  4120. Bytevectors may also be accessed with the SRFI-4 API. @xref{SRFI-4 and
  4121. Bytevectors}, for more information.
  4122. @node Symbols
  4123. @subsection Symbols
  4124. @tpindex Symbols
  4125. Symbols in Scheme are widely used in three ways: as items of discrete
  4126. data, as lookup keys for alists and hash tables, and to denote variable
  4127. references.
  4128. A @dfn{symbol} is similar to a string in that it is defined by a
  4129. sequence of characters. The sequence of characters is known as the
  4130. symbol's @dfn{name}. In the usual case --- that is, where the symbol's
  4131. name doesn't include any characters that could be confused with other
  4132. elements of Scheme syntax --- a symbol is written in a Scheme program by
  4133. writing the sequence of characters that make up the name, @emph{without}
  4134. any quotation marks or other special syntax. For example, the symbol
  4135. whose name is ``multiply-by-2'' is written, simply:
  4136. @lisp
  4137. multiply-by-2
  4138. @end lisp
  4139. Notice how this differs from a @emph{string} with contents
  4140. ``multiply-by-2'', which is written with double quotation marks, like
  4141. this:
  4142. @lisp
  4143. "multiply-by-2"
  4144. @end lisp
  4145. Looking beyond how they are written, symbols are different from strings
  4146. in two important respects.
  4147. The first important difference is uniqueness. If the same-looking
  4148. string is read twice from two different places in a program, the result
  4149. is two @emph{different} string objects whose contents just happen to be
  4150. the same. If, on the other hand, the same-looking symbol is read twice
  4151. from two different places in a program, the result is the @emph{same}
  4152. symbol object both times.
  4153. Given two read symbols, you can use @code{eq?} to test whether they are
  4154. the same (that is, have the same name). @code{eq?} is the most
  4155. efficient comparison operator in Scheme, and comparing two symbols like
  4156. this is as fast as comparing, for example, two numbers. Given two
  4157. strings, on the other hand, you must use @code{equal?} or
  4158. @code{string=?}, which are much slower comparison operators, to
  4159. determine whether the strings have the same contents.
  4160. @lisp
  4161. (define sym1 (quote hello))
  4162. (define sym2 (quote hello))
  4163. (eq? sym1 sym2) @result{} #t
  4164. (define str1 "hello")
  4165. (define str2 "hello")
  4166. (eq? str1 str2) @result{} #f
  4167. (equal? str1 str2) @result{} #t
  4168. @end lisp
  4169. The second important difference is that symbols, unlike strings, are not
  4170. self-evaluating. This is why we need the @code{(quote @dots{})}s in the
  4171. example above: @code{(quote hello)} evaluates to the symbol named
  4172. "hello" itself, whereas an unquoted @code{hello} is @emph{read} as the
  4173. symbol named "hello" and evaluated as a variable reference @dots{} about
  4174. which more below (@pxref{Symbol Variables}).
  4175. @menu
  4176. * Symbol Data:: Symbols as discrete data.
  4177. * Symbol Keys:: Symbols as lookup keys.
  4178. * Symbol Variables:: Symbols as denoting variables.
  4179. * Symbol Primitives:: Operations related to symbols.
  4180. * Symbol Props:: Function slots and property lists.
  4181. * Symbol Read Syntax:: Extended read syntax for symbols.
  4182. * Symbol Uninterned:: Uninterned symbols.
  4183. @end menu
  4184. @node Symbol Data
  4185. @subsubsection Symbols as Discrete Data
  4186. Numbers and symbols are similar to the extent that they both lend
  4187. themselves to @code{eq?} comparison. But symbols are more descriptive
  4188. than numbers, because a symbol's name can be used directly to describe
  4189. the concept for which that symbol stands.
  4190. For example, imagine that you need to represent some colours in a
  4191. computer program. Using numbers, you would have to choose arbitrarily
  4192. some mapping between numbers and colours, and then take care to use that
  4193. mapping consistently:
  4194. @lisp
  4195. ;; 1=red, 2=green, 3=purple
  4196. (if (eq? (colour-of car) 1)
  4197. ...)
  4198. @end lisp
  4199. @noindent
  4200. You can make the mapping more explicit and the code more readable by
  4201. defining constants:
  4202. @lisp
  4203. (define red 1)
  4204. (define green 2)
  4205. (define purple 3)
  4206. (if (eq? (colour-of car) red)
  4207. ...)
  4208. @end lisp
  4209. @noindent
  4210. But the simplest and clearest approach is not to use numbers at all, but
  4211. symbols whose names specify the colours that they refer to:
  4212. @lisp
  4213. (if (eq? (colour-of car) 'red)
  4214. ...)
  4215. @end lisp
  4216. The descriptive advantages of symbols over numbers increase as the set
  4217. of concepts that you want to describe grows. Suppose that a car object
  4218. can have other properties as well, such as whether it has or uses:
  4219. @itemize @bullet
  4220. @item
  4221. automatic or manual transmission
  4222. @item
  4223. leaded or unleaded fuel
  4224. @item
  4225. power steering (or not).
  4226. @end itemize
  4227. @noindent
  4228. Then a car's combined property set could be naturally represented and
  4229. manipulated as a list of symbols:
  4230. @lisp
  4231. (properties-of car1)
  4232. @result{}
  4233. (red manual unleaded power-steering)
  4234. (if (memq 'power-steering (properties-of car1))
  4235. (display "Unfit people can drive this car.\n")
  4236. (display "You'll need strong arms to drive this car!\n"))
  4237. @print{}
  4238. Unfit people can drive this car.
  4239. @end lisp
  4240. Remember, the fundamental property of symbols that we are relying on
  4241. here is that an occurrence of @code{'red} in one part of a program is an
  4242. @emph{indistinguishable} symbol from an occurrence of @code{'red} in
  4243. another part of a program; this means that symbols can usefully be
  4244. compared using @code{eq?}. At the same time, symbols have naturally
  4245. descriptive names. This combination of efficiency and descriptive power
  4246. makes them ideal for use as discrete data.
  4247. @node Symbol Keys
  4248. @subsubsection Symbols as Lookup Keys
  4249. Given their efficiency and descriptive power, it is natural to use
  4250. symbols as the keys in an association list or hash table.
  4251. To illustrate this, consider a more structured representation of the car
  4252. properties example from the preceding subsection. Rather than
  4253. mixing all the properties up together in a flat list, we could use an
  4254. association list like this:
  4255. @lisp
  4256. (define car1-properties '((colour . red)
  4257. (transmission . manual)
  4258. (fuel . unleaded)
  4259. (steering . power-assisted)))
  4260. @end lisp
  4261. Notice how this structure is more explicit and extensible than the flat
  4262. list. For example it makes clear that @code{manual} refers to the
  4263. transmission rather than, say, the windows or the locking of the car.
  4264. It also allows further properties to use the same symbols among their
  4265. possible values without becoming ambiguous:
  4266. @lisp
  4267. (define car1-properties '((colour . red)
  4268. (transmission . manual)
  4269. (fuel . unleaded)
  4270. (steering . power-assisted)
  4271. (seat-colour . red)
  4272. (locking . manual)))
  4273. @end lisp
  4274. With a representation like this, it is easy to use the efficient
  4275. @code{assq-XXX} family of procedures (@pxref{Association Lists}) to
  4276. extract or change individual pieces of information:
  4277. @lisp
  4278. (assq-ref car1-properties 'fuel) @result{} unleaded
  4279. (assq-ref car1-properties 'transmission) @result{} manual
  4280. (assq-set! car1-properties 'seat-colour 'black)
  4281. @result{}
  4282. ((colour . red)
  4283. (transmission . manual)
  4284. (fuel . unleaded)
  4285. (steering . power-assisted)
  4286. (seat-colour . black)
  4287. (locking . manual)))
  4288. @end lisp
  4289. Hash tables also have keys, and exactly the same arguments apply to the
  4290. use of symbols in hash tables as in association lists. The hash value
  4291. that Guile uses to decide where to add a symbol-keyed entry to a hash
  4292. table can be obtained by calling the @code{symbol-hash} procedure:
  4293. @deffn {Scheme Procedure} symbol-hash symbol
  4294. @deffnx {C Function} scm_symbol_hash (symbol)
  4295. Return a hash value for @var{symbol}.
  4296. @end deffn
  4297. See @ref{Hash Tables} for information about hash tables in general, and
  4298. for why you might choose to use a hash table rather than an association
  4299. list.
  4300. @node Symbol Variables
  4301. @subsubsection Symbols as Denoting Variables
  4302. When an unquoted symbol in a Scheme program is evaluated, it is
  4303. interpreted as a variable reference, and the result of the evaluation is
  4304. the appropriate variable's value.
  4305. For example, when the expression @code{(string-length "abcd")} is read
  4306. and evaluated, the sequence of characters @code{string-length} is read
  4307. as the symbol whose name is "string-length". This symbol is associated
  4308. with a variable whose value is the procedure that implements string
  4309. length calculation. Therefore evaluation of the @code{string-length}
  4310. symbol results in that procedure.
  4311. The details of the connection between an unquoted symbol and the
  4312. variable to which it refers are explained elsewhere. See @ref{Binding
  4313. Constructs}, for how associations between symbols and variables are
  4314. created, and @ref{Modules}, for how those associations are affected by
  4315. Guile's module system.
  4316. @node Symbol Primitives
  4317. @subsubsection Operations Related to Symbols
  4318. Given any Scheme value, you can determine whether it is a symbol using
  4319. the @code{symbol?} primitive:
  4320. @rnindex symbol?
  4321. @deffn {Scheme Procedure} symbol? obj
  4322. @deffnx {C Function} scm_symbol_p (obj)
  4323. Return @code{#t} if @var{obj} is a symbol, otherwise return
  4324. @code{#f}.
  4325. @end deffn
  4326. @deftypefn {C Function} int scm_is_symbol (SCM val)
  4327. Equivalent to @code{scm_is_true (scm_symbol_p (val))}.
  4328. @end deftypefn
  4329. Once you know that you have a symbol, you can obtain its name as a
  4330. string by calling @code{symbol->string}. Note that Guile differs by
  4331. default from R5RS on the details of @code{symbol->string} as regards
  4332. case-sensitivity:
  4333. @rnindex symbol->string
  4334. @deffn {Scheme Procedure} symbol->string s
  4335. @deffnx {C Function} scm_symbol_to_string (s)
  4336. Return the name of symbol @var{s} as a string. By default, Guile reads
  4337. symbols case-sensitively, so the string returned will have the same case
  4338. variation as the sequence of characters that caused @var{s} to be
  4339. created.
  4340. If Guile is set to read symbols case-insensitively (as specified by
  4341. R5RS), and @var{s} comes into being as part of a literal expression
  4342. (@pxref{Literal expressions,,,r5rs, The Revised^5 Report on Scheme}) or
  4343. by a call to the @code{read} or @code{string-ci->symbol} procedures,
  4344. Guile converts any alphabetic characters in the symbol's name to
  4345. lower case before creating the symbol object, so the string returned
  4346. here will be in lower case.
  4347. If @var{s} was created by @code{string->symbol}, the case of characters
  4348. in the string returned will be the same as that in the string that was
  4349. passed to @code{string->symbol}, regardless of Guile's case-sensitivity
  4350. setting at the time @var{s} was created.
  4351. It is an error to apply mutation procedures like @code{string-set!} to
  4352. strings returned by this procedure.
  4353. @end deffn
  4354. Most symbols are created by writing them literally in code. However it
  4355. is also possible to create symbols programmatically using the following
  4356. procedures:
  4357. @deffn {Scheme Procedure} symbol char@dots{}
  4358. @rnindex symbol
  4359. Return a newly allocated symbol made from the given character arguments.
  4360. @example
  4361. (symbol #\x #\y #\z) @result{} xyz
  4362. @end example
  4363. @end deffn
  4364. @deffn {Scheme Procedure} list->symbol lst
  4365. @rnindex list->symbol
  4366. Return a newly allocated symbol made from a list of characters.
  4367. @example
  4368. (list->symbol '(#\a #\b #\c)) @result{} abc
  4369. @end example
  4370. @end deffn
  4371. @rnindex symbol-append
  4372. @deffn {Scheme Procedure} symbol-append arg @dots{}
  4373. Return a newly allocated symbol whose characters form the
  4374. concatenation of the given symbols, @var{arg} @enddots{}.
  4375. @example
  4376. (let ((h 'hello))
  4377. (symbol-append h 'world))
  4378. @result{} helloworld
  4379. @end example
  4380. @end deffn
  4381. @rnindex string->symbol
  4382. @deffn {Scheme Procedure} string->symbol string
  4383. @deffnx {C Function} scm_string_to_symbol (string)
  4384. Return the symbol whose name is @var{string}. This procedure can create
  4385. symbols with names containing special characters or letters in the
  4386. non-standard case, but it is usually a bad idea to create such symbols
  4387. because in some implementations of Scheme they cannot be read as
  4388. themselves.
  4389. @end deffn
  4390. @deffn {Scheme Procedure} string-ci->symbol str
  4391. @deffnx {C Function} scm_string_ci_to_symbol (str)
  4392. Return the symbol whose name is @var{str}. If Guile is currently
  4393. reading symbols case-insensitively, @var{str} is converted to lowercase
  4394. before the returned symbol is looked up or created.
  4395. @end deffn
  4396. The following examples illustrate Guile's detailed behaviour as regards
  4397. the case-sensitivity of symbols:
  4398. @lisp
  4399. (read-enable 'case-insensitive) ; R5RS compliant behaviour
  4400. (symbol->string 'flying-fish) @result{} "flying-fish"
  4401. (symbol->string 'Martin) @result{} "martin"
  4402. (symbol->string
  4403. (string->symbol "Malvina")) @result{} "Malvina"
  4404. (eq? 'mISSISSIppi 'mississippi) @result{} #t
  4405. (string->symbol "mISSISSIppi") @result{} mISSISSIppi
  4406. (eq? 'bitBlt (string->symbol "bitBlt")) @result{} #f
  4407. (eq? 'LolliPop
  4408. (string->symbol (symbol->string 'LolliPop))) @result{} #t
  4409. (string=? "K. Harper, M.D."
  4410. (symbol->string
  4411. (string->symbol "K. Harper, M.D."))) @result{} #t
  4412. (read-disable 'case-insensitive) ; Guile default behaviour
  4413. (symbol->string 'flying-fish) @result{} "flying-fish"
  4414. (symbol->string 'Martin) @result{} "Martin"
  4415. (symbol->string
  4416. (string->symbol "Malvina")) @result{} "Malvina"
  4417. (eq? 'mISSISSIppi 'mississippi) @result{} #f
  4418. (string->symbol "mISSISSIppi") @result{} mISSISSIppi
  4419. (eq? 'bitBlt (string->symbol "bitBlt")) @result{} #t
  4420. (eq? 'LolliPop
  4421. (string->symbol (symbol->string 'LolliPop))) @result{} #t
  4422. (string=? "K. Harper, M.D."
  4423. (symbol->string
  4424. (string->symbol "K. Harper, M.D."))) @result{} #t
  4425. @end lisp
  4426. From C, there are lower level functions that construct a Scheme symbol
  4427. from a C string in the current locale encoding.
  4428. When you want to do more from C, you should convert between symbols
  4429. and strings using @code{scm_symbol_to_string} and
  4430. @code{scm_string_to_symbol} and work with the strings.
  4431. @deftypefn {C Function} SCM scm_from_latin1_symbol (const char *name)
  4432. @deftypefnx {C Function} SCM scm_from_utf8_symbol (const char *name)
  4433. Construct and return a Scheme symbol whose name is specified by the
  4434. null-terminated C string @var{name}. These are appropriate when
  4435. the C string is hard-coded in the source code.
  4436. @end deftypefn
  4437. @deftypefn {C Function} SCM scm_from_locale_symbol (const char *name)
  4438. @deftypefnx {C Function} SCM scm_from_locale_symboln (const char *name, size_t len)
  4439. Construct and return a Scheme symbol whose name is specified by
  4440. @var{name}. For @code{scm_from_locale_symbol}, @var{name} must be null
  4441. terminated; for @code{scm_from_locale_symboln} the length of @var{name} is
  4442. specified explicitly by @var{len}.
  4443. Note that these functions should @emph{not} be used when @var{name} is a
  4444. C string constant, because there is no guarantee that the current locale
  4445. will match that of the execution character set, used for string and
  4446. character constants. Most modern C compilers use UTF-8 by default, so
  4447. in such cases we recommend @code{scm_from_utf8_symbol}.
  4448. @end deftypefn
  4449. @deftypefn {C Function} SCM scm_take_locale_symbol (char *str)
  4450. @deftypefnx {C Function} SCM scm_take_locale_symboln (char *str, size_t len)
  4451. Like @code{scm_from_locale_symbol} and @code{scm_from_locale_symboln},
  4452. respectively, but also frees @var{str} with @code{free} eventually.
  4453. Thus, you can use this function when you would free @var{str} anyway
  4454. immediately after creating the Scheme string. In certain cases, Guile
  4455. can then use @var{str} directly as its internal representation.
  4456. @end deftypefn
  4457. The size of a symbol can also be obtained from C:
  4458. @deftypefn {C Function} size_t scm_c_symbol_length (SCM sym)
  4459. Return the number of characters in @var{sym}.
  4460. @end deftypefn
  4461. Finally, some applications, especially those that generate new Scheme
  4462. code dynamically, need to generate symbols for use in the generated
  4463. code. The @code{gensym} primitive meets this need:
  4464. @deffn {Scheme Procedure} gensym [prefix]
  4465. @deffnx {C Function} scm_gensym (prefix)
  4466. Create a new symbol with a name constructed from a prefix and a counter
  4467. value. The string @var{prefix} can be specified as an optional
  4468. argument. Default prefix is @samp{@w{ g}}. The counter is increased by 1
  4469. at each call. There is no provision for resetting the counter.
  4470. @end deffn
  4471. The symbols generated by @code{gensym} are @emph{likely} to be unique,
  4472. since their names begin with a space and it is only otherwise possible
  4473. to generate such symbols if a programmer goes out of their way to do
  4474. so. Uniqueness can be guaranteed by instead using uninterned symbols
  4475. (@pxref{Symbol Uninterned}), though they can't be usefully written out
  4476. and read back in.
  4477. @node Symbol Props
  4478. @subsubsection Function Slots and Property Lists
  4479. In traditional Lisp dialects, symbols are often understood as having
  4480. three kinds of value at once:
  4481. @itemize @bullet
  4482. @item
  4483. a @dfn{variable} value, which is used when the symbol appears in
  4484. code in a variable reference context
  4485. @item
  4486. a @dfn{function} value, which is used when the symbol appears in
  4487. code in a function name position (i.e.@: as the first element in an
  4488. unquoted list)
  4489. @item
  4490. a @dfn{property list} value, which is used when the symbol is given as
  4491. the first argument to Lisp's @code{put} or @code{get} functions.
  4492. @end itemize
  4493. Although Scheme (as one of its simplifications with respect to Lisp)
  4494. does away with the distinction between variable and function namespaces,
  4495. Guile currently retains some elements of the traditional structure in
  4496. case they turn out to be useful when implementing translators for other
  4497. languages, in particular Emacs Lisp.
  4498. Specifically, Guile symbols have two extra slots, one for a symbol's
  4499. property list, and one for its ``function value.'' The following procedures
  4500. are provided to access these slots.
  4501. @deffn {Scheme Procedure} symbol-fref symbol
  4502. @deffnx {C Function} scm_symbol_fref (symbol)
  4503. Return the contents of @var{symbol}'s @dfn{function slot}.
  4504. @end deffn
  4505. @deffn {Scheme Procedure} symbol-fset! symbol value
  4506. @deffnx {C Function} scm_symbol_fset_x (symbol, value)
  4507. Set the contents of @var{symbol}'s function slot to @var{value}.
  4508. @end deffn
  4509. @deffn {Scheme Procedure} symbol-pref symbol
  4510. @deffnx {C Function} scm_symbol_pref (symbol)
  4511. Return the @dfn{property list} currently associated with @var{symbol}.
  4512. @end deffn
  4513. @deffn {Scheme Procedure} symbol-pset! symbol value
  4514. @deffnx {C Function} scm_symbol_pset_x (symbol, value)
  4515. Set @var{symbol}'s property list to @var{value}.
  4516. @end deffn
  4517. @deffn {Scheme Procedure} symbol-property sym prop
  4518. From @var{sym}'s property list, return the value for property
  4519. @var{prop}. The assumption is that @var{sym}'s property list is an
  4520. association list whose keys are distinguished from each other using
  4521. @code{equal?}; @var{prop} should be one of the keys in that list. If
  4522. the property list has no entry for @var{prop}, @code{symbol-property}
  4523. returns @code{#f}.
  4524. @end deffn
  4525. @deffn {Scheme Procedure} set-symbol-property! sym prop val
  4526. In @var{sym}'s property list, set the value for property @var{prop} to
  4527. @var{val}, or add a new entry for @var{prop}, with value @var{val}, if
  4528. none already exists. For the structure of the property list, see
  4529. @code{symbol-property}.
  4530. @end deffn
  4531. @deffn {Scheme Procedure} symbol-property-remove! sym prop
  4532. From @var{sym}'s property list, remove the entry for property
  4533. @var{prop}, if there is one. For the structure of the property list,
  4534. see @code{symbol-property}.
  4535. @end deffn
  4536. Support for these extra slots may be removed in a future release, and it
  4537. is probably better to avoid using them. For a more modern and Schemely
  4538. approach to properties, see @ref{Object Properties}.
  4539. @node Symbol Read Syntax
  4540. @subsubsection Extended Read Syntax for Symbols
  4541. The read syntax for a symbol is a sequence of letters, digits, and
  4542. @dfn{extended alphabetic characters}, beginning with a character that
  4543. cannot begin a number. In addition, the special cases of @code{+},
  4544. @code{-}, and @code{...} are read as symbols even though numbers can
  4545. begin with @code{+}, @code{-} or @code{.}.
  4546. Extended alphabetic characters may be used within identifiers as if
  4547. they were letters. The set of extended alphabetic characters is:
  4548. @example
  4549. ! $ % & * + - . / : < = > ? @@ ^ _ ~
  4550. @end example
  4551. In addition to the standard read syntax defined above (which is taken
  4552. from R5RS (@pxref{Formal syntax,,,r5rs,The Revised^5 Report on
  4553. Scheme})), Guile provides an extended symbol read syntax that allows the
  4554. inclusion of unusual characters such as space characters, newlines and
  4555. parentheses. If (for whatever reason) you need to write a symbol
  4556. containing characters not mentioned above, you can do so as follows.
  4557. @itemize @bullet
  4558. @item
  4559. Begin the symbol with the characters @code{#@{},
  4560. @item
  4561. write the characters of the symbol and
  4562. @item
  4563. finish the symbol with the characters @code{@}#}.
  4564. @end itemize
  4565. Here are a few examples of this form of read syntax. The first symbol
  4566. needs to use extended syntax because it contains a space character, the
  4567. second because it contains a line break, and the last because it looks
  4568. like a number.
  4569. @lisp
  4570. #@{foo bar@}#
  4571. #@{what
  4572. ever@}#
  4573. #@{4242@}#
  4574. @end lisp
  4575. Although Guile provides this extended read syntax for symbols,
  4576. widespread usage of it is discouraged because it is not portable and not
  4577. very readable.
  4578. @node Symbol Uninterned
  4579. @subsubsection Uninterned Symbols
  4580. What makes symbols useful is that they are automatically kept unique.
  4581. There are no two symbols that are distinct objects but have the same
  4582. name. But of course, there is no rule without exception. In addition
  4583. to the normal symbols that have been discussed up to now, you can also
  4584. create special @dfn{uninterned} symbols that behave slightly
  4585. differently.
  4586. To understand what is different about them and why they might be useful,
  4587. we look at how normal symbols are actually kept unique.
  4588. Whenever Guile wants to find the symbol with a specific name, for
  4589. example during @code{read} or when executing @code{string->symbol}, it
  4590. first looks into a table of all existing symbols to find out whether a
  4591. symbol with the given name already exists. When this is the case, Guile
  4592. just returns that symbol. When not, a new symbol with the name is
  4593. created and entered into the table so that it can be found later.
  4594. Sometimes you might want to create a symbol that is guaranteed `fresh',
  4595. i.e.@: a symbol that did not exist previously. You might also want to
  4596. somehow guarantee that no one else will ever unintentionally stumble
  4597. across your symbol in the future. These properties of a symbol are
  4598. often needed when generating code during macro expansion. When
  4599. introducing new temporary variables, you want to guarantee that they
  4600. don't conflict with variables in other people's code.
  4601. The simplest way to arrange for this is to create a new symbol but
  4602. not enter it into the global table of all symbols. That way, no one
  4603. will ever get access to your symbol by chance. Symbols that are not in
  4604. the table are called @dfn{uninterned}. Of course, symbols that
  4605. @emph{are} in the table are called @dfn{interned}.
  4606. You create new uninterned symbols with the function @code{make-symbol}.
  4607. You can test whether a symbol is interned or not with
  4608. @code{symbol-interned?}.
  4609. Uninterned symbols break the rule that the name of a symbol uniquely
  4610. identifies the symbol object. Because of this, they can not be written
  4611. out and read back in like interned symbols. Currently, Guile has no
  4612. support for reading uninterned symbols. Note that the function
  4613. @code{gensym} does not return uninterned symbols for this reason.
  4614. @deffn {Scheme Procedure} make-symbol name
  4615. @deffnx {C Function} scm_make_symbol (name)
  4616. Return a new uninterned symbol with the name @var{name}. The returned
  4617. symbol is guaranteed to be unique and future calls to
  4618. @code{string->symbol} will not return it.
  4619. @end deffn
  4620. @deffn {Scheme Procedure} symbol-interned? symbol
  4621. @deffnx {C Function} scm_symbol_interned_p (symbol)
  4622. Return @code{#t} if @var{symbol} is interned, otherwise return
  4623. @code{#f}.
  4624. @end deffn
  4625. For example:
  4626. @lisp
  4627. (define foo-1 (string->symbol "foo"))
  4628. (define foo-2 (string->symbol "foo"))
  4629. (define foo-3 (make-symbol "foo"))
  4630. (define foo-4 (make-symbol "foo"))
  4631. (eq? foo-1 foo-2)
  4632. @result{} #t
  4633. ; Two interned symbols with the same name are the same object,
  4634. (eq? foo-1 foo-3)
  4635. @result{} #f
  4636. ; but a call to make-symbol with the same name returns a
  4637. ; distinct object.
  4638. (eq? foo-3 foo-4)
  4639. @result{} #f
  4640. ; A call to make-symbol always returns a new object, even for
  4641. ; the same name.
  4642. foo-3
  4643. @result{} #<uninterned-symbol foo 8085290>
  4644. ; Uninterned symbols print differently from interned symbols,
  4645. (symbol? foo-3)
  4646. @result{} #t
  4647. ; but they are still symbols,
  4648. (symbol-interned? foo-3)
  4649. @result{} #f
  4650. ; just not interned.
  4651. @end lisp
  4652. @node Keywords
  4653. @subsection Keywords
  4654. @tpindex Keywords
  4655. Keywords are self-evaluating objects with a convenient read syntax that
  4656. makes them easy to type.
  4657. Guile's keyword support conforms to R5RS, and adds a (switchable) read
  4658. syntax extension to permit keywords to begin with @code{:} as well as
  4659. @code{#:}, or to end with @code{:}.
  4660. @menu
  4661. * Why Use Keywords?:: Motivation for keyword usage.
  4662. * Coding With Keywords:: How to use keywords.
  4663. * Keyword Read Syntax:: Read syntax for keywords.
  4664. * Keyword Procedures:: Procedures for dealing with keywords.
  4665. @end menu
  4666. @node Why Use Keywords?
  4667. @subsubsection Why Use Keywords?
  4668. Keywords are useful in contexts where a program or procedure wants to be
  4669. able to accept a large number of optional arguments without making its
  4670. interface unmanageable.
  4671. To illustrate this, consider a hypothetical @code{make-window}
  4672. procedure, which creates a new window on the screen for drawing into
  4673. using some graphical toolkit. There are many parameters that the caller
  4674. might like to specify, but which could also be sensibly defaulted, for
  4675. example:
  4676. @itemize @bullet
  4677. @item
  4678. color depth -- Default: the color depth for the screen
  4679. @item
  4680. background color -- Default: white
  4681. @item
  4682. width -- Default: 600
  4683. @item
  4684. height -- Default: 400
  4685. @end itemize
  4686. If @code{make-window} did not use keywords, the caller would have to
  4687. pass in a value for each possible argument, remembering the correct
  4688. argument order and using a special value to indicate the default value
  4689. for that argument:
  4690. @lisp
  4691. (make-window 'default ;; Color depth
  4692. 'default ;; Background color
  4693. 800 ;; Width
  4694. 100 ;; Height
  4695. @dots{}) ;; More make-window arguments
  4696. @end lisp
  4697. With keywords, on the other hand, defaulted arguments are omitted, and
  4698. non-default arguments are clearly tagged by the appropriate keyword. As
  4699. a result, the invocation becomes much clearer:
  4700. @lisp
  4701. (make-window #:width 800 #:height 100)
  4702. @end lisp
  4703. On the other hand, for a simpler procedure with few arguments, the use
  4704. of keywords would be a hindrance rather than a help. The primitive
  4705. procedure @code{cons}, for example, would not be improved if it had to
  4706. be invoked as
  4707. @lisp
  4708. (cons #:car x #:cdr y)
  4709. @end lisp
  4710. So the decision whether to use keywords or not is purely pragmatic: use
  4711. them if they will clarify the procedure invocation at point of call.
  4712. @node Coding With Keywords
  4713. @subsubsection Coding With Keywords
  4714. If a procedure wants to support keywords, it should take a rest argument
  4715. and then use whatever means is convenient to extract keywords and their
  4716. corresponding arguments from the contents of that rest argument.
  4717. The following example illustrates the principle: the code for
  4718. @code{make-window} uses a helper procedure called
  4719. @code{get-keyword-value} to extract individual keyword arguments from
  4720. the rest argument.
  4721. @lisp
  4722. (define (get-keyword-value args keyword default)
  4723. (let ((kv (memq keyword args)))
  4724. (if (and kv (>= (length kv) 2))
  4725. (cadr kv)
  4726. default)))
  4727. (define (make-window . args)
  4728. (let ((depth (get-keyword-value args #:depth screen-depth))
  4729. (bg (get-keyword-value args #:bg "white"))
  4730. (width (get-keyword-value args #:width 800))
  4731. (height (get-keyword-value args #:height 100))
  4732. @dots{})
  4733. @dots{}))
  4734. @end lisp
  4735. But you don't need to write @code{get-keyword-value}. The @code{(ice-9
  4736. optargs)} module provides a set of powerful macros that you can use to
  4737. implement keyword-supporting procedures like this:
  4738. @lisp
  4739. (use-modules (ice-9 optargs))
  4740. (define (make-window . args)
  4741. (let-keywords args #f ((depth screen-depth)
  4742. (bg "white")
  4743. (width 800)
  4744. (height 100))
  4745. ...))
  4746. @end lisp
  4747. @noindent
  4748. Or, even more economically, like this:
  4749. @lisp
  4750. (use-modules (ice-9 optargs))
  4751. (define* (make-window #:key (depth screen-depth)
  4752. (bg "white")
  4753. (width 800)
  4754. (height 100))
  4755. ...)
  4756. @end lisp
  4757. For further details on @code{let-keywords}, @code{define*} and other
  4758. facilities provided by the @code{(ice-9 optargs)} module, see
  4759. @ref{Optional Arguments}.
  4760. To handle keyword arguments from procedures implemented in C,
  4761. use @code{scm_c_bind_keyword_arguments} (@pxref{Keyword Procedures}).
  4762. @node Keyword Read Syntax
  4763. @subsubsection Keyword Read Syntax
  4764. Guile, by default, only recognizes a keyword syntax that is compatible
  4765. with R5RS. A token of the form @code{#:NAME}, where @code{NAME} has the
  4766. same syntax as a Scheme symbol (@pxref{Symbol Read Syntax}), is the
  4767. external representation of the keyword named @code{NAME}. Keyword
  4768. objects print using this syntax as well, so values containing keyword
  4769. objects can be read back into Guile. When used in an expression,
  4770. keywords are self-quoting objects.
  4771. If the @code{keyword} read option is set to @code{'prefix}, Guile also
  4772. recognizes the alternative read syntax @code{:NAME}. Otherwise, tokens
  4773. of the form @code{:NAME} are read as symbols, as required by R5RS.
  4774. @cindex SRFI-88 keyword syntax
  4775. If the @code{keyword} read option is set to @code{'postfix}, Guile
  4776. recognizes the SRFI-88 read syntax @code{NAME:} (@pxref{SRFI-88}).
  4777. Otherwise, tokens of this form are read as symbols.
  4778. To enable and disable the alternative non-R5RS keyword syntax, you use
  4779. the @code{read-set!} procedure documented @ref{Scheme Read}. Note that
  4780. the @code{prefix} and @code{postfix} syntax are mutually exclusive.
  4781. @lisp
  4782. (read-set! keywords 'prefix)
  4783. #:type
  4784. @result{}
  4785. #:type
  4786. :type
  4787. @result{}
  4788. #:type
  4789. (read-set! keywords 'postfix)
  4790. type:
  4791. @result{}
  4792. #:type
  4793. :type
  4794. @result{}
  4795. :type
  4796. (read-set! keywords #f)
  4797. #:type
  4798. @result{}
  4799. #:type
  4800. :type
  4801. @print{}
  4802. ERROR: In expression :type:
  4803. ERROR: Unbound variable: :type
  4804. ABORT: (unbound-variable)
  4805. @end lisp
  4806. @node Keyword Procedures
  4807. @subsubsection Keyword Procedures
  4808. @deffn {Scheme Procedure} keyword? obj
  4809. @deffnx {C Function} scm_keyword_p (obj)
  4810. Return @code{#t} if the argument @var{obj} is a keyword, else
  4811. @code{#f}.
  4812. @end deffn
  4813. @deffn {Scheme Procedure} keyword->symbol keyword
  4814. @deffnx {C Function} scm_keyword_to_symbol (keyword)
  4815. Return the symbol with the same name as @var{keyword}.
  4816. @end deffn
  4817. @deffn {Scheme Procedure} symbol->keyword symbol
  4818. @deffnx {C Function} scm_symbol_to_keyword (symbol)
  4819. Return the keyword with the same name as @var{symbol}.
  4820. @end deffn
  4821. @deftypefn {C Function} int scm_is_keyword (SCM obj)
  4822. Equivalent to @code{scm_is_true (scm_keyword_p (@var{obj}))}.
  4823. @end deftypefn
  4824. @deftypefn {C Function} SCM scm_from_locale_keyword (const char *name)
  4825. @deftypefnx {C Function} SCM scm_from_locale_keywordn (const char *name, size_t len)
  4826. Equivalent to @code{scm_symbol_to_keyword (scm_from_locale_symbol
  4827. (@var{name}))} and @code{scm_symbol_to_keyword (scm_from_locale_symboln
  4828. (@var{name}, @var{len}))}, respectively.
  4829. Note that these functions should @emph{not} be used when @var{name} is a
  4830. C string constant, because there is no guarantee that the current locale
  4831. will match that of the execution character set, used for string and
  4832. character constants. Most modern C compilers use UTF-8 by default, so
  4833. in such cases we recommend @code{scm_from_utf8_keyword}.
  4834. @end deftypefn
  4835. @deftypefn {C Function} SCM scm_from_latin1_keyword (const char *name)
  4836. @deftypefnx {C Function} SCM scm_from_utf8_keyword (const char *name)
  4837. Equivalent to @code{scm_symbol_to_keyword (scm_from_latin1_symbol
  4838. (@var{name}))} and @code{scm_symbol_to_keyword (scm_from_utf8_symbol
  4839. (@var{name}))}, respectively.
  4840. @end deftypefn
  4841. @deftypefn {C Function} void scm_c_bind_keyword_arguments (const char *subr, @
  4842. SCM rest, scm_t_keyword_arguments_flags flags, @
  4843. SCM keyword1, SCM *argp1, @
  4844. @dots{}, @
  4845. SCM keywordN, SCM *argpN, @
  4846. @nicode{SCM_UNDEFINED})
  4847. Extract the specified keyword arguments from @var{rest}, which is not
  4848. modified. If the keyword argument @var{keyword1} is present in
  4849. @var{rest} with an associated value, that value is stored in the
  4850. variable pointed to by @var{argp1}, otherwise the variable is left
  4851. unchanged. Similarly for the other keywords and argument pointers up to
  4852. @var{keywordN} and @var{argpN}. The argument list to
  4853. @code{scm_c_bind_keyword_arguments} must be terminated by
  4854. @code{SCM_UNDEFINED}.
  4855. Note that since the variables pointed to by @var{argp1} through
  4856. @var{argpN} are left unchanged if the associated keyword argument is not
  4857. present, they should be initialized to their default values before
  4858. calling @code{scm_c_bind_keyword_arguments}. Alternatively, you can
  4859. initialize them to @code{SCM_UNDEFINED} before the call, and then use
  4860. @code{SCM_UNBNDP} after the call to see which ones were provided.
  4861. If an unrecognized keyword argument is present in @var{rest} and
  4862. @var{flags} does not contain @code{SCM_ALLOW_OTHER_KEYS}, or if
  4863. non-keyword arguments are present and @var{flags} does not contain
  4864. @code{SCM_ALLOW_NON_KEYWORD_ARGUMENTS}, an exception is raised.
  4865. @var{subr} should be the name of the procedure receiving the keyword
  4866. arguments, for purposes of error reporting.
  4867. For example:
  4868. @example
  4869. SCM k_delimiter;
  4870. SCM k_grammar;
  4871. SCM sym_infix;
  4872. SCM my_string_join (SCM strings, SCM rest)
  4873. @{
  4874. SCM delimiter = SCM_UNDEFINED;
  4875. SCM grammar = sym_infix;
  4876. scm_c_bind_keyword_arguments ("my-string-join", rest, 0,
  4877. k_delimiter, &delimiter,
  4878. k_grammar, &grammar,
  4879. SCM_UNDEFINED);
  4880. if (SCM_UNBNDP (delimiter))
  4881. delimiter = scm_from_utf8_string (" ");
  4882. return scm_string_join (strings, delimiter, grammar);
  4883. @}
  4884. void my_init ()
  4885. @{
  4886. k_delimiter = scm_from_utf8_keyword ("delimiter");
  4887. k_grammar = scm_from_utf8_keyword ("grammar");
  4888. sym_infix = scm_from_utf8_symbol ("infix");
  4889. scm_c_define_gsubr ("my-string-join", 1, 0, 1, my_string_join);
  4890. @}
  4891. @end example
  4892. @end deftypefn
  4893. @node Other Types
  4894. @subsection ``Functionality-Centric'' Data Types
  4895. Procedures and macros are documented in their own sections: see
  4896. @ref{Procedures} and @ref{Macros}.
  4897. Variable objects are documented as part of the description of Guile's
  4898. module system: see @ref{Variables}.
  4899. Asyncs, dynamic roots and fluids are described in the section on
  4900. scheduling: see @ref{Scheduling}.
  4901. Hooks are documented in the section on general utility functions: see
  4902. @ref{Hooks}.
  4903. Ports are described in the section on I/O: see @ref{Input and Output}.
  4904. Regular expressions are described in their own section: see @ref{Regular
  4905. Expressions}.
  4906. @c Local Variables:
  4907. @c TeX-master: "guile.texi"
  4908. @c End: