api-data.texi 206 KB

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