api-data.texi 370 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000-2004, 2006-2017, 2019-2020, 2022-2023
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Data Types
  7. @section Data Types
  8. Guile's data types form a powerful built-in library of representations
  9. and functionality that you can apply to your problem domain. This
  10. chapter surveys the data types built-in to Guile, from the simple to the
  11. complex.
  12. @menu
  13. * Booleans:: True/false values.
  14. * Numbers:: Numerical data types.
  15. * Characters:: Single characters.
  16. * Character Sets:: Sets of characters.
  17. * Strings:: Sequences of characters.
  18. * Symbols:: Symbols.
  19. * Keywords:: Self-quoting, customizable display keywords.
  20. * Pairs:: Scheme's basic building block.
  21. * Lists:: Special list functions supported by Guile.
  22. * Vectors:: One-dimensional arrays of Scheme objects.
  23. * Bit Vectors:: Vectors of bits.
  24. * Bytevectors:: Sequences of bytes.
  25. * Arrays:: Multidimensional matrices.
  26. * VLists:: Vector-like lists.
  27. * Record Overview:: Walking through the maze of record APIs.
  28. * SRFI-9 Records:: The standard, recommended record API.
  29. * Records:: Guile's historical record API.
  30. * Structures:: Low-level record representation.
  31. * Dictionary Types:: About dictionary types in general.
  32. * Association Lists:: List-based dictionaries.
  33. * VHashes:: VList-based dictionaries.
  34. * Hash Tables:: Table-based dictionaries.
  35. * Other Types:: Other sections describe data types too.
  36. @end menu
  37. @node Booleans
  38. @subsection Booleans
  39. @tpindex Booleans
  40. The two boolean values are @code{#t} for true and @code{#f} for false.
  41. They can also be written as @code{#true} and @code{#false}, as per R7RS.
  42. Boolean values are returned by predicate procedures, such as the general
  43. equality predicates @code{eq?}, @code{eqv?} and @code{equal?}
  44. (@pxref{Equality}) and numerical and string comparison operators like
  45. @code{string=?} (@pxref{String Comparison}) and @code{<=}
  46. (@pxref{Comparison}).
  47. @lisp
  48. (<= 3 8)
  49. @result{} #t
  50. (<= 3 -3)
  51. @result{} #f
  52. (equal? "house" "houses")
  53. @result{} #f
  54. (eq? #f #f)
  55. @result{}
  56. #t
  57. @end lisp
  58. In test condition contexts like @code{if} and @code{cond}
  59. (@pxref{Conditionals}), where a group of subexpressions will be
  60. evaluated only if a @var{condition} expression evaluates to ``true'',
  61. ``true'' means any value at all except @code{#f}.
  62. @lisp
  63. (if #t "yes" "no")
  64. @result{} "yes"
  65. (if 0 "yes" "no")
  66. @result{} "yes"
  67. (if #f "yes" "no")
  68. @result{} "no"
  69. @end lisp
  70. A result of this asymmetry is that typical Scheme source code more often
  71. uses @code{#f} explicitly than @code{#t}: @code{#f} is necessary to
  72. represent an @code{if} or @code{cond} false value, whereas @code{#t} is
  73. not necessary to represent an @code{if} or @code{cond} true value.
  74. It is important to note that @code{#f} is @strong{not} equivalent to any
  75. other Scheme value. In particular, @code{#f} is not the same as the
  76. number 0 (like in C and C++), and not the same as the ``empty list''
  77. (like in some Lisp dialects).
  78. In C, the two Scheme boolean values are available as the two constants
  79. @code{SCM_BOOL_T} for @code{#t} and @code{SCM_BOOL_F} for @code{#f}.
  80. Care must be taken with the false value @code{SCM_BOOL_F}: it is not
  81. false when used in C conditionals. In order to test for it, use
  82. @code{scm_is_false} or @code{scm_is_true}.
  83. @rnindex not
  84. @deffn {Scheme Procedure} not x
  85. @deffnx {C Function} scm_not (x)
  86. Return @code{#t} if @var{x} is @code{#f}, else return @code{#f}.
  87. @end deffn
  88. @rnindex boolean?
  89. @deffn {Scheme Procedure} boolean? obj
  90. @deffnx {C Function} scm_boolean_p (obj)
  91. Return @code{#t} if @var{obj} is either @code{#t} or @code{#f}, else
  92. return @code{#f}.
  93. @end deffn
  94. @deftypevr {C Macro} SCM SCM_BOOL_T
  95. The @code{SCM} representation of the Scheme object @code{#t}.
  96. @end deftypevr
  97. @deftypevr {C Macro} SCM SCM_BOOL_F
  98. The @code{SCM} representation of the Scheme object @code{#f}.
  99. @end deftypevr
  100. @deftypefn {C Function} int scm_is_true (SCM obj)
  101. Return @code{0} if @var{obj} is @code{#f}, else return @code{1}.
  102. @end deftypefn
  103. @deftypefn {C Function} int scm_is_false (SCM obj)
  104. Return @code{1} if @var{obj} is @code{#f}, else return @code{0}.
  105. @end deftypefn
  106. @deftypefn {C Function} int scm_is_bool (SCM obj)
  107. Return @code{1} if @var{obj} is either @code{#t} or @code{#f}, else
  108. return @code{0}.
  109. @end deftypefn
  110. @deftypefn {C Function} SCM scm_from_bool (int val)
  111. Return @code{#f} if @var{val} is @code{0}, else return @code{#t}.
  112. @end deftypefn
  113. @deftypefn {C Function} int scm_to_bool (SCM val)
  114. Return @code{1} if @var{val} is @code{SCM_BOOL_T}, return @code{0}
  115. when @var{val} is @code{SCM_BOOL_F}, else signal a `wrong type' error.
  116. You should probably use @code{scm_is_true} instead of this function
  117. when you just want to test a @code{SCM} value for trueness.
  118. @end deftypefn
  119. @node Numbers
  120. @subsection Numerical data types
  121. @tpindex Numbers
  122. Guile supports a rich ``tower'' of numerical types --- integer,
  123. rational, real and complex --- and provides an extensive set of
  124. mathematical and scientific functions for operating on numerical
  125. data. This section of the manual documents those types and functions.
  126. You may also find it illuminating to read R5RS's presentation of numbers
  127. in Scheme, which is particularly clear and accessible: see
  128. @ref{Numbers,,,r5rs,R5RS}.
  129. @menu
  130. * Numerical Tower:: Scheme's numerical "tower".
  131. * Integers:: Whole numbers.
  132. * Reals and Rationals:: Real and rational numbers.
  133. * Complex Numbers:: Complex numbers.
  134. * Exactness:: Exactness and inexactness.
  135. * Number Syntax:: Read syntax for numerical data.
  136. * Integer Operations:: Operations on integer values.
  137. * Comparison:: Comparison predicates.
  138. * Conversion:: Converting numbers to and from strings.
  139. * Complex:: Complex number operations.
  140. * Arithmetic:: Arithmetic functions.
  141. * Scientific:: Scientific functions.
  142. * Bitwise Operations:: Logical AND, OR, NOT, and so on.
  143. * Random:: Random number generation.
  144. @end menu
  145. @node Numerical Tower
  146. @subsubsection Scheme's Numerical ``Tower''
  147. @rnindex number?
  148. Scheme's numerical ``tower'' consists of the following categories of
  149. numbers:
  150. @table @dfn
  151. @item integers
  152. Whole numbers, positive or negative; e.g.@: --5, 0, 18.
  153. @item rationals
  154. The set of numbers that can be expressed as @math{@var{p}/@var{q}}
  155. where @var{p} and @var{q} are integers; e.g.@: @math{9/16} works, but
  156. pi (an irrational number) doesn't. These include integers
  157. (@math{@var{n}/1}).
  158. @item real numbers
  159. The set of numbers that describes all possible positions along a
  160. one-dimensional line. This includes rationals as well as irrational
  161. numbers.
  162. @item complex numbers
  163. The set of numbers that describes all possible positions in a two
  164. dimensional space. This includes real as well as imaginary numbers
  165. (@math{@var{a}+@var{b}i}, where @var{a} is the @dfn{real part},
  166. @var{b} is the @dfn{imaginary part}, and @math{i} is the square root of
  167. @minus{}1.)
  168. @end table
  169. It is called a tower because each category ``sits on'' the one that
  170. follows it, in the sense that every integer is also a rational, every
  171. rational is also real, and every real number is also a complex number
  172. (but with zero imaginary part).
  173. In addition to the classification into integers, rationals, reals and
  174. complex numbers, Scheme also distinguishes between whether a number is
  175. represented exactly or not. For example, the result of
  176. @m{2\sin(\pi/4),2*sin(pi/4)} is exactly @m{\sqrt{2},2^(1/2)}, but Guile
  177. can represent neither @m{\pi/4,pi/4} nor @m{\sqrt{2},2^(1/2)} exactly.
  178. Instead, it stores an inexact approximation, using the C type
  179. @code{double}.
  180. Guile can represent exact rationals of any magnitude, inexact
  181. rationals that fit into a C @code{double}, and inexact complex numbers
  182. with @code{double} real and imaginary parts.
  183. The @code{number?} predicate may be applied to any Scheme value to
  184. discover whether the value is any of the supported numerical types.
  185. @deffn {Scheme Procedure} number? obj
  186. @deffnx {C Function} scm_number_p (obj)
  187. Return @code{#t} if @var{obj} is any kind of number, else @code{#f}.
  188. @end deffn
  189. For example:
  190. @lisp
  191. (number? 3)
  192. @result{} #t
  193. (number? "hello there!")
  194. @result{} #f
  195. (define pi 3.141592654)
  196. (number? pi)
  197. @result{} #t
  198. @end lisp
  199. @deftypefn {C Function} int scm_is_number (SCM obj)
  200. This is equivalent to @code{scm_is_true (scm_number_p (obj))}.
  201. @end deftypefn
  202. The next few subsections document each of Guile's numerical data types
  203. in detail.
  204. @node Integers
  205. @subsubsection Integers
  206. @tpindex Integer numbers
  207. @rnindex integer?
  208. Integers are whole numbers, that is numbers with no fractional part,
  209. such as 2, 83, and @minus{}3789.
  210. Integers in Guile can be arbitrarily big, as shown by the following
  211. example.
  212. @lisp
  213. (define (factorial n)
  214. (let loop ((n n) (product 1))
  215. (if (= n 0)
  216. product
  217. (loop (- n 1) (* product n)))))
  218. (factorial 3)
  219. @result{} 6
  220. (factorial 20)
  221. @result{} 2432902008176640000
  222. (- (factorial 45))
  223. @result{} -119622220865480194561963161495657715064383733760000000000
  224. @end lisp
  225. Readers whose background is in programming languages where integers are
  226. limited by the need to fit into just 4 or 8 bytes of memory may find
  227. this surprising, or suspect that Guile's representation of integers is
  228. inefficient. In fact, Guile achieves a near optimal balance of
  229. convenience and efficiency by using the host computer's native
  230. representation of integers where possible, and a more general
  231. representation where the required number does not fit in the native
  232. form. Conversion between these two representations is automatic and
  233. completely invisible to the Scheme level programmer.
  234. C has a host of different integer types, and Guile offers a host of
  235. functions to convert between them and the @code{SCM} representation.
  236. For example, a C @code{int} can be handled with @code{scm_to_int} and
  237. @code{scm_from_int}. Guile also defines a few C integer types of its
  238. own, to help with differences between systems.
  239. C integer types that are not covered can be handled with the generic
  240. @code{scm_to_signed_integer} and @code{scm_from_signed_integer} for
  241. signed types, or with @code{scm_to_unsigned_integer} and
  242. @code{scm_from_unsigned_integer} for unsigned types.
  243. Scheme integers can be exact and inexact. For example, a number
  244. written as @code{3.0} with an explicit decimal-point is inexact, but
  245. it is also an integer. The functions @code{integer?} and
  246. @code{scm_is_integer} report true for such a number, but the functions
  247. @code{exact-integer?}, @code{scm_is_exact_integer},
  248. @code{scm_is_signed_integer}, and @code{scm_is_unsigned_integer} only
  249. allow exact integers and thus report false. Likewise, the conversion
  250. functions like @code{scm_to_signed_integer} only accept exact
  251. integers.
  252. The motivation for this behavior is that the inexactness of a number
  253. should not be lost silently. If you want to allow inexact integers,
  254. you can explicitly insert a call to @code{inexact->exact} or to its C
  255. equivalent @code{scm_inexact_to_exact}. (Only inexact integers will
  256. be converted by this call into exact integers; inexact non-integers
  257. will become exact fractions.)
  258. @deffn {Scheme Procedure} integer? x
  259. @deffnx {C Function} scm_integer_p (x)
  260. Return @code{#t} if @var{x} is an exact or inexact integer number, else
  261. return @code{#f}.
  262. @lisp
  263. (integer? 487)
  264. @result{} #t
  265. (integer? 3.0)
  266. @result{} #t
  267. (integer? -3.4)
  268. @result{} #f
  269. (integer? +inf.0)
  270. @result{} #f
  271. @end lisp
  272. @end deffn
  273. @deftypefn {C Function} int scm_is_integer (SCM x)
  274. This is equivalent to @code{scm_is_true (scm_integer_p (x))}.
  275. @end deftypefn
  276. @deffn {Scheme Procedure} exact-integer? x
  277. @deffnx {C Function} scm_exact_integer_p (x)
  278. Return @code{#t} if @var{x} is an exact integer number, else
  279. return @code{#f}.
  280. @lisp
  281. (exact-integer? 37)
  282. @result{} #t
  283. (exact-integer? 3.0)
  284. @result{} #f
  285. @end lisp
  286. @end deffn
  287. @deftypefn {C Function} int scm_is_exact_integer (SCM x)
  288. This is equivalent to @code{scm_is_true (scm_exact_integer_p (x))}.
  289. @end deftypefn
  290. @defvr {C Type} scm_t_int8
  291. @defvrx {C Type} scm_t_uint8
  292. @defvrx {C Type} scm_t_int16
  293. @defvrx {C Type} scm_t_uint16
  294. @defvrx {C Type} scm_t_int32
  295. @defvrx {C Type} scm_t_uint32
  296. @defvrx {C Type} scm_t_int64
  297. @defvrx {C Type} scm_t_uint64
  298. @defvrx {C Type} scm_t_intmax
  299. @defvrx {C Type} scm_t_uintmax
  300. The C types are equivalent to the corresponding ISO C types but are
  301. defined on all platforms, with the exception of @code{scm_t_int64} and
  302. @code{scm_t_uint64}, which are only defined when a 64-bit type is
  303. available. For example, @code{scm_t_int8} is equivalent to
  304. @code{int8_t}.
  305. You can regard these definitions as a stop-gap measure until all
  306. platforms provide these types. If you know that all the platforms
  307. that you are interested in already provide these types, it is better
  308. to use them directly instead of the types provided by Guile.
  309. @end defvr
  310. @deftypefn {C Function} int scm_is_signed_integer (SCM x, scm_t_intmax min, scm_t_intmax max)
  311. @deftypefnx {C Function} int scm_is_unsigned_integer (SCM x, scm_t_uintmax min, scm_t_uintmax max)
  312. Return @code{1} when @var{x} represents an exact integer that is
  313. between @var{min} and @var{max}, inclusive.
  314. These functions can be used to check whether a @code{SCM} value will
  315. fit into a given range, such as the range of a given C integer type.
  316. If you just want to convert a @code{SCM} value to a given C integer
  317. type, use one of the conversion functions directly.
  318. @end deftypefn
  319. @deftypefn {C Function} scm_t_intmax scm_to_signed_integer (SCM x, scm_t_intmax min, scm_t_intmax max)
  320. @deftypefnx {C Function} scm_t_uintmax scm_to_unsigned_integer (SCM x, scm_t_uintmax min, scm_t_uintmax max)
  321. When @var{x} represents an exact integer that is between @var{min} and
  322. @var{max} inclusive, return that integer. Else signal an error,
  323. either a `wrong-type' error when @var{x} is not an exact integer, or
  324. an `out-of-range' error when it doesn't fit the given range.
  325. @end deftypefn
  326. @deftypefn {C Function} SCM scm_from_signed_integer (scm_t_intmax x)
  327. @deftypefnx {C Function} SCM scm_from_unsigned_integer (scm_t_uintmax x)
  328. Return the @code{SCM} value that represents the integer @var{x}. This
  329. function will always succeed and will always return an exact number.
  330. @end deftypefn
  331. @deftypefn {C Function} char scm_to_char (SCM x)
  332. @deftypefnx {C Function} {signed char} scm_to_schar (SCM x)
  333. @deftypefnx {C Function} {unsigned char} scm_to_uchar (SCM x)
  334. @deftypefnx {C Function} short scm_to_short (SCM x)
  335. @deftypefnx {C Function} {unsigned short} scm_to_ushort (SCM x)
  336. @deftypefnx {C Function} int scm_to_int (SCM x)
  337. @deftypefnx {C Function} {unsigned int} scm_to_uint (SCM x)
  338. @deftypefnx {C Function} long scm_to_long (SCM x)
  339. @deftypefnx {C Function} {unsigned long} scm_to_ulong (SCM x)
  340. @deftypefnx {C Function} {long long} scm_to_long_long (SCM x)
  341. @deftypefnx {C Function} {unsigned long long} scm_to_ulong_long (SCM x)
  342. @deftypefnx {C Function} size_t scm_to_size_t (SCM x)
  343. @deftypefnx {C Function} ssize_t scm_to_ssize_t (SCM x)
  344. @deftypefnx {C Function} scm_t_uintptr scm_to_uintptr_t (SCM x)
  345. @deftypefnx {C Function} scm_t_ptrdiff scm_to_ptrdiff_t (SCM x)
  346. @deftypefnx {C Function} scm_t_int8 scm_to_int8 (SCM x)
  347. @deftypefnx {C Function} scm_t_uint8 scm_to_uint8 (SCM x)
  348. @deftypefnx {C Function} scm_t_int16 scm_to_int16 (SCM x)
  349. @deftypefnx {C Function} scm_t_uint16 scm_to_uint16 (SCM x)
  350. @deftypefnx {C Function} scm_t_int32 scm_to_int32 (SCM x)
  351. @deftypefnx {C Function} scm_t_uint32 scm_to_uint32 (SCM x)
  352. @deftypefnx {C Function} scm_t_int64 scm_to_int64 (SCM x)
  353. @deftypefnx {C Function} scm_t_uint64 scm_to_uint64 (SCM x)
  354. @deftypefnx {C Function} scm_t_intmax scm_to_intmax (SCM x)
  355. @deftypefnx {C Function} scm_t_uintmax scm_to_uintmax (SCM x)
  356. @deftypefnx {C Function} scm_t_intptr scm_to_intptr_t (SCM x)
  357. @deftypefnx {C Function} scm_t_uintptr scm_to_uintptr_t (SCM x)
  358. When @var{x} represents an exact integer that fits into the indicated
  359. C type, return that integer. Else signal an error, either a
  360. `wrong-type' error when @var{x} is not an exact integer, or an
  361. `out-of-range' error when it doesn't fit the given range.
  362. The functions @code{scm_to_long_long}, @code{scm_to_ulong_long},
  363. @code{scm_to_int64}, and @code{scm_to_uint64} are only available when
  364. the corresponding types are.
  365. @end deftypefn
  366. @deftypefn {C Function} SCM scm_from_char (char x)
  367. @deftypefnx {C Function} SCM scm_from_schar (signed char x)
  368. @deftypefnx {C Function} SCM scm_from_uchar (unsigned char x)
  369. @deftypefnx {C Function} SCM scm_from_short (short x)
  370. @deftypefnx {C Function} SCM scm_from_ushort (unsigned short x)
  371. @deftypefnx {C Function} SCM scm_from_int (int x)
  372. @deftypefnx {C Function} SCM scm_from_uint (unsigned int x)
  373. @deftypefnx {C Function} SCM scm_from_long (long x)
  374. @deftypefnx {C Function} SCM scm_from_ulong (unsigned long x)
  375. @deftypefnx {C Function} SCM scm_from_long_long (long long x)
  376. @deftypefnx {C Function} SCM scm_from_ulong_long (unsigned long long x)
  377. @deftypefnx {C Function} SCM scm_from_size_t (size_t x)
  378. @deftypefnx {C Function} SCM scm_from_ssize_t (ssize_t x)
  379. @deftypefnx {C Function} SCM scm_from_uintptr_t (uintptr_t x)
  380. @deftypefnx {C Function} SCM scm_from_ptrdiff_t (scm_t_ptrdiff x)
  381. @deftypefnx {C Function} SCM scm_from_int8 (scm_t_int8 x)
  382. @deftypefnx {C Function} SCM scm_from_uint8 (scm_t_uint8 x)
  383. @deftypefnx {C Function} SCM scm_from_int16 (scm_t_int16 x)
  384. @deftypefnx {C Function} SCM scm_from_uint16 (scm_t_uint16 x)
  385. @deftypefnx {C Function} SCM scm_from_int32 (scm_t_int32 x)
  386. @deftypefnx {C Function} SCM scm_from_uint32 (scm_t_uint32 x)
  387. @deftypefnx {C Function} SCM scm_from_int64 (scm_t_int64 x)
  388. @deftypefnx {C Function} SCM scm_from_uint64 (scm_t_uint64 x)
  389. @deftypefnx {C Function} SCM scm_from_intmax (scm_t_intmax x)
  390. @deftypefnx {C Function} SCM scm_from_uintmax (scm_t_uintmax x)
  391. @deftypefnx {C Function} SCM scm_from_intptr_t (scm_t_intptr x)
  392. @deftypefnx {C Function} SCM scm_from_uintptr_t (scm_t_uintptr x)
  393. Return the @code{SCM} value that represents the integer @var{x}.
  394. These functions will always succeed and will always return an exact
  395. number.
  396. @end deftypefn
  397. @deftypefn {C Function} void scm_to_mpz (SCM val, mpz_t rop)
  398. Assign @var{val} to the multiple precision integer @var{rop}.
  399. @var{val} must be an exact integer, otherwise an error will be
  400. signaled. @var{rop} must have been initialized with @code{mpz_init}
  401. before this function is called. When @var{rop} is no longer needed
  402. the occupied space must be freed with @code{mpz_clear}.
  403. @xref{Initializing Integers,,, gmp, GNU MP Manual}, for details.
  404. @end deftypefn
  405. @deftypefn {C Function} SCM scm_from_mpz (mpz_t val)
  406. Return the @code{SCM} value that represents @var{val}.
  407. @end deftypefn
  408. @node Reals and Rationals
  409. @subsubsection Real and Rational Numbers
  410. @tpindex Real numbers
  411. @tpindex Rational numbers
  412. @rnindex real?
  413. @rnindex rational?
  414. Mathematically, the real numbers are the set of numbers that describe
  415. all possible points along a continuous, infinite, one-dimensional line.
  416. The rational numbers are the set of all numbers that can be written as
  417. fractions @var{p}/@var{q}, where @var{p} and @var{q} are integers.
  418. All rational numbers are also real, but there are real numbers that
  419. are not rational, for example @m{\sqrt{2}, the square root of 2}, and
  420. @m{\pi,pi}.
  421. Guile can represent both exact and inexact rational numbers, but it
  422. cannot represent precise finite irrational numbers. Exact rationals are
  423. represented by storing the numerator and denominator as two exact
  424. integers. Inexact rationals are stored as floating point numbers using
  425. the C type @code{double}.
  426. Exact rationals are written as a fraction of integers. There must be
  427. no whitespace around the slash:
  428. @lisp
  429. 1/2
  430. -22/7
  431. @end lisp
  432. Even though the actual encoding of inexact rationals is in binary, it
  433. may be helpful to think of it as a decimal number with a limited
  434. number of significant figures and a decimal point somewhere, since
  435. this corresponds to the standard notation for non-whole numbers. For
  436. example:
  437. @lisp
  438. 0.34
  439. -0.00000142857931198
  440. -5648394822220000000000.0
  441. 4.0
  442. @end lisp
  443. The limited precision of Guile's encoding means that any finite ``real''
  444. number in Guile can be written in a rational form, by multiplying and
  445. then dividing by sufficient powers of 10 (or in fact, 2). For example,
  446. @samp{-0.00000142857931198} is the same as @minus{}142857931198 divided
  447. by 100000000000000000. In Guile's current incarnation, therefore, the
  448. @code{rational?} and @code{real?} predicates are equivalent for finite
  449. numbers.
  450. Dividing by an exact zero leads to a error message, as one might expect.
  451. However, dividing by an inexact zero does not produce an error.
  452. Instead, the result of the division is either plus or minus infinity,
  453. depending on the sign of the divided number and the sign of the zero
  454. divisor (some platforms support signed zeroes @samp{-0.0} and
  455. @samp{+0.0}; @samp{0.0} is the same as @samp{+0.0}).
  456. Dividing zero by an inexact zero yields a @acronym{NaN} (`not a number')
  457. value, although they are actually considered numbers by Scheme.
  458. Attempts to compare a @acronym{NaN} value with any number (including
  459. itself) using @code{=}, @code{<}, @code{>}, @code{<=} or @code{>=}
  460. always returns @code{#f}. Although a @acronym{NaN} value is not
  461. @code{=} to itself, it is both @code{eqv?} and @code{equal?} to itself
  462. and other @acronym{NaN} values. However, the preferred way to test for
  463. them is by using @code{nan?}.
  464. The real @acronym{NaN} values and infinities are written @samp{+nan.0},
  465. @samp{+inf.0} and @samp{-inf.0}. This syntax is also recognized by
  466. @code{read} as an extension to the usual Scheme syntax. These special
  467. values are considered by Scheme to be inexact real numbers but not
  468. rational. Note that non-real complex numbers may also contain
  469. infinities or @acronym{NaN} values in their real or imaginary parts. To
  470. test a real number to see if it is infinite, a @acronym{NaN} value, or
  471. neither, use @code{inf?}, @code{nan?}, or @code{finite?}, respectively.
  472. Every real number in Scheme belongs to precisely one of those three
  473. classes.
  474. On platforms that follow @acronym{IEEE} 754 for their floating point
  475. arithmetic, the @samp{+inf.0}, @samp{-inf.0}, and @samp{+nan.0} values
  476. are implemented using the corresponding @acronym{IEEE} 754 values.
  477. They behave in arithmetic operations like @acronym{IEEE} 754 describes
  478. it, i.e., @code{(= +nan.0 +nan.0)} @result{} @code{#f}.
  479. @deffn {Scheme Procedure} real? obj
  480. @deffnx {C Function} scm_real_p (obj)
  481. Return @code{#t} if @var{obj} is a real number, else @code{#f}. Note
  482. that the sets of integer and rational values form subsets of the set
  483. of real numbers, so the predicate will also be fulfilled if @var{obj}
  484. is an integer number or a rational number.
  485. @end deffn
  486. @deffn {Scheme Procedure} rational? x
  487. @deffnx {C Function} scm_rational_p (x)
  488. Return @code{#t} if @var{x} is a rational number, @code{#f} otherwise.
  489. Note that the set of integer values forms a subset of the set of
  490. rational numbers, i.e.@: the predicate will also be fulfilled if
  491. @var{x} is an integer number.
  492. @end deffn
  493. @deffn {Scheme Procedure} rationalize x eps
  494. @deffnx {C Function} scm_rationalize (x, eps)
  495. Returns the @emph{simplest} rational number differing
  496. from @var{x} by no more than @var{eps}.
  497. As required by @acronym{R5RS}, @code{rationalize} only returns an
  498. exact result when both its arguments are exact. Thus, you might need
  499. to use @code{inexact->exact} on the arguments.
  500. @lisp
  501. (rationalize (inexact->exact 1.2) 1/100)
  502. @result{} 6/5
  503. @end lisp
  504. @end deffn
  505. @deffn {Scheme Procedure} inf? x
  506. @deffnx {C Function} scm_inf_p (x)
  507. Return @code{#t} if the real number @var{x} is @samp{+inf.0} or
  508. @samp{-inf.0}. Otherwise return @code{#f}.
  509. @end deffn
  510. @deffn {Scheme Procedure} nan? x
  511. @deffnx {C Function} scm_nan_p (x)
  512. Return @code{#t} if the real number @var{x} is @samp{+nan.0}, or
  513. @code{#f} otherwise.
  514. @end deffn
  515. @deffn {Scheme Procedure} finite? x
  516. @deffnx {C Function} scm_finite_p (x)
  517. Return @code{#t} if the real number @var{x} is neither infinite nor a
  518. NaN, @code{#f} otherwise.
  519. @end deffn
  520. @deffn {Scheme Procedure} nan
  521. @deffnx {C Function} scm_nan ()
  522. Return @samp{+nan.0}, a @acronym{NaN} value.
  523. @end deffn
  524. @deffn {Scheme Procedure} inf
  525. @deffnx {C Function} scm_inf ()
  526. Return @samp{+inf.0}, positive infinity.
  527. @end deffn
  528. @deffn {Scheme Procedure} numerator x
  529. @deffnx {C Function} scm_numerator (x)
  530. Return the numerator of the rational number @var{x}.
  531. @end deffn
  532. @deffn {Scheme Procedure} denominator x
  533. @deffnx {C Function} scm_denominator (x)
  534. Return the denominator of the rational number @var{x}.
  535. @end deffn
  536. @deftypefn {C Function} int scm_is_real (SCM val)
  537. @deftypefnx {C Function} int scm_is_rational (SCM val)
  538. Equivalent to @code{scm_is_true (scm_real_p (val))} and
  539. @code{scm_is_true (scm_rational_p (val))}, respectively.
  540. @end deftypefn
  541. @deftypefn {C Function} double scm_to_double (SCM val)
  542. Returns the number closest to @var{val} that is representable as a
  543. @code{double}. Returns infinity for a @var{val} that is too large in
  544. magnitude. The argument @var{val} must be a real number.
  545. @end deftypefn
  546. @deftypefn {C Function} SCM scm_from_double (double val)
  547. Return the @code{SCM} value that represents @var{val}. The returned
  548. value is inexact according to the predicate @code{inexact?}, but it
  549. will be exactly equal to @var{val}.
  550. @end deftypefn
  551. @node Complex Numbers
  552. @subsubsection Complex Numbers
  553. @tpindex Complex numbers
  554. @rnindex complex?
  555. Complex numbers are the set of numbers that describe all possible points
  556. in a two-dimensional space. The two coordinates of a particular point
  557. in this space are known as the @dfn{real} and @dfn{imaginary} parts of
  558. the complex number that describes that point.
  559. In Guile, complex numbers are written in rectangular form as the sum of
  560. their real and imaginary parts, using the symbol @code{i} to indicate
  561. the imaginary part.
  562. @lisp
  563. 3+4i
  564. @result{}
  565. 3.0+4.0i
  566. (* 3-8i 2.3+0.3i)
  567. @result{}
  568. 9.3-17.5i
  569. @end lisp
  570. @cindex polar form
  571. @noindent
  572. Polar form can also be used, with an @samp{@@} between magnitude and
  573. angle,
  574. @lisp
  575. 1@@3.141592 @result{} -1.0 (approx)
  576. -1@@1.57079 @result{} 0.0-1.0i (approx)
  577. @end lisp
  578. Guile represents a complex number as a pair of inexact reals, so the
  579. real and imaginary parts of a complex number have the same properties of
  580. inexactness and limited precision as single inexact real numbers.
  581. Note that each part of a complex number may contain any inexact real
  582. value, including the special values @samp{+nan.0}, @samp{+inf.0} and
  583. @samp{-inf.0}, as well as either of the signed zeroes @samp{0.0} or
  584. @samp{-0.0}.
  585. @deffn {Scheme Procedure} complex? z
  586. @deffnx {C Function} scm_complex_p (z)
  587. Return @code{#t} if @var{z} is a complex number, @code{#f}
  588. otherwise. Note that the sets of real, rational and integer
  589. values form subsets of the set of complex numbers, i.e.@: the
  590. predicate will also be fulfilled if @var{z} is a real,
  591. rational or integer number.
  592. @end deffn
  593. @deftypefn {C Function} int scm_is_complex (SCM val)
  594. Equivalent to @code{scm_is_true (scm_complex_p (val))}.
  595. @end deftypefn
  596. @node Exactness
  597. @subsubsection Exact and Inexact Numbers
  598. @tpindex Exact numbers
  599. @tpindex Inexact numbers
  600. @rnindex exact?
  601. @rnindex inexact?
  602. @rnindex exact->inexact
  603. @rnindex inexact->exact
  604. R5RS requires that, with few exceptions, a calculation involving inexact
  605. numbers always produces an inexact result. To meet this requirement,
  606. Guile distinguishes between an exact integer value such as @samp{5} and
  607. the corresponding inexact integer value which, to the limited precision
  608. available, has no fractional part, and is printed as @samp{5.0}. Guile
  609. will only convert the latter value to the former when forced to do so by
  610. an invocation of the @code{inexact->exact} procedure.
  611. The only exception to the above requirement is when the values of the
  612. inexact numbers do not affect the result. For example @code{(expt n 0)}
  613. is @samp{1} for any value of @code{n}, therefore @code{(expt 5.0 0)} is
  614. permitted to return an exact @samp{1}.
  615. @deffn {Scheme Procedure} exact? z
  616. @deffnx {C Function} scm_exact_p (z)
  617. Return @code{#t} if the number @var{z} is exact, @code{#f}
  618. otherwise.
  619. @lisp
  620. (exact? 2)
  621. @result{} #t
  622. (exact? 0.5)
  623. @result{} #f
  624. (exact? (/ 2))
  625. @result{} #t
  626. @end lisp
  627. @end deffn
  628. @deftypefn {C Function} int scm_is_exact (SCM z)
  629. Return a @code{1} if the number @var{z} is exact, and @code{0}
  630. otherwise. This is equivalent to @code{scm_is_true (scm_exact_p (z))}.
  631. An alternate approch to testing the exactness of a number is to
  632. use @code{scm_is_signed_integer} or @code{scm_is_unsigned_integer}.
  633. @end deftypefn
  634. @deffn {Scheme Procedure} inexact? z
  635. @deffnx {C Function} scm_inexact_p (z)
  636. Return @code{#t} if the number @var{z} is inexact, @code{#f}
  637. else.
  638. @end deffn
  639. @deftypefn {C Function} int scm_is_inexact (SCM z)
  640. Return a @code{1} if the number @var{z} is inexact, and @code{0}
  641. otherwise. This is equivalent to @code{scm_is_true (scm_inexact_p (z))}.
  642. @end deftypefn
  643. @deffn {Scheme Procedure} inexact->exact z
  644. @deffnx {C Function} scm_inexact_to_exact (z)
  645. Return an exact number that is numerically closest to @var{z}, when
  646. there is one. For inexact rationals, Guile returns the exact rational
  647. that is numerically equal to the inexact rational. Inexact complex
  648. numbers with a non-zero imaginary part can not be made exact.
  649. @lisp
  650. (inexact->exact 0.5)
  651. @result{} 1/2
  652. @end lisp
  653. The following happens because 12/10 is not exactly representable as a
  654. @code{double} (on most platforms). However, when reading a decimal
  655. number that has been marked exact with the ``#e'' prefix, Guile is
  656. able to represent it correctly.
  657. @lisp
  658. (inexact->exact 1.2)
  659. @result{} 5404319552844595/4503599627370496
  660. #e1.2
  661. @result{} 6/5
  662. @end lisp
  663. @end deffn
  664. @c begin (texi-doc-string "guile" "exact->inexact")
  665. @deffn {Scheme Procedure} exact->inexact z
  666. @deffnx {C Function} scm_exact_to_inexact (z)
  667. Convert the number @var{z} to its inexact representation.
  668. @end deffn
  669. @node Number Syntax
  670. @subsubsection Read Syntax for Numerical Data
  671. The read syntax for integers is a string of digits, optionally
  672. preceded by a minus or plus character, a code indicating the
  673. base in which the integer is encoded, and a code indicating whether
  674. the number is exact or inexact. The supported base codes are:
  675. @table @code
  676. @item #b
  677. @itemx #B
  678. the integer is written in binary (base 2)
  679. @item #o
  680. @itemx #O
  681. the integer is written in octal (base 8)
  682. @item #d
  683. @itemx #D
  684. the integer is written in decimal (base 10)
  685. @item #x
  686. @itemx #X
  687. the integer is written in hexadecimal (base 16)
  688. @end table
  689. If the base code is omitted, the integer is assumed to be decimal. The
  690. following examples show how these base codes are used.
  691. @lisp
  692. -13
  693. @result{} -13
  694. #d-13
  695. @result{} -13
  696. #x-13
  697. @result{} -19
  698. #b+1101
  699. @result{} 13
  700. #o377
  701. @result{} 255
  702. @end lisp
  703. The codes for indicating exactness (which can, incidentally, be applied
  704. to all numerical values) are:
  705. @table @code
  706. @item #e
  707. @itemx #E
  708. the number is exact
  709. @item #i
  710. @itemx #I
  711. the number is inexact.
  712. @end table
  713. If the exactness indicator is omitted, the number is exact unless it
  714. contains a radix point. Since Guile can not represent exact complex
  715. numbers, an error is signaled when asking for them.
  716. @lisp
  717. (exact? 1.2)
  718. @result{} #f
  719. (exact? #e1.2)
  720. @result{} #t
  721. (exact? #e+1i)
  722. ERROR: Wrong type argument
  723. @end lisp
  724. Guile also understands the syntax @samp{+inf.0} and @samp{-inf.0} for
  725. plus and minus infinity, respectively. The value must be written
  726. exactly as shown, that is, they always must have a sign and exactly
  727. one zero digit after the decimal point. It also understands
  728. @samp{+nan.0} and @samp{-nan.0} for the special `not-a-number' value.
  729. The sign is ignored for `not-a-number' and the value is always printed
  730. as @samp{+nan.0}.
  731. @node Integer Operations
  732. @subsubsection Operations on Integer Values
  733. @rnindex odd?
  734. @rnindex even?
  735. @rnindex quotient
  736. @rnindex remainder
  737. @rnindex modulo
  738. @rnindex gcd
  739. @rnindex lcm
  740. @deffn {Scheme Procedure} odd? n
  741. @deffnx {C Function} scm_odd_p (n)
  742. Return @code{#t} if @var{n} is an odd number, @code{#f}
  743. otherwise.
  744. @end deffn
  745. @deffn {Scheme Procedure} even? n
  746. @deffnx {C Function} scm_even_p (n)
  747. Return @code{#t} if @var{n} is an even number, @code{#f}
  748. otherwise.
  749. @end deffn
  750. @c begin (texi-doc-string "guile" "quotient")
  751. @c begin (texi-doc-string "guile" "remainder")
  752. @deffn {Scheme Procedure} quotient n d
  753. @deffnx {Scheme Procedure} remainder n d
  754. @deffnx {C Function} scm_quotient (n, d)
  755. @deffnx {C Function} scm_remainder (n, d)
  756. Return the quotient or remainder from @var{n} divided by @var{d}. The
  757. quotient is rounded towards zero, and the remainder will have the same
  758. sign as @var{n}. In all cases quotient and remainder satisfy
  759. @math{@var{n} = @var{q}*@var{d} + @var{r}}.
  760. @lisp
  761. (remainder 13 4) @result{} 1
  762. (remainder -13 4) @result{} -1
  763. @end lisp
  764. See also @code{truncate-quotient}, @code{truncate-remainder} and
  765. related operations in @ref{Arithmetic}.
  766. @end deffn
  767. @c begin (texi-doc-string "guile" "modulo")
  768. @deffn {Scheme Procedure} modulo n d
  769. @deffnx {C Function} scm_modulo (n, d)
  770. Return the remainder from @var{n} divided by @var{d}, with the same
  771. sign as @var{d}.
  772. @lisp
  773. (modulo 13 4) @result{} 1
  774. (modulo -13 4) @result{} 3
  775. (modulo 13 -4) @result{} -3
  776. (modulo -13 -4) @result{} -1
  777. @end lisp
  778. See also @code{floor-quotient}, @code{floor-remainder} and
  779. related operations in @ref{Arithmetic}.
  780. @end deffn
  781. @c begin (texi-doc-string "guile" "gcd")
  782. @deffn {Scheme Procedure} gcd x@dots{}
  783. @deffnx {C Function} scm_gcd (x, y)
  784. Return the greatest common divisor of all arguments.
  785. If called without arguments, 0 is returned.
  786. The C function @code{scm_gcd} always takes two arguments, while the
  787. Scheme function can take an arbitrary number.
  788. @end deffn
  789. @c begin (texi-doc-string "guile" "lcm")
  790. @deffn {Scheme Procedure} lcm x@dots{}
  791. @deffnx {C Function} scm_lcm (x, y)
  792. Return the least common multiple of the arguments.
  793. If called without arguments, 1 is returned.
  794. The C function @code{scm_lcm} always takes two arguments, while the
  795. Scheme function can take an arbitrary number.
  796. @end deffn
  797. @deffn {Scheme Procedure} modulo-expt n k m
  798. @deffnx {C Function} scm_modulo_expt (n, k, m)
  799. Return @var{n} raised to the integer exponent
  800. @var{k}, modulo @var{m}.
  801. @lisp
  802. (modulo-expt 2 3 5)
  803. @result{} 3
  804. @end lisp
  805. @end deffn
  806. @deftypefn {Scheme Procedure} {} exact-integer-sqrt @var{k}
  807. @deftypefnx {C Function} void scm_exact_integer_sqrt (SCM @var{k}, SCM *@var{s}, SCM *@var{r})
  808. Return two exact non-negative integers @var{s} and @var{r}
  809. such that @math{@var{k} = @var{s}^2 + @var{r}} and
  810. @math{@var{s}^2 <= @var{k} < (@var{s} + 1)^2}.
  811. An error is raised if @var{k} is not an exact non-negative integer.
  812. @lisp
  813. (exact-integer-sqrt 10) @result{} 3 and 1
  814. @end lisp
  815. @end deftypefn
  816. @node Comparison
  817. @subsubsection Comparison Predicates
  818. @rnindex zero?
  819. @rnindex positive?
  820. @rnindex negative?
  821. The C comparison functions below always takes two arguments, while the
  822. Scheme functions can take an arbitrary number. Also keep in mind that
  823. the C functions return one of the Scheme boolean values
  824. @code{SCM_BOOL_T} or @code{SCM_BOOL_F} which are both true as far as C
  825. is concerned. Thus, always write @code{scm_is_true (scm_num_eq_p (x,
  826. y))} when testing the two Scheme numbers @code{x} and @code{y} for
  827. equality, for example.
  828. @c begin (texi-doc-string "guile" "=")
  829. @deffn {Scheme Procedure} =
  830. @deffnx {C Function} scm_num_eq_p (x, y)
  831. Return @code{#t} if all parameters are numerically equal.
  832. @end deffn
  833. @c begin (texi-doc-string "guile" "<")
  834. @deffn {Scheme Procedure} <
  835. @deffnx {C Function} scm_less_p (x, y)
  836. Return @code{#t} if the list of parameters is monotonically
  837. increasing.
  838. @end deffn
  839. @c begin (texi-doc-string "guile" ">")
  840. @deffn {Scheme Procedure} >
  841. @deffnx {C Function} scm_gr_p (x, y)
  842. Return @code{#t} if the list of parameters is monotonically
  843. decreasing.
  844. @end deffn
  845. @c begin (texi-doc-string "guile" "<=")
  846. @deffn {Scheme Procedure} <=
  847. @deffnx {C Function} scm_leq_p (x, y)
  848. Return @code{#t} if the list of parameters is monotonically
  849. non-decreasing.
  850. @end deffn
  851. @c begin (texi-doc-string "guile" ">=")
  852. @deffn {Scheme Procedure} >=
  853. @deffnx {C Function} scm_geq_p (x, y)
  854. Return @code{#t} if the list of parameters is monotonically
  855. non-increasing.
  856. @end deffn
  857. @c begin (texi-doc-string "guile" "zero?")
  858. @deffn {Scheme Procedure} zero? z
  859. @deffnx {C Function} scm_zero_p (z)
  860. Return @code{#t} if @var{z} is an exact or inexact number equal to
  861. zero.
  862. @end deffn
  863. @c begin (texi-doc-string "guile" "positive?")
  864. @deffn {Scheme Procedure} positive? x
  865. @deffnx {C Function} scm_positive_p (x)
  866. Return @code{#t} if @var{x} is an exact or inexact number greater than
  867. zero.
  868. @end deffn
  869. @c begin (texi-doc-string "guile" "negative?")
  870. @deffn {Scheme Procedure} negative? x
  871. @deffnx {C Function} scm_negative_p (x)
  872. Return @code{#t} if @var{x} is an exact or inexact number less than
  873. zero.
  874. @end deffn
  875. @node Conversion
  876. @subsubsection Converting Numbers To and From Strings
  877. @rnindex number->string
  878. @rnindex string->number
  879. The following procedures read and write numbers according to their
  880. external representation as defined by R5RS (@pxref{Lexical structure,
  881. R5RS Lexical Structure,, r5rs, The Revised^5 Report on the Algorithmic
  882. Language Scheme}). @xref{Number Input and Output, the @code{(ice-9
  883. i18n)} module}, for locale-dependent number parsing.
  884. @deffn {Scheme Procedure} number->string n [radix]
  885. @deffnx {C Function} scm_number_to_string (n, radix)
  886. Return a string holding the external representation of the
  887. number @var{n} in the given @var{radix}. If @var{n} is
  888. inexact, a radix of 10 will be used.
  889. @end deffn
  890. @deffn {Scheme Procedure} string->number string [radix]
  891. @deffnx {C Function} scm_string_to_number (string, radix)
  892. Return a number of the maximally precise representation
  893. expressed by the given @var{string}. @var{radix} must be an
  894. exact integer, either 2, 8, 10, or 16. If supplied, @var{radix}
  895. is a default radix that may be overridden by an explicit radix
  896. prefix in @var{string} (e.g.@: "#o177"). If @var{radix} is not
  897. supplied, then the default radix is 10. If string is not a
  898. syntactically valid notation for a number, then
  899. @code{string->number} returns @code{#f}.
  900. @end deffn
  901. @deftypefn {C Function} SCM scm_c_locale_stringn_to_number (const char *string, size_t len, unsigned radix)
  902. As per @code{string->number} above, but taking a C string, as pointer
  903. and length. The string characters should be in the current locale
  904. encoding (@code{locale} in the name refers only to that, there's no
  905. locale-dependent parsing).
  906. @end deftypefn
  907. @node Complex
  908. @subsubsection Complex Number Operations
  909. @rnindex make-rectangular
  910. @rnindex make-polar
  911. @rnindex real-part
  912. @rnindex imag-part
  913. @rnindex magnitude
  914. @rnindex angle
  915. @deffn {Scheme Procedure} make-rectangular real_part imaginary_part
  916. @deffnx {C Function} scm_make_rectangular (real_part, imaginary_part)
  917. Return a complex number constructed of the given @var{real-part} and @var{imaginary-part} parts.
  918. @end deffn
  919. @deffn {Scheme Procedure} make-polar mag ang
  920. @deffnx {C Function} scm_make_polar (mag, ang)
  921. @cindex polar form
  922. Return the complex number @var{mag} * e^(i * @var{ang}).
  923. @end deffn
  924. @c begin (texi-doc-string "guile" "real-part")
  925. @deffn {Scheme Procedure} real-part z
  926. @deffnx {C Function} scm_real_part (z)
  927. Return the real part of the number @var{z}.
  928. @end deffn
  929. @c begin (texi-doc-string "guile" "imag-part")
  930. @deffn {Scheme Procedure} imag-part z
  931. @deffnx {C Function} scm_imag_part (z)
  932. Return the imaginary part of the number @var{z}.
  933. @end deffn
  934. @c begin (texi-doc-string "guile" "magnitude")
  935. @deffn {Scheme Procedure} magnitude z
  936. @deffnx {C Function} scm_magnitude (z)
  937. Return the magnitude of the number @var{z}. This is the same as
  938. @code{abs} for real arguments, but also allows complex numbers.
  939. @end deffn
  940. @c begin (texi-doc-string "guile" "angle")
  941. @deffn {Scheme Procedure} angle z
  942. @deffnx {C Function} scm_angle (z)
  943. Return the angle of the complex number @var{z}.
  944. @end deffn
  945. @deftypefn {C Function} SCM scm_c_make_rectangular (double re, double im)
  946. @deftypefnx {C Function} SCM scm_c_make_polar (double x, double y)
  947. Like @code{scm_make_rectangular} or @code{scm_make_polar},
  948. respectively, but these functions take @code{double}s as their
  949. arguments.
  950. @end deftypefn
  951. @deftypefn {C Function} double scm_c_real_part (z)
  952. @deftypefnx {C Function} double scm_c_imag_part (z)
  953. Returns the real or imaginary part of @var{z} as a @code{double}.
  954. @end deftypefn
  955. @deftypefn {C Function} double scm_c_magnitude (z)
  956. @deftypefnx {C Function} double scm_c_angle (z)
  957. Returns the magnitude or angle of @var{z} as a @code{double}.
  958. @end deftypefn
  959. @node Arithmetic
  960. @subsubsection Arithmetic Functions
  961. @rnindex max
  962. @rnindex min
  963. @rnindex +
  964. @rnindex *
  965. @rnindex -
  966. @rnindex /
  967. @findex 1+
  968. @findex 1-
  969. @rnindex abs
  970. @rnindex floor
  971. @rnindex ceiling
  972. @rnindex truncate
  973. @rnindex round
  974. @rnindex euclidean/
  975. @rnindex euclidean-quotient
  976. @rnindex euclidean-remainder
  977. @rnindex floor/
  978. @rnindex floor-quotient
  979. @rnindex floor-remainder
  980. @rnindex ceiling/
  981. @rnindex ceiling-quotient
  982. @rnindex ceiling-remainder
  983. @rnindex truncate/
  984. @rnindex truncate-quotient
  985. @rnindex truncate-remainder
  986. @rnindex centered/
  987. @rnindex centered-quotient
  988. @rnindex centered-remainder
  989. @rnindex round/
  990. @rnindex round-quotient
  991. @rnindex round-remainder
  992. The C arithmetic functions below always takes two arguments, while the
  993. Scheme functions can take an arbitrary number. When you need to
  994. invoke them with just one argument, for example to compute the
  995. equivalent of @code{(- x)}, pass @code{SCM_UNDEFINED} as the second
  996. one: @code{scm_difference (x, SCM_UNDEFINED)}.
  997. @c begin (texi-doc-string "guile" "+")
  998. @deffn {Scheme Procedure} + z1 @dots{}
  999. @deffnx {C Function} scm_sum (z1, z2)
  1000. Return the sum of all parameter values. Return 0 if called without any
  1001. parameters.
  1002. @end deffn
  1003. @c begin (texi-doc-string "guile" "-")
  1004. @deffn {Scheme Procedure} - z1 z2 @dots{}
  1005. @deffnx {C Function} scm_difference (z1, z2)
  1006. If called with one argument @var{z1}, -@var{z1} is returned. Otherwise
  1007. the sum of all but the first argument are subtracted from the first
  1008. argument.
  1009. @end deffn
  1010. @c begin (texi-doc-string "guile" "*")
  1011. @deffn {Scheme Procedure} * z1 @dots{}
  1012. @deffnx {C Function} scm_product (z1, z2)
  1013. Return the product of all arguments. If called without arguments, 1 is
  1014. returned.
  1015. @end deffn
  1016. @c begin (texi-doc-string "guile" "/")
  1017. @deffn {Scheme Procedure} / z1 z2 @dots{}
  1018. @deffnx {C Function} scm_divide (z1, z2)
  1019. Divide the first argument by the product of the remaining arguments. If
  1020. called with one argument @var{z1}, 1/@var{z1} is returned.
  1021. @end deffn
  1022. @deffn {Scheme Procedure} 1+ z
  1023. @deffnx {C Function} scm_oneplus (z)
  1024. Return @math{@var{z} + 1}.
  1025. @end deffn
  1026. @deffn {Scheme Procedure} 1- z
  1027. @deffnx {C function} scm_oneminus (z)
  1028. Return @math{@var{z} - 1}.
  1029. @end deffn
  1030. @c begin (texi-doc-string "guile" "abs")
  1031. @deffn {Scheme Procedure} abs x
  1032. @deffnx {C Function} scm_abs (x)
  1033. Return the absolute value of @var{x}.
  1034. @var{x} must be a number with zero imaginary part. To calculate the
  1035. magnitude of a complex number, use @code{magnitude} instead.
  1036. @end deffn
  1037. @c begin (texi-doc-string "guile" "max")
  1038. @deffn {Scheme Procedure} max x1 x2 @dots{}
  1039. @deffnx {C Function} scm_max (x1, x2)
  1040. Return the maximum of all parameter values.
  1041. @end deffn
  1042. @c begin (texi-doc-string "guile" "min")
  1043. @deffn {Scheme Procedure} min x1 x2 @dots{}
  1044. @deffnx {C Function} scm_min (x1, x2)
  1045. Return the minimum of all parameter values.
  1046. @end deffn
  1047. @c begin (texi-doc-string "guile" "truncate")
  1048. @deffn {Scheme Procedure} truncate x
  1049. @deffnx {C Function} scm_truncate_number (x)
  1050. Round the inexact number @var{x} towards zero.
  1051. @end deffn
  1052. @c begin (texi-doc-string "guile" "round")
  1053. @deffn {Scheme Procedure} round x
  1054. @deffnx {C Function} scm_round_number (x)
  1055. Round the inexact number @var{x} to the nearest integer. When exactly
  1056. halfway between two integers, round to the even one.
  1057. @end deffn
  1058. @c begin (texi-doc-string "guile" "floor")
  1059. @deffn {Scheme Procedure} floor x
  1060. @deffnx {C Function} scm_floor (x)
  1061. Round the number @var{x} towards minus infinity.
  1062. @end deffn
  1063. @c begin (texi-doc-string "guile" "ceiling")
  1064. @deffn {Scheme Procedure} ceiling x
  1065. @deffnx {C Function} scm_ceiling (x)
  1066. Round the number @var{x} towards infinity.
  1067. @end deffn
  1068. @deftypefn {C Function} double scm_c_truncate (double x)
  1069. @deftypefnx {C Function} double scm_c_round (double x)
  1070. Like @code{scm_truncate_number} or @code{scm_round_number},
  1071. respectively, but these functions take and return @code{double}
  1072. values.
  1073. @end deftypefn
  1074. @deftypefn {Scheme Procedure} {} euclidean/ @var{x} @var{y}
  1075. @deftypefnx {Scheme Procedure} {} euclidean-quotient @var{x} @var{y}
  1076. @deftypefnx {Scheme Procedure} {} euclidean-remainder @var{x} @var{y}
  1077. @deftypefnx {C Function} void scm_euclidean_divide (SCM @var{x}, SCM @var{y}, SCM *@var{q}, SCM *@var{r})
  1078. @deftypefnx {C Function} SCM scm_euclidean_quotient (SCM @var{x}, SCM @var{y})
  1079. @deftypefnx {C Function} SCM scm_euclidean_remainder (SCM @var{x}, SCM @var{y})
  1080. These procedures accept two real numbers @var{x} and @var{y}, where the
  1081. divisor @var{y} must be non-zero. @code{euclidean-quotient} returns the
  1082. integer @var{q} and @code{euclidean-remainder} returns the real number
  1083. @var{r} such that @math{@var{x} = @var{q}*@var{y} + @var{r}} and
  1084. @math{0 <= @var{r} < |@var{y}|}. @code{euclidean/} returns both @var{q} and
  1085. @var{r}, and is more efficient than computing each separately. Note
  1086. that when @math{@var{y} > 0}, @code{euclidean-quotient} returns
  1087. @math{floor(@var{x}/@var{y})}, otherwise it returns
  1088. @math{ceiling(@var{x}/@var{y})}.
  1089. Note that these operators are equivalent to the R6RS operators
  1090. @code{div}, @code{mod}, and @code{div-and-mod}.
  1091. @lisp
  1092. (euclidean-quotient 123 10) @result{} 12
  1093. (euclidean-remainder 123 10) @result{} 3
  1094. (euclidean/ 123 10) @result{} 12 and 3
  1095. (euclidean/ 123 -10) @result{} -12 and 3
  1096. (euclidean/ -123 10) @result{} -13 and 7
  1097. (euclidean/ -123 -10) @result{} 13 and 7
  1098. (euclidean/ -123.2 -63.5) @result{} 2.0 and 3.8
  1099. (euclidean/ 16/3 -10/7) @result{} -3 and 22/21
  1100. @end lisp
  1101. @end deftypefn
  1102. @deftypefn {Scheme Procedure} {} floor/ @var{x} @var{y}
  1103. @deftypefnx {Scheme Procedure} {} floor-quotient @var{x} @var{y}
  1104. @deftypefnx {Scheme Procedure} {} floor-remainder @var{x} @var{y}
  1105. @deftypefnx {C Function} void scm_floor_divide (SCM @var{x}, SCM @var{y}, SCM *@var{q}, SCM *@var{r})
  1106. @deftypefnx {C Function} SCM scm_floor_quotient (@var{x}, @var{y})
  1107. @deftypefnx {C Function} SCM scm_floor_remainder (@var{x}, @var{y})
  1108. These procedures accept two real numbers @var{x} and @var{y}, where the
  1109. divisor @var{y} must be non-zero. @code{floor-quotient} returns the
  1110. integer @var{q} and @code{floor-remainder} returns the real number
  1111. @var{r} such that @math{@var{q} = floor(@var{x}/@var{y})} and
  1112. @math{@var{x} = @var{q}*@var{y} + @var{r}}. @code{floor/} returns
  1113. both @var{q} and @var{r}, and is more efficient than computing each
  1114. separately. Note that @var{r}, if non-zero, will have the same sign
  1115. as @var{y}.
  1116. When @var{x} and @var{y} are integers, @code{floor-remainder} is
  1117. equivalent to the R5RS integer-only operator @code{modulo}.
  1118. @lisp
  1119. (floor-quotient 123 10) @result{} 12
  1120. (floor-remainder 123 10) @result{} 3
  1121. (floor/ 123 10) @result{} 12 and 3
  1122. (floor/ 123 -10) @result{} -13 and -7
  1123. (floor/ -123 10) @result{} -13 and 7
  1124. (floor/ -123 -10) @result{} 12 and -3
  1125. (floor/ -123.2 -63.5) @result{} 1.0 and -59.7
  1126. (floor/ 16/3 -10/7) @result{} -4 and -8/21
  1127. @end lisp
  1128. @end deftypefn
  1129. @deftypefn {Scheme Procedure} {} ceiling/ @var{x} @var{y}
  1130. @deftypefnx {Scheme Procedure} {} ceiling-quotient @var{x} @var{y}
  1131. @deftypefnx {Scheme Procedure} {} ceiling-remainder @var{x} @var{y}
  1132. @deftypefnx {C Function} void scm_ceiling_divide (SCM @var{x}, SCM @var{y}, SCM *@var{q}, SCM *@var{r})
  1133. @deftypefnx {C Function} SCM scm_ceiling_quotient (@var{x}, @var{y})
  1134. @deftypefnx {C Function} SCM scm_ceiling_remainder (@var{x}, @var{y})
  1135. These procedures accept two real numbers @var{x} and @var{y}, where the
  1136. divisor @var{y} must be non-zero. @code{ceiling-quotient} returns the
  1137. integer @var{q} and @code{ceiling-remainder} returns the real number
  1138. @var{r} such that @math{@var{q} = ceiling(@var{x}/@var{y})} and
  1139. @math{@var{x} = @var{q}*@var{y} + @var{r}}. @code{ceiling/} returns
  1140. both @var{q} and @var{r}, and is more efficient than computing each
  1141. separately. Note that @var{r}, if non-zero, will have the opposite sign
  1142. of @var{y}.
  1143. @lisp
  1144. (ceiling-quotient 123 10) @result{} 13
  1145. (ceiling-remainder 123 10) @result{} -7
  1146. (ceiling/ 123 10) @result{} 13 and -7
  1147. (ceiling/ 123 -10) @result{} -12 and 3
  1148. (ceiling/ -123 10) @result{} -12 and -3
  1149. (ceiling/ -123 -10) @result{} 13 and 7
  1150. (ceiling/ -123.2 -63.5) @result{} 2.0 and 3.8
  1151. (ceiling/ 16/3 -10/7) @result{} -3 and 22/21
  1152. @end lisp
  1153. @end deftypefn
  1154. @deftypefn {Scheme Procedure} {} truncate/ @var{x} @var{y}
  1155. @deftypefnx {Scheme Procedure} {} truncate-quotient @var{x} @var{y}
  1156. @deftypefnx {Scheme Procedure} {} truncate-remainder @var{x} @var{y}
  1157. @deftypefnx {C Function} void scm_truncate_divide (SCM @var{x}, SCM @var{y}, SCM *@var{q}, SCM *@var{r})
  1158. @deftypefnx {C Function} SCM scm_truncate_quotient (@var{x}, @var{y})
  1159. @deftypefnx {C Function} SCM scm_truncate_remainder (@var{x}, @var{y})
  1160. These procedures accept two real numbers @var{x} and @var{y}, where the
  1161. divisor @var{y} must be non-zero. @code{truncate-quotient} returns the
  1162. integer @var{q} and @code{truncate-remainder} returns the real number
  1163. @var{r} such that @var{q} is @math{@var{x}/@var{y}} rounded toward zero,
  1164. and @math{@var{x} = @var{q}*@var{y} + @var{r}}. @code{truncate/} returns
  1165. both @var{q} and @var{r}, and is more efficient than computing each
  1166. separately. Note that @var{r}, if non-zero, will have the same sign
  1167. as @var{x}.
  1168. When @var{x} and @var{y} are integers, these operators are
  1169. equivalent to the R5RS integer-only operators @code{quotient} and
  1170. @code{remainder}.
  1171. @lisp
  1172. (truncate-quotient 123 10) @result{} 12
  1173. (truncate-remainder 123 10) @result{} 3
  1174. (truncate/ 123 10) @result{} 12 and 3
  1175. (truncate/ 123 -10) @result{} -12 and 3
  1176. (truncate/ -123 10) @result{} -12 and -3
  1177. (truncate/ -123 -10) @result{} 12 and -3
  1178. (truncate/ -123.2 -63.5) @result{} 1.0 and -59.7
  1179. (truncate/ 16/3 -10/7) @result{} -3 and 22/21
  1180. @end lisp
  1181. @end deftypefn
  1182. @deftypefn {Scheme Procedure} {} centered/ @var{x} @var{y}
  1183. @deftypefnx {Scheme Procedure} {} centered-quotient @var{x} @var{y}
  1184. @deftypefnx {Scheme Procedure} {} centered-remainder @var{x} @var{y}
  1185. @deftypefnx {C Function} void scm_centered_divide (SCM @var{x}, SCM @var{y}, SCM *@var{q}, SCM *@var{r})
  1186. @deftypefnx {C Function} SCM scm_centered_quotient (SCM @var{x}, SCM @var{y})
  1187. @deftypefnx {C Function} SCM scm_centered_remainder (SCM @var{x}, SCM @var{y})
  1188. These procedures accept two real numbers @var{x} and @var{y}, where the
  1189. divisor @var{y} must be non-zero. @code{centered-quotient} returns the
  1190. integer @var{q} and @code{centered-remainder} returns the real number
  1191. @var{r} such that @math{@var{x} = @var{q}*@var{y} + @var{r}} and
  1192. @math{-|@var{y}/2| <= @var{r} < |@var{y}/2|}. @code{centered/}
  1193. returns both @var{q} and @var{r}, and is more efficient than computing
  1194. each separately.
  1195. Note that @code{centered-quotient} returns @math{@var{x}/@var{y}}
  1196. rounded to the nearest integer. When @math{@var{x}/@var{y}} lies
  1197. exactly half-way between two integers, the tie is broken according to
  1198. the sign of @var{y}. If @math{@var{y} > 0}, ties are rounded toward
  1199. positive infinity, otherwise they are rounded toward negative infinity.
  1200. This is a consequence of the requirement that
  1201. @math{-|@var{y}/2| <= @var{r} < |@var{y}/2|}.
  1202. Note that these operators are equivalent to the R6RS operators
  1203. @code{div0}, @code{mod0}, and @code{div0-and-mod0}.
  1204. @lisp
  1205. (centered-quotient 123 10) @result{} 12
  1206. (centered-remainder 123 10) @result{} 3
  1207. (centered/ 123 10) @result{} 12 and 3
  1208. (centered/ 123 -10) @result{} -12 and 3
  1209. (centered/ -123 10) @result{} -12 and -3
  1210. (centered/ -123 -10) @result{} 12 and -3
  1211. (centered/ 125 10) @result{} 13 and -5
  1212. (centered/ 127 10) @result{} 13 and -3
  1213. (centered/ 135 10) @result{} 14 and -5
  1214. (centered/ -123.2 -63.5) @result{} 2.0 and 3.8
  1215. (centered/ 16/3 -10/7) @result{} -4 and -8/21
  1216. @end lisp
  1217. @end deftypefn
  1218. @deftypefn {Scheme Procedure} {} round/ @var{x} @var{y}
  1219. @deftypefnx {Scheme Procedure} {} round-quotient @var{x} @var{y}
  1220. @deftypefnx {Scheme Procedure} {} round-remainder @var{x} @var{y}
  1221. @deftypefnx {C Function} void scm_round_divide (SCM @var{x}, SCM @var{y}, SCM *@var{q}, SCM *@var{r})
  1222. @deftypefnx {C Function} SCM scm_round_quotient (@var{x}, @var{y})
  1223. @deftypefnx {C Function} SCM scm_round_remainder (@var{x}, @var{y})
  1224. These procedures accept two real numbers @var{x} and @var{y}, where the
  1225. divisor @var{y} must be non-zero. @code{round-quotient} returns the
  1226. integer @var{q} and @code{round-remainder} returns the real number
  1227. @var{r} such that @math{@var{x} = @var{q}*@var{y} + @var{r}} and
  1228. @var{q} is @math{@var{x}/@var{y}} rounded to the nearest integer,
  1229. with ties going to the nearest even integer. @code{round/}
  1230. returns both @var{q} and @var{r}, and is more efficient than computing
  1231. each separately.
  1232. Note that @code{round/} and @code{centered/} are almost equivalent, but
  1233. their behavior differs when @math{@var{x}/@var{y}} lies exactly half-way
  1234. between two integers. In this case, @code{round/} chooses the nearest
  1235. even integer, whereas @code{centered/} chooses in such a way to satisfy
  1236. the constraint @math{-|@var{y}/2| <= @var{r} < |@var{y}/2|}, which
  1237. is stronger than the corresponding constraint for @code{round/},
  1238. @math{-|@var{y}/2| <= @var{r} <= |@var{y}/2|}. In particular,
  1239. when @var{x} and @var{y} are integers, the number of possible remainders
  1240. returned by @code{centered/} is @math{|@var{y}|}, whereas the number of
  1241. possible remainders returned by @code{round/} is @math{|@var{y}|+1} when
  1242. @var{y} is even.
  1243. @lisp
  1244. (round-quotient 123 10) @result{} 12
  1245. (round-remainder 123 10) @result{} 3
  1246. (round/ 123 10) @result{} 12 and 3
  1247. (round/ 123 -10) @result{} -12 and 3
  1248. (round/ -123 10) @result{} -12 and -3
  1249. (round/ -123 -10) @result{} 12 and -3
  1250. (round/ 125 10) @result{} 12 and 5
  1251. (round/ 127 10) @result{} 13 and -3
  1252. (round/ 135 10) @result{} 14 and -5
  1253. (round/ -123.2 -63.5) @result{} 2.0 and 3.8
  1254. (round/ 16/3 -10/7) @result{} -4 and -8/21
  1255. @end lisp
  1256. @end deftypefn
  1257. @node Scientific
  1258. @subsubsection Scientific Functions
  1259. The following procedures accept any kind of number as arguments,
  1260. including complex numbers.
  1261. @rnindex sqrt
  1262. @c begin (texi-doc-string "guile" "sqrt")
  1263. @deffn {Scheme Procedure} sqrt z
  1264. Return the square root of @var{z}. Of the two possible roots
  1265. (positive and negative), the one with a positive real part is
  1266. returned, or if that's zero then a positive imaginary part. Thus,
  1267. @example
  1268. (sqrt 9.0) @result{} 3.0
  1269. (sqrt -9.0) @result{} 0.0+3.0i
  1270. (sqrt 1.0+1.0i) @result{} 1.09868411346781+0.455089860562227i
  1271. (sqrt -1.0-1.0i) @result{} 0.455089860562227-1.09868411346781i
  1272. @end example
  1273. @end deffn
  1274. @rnindex expt
  1275. @c begin (texi-doc-string "guile" "expt")
  1276. @deffn {Scheme Procedure} expt z1 z2
  1277. Return @var{z1} raised to the power of @var{z2}.
  1278. @end deffn
  1279. @rnindex sin
  1280. @c begin (texi-doc-string "guile" "sin")
  1281. @deffn {Scheme Procedure} sin z
  1282. Return the sine of @var{z}.
  1283. @end deffn
  1284. @rnindex cos
  1285. @c begin (texi-doc-string "guile" "cos")
  1286. @deffn {Scheme Procedure} cos z
  1287. Return the cosine of @var{z}.
  1288. @end deffn
  1289. @rnindex tan
  1290. @c begin (texi-doc-string "guile" "tan")
  1291. @deffn {Scheme Procedure} tan z
  1292. Return the tangent of @var{z}.
  1293. @end deffn
  1294. @rnindex asin
  1295. @c begin (texi-doc-string "guile" "asin")
  1296. @deffn {Scheme Procedure} asin z
  1297. Return the arcsine of @var{z}.
  1298. @end deffn
  1299. @rnindex acos
  1300. @c begin (texi-doc-string "guile" "acos")
  1301. @deffn {Scheme Procedure} acos z
  1302. Return the arccosine of @var{z}.
  1303. @end deffn
  1304. @rnindex atan
  1305. @c begin (texi-doc-string "guile" "atan")
  1306. @deffn {Scheme Procedure} atan z
  1307. @deffnx {Scheme Procedure} atan y x
  1308. Return the arctangent of @var{z}, or of @math{@var{y}/@var{x}}.
  1309. @end deffn
  1310. @rnindex exp
  1311. @c begin (texi-doc-string "guile" "exp")
  1312. @deffn {Scheme Procedure} exp z
  1313. Return e to the power of @var{z}, where e is the base of natural
  1314. logarithms (2.71828@dots{}).
  1315. @end deffn
  1316. @rnindex log
  1317. @c begin (texi-doc-string "guile" "log")
  1318. @deffn {Scheme Procedure} log z
  1319. Return the natural logarithm of @var{z}.
  1320. @end deffn
  1321. @c begin (texi-doc-string "guile" "log10")
  1322. @deffn {Scheme Procedure} log10 z
  1323. Return the base 10 logarithm of @var{z}.
  1324. @end deffn
  1325. @c begin (texi-doc-string "guile" "sinh")
  1326. @deffn {Scheme Procedure} sinh z
  1327. Return the hyperbolic sine of @var{z}.
  1328. @end deffn
  1329. @c begin (texi-doc-string "guile" "cosh")
  1330. @deffn {Scheme Procedure} cosh z
  1331. Return the hyperbolic cosine of @var{z}.
  1332. @end deffn
  1333. @c begin (texi-doc-string "guile" "tanh")
  1334. @deffn {Scheme Procedure} tanh z
  1335. Return the hyperbolic tangent of @var{z}.
  1336. @end deffn
  1337. @c begin (texi-doc-string "guile" "asinh")
  1338. @deffn {Scheme Procedure} asinh z
  1339. Return the hyperbolic arcsine of @var{z}.
  1340. @end deffn
  1341. @c begin (texi-doc-string "guile" "acosh")
  1342. @deffn {Scheme Procedure} acosh z
  1343. Return the hyperbolic arccosine of @var{z}.
  1344. @end deffn
  1345. @c begin (texi-doc-string "guile" "atanh")
  1346. @deffn {Scheme Procedure} atanh z
  1347. Return the hyperbolic arctangent of @var{z}.
  1348. @end deffn
  1349. @node Bitwise Operations
  1350. @subsubsection Bitwise Operations
  1351. For the following bitwise functions, negative numbers are treated as
  1352. infinite precision twos-complements. For instance @math{-6} is bits
  1353. @math{@dots{}111010}, with infinitely many ones on the left. It can
  1354. be seen that adding 6 (binary 110) to such a bit pattern gives all
  1355. zeros.
  1356. @deffn {Scheme Procedure} logand n1 n2 @dots{}
  1357. @deffnx {C Function} scm_logand (n1, n2)
  1358. Return the bitwise @sc{and} of the integer arguments.
  1359. @lisp
  1360. (logand) @result{} -1
  1361. (logand 7) @result{} 7
  1362. (logand #b111 #b011 #b001) @result{} 1
  1363. @end lisp
  1364. @end deffn
  1365. @deffn {Scheme Procedure} logior n1 n2 @dots{}
  1366. @deffnx {C Function} scm_logior (n1, n2)
  1367. Return the bitwise @sc{or} of the integer arguments.
  1368. @lisp
  1369. (logior) @result{} 0
  1370. (logior 7) @result{} 7
  1371. (logior #b000 #b001 #b011) @result{} 3
  1372. @end lisp
  1373. @end deffn
  1374. @deffn {Scheme Procedure} logxor n1 n2 @dots{}
  1375. @deffnx {C Function} scm_loxor (n1, n2)
  1376. Return the bitwise @sc{xor} of the integer arguments. A bit is
  1377. set in the result if it is set in an odd number of arguments.
  1378. @lisp
  1379. (logxor) @result{} 0
  1380. (logxor 7) @result{} 7
  1381. (logxor #b000 #b001 #b011) @result{} 2
  1382. (logxor #b000 #b001 #b011 #b011) @result{} 1
  1383. @end lisp
  1384. @end deffn
  1385. @deffn {Scheme Procedure} lognot n
  1386. @deffnx {C Function} scm_lognot (n)
  1387. Return the integer which is the ones-complement of the integer
  1388. argument, ie.@: each 0 bit is changed to 1 and each 1 bit to 0.
  1389. @lisp
  1390. (number->string (lognot #b10000000) 2)
  1391. @result{} "-10000001"
  1392. (number->string (lognot #b0) 2)
  1393. @result{} "-1"
  1394. @end lisp
  1395. @end deffn
  1396. @deffn {Scheme Procedure} logtest j k
  1397. @deffnx {C Function} scm_logtest (j, k)
  1398. Test whether @var{j} and @var{k} have any 1 bits in common. This is
  1399. equivalent to @code{(not (zero? (logand j k)))}, but without actually
  1400. calculating the @code{logand}, just testing for non-zero.
  1401. @lisp
  1402. (logtest #b0100 #b1011) @result{} #f
  1403. (logtest #b0100 #b0111) @result{} #t
  1404. @end lisp
  1405. @end deffn
  1406. @deffn {Scheme Procedure} logbit? index j
  1407. @deffnx {C Function} scm_logbit_p (index, j)
  1408. Test whether bit number @var{index} in @var{j} is set. @var{index}
  1409. starts from 0 for the least significant bit.
  1410. @lisp
  1411. (logbit? 0 #b1101) @result{} #t
  1412. (logbit? 1 #b1101) @result{} #f
  1413. (logbit? 2 #b1101) @result{} #t
  1414. (logbit? 3 #b1101) @result{} #t
  1415. (logbit? 4 #b1101) @result{} #f
  1416. @end lisp
  1417. @end deffn
  1418. @deffn {Scheme Procedure} ash n count
  1419. @deffnx {C Function} scm_ash (n, count)
  1420. Return @math{floor(n * 2^{count})}.
  1421. @var{n} and @var{count} must be exact integers.
  1422. With @var{n} viewed as an infinite-precision twos-complement
  1423. integer, @code{ash} means a left shift introducing zero bits
  1424. when @var{count} is positive, or a right shift dropping bits
  1425. when @var{count} is negative. This is an ``arithmetic'' shift.
  1426. @lisp
  1427. (number->string (ash #b1 3) 2) @result{} "1000"
  1428. (number->string (ash #b1010 -1) 2) @result{} "101"
  1429. ;; -23 is bits ...11101001, -6 is bits ...111010
  1430. (ash -23 -2) @result{} -6
  1431. @end lisp
  1432. @end deffn
  1433. @deffn {Scheme Procedure} round-ash n count
  1434. @deffnx {C Function} scm_round_ash (n, count)
  1435. Return @math{round(n * 2^count)}.
  1436. @var{n} and @var{count} must be exact integers.
  1437. With @var{n} viewed as an infinite-precision twos-complement
  1438. integer, @code{round-ash} means a left shift introducing zero
  1439. bits when @var{count} is positive, or a right shift rounding
  1440. to the nearest integer (with ties going to the nearest even
  1441. integer) when @var{count} is negative. This is a rounded
  1442. ``arithmetic'' shift.
  1443. @lisp
  1444. (number->string (round-ash #b1 3) 2) @result{} \"1000\"
  1445. (number->string (round-ash #b1010 -1) 2) @result{} \"101\"
  1446. (number->string (round-ash #b1010 -2) 2) @result{} \"10\"
  1447. (number->string (round-ash #b1011 -2) 2) @result{} \"11\"
  1448. (number->string (round-ash #b1101 -2) 2) @result{} \"11\"
  1449. (number->string (round-ash #b1110 -2) 2) @result{} \"100\"
  1450. @end lisp
  1451. @end deffn
  1452. @deffn {Scheme Procedure} logcount n
  1453. @deffnx {C Function} scm_logcount (n)
  1454. Return the number of bits in integer @var{n}. If @var{n} is
  1455. positive, the 1-bits in its binary representation are counted.
  1456. If negative, the 0-bits in its two's-complement binary
  1457. representation are counted. If zero, 0 is returned.
  1458. @lisp
  1459. (logcount #b10101010)
  1460. @result{} 4
  1461. (logcount 0)
  1462. @result{} 0
  1463. (logcount -2)
  1464. @result{} 1
  1465. @end lisp
  1466. @end deffn
  1467. @deffn {Scheme Procedure} integer-length n
  1468. @deffnx {C Function} scm_integer_length (n)
  1469. Return the number of bits necessary to represent @var{n}.
  1470. For positive @var{n} this is how many bits to the most significant one
  1471. bit. For negative @var{n} it's how many bits to the most significant
  1472. zero bit in twos complement form.
  1473. @lisp
  1474. (integer-length #b10101010) @result{} 8
  1475. (integer-length #b1111) @result{} 4
  1476. (integer-length 0) @result{} 0
  1477. (integer-length -1) @result{} 0
  1478. (integer-length -256) @result{} 8
  1479. (integer-length -257) @result{} 9
  1480. @end lisp
  1481. @end deffn
  1482. @deffn {Scheme Procedure} integer-expt n k
  1483. @deffnx {C Function} scm_integer_expt (n, k)
  1484. Return @var{n} raised to the power @var{k}. @var{k} must be an exact
  1485. integer, @var{n} can be any number.
  1486. Negative @var{k} is supported, and results in @m{1/n^|k|, 1/n^abs(k)}
  1487. in the usual way. @math{@var{n}^0} is 1, as usual, and that includes
  1488. @math{0^0} is 1.
  1489. @lisp
  1490. (integer-expt 2 5) @result{} 32
  1491. (integer-expt -3 3) @result{} -27
  1492. (integer-expt 5 -3) @result{} 1/125
  1493. (integer-expt 0 0) @result{} 1
  1494. @end lisp
  1495. @end deffn
  1496. @deffn {Scheme Procedure} bit-extract n start end
  1497. @deffnx {C Function} scm_bit_extract (n, start, end)
  1498. Return the integer composed of the @var{start} (inclusive)
  1499. through @var{end} (exclusive) bits of @var{n}. The
  1500. @var{start}th bit becomes the 0-th bit in the result.
  1501. @lisp
  1502. (number->string (bit-extract #b1101101010 0 4) 2)
  1503. @result{} "1010"
  1504. (number->string (bit-extract #b1101101010 4 9) 2)
  1505. @result{} "10110"
  1506. @end lisp
  1507. @end deffn
  1508. @node Random
  1509. @subsubsection Random Number Generation
  1510. Pseudo-random numbers are generated from a random state object, which
  1511. can be created with @code{seed->random-state} or
  1512. @code{datum->random-state}. An external representation (i.e.@: one
  1513. which can written with @code{write} and read with @code{read}) of a
  1514. random state object can be obtained via
  1515. @code{random-state->datum}. The @var{state} parameter to the
  1516. various functions below is optional, it defaults to the state object
  1517. in the @code{*random-state*} variable.
  1518. @deffn {Scheme Procedure} copy-random-state [state]
  1519. @deffnx {C Function} scm_copy_random_state (state)
  1520. Return a copy of the random state @var{state}.
  1521. @end deffn
  1522. @deffn {Scheme Procedure} random n [state]
  1523. @deffnx {C Function} scm_random (n, state)
  1524. Return a number in [0, @var{n}).
  1525. Accepts a positive integer or real n and returns a
  1526. number of the same type between zero (inclusive) and
  1527. @var{n} (exclusive). The values returned have a uniform
  1528. distribution.
  1529. @end deffn
  1530. @deffn {Scheme Procedure} random:exp [state]
  1531. @deffnx {C Function} scm_random_exp (state)
  1532. Return an inexact real in an exponential distribution with mean
  1533. 1. For an exponential distribution with mean @var{u} use @code{(*
  1534. @var{u} (random:exp))}.
  1535. @end deffn
  1536. @deffn {Scheme Procedure} random:hollow-sphere! vect [state]
  1537. @deffnx {C Function} scm_random_hollow_sphere_x (vect, state)
  1538. Fills @var{vect} with inexact real random numbers the sum of whose
  1539. squares is equal to 1.0. Thinking of @var{vect} as coordinates in
  1540. space of dimension @var{n} @math{=} @code{(vector-length @var{vect})},
  1541. the coordinates are uniformly distributed over the surface of the unit
  1542. n-sphere.
  1543. @end deffn
  1544. @deffn {Scheme Procedure} random:normal [state]
  1545. @deffnx {C Function} scm_random_normal (state)
  1546. Return an inexact real in a normal distribution. The distribution
  1547. used has mean 0 and standard deviation 1. For a normal distribution
  1548. with mean @var{m} and standard deviation @var{d} use @code{(+ @var{m}
  1549. (* @var{d} (random:normal)))}.
  1550. @end deffn
  1551. @deffn {Scheme Procedure} random:normal-vector! vect [state]
  1552. @deffnx {C Function} scm_random_normal_vector_x (vect, state)
  1553. Fills @var{vect} with inexact real random numbers that are
  1554. independent and standard normally distributed
  1555. (i.e., with mean 0 and variance 1).
  1556. @end deffn
  1557. @deffn {Scheme Procedure} random:solid-sphere! vect [state]
  1558. @deffnx {C Function} scm_random_solid_sphere_x (vect, state)
  1559. Fills @var{vect} with inexact real random numbers the sum of whose
  1560. squares is less than 1.0. Thinking of @var{vect} as coordinates in
  1561. space of dimension @var{n} @math{=} @code{(vector-length @var{vect})},
  1562. the coordinates are uniformly distributed within the unit
  1563. @var{n}-sphere.
  1564. @c FIXME: What does this mean, particularly the n-sphere part?
  1565. @end deffn
  1566. @deffn {Scheme Procedure} random:uniform [state]
  1567. @deffnx {C Function} scm_random_uniform (state)
  1568. Return a uniformly distributed inexact real random number in
  1569. [0,1).
  1570. @end deffn
  1571. @deffn {Scheme Procedure} seed->random-state seed
  1572. @deffnx {C Function} scm_seed_to_random_state (seed)
  1573. Return a new random state using @var{seed}.
  1574. @end deffn
  1575. @deffn {Scheme Procedure} datum->random-state datum
  1576. @deffnx {C Function} scm_datum_to_random_state (datum)
  1577. Return a new random state from @var{datum}, which should have been
  1578. obtained by @code{random-state->datum}.
  1579. @end deffn
  1580. @deffn {Scheme Procedure} random-state->datum state
  1581. @deffnx {C Function} scm_random_state_to_datum (state)
  1582. Return a datum representation of @var{state} that may be written out and
  1583. read back with the Scheme reader.
  1584. @end deffn
  1585. @deffn {Scheme Procedure} random-state-from-platform
  1586. @deffnx {C Function} scm_random_state_from_platform ()
  1587. Construct a new random state seeded from a platform-specific source of
  1588. entropy, appropriate for use in non-security-critical applications.
  1589. Currently @file{/dev/urandom} is tried first, or else the seed is based
  1590. on the time, date, process ID, an address from a freshly allocated heap
  1591. cell, an address from the local stack frame, and a high-resolution timer
  1592. if available.
  1593. @end deffn
  1594. @defvar *random-state*
  1595. The global random state used by the above functions when the
  1596. @var{state} parameter is not given.
  1597. @end defvar
  1598. Note that the initial value of @code{*random-state*} is the same every
  1599. time Guile starts up. Therefore, if you don't pass a @var{state}
  1600. parameter to the above procedures, and you don't set
  1601. @code{*random-state*} to @code{(seed->random-state your-seed)}, where
  1602. @code{your-seed} is something that @emph{isn't} the same every time,
  1603. you'll get the same sequence of ``random'' numbers on every run.
  1604. For example, unless the relevant source code has changed, @code{(map
  1605. random (cdr (iota 30)))}, if the first use of random numbers since
  1606. Guile started up, will always give:
  1607. @lisp
  1608. (map random (cdr (iota 19)))
  1609. @result{}
  1610. (0 1 1 2 2 2 1 2 6 7 10 0 5 3 12 5 5 12)
  1611. @end lisp
  1612. To seed the random state in a sensible way for non-security-critical
  1613. applications, do this during initialization of your program:
  1614. @lisp
  1615. (set! *random-state* (random-state-from-platform))
  1616. @end lisp
  1617. @node Characters
  1618. @subsection Characters
  1619. @tpindex Characters
  1620. In Scheme, there is a data type to describe a single character.
  1621. Defining what exactly a character @emph{is} can be more complicated
  1622. than it seems. Guile follows the advice of R6RS and uses The Unicode
  1623. Standard to help define what a character is. So, for Guile, a
  1624. character is anything in the Unicode Character Database.
  1625. @cindex code point
  1626. @cindex Unicode code point
  1627. The Unicode Character Database is basically a table of characters
  1628. indexed using integers called 'code points'. Valid code points are in
  1629. the ranges 0 to @code{#xD7FF} inclusive or @code{#xE000} to
  1630. @code{#x10FFFF} inclusive, which is about 1.1 million code points.
  1631. @cindex designated code point
  1632. @cindex code point, designated
  1633. Any code point that has been assigned to a character or that has
  1634. otherwise been given a meaning by Unicode is called a 'designated code
  1635. point'. Most of the designated code points, about 200,000 of them,
  1636. indicate characters, accents or other combining marks that modify
  1637. other characters, symbols, whitespace, and control characters. Some
  1638. are not characters but indicators that suggest how to format or
  1639. display neighboring characters.
  1640. @cindex reserved code point
  1641. @cindex code point, reserved
  1642. If a code point is not a designated code point -- if it has not been
  1643. assigned to a character by The Unicode Standard -- it is a 'reserved
  1644. code point', meaning that they are reserved for future use. Most of
  1645. the code points, about 800,000, are 'reserved code points'.
  1646. By convention, a Unicode code point is written as
  1647. ``U+XXXX'' where ``XXXX'' is a hexadecimal number. Please note that
  1648. this convenient notation is not valid code. Guile does not interpret
  1649. ``U+XXXX'' as a character.
  1650. In Scheme, a character literal is written as @code{#\@var{name}} where
  1651. @var{name} is the name of the character that you want. Printable
  1652. characters have their usual single character name; for example,
  1653. @code{#\a} is a lower case @code{a}.
  1654. Some of the code points are 'combining characters' that are not meant
  1655. to be printed by themselves but are instead meant to modify the
  1656. appearance of the previous character. For combining characters, an
  1657. alternate form of the character literal is @code{#\} followed by
  1658. U+25CC (a small, dotted circle), followed by the combining character.
  1659. This allows the combining character to be drawn on the circle, not on
  1660. the backslash of @code{#\}.
  1661. Many of the non-printing characters, such as whitespace characters and
  1662. control characters, also have names.
  1663. The most commonly used non-printing characters have long character
  1664. names, described in the table below.
  1665. @multitable {@code{#\backspace}} {Preferred}
  1666. @item Character Name @tab Codepoint
  1667. @item @code{#\nul} @tab U+0000
  1668. @item @code{#\alarm} @tab U+0007
  1669. @item @code{#\backspace} @tab U+0008
  1670. @item @code{#\tab} @tab U+0009
  1671. @item @code{#\linefeed} @tab U+000A
  1672. @item @code{#\newline} @tab U+000A
  1673. @item @code{#\vtab} @tab U+000B
  1674. @item @code{#\page} @tab U+000C
  1675. @item @code{#\return} @tab U+000D
  1676. @item @code{#\esc} @tab U+001B
  1677. @item @code{#\space} @tab U+0020
  1678. @item @code{#\delete} @tab U+007F
  1679. @end multitable
  1680. There are also short names for all of the ``C0 control characters''
  1681. (those with code points below 32). The following table lists the short
  1682. name for each character.
  1683. @multitable @columnfractions .25 .25 .25 .25
  1684. @item 0 = @code{#\nul}
  1685. @tab 1 = @code{#\soh}
  1686. @tab 2 = @code{#\stx}
  1687. @tab 3 = @code{#\etx}
  1688. @item 4 = @code{#\eot}
  1689. @tab 5 = @code{#\enq}
  1690. @tab 6 = @code{#\ack}
  1691. @tab 7 = @code{#\bel}
  1692. @item 8 = @code{#\bs}
  1693. @tab 9 = @code{#\ht}
  1694. @tab 10 = @code{#\lf}
  1695. @tab 11 = @code{#\vt}
  1696. @item 12 = @code{#\ff}
  1697. @tab 13 = @code{#\cr}
  1698. @tab 14 = @code{#\so}
  1699. @tab 15 = @code{#\si}
  1700. @item 16 = @code{#\dle}
  1701. @tab 17 = @code{#\dc1}
  1702. @tab 18 = @code{#\dc2}
  1703. @tab 19 = @code{#\dc3}
  1704. @item 20 = @code{#\dc4}
  1705. @tab 21 = @code{#\nak}
  1706. @tab 22 = @code{#\syn}
  1707. @tab 23 = @code{#\etb}
  1708. @item 24 = @code{#\can}
  1709. @tab 25 = @code{#\em}
  1710. @tab 26 = @code{#\sub}
  1711. @tab 27 = @code{#\esc}
  1712. @item 28 = @code{#\fs}
  1713. @tab 29 = @code{#\gs}
  1714. @tab 30 = @code{#\rs}
  1715. @tab 31 = @code{#\us}
  1716. @item 32 = @code{#\sp}
  1717. @end multitable
  1718. The short name for the ``delete'' character (code point U+007F) is
  1719. @code{#\del}.
  1720. The R7RS name for the ``escape'' character (code point U+001B) is
  1721. @code{#\escape}.
  1722. There are also a few alternative names left over for compatibility with
  1723. previous versions of Guile.
  1724. @multitable {@code{#\backspace}} {Preferred}
  1725. @item Alternate @tab Standard
  1726. @item @code{#\nl} @tab @code{#\newline}
  1727. @item @code{#\np} @tab @code{#\page}
  1728. @item @code{#\null} @tab @code{#\nul}
  1729. @end multitable
  1730. Characters may also be written using their code point values. They can
  1731. be written with as an octal number, such as @code{#\10} for
  1732. @code{#\bs} or @code{#\177} for @code{#\del}.
  1733. If one prefers hex to octal, there is an additional syntax for character
  1734. escapes: @code{#\xHHHH} -- the letter 'x' followed by a hexadecimal
  1735. number of one to eight digits.
  1736. @rnindex char?
  1737. @deffn {Scheme Procedure} char? x
  1738. @deffnx {C Function} scm_char_p (x)
  1739. Return @code{#t} if @var{x} is a character, else @code{#f}.
  1740. @end deffn
  1741. Fundamentally, the character comparison operations below are
  1742. numeric comparisons of the character's code points.
  1743. @rnindex char=?
  1744. @deffn {Scheme Procedure} char=? x y
  1745. Return @code{#t} if code point of @var{x} is equal to the code point
  1746. of @var{y}, else @code{#f}.
  1747. @end deffn
  1748. @rnindex char<?
  1749. @deffn {Scheme Procedure} char<? x y
  1750. Return @code{#t} if the code point of @var{x} is less than the code
  1751. point of @var{y}, else @code{#f}.
  1752. @end deffn
  1753. @rnindex char<=?
  1754. @deffn {Scheme Procedure} char<=? x y
  1755. Return @code{#t} if the code point of @var{x} is less than or equal
  1756. to the code point of @var{y}, else @code{#f}.
  1757. @end deffn
  1758. @rnindex char>?
  1759. @deffn {Scheme Procedure} char>? x y
  1760. Return @code{#t} if the code point of @var{x} is greater than the
  1761. code point of @var{y}, else @code{#f}.
  1762. @end deffn
  1763. @rnindex char>=?
  1764. @deffn {Scheme Procedure} char>=? x y
  1765. Return @code{#t} if the code point of @var{x} is greater than or
  1766. equal to the code point of @var{y}, else @code{#f}.
  1767. @end deffn
  1768. @cindex case folding
  1769. Case-insensitive character comparisons use @emph{Unicode case
  1770. folding}. In case folding comparisons, if a character is lowercase
  1771. and has an uppercase form that can be expressed as a single character,
  1772. it is converted to uppercase before comparison. All other characters
  1773. undergo no conversion before the comparison occurs. This includes the
  1774. German sharp S (Eszett) which is not uppercased before conversion
  1775. because its uppercase form has two characters. Unicode case folding
  1776. is language independent: it uses rules that are generally true, but,
  1777. it cannot cover all cases for all languages.
  1778. @rnindex char-ci=?
  1779. @deffn {Scheme Procedure} char-ci=? x y
  1780. Return @code{#t} if the case-folded code point of @var{x} is the same
  1781. as the case-folded code point of @var{y}, else @code{#f}.
  1782. @end deffn
  1783. @rnindex char-ci<?
  1784. @deffn {Scheme Procedure} char-ci<? x y
  1785. Return @code{#t} if the case-folded code point of @var{x} is less
  1786. than the case-folded code point of @var{y}, else @code{#f}.
  1787. @end deffn
  1788. @rnindex char-ci<=?
  1789. @deffn {Scheme Procedure} char-ci<=? x y
  1790. Return @code{#t} if the case-folded code point of @var{x} is less
  1791. than or equal to the case-folded code point of @var{y}, else
  1792. @code{#f}.
  1793. @end deffn
  1794. @rnindex char-ci>?
  1795. @deffn {Scheme Procedure} char-ci>? x y
  1796. Return @code{#t} if the case-folded code point of @var{x} is greater
  1797. than the case-folded code point of @var{y}, else @code{#f}.
  1798. @end deffn
  1799. @rnindex char-ci>=?
  1800. @deffn {Scheme Procedure} char-ci>=? x y
  1801. Return @code{#t} if the case-folded code point of @var{x} is greater
  1802. than or equal to the case-folded code point of @var{y}, else
  1803. @code{#f}.
  1804. @end deffn
  1805. @rnindex char-alphabetic?
  1806. @deffn {Scheme Procedure} char-alphabetic? chr
  1807. @deffnx {C Function} scm_char_alphabetic_p (chr)
  1808. Return @code{#t} if @var{chr} is alphabetic, else @code{#f}.
  1809. @end deffn
  1810. @rnindex char-numeric?
  1811. @deffn {Scheme Procedure} char-numeric? chr
  1812. @deffnx {C Function} scm_char_numeric_p (chr)
  1813. Return @code{#t} if @var{chr} is numeric, else @code{#f}.
  1814. @end deffn
  1815. @rnindex char-whitespace?
  1816. @deffn {Scheme Procedure} char-whitespace? chr
  1817. @deffnx {C Function} scm_char_whitespace_p (chr)
  1818. Return @code{#t} if @var{chr} is whitespace, else @code{#f}.
  1819. @end deffn
  1820. @rnindex char-upper-case?
  1821. @deffn {Scheme Procedure} char-upper-case? chr
  1822. @deffnx {C Function} scm_char_upper_case_p (chr)
  1823. Return @code{#t} if @var{chr} is uppercase, else @code{#f}.
  1824. @end deffn
  1825. @rnindex char-lower-case?
  1826. @deffn {Scheme Procedure} char-lower-case? chr
  1827. @deffnx {C Function} scm_char_lower_case_p (chr)
  1828. Return @code{#t} if @var{chr} is lowercase, else @code{#f}.
  1829. @end deffn
  1830. @deffn {Scheme Procedure} char-is-both? chr
  1831. @deffnx {C Function} scm_char_is_both_p (chr)
  1832. Return @code{#t} if @var{chr} is either uppercase or lowercase, else
  1833. @code{#f}.
  1834. @end deffn
  1835. @deffn {Scheme Procedure} char-general-category chr
  1836. @deffnx {C Function} scm_char_general_category (chr)
  1837. Return a symbol giving the two-letter name of the Unicode general
  1838. category assigned to @var{chr} or @code{#f} if no named category is
  1839. assigned. The following table provides a list of category names along
  1840. with their meanings.
  1841. @multitable @columnfractions .1 .4 .1 .4
  1842. @item Lu
  1843. @tab Uppercase letter
  1844. @tab Pf
  1845. @tab Final quote punctuation
  1846. @item Ll
  1847. @tab Lowercase letter
  1848. @tab Po
  1849. @tab Other punctuation
  1850. @item Lt
  1851. @tab Titlecase letter
  1852. @tab Sm
  1853. @tab Math symbol
  1854. @item Lm
  1855. @tab Modifier letter
  1856. @tab Sc
  1857. @tab Currency symbol
  1858. @item Lo
  1859. @tab Other letter
  1860. @tab Sk
  1861. @tab Modifier symbol
  1862. @item Mn
  1863. @tab Non-spacing mark
  1864. @tab So
  1865. @tab Other symbol
  1866. @item Mc
  1867. @tab Combining spacing mark
  1868. @tab Zs
  1869. @tab Space separator
  1870. @item Me
  1871. @tab Enclosing mark
  1872. @tab Zl
  1873. @tab Line separator
  1874. @item Nd
  1875. @tab Decimal digit number
  1876. @tab Zp
  1877. @tab Paragraph separator
  1878. @item Nl
  1879. @tab Letter number
  1880. @tab Cc
  1881. @tab Control
  1882. @item No
  1883. @tab Other number
  1884. @tab Cf
  1885. @tab Format
  1886. @item Pc
  1887. @tab Connector punctuation
  1888. @tab Cs
  1889. @tab Surrogate
  1890. @item Pd
  1891. @tab Dash punctuation
  1892. @tab Co
  1893. @tab Private use
  1894. @item Ps
  1895. @tab Open punctuation
  1896. @tab Cn
  1897. @tab Unassigned
  1898. @item Pe
  1899. @tab Close punctuation
  1900. @tab
  1901. @tab
  1902. @item Pi
  1903. @tab Initial quote punctuation
  1904. @tab
  1905. @tab
  1906. @end multitable
  1907. @end deffn
  1908. @rnindex char->integer
  1909. @deffn {Scheme Procedure} char->integer chr
  1910. @deffnx {C Function} scm_char_to_integer (chr)
  1911. Return the code point of @var{chr}.
  1912. @end deffn
  1913. @rnindex integer->char
  1914. @deffn {Scheme Procedure} integer->char n
  1915. @deffnx {C Function} scm_integer_to_char (n)
  1916. Return the character that has code point @var{n}. The integer @var{n}
  1917. must be a valid code point. Valid code points are in the ranges 0 to
  1918. @code{#xD7FF} inclusive or @code{#xE000} to @code{#x10FFFF} inclusive.
  1919. @end deffn
  1920. @rnindex char-upcase
  1921. @deffn {Scheme Procedure} char-upcase chr
  1922. @deffnx {C Function} scm_char_upcase (chr)
  1923. Return the uppercase character version of @var{chr}.
  1924. @end deffn
  1925. @rnindex char-downcase
  1926. @deffn {Scheme Procedure} char-downcase chr
  1927. @deffnx {C Function} scm_char_downcase (chr)
  1928. Return the lowercase character version of @var{chr}.
  1929. @end deffn
  1930. @rnindex char-titlecase
  1931. @deffn {Scheme Procedure} char-titlecase chr
  1932. @deffnx {C Function} scm_char_titlecase (chr)
  1933. Return the titlecase character version of @var{chr} if one exists;
  1934. otherwise return the uppercase version.
  1935. For most characters these will be the same, but the Unicode Standard
  1936. includes certain digraph compatibility characters, such as @code{U+01F3}
  1937. ``dz'', for which the uppercase and titlecase characters are different
  1938. (@code{U+01F1} ``DZ'' and @code{U+01F2} ``Dz'' in this case,
  1939. respectively).
  1940. @end deffn
  1941. @tindex scm_t_wchar
  1942. @deftypefn {C Function} scm_t_wchar scm_c_upcase (scm_t_wchar @var{c})
  1943. @deftypefnx {C Function} scm_t_wchar scm_c_downcase (scm_t_wchar @var{c})
  1944. @deftypefnx {C Function} scm_t_wchar scm_c_titlecase (scm_t_wchar @var{c})
  1945. These C functions take an integer representation of a Unicode
  1946. codepoint and return the codepoint corresponding to its uppercase,
  1947. lowercase, and titlecase forms respectively. The type
  1948. @code{scm_t_wchar} is a signed, 32-bit integer.
  1949. @end deftypefn
  1950. Characters also have ``formal names'', which are defined by Unicode.
  1951. These names can be accessed in Guile from the @code{(ice-9 unicode)}
  1952. module:
  1953. @example
  1954. (use-modules (ice-9 unicode))
  1955. @end example
  1956. @deffn {Scheme Procedure} char->formal-name chr
  1957. Return the formal all-upper-case Unicode name of @var{ch},
  1958. as a string, or @code{#f} if the character has no name.
  1959. @end deffn
  1960. @deffn {Scheme Procedure} formal-name->char name
  1961. Return the character whose formal all-upper-case Unicode name is
  1962. @var{name}, or @code{#f} if no such character is known.
  1963. @end deffn
  1964. @node Character Sets
  1965. @subsection Character Sets
  1966. The features described in this section correspond directly to SRFI-14.
  1967. The data type @dfn{charset} implements sets of characters
  1968. (@pxref{Characters}). Because the internal representation of
  1969. character sets is not visible to the user, a lot of procedures for
  1970. handling them are provided.
  1971. Character sets can be created, extended, tested for the membership of a
  1972. characters and be compared to other character sets.
  1973. @menu
  1974. * Character Set Predicates/Comparison::
  1975. * Iterating Over Character Sets:: Enumerate charset elements.
  1976. * Creating Character Sets:: Making new charsets.
  1977. * Querying Character Sets:: Test charsets for membership etc.
  1978. * Character-Set Algebra:: Calculating new charsets.
  1979. * Standard Character Sets:: Variables containing predefined charsets.
  1980. @end menu
  1981. @node Character Set Predicates/Comparison
  1982. @subsubsection Character Set Predicates/Comparison
  1983. Use these procedures for testing whether an object is a character set,
  1984. or whether several character sets are equal or subsets of each other.
  1985. @code{char-set-hash} can be used for calculating a hash value, maybe for
  1986. usage in fast lookup procedures.
  1987. @deffn {Scheme Procedure} char-set? obj
  1988. @deffnx {C Function} scm_char_set_p (obj)
  1989. Return @code{#t} if @var{obj} is a character set, @code{#f}
  1990. otherwise.
  1991. @end deffn
  1992. @deffn {Scheme Procedure} char-set= char_set @dots{}
  1993. @deffnx {C Function} scm_char_set_eq (char_sets)
  1994. Return @code{#t} if all given character sets are equal.
  1995. @end deffn
  1996. @deffn {Scheme Procedure} char-set<= char_set @dots{}
  1997. @deffnx {C Function} scm_char_set_leq (char_sets)
  1998. Return @code{#t} if every character set @var{char_set}i is a subset
  1999. of character set @var{char_set}i+1.
  2000. @end deffn
  2001. @deffn {Scheme Procedure} char-set-hash cs [bound]
  2002. @deffnx {C Function} scm_char_set_hash (cs, bound)
  2003. Compute a hash value for the character set @var{cs}. If
  2004. @var{bound} is given and non-zero, it restricts the
  2005. returned value to the range 0 @dots{} @var{bound} - 1.
  2006. @end deffn
  2007. @c ===================================================================
  2008. @node Iterating Over Character Sets
  2009. @subsubsection Iterating Over Character Sets
  2010. Character set cursors are a means for iterating over the members of a
  2011. character sets. After creating a character set cursor with
  2012. @code{char-set-cursor}, a cursor can be dereferenced with
  2013. @code{char-set-ref}, advanced to the next member with
  2014. @code{char-set-cursor-next}. Whether a cursor has passed past the last
  2015. element of the set can be checked with @code{end-of-char-set?}.
  2016. Additionally, mapping and (un-)folding procedures for character sets are
  2017. provided.
  2018. @deffn {Scheme Procedure} char-set-cursor cs
  2019. @deffnx {C Function} scm_char_set_cursor (cs)
  2020. Return a cursor into the character set @var{cs}.
  2021. @end deffn
  2022. @deffn {Scheme Procedure} char-set-ref cs cursor
  2023. @deffnx {C Function} scm_char_set_ref (cs, cursor)
  2024. Return the character at the current cursor position
  2025. @var{cursor} in the character set @var{cs}. It is an error to
  2026. pass a cursor for which @code{end-of-char-set?} returns true.
  2027. @end deffn
  2028. @deffn {Scheme Procedure} char-set-cursor-next cs cursor
  2029. @deffnx {C Function} scm_char_set_cursor_next (cs, cursor)
  2030. Advance the character set cursor @var{cursor} to the next
  2031. character in the character set @var{cs}. It is an error if the
  2032. cursor given satisfies @code{end-of-char-set?}.
  2033. @end deffn
  2034. @deffn {Scheme Procedure} end-of-char-set? cursor
  2035. @deffnx {C Function} scm_end_of_char_set_p (cursor)
  2036. Return @code{#t} if @var{cursor} has reached the end of a
  2037. character set, @code{#f} otherwise.
  2038. @end deffn
  2039. @deffn {Scheme Procedure} char-set-fold kons knil cs
  2040. @deffnx {C Function} scm_char_set_fold (kons, knil, cs)
  2041. Fold the procedure @var{kons} over the character set @var{cs},
  2042. initializing it with @var{knil}.
  2043. @end deffn
  2044. @deffn {Scheme Procedure} char-set-unfold p f g seed [base_cs]
  2045. @deffnx {C Function} scm_char_set_unfold (p, f, g, seed, base_cs)
  2046. This is a fundamental constructor for character sets.
  2047. @itemize @bullet
  2048. @item @var{g} is used to generate a series of ``seed'' values
  2049. from the initial seed: @var{seed}, (@var{g} @var{seed}),
  2050. (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}), @dots{}
  2051. @item @var{p} tells us when to stop -- when it returns true
  2052. when applied to one of the seed values.
  2053. @item @var{f} maps each seed value to a character. These
  2054. characters are added to the base character set @var{base_cs} to
  2055. form the result; @var{base_cs} defaults to the empty set.
  2056. @end itemize
  2057. @end deffn
  2058. @deffn {Scheme Procedure} char-set-unfold! p f g seed base_cs
  2059. @deffnx {C Function} scm_char_set_unfold_x (p, f, g, seed, base_cs)
  2060. This is a fundamental constructor for character sets.
  2061. @itemize @bullet
  2062. @item @var{g} is used to generate a series of ``seed'' values
  2063. from the initial seed: @var{seed}, (@var{g} @var{seed}),
  2064. (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}), @dots{}
  2065. @item @var{p} tells us when to stop -- when it returns true
  2066. when applied to one of the seed values.
  2067. @item @var{f} maps each seed value to a character. These
  2068. characters are added to the base character set @var{base_cs} to
  2069. form the result; @var{base_cs} defaults to the empty set.
  2070. @end itemize
  2071. @end deffn
  2072. @deffn {Scheme Procedure} char-set-for-each proc cs
  2073. @deffnx {C Function} scm_char_set_for_each (proc, cs)
  2074. Apply @var{proc} to every character in the character set
  2075. @var{cs}. The return value is not specified.
  2076. @end deffn
  2077. @deffn {Scheme Procedure} char-set-map proc cs
  2078. @deffnx {C Function} scm_char_set_map (proc, cs)
  2079. Map the procedure @var{proc} over every character in @var{cs}.
  2080. @var{proc} must be a character -> character procedure.
  2081. @end deffn
  2082. @c ===================================================================
  2083. @node Creating Character Sets
  2084. @subsubsection Creating Character Sets
  2085. New character sets are produced with these procedures.
  2086. @deffn {Scheme Procedure} char-set-copy cs
  2087. @deffnx {C Function} scm_char_set_copy (cs)
  2088. Return a newly allocated character set containing all
  2089. characters in @var{cs}.
  2090. @end deffn
  2091. @deffn {Scheme Procedure} char-set chr @dots{}
  2092. @deffnx {C Function} scm_char_set (chrs)
  2093. Return a character set containing all given characters.
  2094. @end deffn
  2095. @deffn {Scheme Procedure} list->char-set list [base_cs]
  2096. @deffnx {C Function} scm_list_to_char_set (list, base_cs)
  2097. Convert the character list @var{list} to a character set. If
  2098. the character set @var{base_cs} is given, the character in this
  2099. set are also included in the result.
  2100. @end deffn
  2101. @deffn {Scheme Procedure} list->char-set! list base_cs
  2102. @deffnx {C Function} scm_list_to_char_set_x (list, base_cs)
  2103. Convert the character list @var{list} to a character set. The
  2104. characters are added to @var{base_cs} and @var{base_cs} is
  2105. returned.
  2106. @end deffn
  2107. @deffn {Scheme Procedure} string->char-set str [base_cs]
  2108. @deffnx {C Function} scm_string_to_char_set (str, base_cs)
  2109. Convert the string @var{str} to a character set. If the
  2110. character set @var{base_cs} is given, the characters in this
  2111. set are also included in the result.
  2112. @end deffn
  2113. @deffn {Scheme Procedure} string->char-set! str base_cs
  2114. @deffnx {C Function} scm_string_to_char_set_x (str, base_cs)
  2115. Convert the string @var{str} to a character set. The
  2116. characters from the string are added to @var{base_cs}, and
  2117. @var{base_cs} is returned.
  2118. @end deffn
  2119. @deffn {Scheme Procedure} char-set-filter pred cs [base_cs]
  2120. @deffnx {C Function} scm_char_set_filter (pred, cs, base_cs)
  2121. Return a character set containing every character from @var{cs}
  2122. so that it satisfies @var{pred}. If provided, the characters
  2123. from @var{base_cs} are added to the result.
  2124. @end deffn
  2125. @deffn {Scheme Procedure} char-set-filter! pred cs base_cs
  2126. @deffnx {C Function} scm_char_set_filter_x (pred, cs, base_cs)
  2127. Return a character set containing every character from @var{cs}
  2128. so that it satisfies @var{pred}. The characters are added to
  2129. @var{base_cs} and @var{base_cs} is returned.
  2130. @end deffn
  2131. @deffn {Scheme Procedure} ucs-range->char-set lower upper [error [base_cs]]
  2132. @deffnx {C Function} scm_ucs_range_to_char_set (lower, upper, error, base_cs)
  2133. Return a character set containing all characters whose
  2134. character codes lie in the half-open range
  2135. [@var{lower},@var{upper}).
  2136. If @var{error} is a true value, an error is signaled if the
  2137. specified range contains characters which are not contained in
  2138. the implemented character range. If @var{error} is @code{#f},
  2139. these characters are silently left out of the resulting
  2140. character set.
  2141. The characters in @var{base_cs} are added to the result, if
  2142. given.
  2143. @end deffn
  2144. @deffn {Scheme Procedure} ucs-range->char-set! lower upper error base_cs
  2145. @deffnx {C Function} scm_ucs_range_to_char_set_x (lower, upper, error, base_cs)
  2146. Return a character set containing all characters whose
  2147. character codes lie in the half-open range
  2148. [@var{lower},@var{upper}).
  2149. If @var{error} is a true value, an error is signaled if the
  2150. specified range contains characters which are not contained in
  2151. the implemented character range. If @var{error} is @code{#f},
  2152. these characters are silently left out of the resulting
  2153. character set.
  2154. The characters are added to @var{base_cs} and @var{base_cs} is
  2155. returned.
  2156. @end deffn
  2157. @deffn {Scheme Procedure} ->char-set x
  2158. @deffnx {C Function} scm_to_char_set (x)
  2159. Coerces x into a char-set. @var{x} may be a string, character or
  2160. char-set. A string is converted to the set of its constituent
  2161. characters; a character is converted to a singleton set; a char-set is
  2162. returned as-is.
  2163. @end deffn
  2164. @c ===================================================================
  2165. @node Querying Character Sets
  2166. @subsubsection Querying Character Sets
  2167. Access the elements and other information of a character set with these
  2168. procedures.
  2169. @deffn {Scheme Procedure} %char-set-dump cs
  2170. Returns an association list containing debugging information
  2171. for @var{cs}. The association list has the following entries.
  2172. @table @code
  2173. @item char-set
  2174. The char-set itself
  2175. @item len
  2176. The number of groups of contiguous code points the char-set
  2177. contains
  2178. @item ranges
  2179. A list of lists where each sublist is a range of code points
  2180. and their associated characters
  2181. @end table
  2182. The return value of this function cannot be relied upon to be
  2183. consistent between versions of Guile and should not be used in code.
  2184. @end deffn
  2185. @deffn {Scheme Procedure} char-set-size cs
  2186. @deffnx {C Function} scm_char_set_size (cs)
  2187. Return the number of elements in character set @var{cs}.
  2188. @end deffn
  2189. @deffn {Scheme Procedure} char-set-count pred cs
  2190. @deffnx {C Function} scm_char_set_count (pred, cs)
  2191. Return the number of the elements int the character set
  2192. @var{cs} which satisfy the predicate @var{pred}.
  2193. @end deffn
  2194. @deffn {Scheme Procedure} char-set->list cs
  2195. @deffnx {C Function} scm_char_set_to_list (cs)
  2196. Return a list containing the elements of the character set
  2197. @var{cs}.
  2198. @end deffn
  2199. @deffn {Scheme Procedure} char-set->string cs
  2200. @deffnx {C Function} scm_char_set_to_string (cs)
  2201. Return a string containing the elements of the character set
  2202. @var{cs}. The order in which the characters are placed in the
  2203. string is not defined.
  2204. @end deffn
  2205. @deffn {Scheme Procedure} char-set-contains? cs ch
  2206. @deffnx {C Function} scm_char_set_contains_p (cs, ch)
  2207. Return @code{#t} if the character @var{ch} is contained in the
  2208. character set @var{cs}, or @code{#f} otherwise.
  2209. @end deffn
  2210. @deffn {Scheme Procedure} char-set-every pred cs
  2211. @deffnx {C Function} scm_char_set_every (pred, cs)
  2212. Return a true value if every character in the character set
  2213. @var{cs} satisfies the predicate @var{pred}.
  2214. @end deffn
  2215. @deffn {Scheme Procedure} char-set-any pred cs
  2216. @deffnx {C Function} scm_char_set_any (pred, cs)
  2217. Return a true value if any character in the character set
  2218. @var{cs} satisfies the predicate @var{pred}.
  2219. @end deffn
  2220. @c ===================================================================
  2221. @node Character-Set Algebra
  2222. @subsubsection Character-Set Algebra
  2223. Character sets can be manipulated with the common set algebra operation,
  2224. such as union, complement, intersection etc. All of these procedures
  2225. provide side-effecting variants, which modify their character set
  2226. argument(s).
  2227. @deffn {Scheme Procedure} char-set-adjoin cs chr @dots{}
  2228. @deffnx {C Function} scm_char_set_adjoin (cs, chrs)
  2229. Add all character arguments to the first argument, which must
  2230. be a character set.
  2231. @end deffn
  2232. @deffn {Scheme Procedure} char-set-delete cs chr @dots{}
  2233. @deffnx {C Function} scm_char_set_delete (cs, chrs)
  2234. Delete all character arguments from the first argument, which
  2235. must be a character set.
  2236. @end deffn
  2237. @deffn {Scheme Procedure} char-set-adjoin! cs chr @dots{}
  2238. @deffnx {C Function} scm_char_set_adjoin_x (cs, chrs)
  2239. Add all character arguments to the first argument, which must
  2240. be a character set.
  2241. @end deffn
  2242. @deffn {Scheme Procedure} char-set-delete! cs chr @dots{}
  2243. @deffnx {C Function} scm_char_set_delete_x (cs, chrs)
  2244. Delete all character arguments from the first argument, which
  2245. must be a character set.
  2246. @end deffn
  2247. @deffn {Scheme Procedure} char-set-complement cs
  2248. @deffnx {C Function} scm_char_set_complement (cs)
  2249. Return the complement of the character set @var{cs}.
  2250. @end deffn
  2251. Note that the complement of a character set is likely to contain many
  2252. reserved code points (code points that are not associated with
  2253. characters). It may be helpful to modify the output of
  2254. @code{char-set-complement} by computing its intersection with the set
  2255. of designated code points, @code{char-set:designated}.
  2256. @deffn {Scheme Procedure} char-set-union cs @dots{}
  2257. @deffnx {C Function} scm_char_set_union (char_sets)
  2258. Return the union of all argument character sets.
  2259. @end deffn
  2260. @deffn {Scheme Procedure} char-set-intersection cs @dots{}
  2261. @deffnx {C Function} scm_char_set_intersection (char_sets)
  2262. Return the intersection of all argument character sets.
  2263. @end deffn
  2264. @deffn {Scheme Procedure} char-set-difference cs1 cs @dots{}
  2265. @deffnx {C Function} scm_char_set_difference (cs1, char_sets)
  2266. Return the difference of all argument character sets.
  2267. @end deffn
  2268. @deffn {Scheme Procedure} char-set-xor cs @dots{}
  2269. @deffnx {C Function} scm_char_set_xor (char_sets)
  2270. Return the exclusive-or of all argument character sets.
  2271. @end deffn
  2272. @deffn {Scheme Procedure} char-set-diff+intersection cs1 cs @dots{}
  2273. @deffnx {C Function} scm_char_set_diff_plus_intersection (cs1, char_sets)
  2274. Return the difference and the intersection of all argument
  2275. character sets.
  2276. @end deffn
  2277. @deffn {Scheme Procedure} char-set-complement! cs
  2278. @deffnx {C Function} scm_char_set_complement_x (cs)
  2279. Return the complement of the character set @var{cs}.
  2280. @end deffn
  2281. @deffn {Scheme Procedure} char-set-union! cs1 cs @dots{}
  2282. @deffnx {C Function} scm_char_set_union_x (cs1, char_sets)
  2283. Return the union of all argument character sets.
  2284. @end deffn
  2285. @deffn {Scheme Procedure} char-set-intersection! cs1 cs @dots{}
  2286. @deffnx {C Function} scm_char_set_intersection_x (cs1, char_sets)
  2287. Return the intersection of all argument character sets.
  2288. @end deffn
  2289. @deffn {Scheme Procedure} char-set-difference! cs1 cs @dots{}
  2290. @deffnx {C Function} scm_char_set_difference_x (cs1, char_sets)
  2291. Return the difference of all argument character sets.
  2292. @end deffn
  2293. @deffn {Scheme Procedure} char-set-xor! cs1 cs @dots{}
  2294. @deffnx {C Function} scm_char_set_xor_x (cs1, char_sets)
  2295. Return the exclusive-or of all argument character sets.
  2296. @end deffn
  2297. @deffn {Scheme Procedure} char-set-diff+intersection! cs1 cs2 cs @dots{}
  2298. @deffnx {C Function} scm_char_set_diff_plus_intersection_x (cs1, cs2, char_sets)
  2299. Return the difference and the intersection of all argument
  2300. character sets.
  2301. @end deffn
  2302. @c ===================================================================
  2303. @node Standard Character Sets
  2304. @subsubsection Standard Character Sets
  2305. In order to make the use of the character set data type and procedures
  2306. useful, several predefined character set variables exist.
  2307. @cindex codeset
  2308. @cindex charset
  2309. @cindex locale
  2310. These character sets are locale independent and are not recomputed
  2311. upon a @code{setlocale} call. They contain characters from the whole
  2312. range of Unicode code points. For instance, @code{char-set:letter}
  2313. contains about 100,000 characters.
  2314. @defvr {Scheme Variable} char-set:lower-case
  2315. @defvrx {C Variable} scm_char_set_lower_case
  2316. All lower-case characters.
  2317. @end defvr
  2318. @defvr {Scheme Variable} char-set:upper-case
  2319. @defvrx {C Variable} scm_char_set_upper_case
  2320. All upper-case characters.
  2321. @end defvr
  2322. @defvr {Scheme Variable} char-set:title-case
  2323. @defvrx {C Variable} scm_char_set_title_case
  2324. All single characters that function as if they were an upper-case
  2325. letter followed by a lower-case letter.
  2326. @end defvr
  2327. @defvr {Scheme Variable} char-set:letter
  2328. @defvrx {C Variable} scm_char_set_letter
  2329. All letters. This includes @code{char-set:lower-case},
  2330. @code{char-set:upper-case}, @code{char-set:title-case}, and many
  2331. letters that have no case at all. For example, Chinese and Japanese
  2332. characters typically have no concept of case.
  2333. @end defvr
  2334. @defvr {Scheme Variable} char-set:digit
  2335. @defvrx {C Variable} scm_char_set_digit
  2336. All digits.
  2337. @end defvr
  2338. @defvr {Scheme Variable} char-set:letter+digit
  2339. @defvrx {C Variable} scm_char_set_letter_and_digit
  2340. The union of @code{char-set:letter} and @code{char-set:digit}.
  2341. @end defvr
  2342. @defvr {Scheme Variable} char-set:graphic
  2343. @defvrx {C Variable} scm_char_set_graphic
  2344. All characters which would put ink on the paper.
  2345. @end defvr
  2346. @defvr {Scheme Variable} char-set:printing
  2347. @defvrx {C Variable} scm_char_set_printing
  2348. The union of @code{char-set:graphic} and @code{char-set:whitespace}.
  2349. @end defvr
  2350. @defvr {Scheme Variable} char-set:whitespace
  2351. @defvrx {C Variable} scm_char_set_whitespace
  2352. All whitespace characters.
  2353. @end defvr
  2354. @defvr {Scheme Variable} char-set:blank
  2355. @defvrx {C Variable} scm_char_set_blank
  2356. All horizontal whitespace characters, which notably includes
  2357. @code{#\space} and @code{#\tab}.
  2358. @end defvr
  2359. @defvr {Scheme Variable} char-set:iso-control
  2360. @defvrx {C Variable} scm_char_set_iso_control
  2361. The ISO control characters are the C0 control characters (U+0000 to
  2362. U+001F), delete (U+007F), and the C1 control characters (U+0080 to
  2363. U+009F).
  2364. @end defvr
  2365. @defvr {Scheme Variable} char-set:punctuation
  2366. @defvrx {C Variable} scm_char_set_punctuation
  2367. All punctuation characters, such as the characters
  2368. @code{!"#%&'()*,-./:;?@@[\\]_@{@}}
  2369. @end defvr
  2370. @defvr {Scheme Variable} char-set:symbol
  2371. @defvrx {C Variable} scm_char_set_symbol
  2372. All symbol characters, such as the characters @code{$+<=>^`|~}.
  2373. @end defvr
  2374. @defvr {Scheme Variable} char-set:hex-digit
  2375. @defvrx {C Variable} scm_char_set_hex_digit
  2376. The hexadecimal digits @code{0123456789abcdefABCDEF}.
  2377. @end defvr
  2378. @defvr {Scheme Variable} char-set:ascii
  2379. @defvrx {C Variable} scm_char_set_ascii
  2380. All ASCII characters.
  2381. @end defvr
  2382. @defvr {Scheme Variable} char-set:empty
  2383. @defvrx {C Variable} scm_char_set_empty
  2384. The empty character set.
  2385. @end defvr
  2386. @defvr {Scheme Variable} char-set:designated
  2387. @defvrx {C Variable} scm_char_set_designated
  2388. This character set contains all designated code points. This includes
  2389. all the code points to which Unicode has assigned a character or other
  2390. meaning.
  2391. @end defvr
  2392. @defvr {Scheme Variable} char-set:full
  2393. @defvrx {C Variable} scm_char_set_full
  2394. This character set contains all possible code points. This includes
  2395. both designated and reserved code points.
  2396. @end defvr
  2397. @node Strings
  2398. @subsection Strings
  2399. @tpindex Strings
  2400. Strings are fixed-length sequences of characters. They can be created
  2401. by calling constructor procedures, but they can also literally get
  2402. entered at the @acronym{REPL} or in Scheme source files.
  2403. @c Guile provides a rich set of string processing procedures, because text
  2404. @c handling is very important when Guile is used as a scripting language.
  2405. Strings always carry the information about how many characters they are
  2406. composed of with them, so there is no special end-of-string character,
  2407. like in C. That means that Scheme strings can contain any character,
  2408. even the @samp{#\nul} character @samp{\0}.
  2409. To use strings efficiently, you need to know a bit about how Guile
  2410. implements them. In Guile, a string consists of two parts, a head and
  2411. the actual memory where the characters are stored. When a string (or
  2412. a substring of it) is copied, only a new head gets created, the memory
  2413. is usually not copied. The two heads start out pointing to the same
  2414. memory.
  2415. When one of these two strings is modified, as with @code{string-set!},
  2416. their common memory does get copied so that each string has its own
  2417. memory and modifying one does not accidentally modify the other as well.
  2418. Thus, Guile's strings are `copy on write'; the actual copying of their
  2419. memory is delayed until one string is written to.
  2420. This implementation makes functions like @code{substring} very
  2421. efficient in the common case that no modifications are done to the
  2422. involved strings.
  2423. If you do know that your strings are getting modified right away, you
  2424. can use @code{substring/copy} instead of @code{substring}. This
  2425. function performs the copy immediately at the time of creation. This
  2426. is more efficient, especially in a multi-threaded program. Also,
  2427. @code{substring/copy} can avoid the problem that a short substring
  2428. holds on to the memory of a very large original string that could
  2429. otherwise be recycled.
  2430. If you want to avoid the copy altogether, so that modifications of one
  2431. string show up in the other, you can use @code{substring/shared}. The
  2432. strings created by this procedure are called @dfn{mutation sharing
  2433. substrings} since the substring and the original string share
  2434. modifications to each other.
  2435. If you want to prevent modifications, use @code{substring/read-only}.
  2436. Guile provides all procedures of SRFI-13 and a few more.
  2437. @menu
  2438. * String Syntax:: Read syntax for strings.
  2439. * String Predicates:: Testing strings for certain properties.
  2440. * String Constructors:: Creating new string objects.
  2441. * List/String Conversion:: Converting from/to lists of characters.
  2442. * String Selection:: Select portions from strings.
  2443. * String Modification:: Modify parts or whole strings.
  2444. * String Comparison:: Lexicographic ordering predicates.
  2445. * String Searching:: Searching in strings.
  2446. * Alphabetic Case Mapping:: Convert the alphabetic case of strings.
  2447. * Reversing and Appending Strings:: Appending strings to form a new string.
  2448. * Mapping Folding and Unfolding:: Iterating over strings.
  2449. * Miscellaneous String Operations:: Replicating, insertion, parsing, ...
  2450. * Representing Strings as Bytes:: Encoding and decoding strings.
  2451. * Conversion to/from C::
  2452. * String Internals:: The storage strategy for strings.
  2453. @end menu
  2454. @node String Syntax
  2455. @subsubsection String Read Syntax
  2456. @c In the following @code is used to get a good font in TeX etc, but
  2457. @c is omitted for Info format, so as not to risk any confusion over
  2458. @c whether surrounding ` ' quotes are part of the escape or are
  2459. @c special in a string (they're not).
  2460. The read syntax for strings is an arbitrarily long sequence of
  2461. characters enclosed in double quotes (@nicode{"}).
  2462. Backslash is an escape character and can be used to insert the following
  2463. special characters. @nicode{\"} and @nicode{\\} are R5RS standard,
  2464. @nicode{\|} is R7RS standard, the next seven are R6RS standard ---
  2465. notice they follow C syntax --- and the remaining four are Guile
  2466. extensions.
  2467. @table @asis
  2468. @item @nicode{\\}
  2469. Backslash character.
  2470. @item @nicode{\"}
  2471. Double quote character (an unescaped @nicode{"} is otherwise the end
  2472. of the string).
  2473. @item @nicode{\|}
  2474. Vertical bar character.
  2475. @item @nicode{\a}
  2476. Bell character (ASCII 7).
  2477. @item @nicode{\f}
  2478. Formfeed character (ASCII 12).
  2479. @item @nicode{\n}
  2480. Newline character (ASCII 10).
  2481. @item @nicode{\r}
  2482. Carriage return character (ASCII 13).
  2483. @item @nicode{\t}
  2484. Tab character (ASCII 9).
  2485. @item @nicode{\v}
  2486. Vertical tab character (ASCII 11).
  2487. @item @nicode{\b}
  2488. Backspace character (ASCII 8).
  2489. @item @nicode{\0}
  2490. NUL character (ASCII 0).
  2491. @item @nicode{\(}
  2492. Open parenthesis. This is intended for use at the beginning of lines in
  2493. multiline strings to avoid confusing Emacs lisp modes.
  2494. @item @nicode{\} followed by newline (ASCII 10)
  2495. Nothing. This way if @nicode{\} is the last character in a line, the
  2496. string will continue with the first character from the next line,
  2497. without a line break.
  2498. If the @code{hungry-eol-escapes} reader option is enabled, which is not
  2499. the case by default, leading whitespace on the next line is discarded.
  2500. @lisp
  2501. "foo\
  2502. bar"
  2503. @result{} "foo bar"
  2504. (read-enable 'hungry-eol-escapes)
  2505. "foo\
  2506. bar"
  2507. @result{} "foobar"
  2508. @end lisp
  2509. @item @nicode{\xHH}
  2510. Character code given by two hexadecimal digits. For example
  2511. @nicode{\x7f} for an ASCII DEL (127).
  2512. @item @nicode{\uHHHH}
  2513. Character code given by four hexadecimal digits. For example
  2514. @nicode{\u0100} for a capital A with macron (U+0100).
  2515. @item @nicode{\UHHHHHH}
  2516. Character code given by six hexadecimal digits. For example
  2517. @nicode{\U010402}.
  2518. @end table
  2519. @noindent
  2520. The following are examples of string literals:
  2521. @lisp
  2522. "foo"
  2523. "bar plonk"
  2524. "Hello World"
  2525. "\"Hi\", he said."
  2526. @end lisp
  2527. The three escape sequences @code{\xHH}, @code{\uHHHH} and @code{\UHHHHHH} were
  2528. chosen to not break compatibility with code written for previous versions of
  2529. Guile. The R6RS specification suggests a different, incompatible syntax for hex
  2530. escapes: @code{\xHHHH;} -- a character code followed by one to eight hexadecimal
  2531. digits terminated with a semicolon. If this escape format is desired instead,
  2532. it can be enabled with the reader option @code{r6rs-hex-escapes}.
  2533. @lisp
  2534. (read-enable 'r6rs-hex-escapes)
  2535. @end lisp
  2536. For more on reader options, @xref{Scheme Read}.
  2537. @node String Predicates
  2538. @subsubsection String Predicates
  2539. The following procedures can be used to check whether a given string
  2540. fulfills some specified property.
  2541. @rnindex string?
  2542. @deffn {Scheme Procedure} string? obj
  2543. @deffnx {C Function} scm_string_p (obj)
  2544. Return @code{#t} if @var{obj} is a string, else @code{#f}.
  2545. @end deffn
  2546. @deftypefn {C Function} int scm_is_string (SCM obj)
  2547. Returns @code{1} if @var{obj} is a string, @code{0} otherwise.
  2548. @end deftypefn
  2549. @deffn {Scheme Procedure} string-null? str
  2550. @deffnx {C Function} scm_string_null_p (str)
  2551. Return @code{#t} if @var{str}'s length is zero, and
  2552. @code{#f} otherwise.
  2553. @lisp
  2554. (string-null? "") @result{} #t
  2555. y @result{} "foo"
  2556. (string-null? y) @result{} #f
  2557. @end lisp
  2558. @end deffn
  2559. @deffn {Scheme Procedure} string-any char_pred s [start [end]]
  2560. @deffnx {C Function} scm_string_any (char_pred, s, start, end)
  2561. Check if @var{char_pred} is true for any character in string @var{s}.
  2562. @var{char_pred} can be a character to check for any equal to that, or
  2563. a character set (@pxref{Character Sets}) to check for any in that set,
  2564. or a predicate procedure to call.
  2565. For a procedure, calls @code{(@var{char_pred} c)} are made
  2566. successively on the characters from @var{start} to @var{end}. If
  2567. @var{char_pred} returns true (ie.@: non-@code{#f}), @code{string-any}
  2568. stops and that return value is the return from @code{string-any}. The
  2569. call on the last character (ie.@: at @math{@var{end}-1}), if that
  2570. point is reached, is a tail call.
  2571. If there are no characters in @var{s} (ie.@: @var{start} equals
  2572. @var{end}) then the return is @code{#f}.
  2573. @end deffn
  2574. @deffn {Scheme Procedure} string-every char_pred s [start [end]]
  2575. @deffnx {C Function} scm_string_every (char_pred, s, start, end)
  2576. Check if @var{char_pred} is true for every character in string
  2577. @var{s}.
  2578. @var{char_pred} can be a character to check for every character equal
  2579. to that, or a character set (@pxref{Character Sets}) to check for
  2580. every character being in that set, or a predicate procedure to call.
  2581. For a procedure, calls @code{(@var{char_pred} c)} are made
  2582. successively on the characters from @var{start} to @var{end}. If
  2583. @var{char_pred} returns @code{#f}, @code{string-every} stops and
  2584. returns @code{#f}. The call on the last character (ie.@: at
  2585. @math{@var{end}-1}), if that point is reached, is a tail call and the
  2586. return from that call is the return from @code{string-every}.
  2587. If there are no characters in @var{s} (ie.@: @var{start} equals
  2588. @var{end}) then the return is @code{#t}.
  2589. @end deffn
  2590. @node String Constructors
  2591. @subsubsection String Constructors
  2592. The string constructor procedures create new string objects, possibly
  2593. initializing them with some specified character data. See also
  2594. @xref{String Selection}, for ways to create strings from existing
  2595. strings.
  2596. @c FIXME::martin: list->string belongs into `List/String Conversion'
  2597. @deffn {Scheme Procedure} string char@dots{}
  2598. @rnindex string
  2599. Return a newly allocated string made from the given character
  2600. arguments.
  2601. @example
  2602. (string #\x #\y #\z) @result{} "xyz"
  2603. (string) @result{} ""
  2604. @end example
  2605. @end deffn
  2606. @deffn {Scheme Procedure} list->string lst
  2607. @deffnx {C Function} scm_string (lst)
  2608. @rnindex list->string
  2609. Return a newly allocated string made from a list of characters.
  2610. @example
  2611. (list->string '(#\a #\b #\c)) @result{} "abc"
  2612. @end example
  2613. @end deffn
  2614. @deffn {Scheme Procedure} reverse-list->string lst
  2615. @deffnx {C Function} scm_reverse_list_to_string (lst)
  2616. Return a newly allocated string made from a list of characters, in
  2617. reverse order.
  2618. @example
  2619. (reverse-list->string '(#\a #\B #\c)) @result{} "cBa"
  2620. @end example
  2621. @end deffn
  2622. @rnindex make-string
  2623. @deffn {Scheme Procedure} make-string k [chr]
  2624. @deffnx {C Function} scm_make_string (k, chr)
  2625. Return a newly allocated string of
  2626. length @var{k}. If @var{chr} is given, then all elements of
  2627. the string are initialized to @var{chr}, otherwise the contents
  2628. of the string are unspecified.
  2629. @end deffn
  2630. @deftypefn {C Function} SCM scm_c_make_string (size_t len, SCM chr)
  2631. Like @code{scm_make_string}, but expects the length as a
  2632. @code{size_t}.
  2633. @end deftypefn
  2634. @deffn {Scheme Procedure} string-tabulate proc len
  2635. @deffnx {C Function} scm_string_tabulate (proc, len)
  2636. @var{proc} is an integer->char procedure. Construct a string
  2637. of size @var{len} by applying @var{proc} to each index to
  2638. produce the corresponding string element. The order in which
  2639. @var{proc} is applied to the indices is not specified.
  2640. @end deffn
  2641. @deffn {Scheme Procedure} string-join ls [delimiter [grammar]]
  2642. @deffnx {C Function} scm_string_join (ls, delimiter, grammar)
  2643. Append the string in the string list @var{ls}, using the string
  2644. @var{delimiter} as a delimiter between the elements of @var{ls}.
  2645. @var{delimiter} defaults to @w{@samp{ }}, that is, strings in @var{ls}
  2646. are appended with the space character in between them. @var{grammar} is
  2647. a symbol which specifies how the delimiter is placed between the
  2648. strings, and defaults to the symbol @code{infix}.
  2649. @table @code
  2650. @item infix
  2651. Insert the separator between list elements. An empty string
  2652. will produce an empty list.
  2653. @item strict-infix
  2654. Like @code{infix}, but will raise an error if given the empty
  2655. list.
  2656. @item suffix
  2657. Insert the separator after every list element.
  2658. @item prefix
  2659. Insert the separator before each list element.
  2660. @end table
  2661. @end deffn
  2662. @node List/String Conversion
  2663. @subsubsection List/String conversion
  2664. When processing strings, it is often convenient to first convert them
  2665. into a list representation by using the procedure @code{string->list},
  2666. work with the resulting list, and then convert it back into a string.
  2667. These procedures are useful for similar tasks.
  2668. @rnindex string->list
  2669. @deffn {Scheme Procedure} string->list str [start [end]]
  2670. @deffnx {C Function} scm_substring_to_list (str, start, end)
  2671. @deffnx {C Function} scm_string_to_list (str)
  2672. Convert the string @var{str} into a list of characters.
  2673. @end deffn
  2674. @deffn {Scheme Procedure} string-split str char_pred
  2675. @deffnx {C Function} scm_string_split (str, char_pred)
  2676. Split the string @var{str} into a list of substrings delimited
  2677. by appearances of characters that
  2678. @itemize @bullet
  2679. @item
  2680. equal @var{char_pred}, if it is a character,
  2681. @item
  2682. satisfy the predicate @var{char_pred}, if it is a procedure,
  2683. @item
  2684. are in the set @var{char_pred}, if it is a character set.
  2685. @end itemize
  2686. Note that an empty substring between separator characters will result in
  2687. an empty string in the result list.
  2688. @lisp
  2689. (string-split "root:x:0:0:root:/root:/bin/bash" #\:)
  2690. @result{}
  2691. ("root" "x" "0" "0" "root" "/root" "/bin/bash")
  2692. (string-split "::" #\:)
  2693. @result{}
  2694. ("" "" "")
  2695. (string-split "" #\:)
  2696. @result{}
  2697. ("")
  2698. @end lisp
  2699. @end deffn
  2700. @node String Selection
  2701. @subsubsection String Selection
  2702. Portions of strings can be extracted by these procedures.
  2703. @code{string-ref} delivers individual characters whereas
  2704. @code{substring} can be used to extract substrings from longer strings.
  2705. @rnindex string-length
  2706. @deffn {Scheme Procedure} string-length string
  2707. @deffnx {C Function} scm_string_length (string)
  2708. Return the number of characters in @var{string}.
  2709. @end deffn
  2710. @deftypefn {C Function} size_t scm_c_string_length (SCM str)
  2711. Return the number of characters in @var{str} as a @code{size_t}.
  2712. @end deftypefn
  2713. @rnindex string-ref
  2714. @deffn {Scheme Procedure} string-ref str k
  2715. @deffnx {C Function} scm_string_ref (str, k)
  2716. Return character @var{k} of @var{str} using zero-origin
  2717. indexing. @var{k} must be a valid index of @var{str}.
  2718. @end deffn
  2719. @deftypefn {C Function} SCM scm_c_string_ref (SCM str, size_t k)
  2720. Return character @var{k} of @var{str} using zero-origin
  2721. indexing. @var{k} must be a valid index of @var{str}.
  2722. @end deftypefn
  2723. @rnindex string-copy
  2724. @deffn {Scheme Procedure} string-copy str [start [end]]
  2725. @deffnx {C Function} scm_substring_copy (str, start, end)
  2726. @deffnx {C Function} scm_string_copy (str)
  2727. Return a copy of the given string @var{str}.
  2728. The returned string shares storage with @var{str} initially, but it is
  2729. copied as soon as one of the two strings is modified.
  2730. @end deffn
  2731. @rnindex substring
  2732. @deffn {Scheme Procedure} substring str start [end]
  2733. @deffnx {C Function} scm_substring (str, start, end)
  2734. Return a new string formed from the characters
  2735. of @var{str} beginning with index @var{start} (inclusive) and
  2736. ending with index @var{end} (exclusive).
  2737. @var{str} must be a string, @var{start} and @var{end} must be
  2738. exact integers satisfying:
  2739. 0 <= @var{start} <= @var{end} <= @code{(string-length @var{str})}.
  2740. The returned string shares storage with @var{str} initially, but it is
  2741. copied as soon as one of the two strings is modified.
  2742. @end deffn
  2743. @deffn {Scheme Procedure} substring/shared str start [end]
  2744. @deffnx {C Function} scm_substring_shared (str, start, end)
  2745. Like @code{substring}, but the strings continue to share their storage
  2746. even if they are modified. Thus, modifications to @var{str} show up
  2747. in the new string, and vice versa.
  2748. @end deffn
  2749. @deffn {Scheme Procedure} substring/copy str start [end]
  2750. @deffnx {C Function} scm_substring_copy (str, start, end)
  2751. Like @code{substring}, but the storage for the new string is copied
  2752. immediately.
  2753. @end deffn
  2754. @deffn {Scheme Procedure} substring/read-only str start [end]
  2755. @deffnx {C Function} scm_substring_read_only (str, start, end)
  2756. Like @code{substring}, but the resulting string can not be modified.
  2757. @end deffn
  2758. @deftypefn {C Function} SCM scm_c_substring (SCM str, size_t start, size_t end)
  2759. @deftypefnx {C Function} SCM scm_c_substring_shared (SCM str, size_t start, size_t end)
  2760. @deftypefnx {C Function} SCM scm_c_substring_copy (SCM str, size_t start, size_t end)
  2761. @deftypefnx {C Function} SCM scm_c_substring_read_only (SCM str, size_t start, size_t end)
  2762. Like @code{scm_substring}, etc. but the bounds are given as a @code{size_t}.
  2763. @end deftypefn
  2764. @deffn {Scheme Procedure} string-take s n
  2765. @deffnx {C Function} scm_string_take (s, n)
  2766. Return the @var{n} first characters of @var{s}.
  2767. @end deffn
  2768. @deffn {Scheme Procedure} string-drop s n
  2769. @deffnx {C Function} scm_string_drop (s, n)
  2770. Return all but the first @var{n} characters of @var{s}.
  2771. @end deffn
  2772. @deffn {Scheme Procedure} string-take-right s n
  2773. @deffnx {C Function} scm_string_take_right (s, n)
  2774. Return the @var{n} last characters of @var{s}.
  2775. @end deffn
  2776. @deffn {Scheme Procedure} string-drop-right s n
  2777. @deffnx {C Function} scm_string_drop_right (s, n)
  2778. Return all but the last @var{n} characters of @var{s}.
  2779. @end deffn
  2780. @deffn {Scheme Procedure} string-pad s len [chr [start [end]]]
  2781. @deffnx {Scheme Procedure} string-pad-right s len [chr [start [end]]]
  2782. @deffnx {C Function} scm_string_pad (s, len, chr, start, end)
  2783. @deffnx {C Function} scm_string_pad_right (s, len, chr, start, end)
  2784. Take characters @var{start} to @var{end} from the string @var{s} and
  2785. either pad with @var{chr} or truncate them to give @var{len}
  2786. characters.
  2787. @code{string-pad} pads or truncates on the left, so for example
  2788. @example
  2789. (string-pad "x" 3) @result{} " x"
  2790. (string-pad "abcde" 3) @result{} "cde"
  2791. @end example
  2792. @code{string-pad-right} pads or truncates on the right, so for example
  2793. @example
  2794. (string-pad-right "x" 3) @result{} "x "
  2795. (string-pad-right "abcde" 3) @result{} "abc"
  2796. @end example
  2797. @end deffn
  2798. @deffn {Scheme Procedure} string-trim s [char_pred [start [end]]]
  2799. @deffnx {Scheme Procedure} string-trim-right s [char_pred [start [end]]]
  2800. @deffnx {Scheme Procedure} string-trim-both s [char_pred [start [end]]]
  2801. @deffnx {C Function} scm_string_trim (s, char_pred, start, end)
  2802. @deffnx {C Function} scm_string_trim_right (s, char_pred, start, end)
  2803. @deffnx {C Function} scm_string_trim_both (s, char_pred, start, end)
  2804. Trim occurrences of @var{char_pred} from the ends of @var{s}.
  2805. @code{string-trim} trims @var{char_pred} characters from the left
  2806. (start) of the string, @code{string-trim-right} trims them from the
  2807. right (end) of the string, @code{string-trim-both} trims from both
  2808. ends.
  2809. @var{char_pred} can be a character, a character set, or a predicate
  2810. procedure to call on each character. If @var{char_pred} is not given
  2811. the default is whitespace as per @code{char-set:whitespace}
  2812. (@pxref{Standard Character Sets}).
  2813. @example
  2814. (string-trim " x ") @result{} "x "
  2815. (string-trim-right "banana" #\a) @result{} "banan"
  2816. (string-trim-both ".,xy:;" char-set:punctuation)
  2817. @result{} "xy"
  2818. (string-trim-both "xyzzy" (lambda (c)
  2819. (or (eqv? c #\x)
  2820. (eqv? c #\y))))
  2821. @result{} "zz"
  2822. @end example
  2823. @end deffn
  2824. @node String Modification
  2825. @subsubsection String Modification
  2826. These procedures are for modifying strings in-place. This means that the
  2827. result of the operation is not a new string; instead, the original string's
  2828. memory representation is modified.
  2829. @rnindex string-set!
  2830. @deffn {Scheme Procedure} string-set! str k chr
  2831. @deffnx {C Function} scm_string_set_x (str, k, chr)
  2832. Store @var{chr} in element @var{k} of @var{str} and return
  2833. an unspecified value. @var{k} must be a valid index of
  2834. @var{str}.
  2835. @end deffn
  2836. @deftypefn {C Function} void scm_c_string_set_x (SCM str, size_t k, SCM chr)
  2837. Like @code{scm_string_set_x}, but the index is given as a @code{size_t}.
  2838. @end deftypefn
  2839. @rnindex string-fill!
  2840. @anchor{x-string-fill!}
  2841. @deffn {Scheme Procedure} string-fill! str chr [start [end]]
  2842. @deffnx {C Function} scm_substring_fill_x (str, chr, start, end)
  2843. @deffnx {C Function} scm_string_fill_x (str, chr)
  2844. Stores @var{chr} in every element of the given @var{str} and
  2845. returns an unspecified value.
  2846. @end deffn
  2847. @deffn {Scheme Procedure} substring-fill! str start end fill
  2848. @deffnx {C Function} scm_substring_fill_x (str, start, end, fill)
  2849. Change every character in @var{str} between @var{start} and
  2850. @var{end} to @var{fill}.
  2851. @lisp
  2852. (define y (string-copy "abcdefg"))
  2853. (substring-fill! y 1 3 #\r)
  2854. y
  2855. @result{} "arrdefg"
  2856. @end lisp
  2857. @end deffn
  2858. @deffn {Scheme Procedure} substring-move! str1 start1 end1 str2 start2
  2859. @deffnx {C Function} scm_substring_move_x (str1, start1, end1, str2, start2)
  2860. Copy the substring of @var{str1} bounded by @var{start1} and @var{end1}
  2861. into @var{str2} beginning at position @var{start2}.
  2862. @var{str1} and @var{str2} can be the same string.
  2863. @end deffn
  2864. @deffn {Scheme Procedure} string-copy! target tstart s [start [end]]
  2865. @deffnx {C Function} scm_string_copy_x (target, tstart, s, start, end)
  2866. Copy the sequence of characters from index range [@var{start},
  2867. @var{end}) in string @var{s} to string @var{target}, beginning
  2868. at index @var{tstart}. The characters are copied left-to-right
  2869. or right-to-left as needed -- the copy is guaranteed to work,
  2870. even if @var{target} and @var{s} are the same string. It is an
  2871. error if the copy operation runs off the end of the target
  2872. string.
  2873. @end deffn
  2874. @node String Comparison
  2875. @subsubsection String Comparison
  2876. The procedures in this section are similar to the character ordering
  2877. predicates (@pxref{Characters}), but are defined on character sequences.
  2878. The first set is specified in R5RS and has names that end in @code{?}.
  2879. The second set is specified in SRFI-13 and the names have not ending
  2880. @code{?}.
  2881. The predicates ending in @code{-ci} ignore the character case
  2882. when comparing strings. For now, case-insensitive comparison is done
  2883. using the R5RS rules, where every lower-case character that has a
  2884. single character upper-case form is converted to uppercase before
  2885. comparison. See @xref{Text Collation, the @code{(ice-9
  2886. i18n)} module}, for locale-dependent string comparison.
  2887. @rnindex string=?
  2888. @deffn {Scheme Procedure} string=? s1 s2 s3 @dots{}
  2889. Lexicographic equality predicate; return @code{#t} if all strings are
  2890. the same length and contain the same characters in the same positions,
  2891. otherwise return @code{#f}.
  2892. The procedure @code{string-ci=?} treats upper and lower case
  2893. letters as though they were the same character, but
  2894. @code{string=?} treats upper and lower case as distinct
  2895. characters.
  2896. @end deffn
  2897. @rnindex string<?
  2898. @deffn {Scheme Procedure} string<? s1 s2 s3 @dots{}
  2899. Lexicographic ordering predicate; return @code{#t} if, for every pair of
  2900. consecutive string arguments @var{str_i} and @var{str_i+1}, @var{str_i} is
  2901. lexicographically less than @var{str_i+1}.
  2902. @end deffn
  2903. @rnindex string<=?
  2904. @deffn {Scheme Procedure} string<=? s1 s2 s3 @dots{}
  2905. Lexicographic ordering predicate; return @code{#t} if, for every pair of
  2906. consecutive string arguments @var{str_i} and @var{str_i+1}, @var{str_i} is
  2907. lexicographically less than or equal to @var{str_i+1}.
  2908. @end deffn
  2909. @rnindex string>?
  2910. @deffn {Scheme Procedure} string>? s1 s2 s3 @dots{}
  2911. Lexicographic ordering predicate; return @code{#t} if, for every pair of
  2912. consecutive string arguments @var{str_i} and @var{str_i+1}, @var{str_i} is
  2913. lexicographically greater than @var{str_i+1}.
  2914. @end deffn
  2915. @rnindex string>=?
  2916. @deffn {Scheme Procedure} string>=? s1 s2 s3 @dots{}
  2917. Lexicographic ordering predicate; return @code{#t} if, for every pair of
  2918. consecutive string arguments @var{str_i} and @var{str_i+1}, @var{str_i} is
  2919. lexicographically greater than or equal to @var{str_i+1}.
  2920. @end deffn
  2921. @rnindex string-ci=?
  2922. @deffn {Scheme Procedure} string-ci=? s1 s2 s3 @dots{}
  2923. Case-insensitive string equality predicate; return @code{#t} if
  2924. all strings are the same length and their component
  2925. characters match (ignoring case) at each position; otherwise
  2926. return @code{#f}.
  2927. @end deffn
  2928. @rnindex string-ci<?
  2929. @deffn {Scheme Procedure} string-ci<? s1 s2 s3 @dots{}
  2930. Case insensitive lexicographic ordering predicate; return @code{#t} if,
  2931. for every pair of consecutive string arguments @var{str_i} and
  2932. @var{str_i+1}, @var{str_i} is lexicographically less than @var{str_i+1}
  2933. regardless of case.
  2934. @end deffn
  2935. @rnindex string<=?
  2936. @deffn {Scheme Procedure} string-ci<=? s1 s2 s3 @dots{}
  2937. Case insensitive lexicographic ordering predicate; return @code{#t} if,
  2938. for every pair of consecutive string arguments @var{str_i} and
  2939. @var{str_i+1}, @var{str_i} is lexicographically less than or equal to
  2940. @var{str_i+1} regardless of case.
  2941. @end deffn
  2942. @rnindex string-ci>?
  2943. @deffn {Scheme Procedure} string-ci>? s1 s2 s3 @dots{}
  2944. Case insensitive lexicographic ordering predicate; return @code{#t} if,
  2945. for every pair of consecutive string arguments @var{str_i} and
  2946. @var{str_i+1}, @var{str_i} is lexicographically greater than
  2947. @var{str_i+1} regardless of case.
  2948. @end deffn
  2949. @rnindex string-ci>=?
  2950. @deffn {Scheme Procedure} string-ci>=? s1 s2 s3 @dots{}
  2951. Case insensitive lexicographic ordering predicate; return @code{#t} if,
  2952. for every pair of consecutive string arguments @var{str_i} and
  2953. @var{str_i+1}, @var{str_i} is lexicographically greater than or equal to
  2954. @var{str_i+1} regardless of case.
  2955. @end deffn
  2956. @deffn {Scheme Procedure} string-compare s1 s2 proc_lt proc_eq proc_gt [start1 [end1 [start2 [end2]]]]
  2957. @deffnx {C Function} scm_string_compare (s1, s2, proc_lt, proc_eq, proc_gt, start1, end1, start2, end2)
  2958. Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the
  2959. mismatch index, depending upon whether @var{s1} is less than,
  2960. equal to, or greater than @var{s2}. The mismatch index is the
  2961. largest index @var{i} such that for every 0 <= @var{j} <
  2962. @var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] -- that is,
  2963. @var{i} is the first position that does not match.
  2964. @end deffn
  2965. @deffn {Scheme Procedure} string-compare-ci s1 s2 proc_lt proc_eq proc_gt [start1 [end1 [start2 [end2]]]]
  2966. @deffnx {C Function} scm_string_compare_ci (s1, s2, proc_lt, proc_eq, proc_gt, start1, end1, start2, end2)
  2967. Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the
  2968. mismatch index, depending upon whether @var{s1} is less than,
  2969. equal to, or greater than @var{s2}. The mismatch index is the
  2970. largest index @var{i} such that for every 0 <= @var{j} <
  2971. @var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] -- that is,
  2972. @var{i} is the first position where the lowercased letters
  2973. do not match.
  2974. @end deffn
  2975. @deffn {Scheme Procedure} string= s1 s2 [start1 [end1 [start2 [end2]]]]
  2976. @deffnx {C Function} scm_string_eq (s1, s2, start1, end1, start2, end2)
  2977. Return @code{#f} if @var{s1} and @var{s2} are not equal, a true
  2978. value otherwise.
  2979. @end deffn
  2980. @deffn {Scheme Procedure} string<> s1 s2 [start1 [end1 [start2 [end2]]]]
  2981. @deffnx {C Function} scm_string_neq (s1, s2, start1, end1, start2, end2)
  2982. Return @code{#f} if @var{s1} and @var{s2} are equal, a true
  2983. value otherwise.
  2984. @end deffn
  2985. @deffn {Scheme Procedure} string< s1 s2 [start1 [end1 [start2 [end2]]]]
  2986. @deffnx {C Function} scm_string_lt (s1, s2, start1, end1, start2, end2)
  2987. Return @code{#f} if @var{s1} is greater or equal to @var{s2}, a
  2988. true value otherwise.
  2989. @end deffn
  2990. @deffn {Scheme Procedure} string> s1 s2 [start1 [end1 [start2 [end2]]]]
  2991. @deffnx {C Function} scm_string_gt (s1, s2, start1, end1, start2, end2)
  2992. Return @code{#f} if @var{s1} is less or equal to @var{s2}, a
  2993. true value otherwise.
  2994. @end deffn
  2995. @deffn {Scheme Procedure} string<= s1 s2 [start1 [end1 [start2 [end2]]]]
  2996. @deffnx {C Function} scm_string_le (s1, s2, start1, end1, start2, end2)
  2997. Return @code{#f} if @var{s1} is greater to @var{s2}, a true
  2998. value otherwise.
  2999. @end deffn
  3000. @deffn {Scheme Procedure} string>= s1 s2 [start1 [end1 [start2 [end2]]]]
  3001. @deffnx {C Function} scm_string_ge (s1, s2, start1, end1, start2, end2)
  3002. Return @code{#f} if @var{s1} is less to @var{s2}, a true value
  3003. otherwise.
  3004. @end deffn
  3005. @deffn {Scheme Procedure} string-ci= s1 s2 [start1 [end1 [start2 [end2]]]]
  3006. @deffnx {C Function} scm_string_ci_eq (s1, s2, start1, end1, start2, end2)
  3007. Return @code{#f} if @var{s1} and @var{s2} are not equal, a true
  3008. value otherwise. The character comparison is done
  3009. case-insensitively.
  3010. @end deffn
  3011. @deffn {Scheme Procedure} string-ci<> s1 s2 [start1 [end1 [start2 [end2]]]]
  3012. @deffnx {C Function} scm_string_ci_neq (s1, s2, start1, end1, start2, end2)
  3013. Return @code{#f} if @var{s1} and @var{s2} are equal, a true
  3014. value otherwise. The character comparison is done
  3015. case-insensitively.
  3016. @end deffn
  3017. @deffn {Scheme Procedure} string-ci< s1 s2 [start1 [end1 [start2 [end2]]]]
  3018. @deffnx {C Function} scm_string_ci_lt (s1, s2, start1, end1, start2, end2)
  3019. Return @code{#f} if @var{s1} is greater or equal to @var{s2}, a
  3020. true value otherwise. The character comparison is done
  3021. case-insensitively.
  3022. @end deffn
  3023. @deffn {Scheme Procedure} string-ci> s1 s2 [start1 [end1 [start2 [end2]]]]
  3024. @deffnx {C Function} scm_string_ci_gt (s1, s2, start1, end1, start2, end2)
  3025. Return @code{#f} if @var{s1} is less or equal to @var{s2}, a
  3026. true value otherwise. The character comparison is done
  3027. case-insensitively.
  3028. @end deffn
  3029. @deffn {Scheme Procedure} string-ci<= s1 s2 [start1 [end1 [start2 [end2]]]]
  3030. @deffnx {C Function} scm_string_ci_le (s1, s2, start1, end1, start2, end2)
  3031. Return @code{#f} if @var{s1} is greater to @var{s2}, a true
  3032. value otherwise. The character comparison is done
  3033. case-insensitively.
  3034. @end deffn
  3035. @deffn {Scheme Procedure} string-ci>= s1 s2 [start1 [end1 [start2 [end2]]]]
  3036. @deffnx {C Function} scm_string_ci_ge (s1, s2, start1, end1, start2, end2)
  3037. Return @code{#f} if @var{s1} is less to @var{s2}, a true value
  3038. otherwise. The character comparison is done
  3039. case-insensitively.
  3040. @end deffn
  3041. @deffn {Scheme Procedure} string-hash s [bound [start [end]]]
  3042. @deffnx {C Function} scm_substring_hash (s, bound, start, end)
  3043. 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).
  3044. @end deffn
  3045. @deffn {Scheme Procedure} string-hash-ci s [bound [start [end]]]
  3046. @deffnx {C Function} scm_substring_hash_ci (s, bound, start, end)
  3047. 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).
  3048. @end deffn
  3049. Because the same visual appearance of an abstract Unicode character can
  3050. be obtained via multiple sequences of Unicode characters, even the
  3051. case-insensitive string comparison functions described above may return
  3052. @code{#f} when presented with strings containing different
  3053. representations of the same character. For example, the Unicode
  3054. character ``LATIN SMALL LETTER S WITH DOT BELOW AND DOT ABOVE'' can be
  3055. represented with a single character (U+1E69) or by the character ``LATIN
  3056. SMALL LETTER S'' (U+0073) followed by the combining marks ``COMBINING
  3057. DOT BELOW'' (U+0323) and ``COMBINING DOT ABOVE'' (U+0307).
  3058. For this reason, it is often desirable to ensure that the strings
  3059. to be compared are using a mutually consistent representation for every
  3060. character. The Unicode standard defines two methods of normalizing the
  3061. contents of strings: Decomposition, which breaks composite characters
  3062. into a set of constituent characters with an ordering defined by the
  3063. Unicode Standard; and composition, which performs the converse.
  3064. There are two decomposition operations. ``Canonical decomposition''
  3065. produces character sequences that share the same visual appearance as
  3066. the original characters, while ``compatibility decomposition'' produces
  3067. ones whose visual appearances may differ from the originals but which
  3068. represent the same abstract character.
  3069. These operations are encapsulated in the following set of normalization
  3070. forms:
  3071. @table @dfn
  3072. @item NFD
  3073. Characters are decomposed to their canonical forms.
  3074. @item NFKD
  3075. Characters are decomposed to their compatibility forms.
  3076. @item NFC
  3077. Characters are decomposed to their canonical forms, then composed.
  3078. @item NFKC
  3079. Characters are decomposed to their compatibility forms, then composed.
  3080. @end table
  3081. The functions below put their arguments into one of the forms described
  3082. above.
  3083. @deffn {Scheme Procedure} string-normalize-nfd s
  3084. @deffnx {C Function} scm_string_normalize_nfd (s)
  3085. Return the @code{NFD} normalized form of @var{s}.
  3086. @end deffn
  3087. @deffn {Scheme Procedure} string-normalize-nfkd s
  3088. @deffnx {C Function} scm_string_normalize_nfkd (s)
  3089. Return the @code{NFKD} normalized form of @var{s}.
  3090. @end deffn
  3091. @deffn {Scheme Procedure} string-normalize-nfc s
  3092. @deffnx {C Function} scm_string_normalize_nfc (s)
  3093. Return the @code{NFC} normalized form of @var{s}.
  3094. @end deffn
  3095. @deffn {Scheme Procedure} string-normalize-nfkc s
  3096. @deffnx {C Function} scm_string_normalize_nfkc (s)
  3097. Return the @code{NFKC} normalized form of @var{s}.
  3098. @end deffn
  3099. @node String Searching
  3100. @subsubsection String Searching
  3101. @deffn {Scheme Procedure} string-index s char_pred [start [end]]
  3102. @deffnx {C Function} scm_string_index (s, char_pred, start, end)
  3103. Search through the string @var{s} from left to right, returning
  3104. the index of the first occurrence of a character which
  3105. @itemize @bullet
  3106. @item
  3107. equals @var{char_pred}, if it is character,
  3108. @item
  3109. satisfies the predicate @var{char_pred}, if it is a procedure,
  3110. @item
  3111. is in the set @var{char_pred}, if it is a character set.
  3112. @end itemize
  3113. Return @code{#f} if no match is found.
  3114. @end deffn
  3115. @deffn {Scheme Procedure} string-rindex s char_pred [start [end]]
  3116. @deffnx {C Function} scm_string_rindex (s, char_pred, start, end)
  3117. Search through the string @var{s} from right to left, returning
  3118. the index of the last occurrence of a character which
  3119. @itemize @bullet
  3120. @item
  3121. equals @var{char_pred}, if it is character,
  3122. @item
  3123. satisfies the predicate @var{char_pred}, if it is a procedure,
  3124. @item
  3125. is in the set if @var{char_pred} is a character set.
  3126. @end itemize
  3127. Return @code{#f} if no match is found.
  3128. @end deffn
  3129. @deffn {Scheme Procedure} string-prefix-length s1 s2 [start1 [end1 [start2 [end2]]]]
  3130. @deffnx {C Function} scm_string_prefix_length (s1, s2, start1, end1, start2, end2)
  3131. Return the length of the longest common prefix of the two
  3132. strings.
  3133. @end deffn
  3134. @deffn {Scheme Procedure} string-prefix-length-ci s1 s2 [start1 [end1 [start2 [end2]]]]
  3135. @deffnx {C Function} scm_string_prefix_length_ci (s1, s2, start1, end1, start2, end2)
  3136. Return the length of the longest common prefix of the two
  3137. strings, ignoring character case.
  3138. @end deffn
  3139. @deffn {Scheme Procedure} string-suffix-length s1 s2 [start1 [end1 [start2 [end2]]]]
  3140. @deffnx {C Function} scm_string_suffix_length (s1, s2, start1, end1, start2, end2)
  3141. Return the length of the longest common suffix of the two
  3142. strings.
  3143. @end deffn
  3144. @deffn {Scheme Procedure} string-suffix-length-ci s1 s2 [start1 [end1 [start2 [end2]]]]
  3145. @deffnx {C Function} scm_string_suffix_length_ci (s1, s2, start1, end1, start2, end2)
  3146. Return the length of the longest common suffix of the two
  3147. strings, ignoring character case.
  3148. @end deffn
  3149. @deffn {Scheme Procedure} string-prefix? s1 s2 [start1 [end1 [start2 [end2]]]]
  3150. @deffnx {C Function} scm_string_prefix_p (s1, s2, start1, end1, start2, end2)
  3151. Is @var{s1} a prefix of @var{s2}?
  3152. @end deffn
  3153. @deffn {Scheme Procedure} string-prefix-ci? s1 s2 [start1 [end1 [start2 [end2]]]]
  3154. @deffnx {C Function} scm_string_prefix_ci_p (s1, s2, start1, end1, start2, end2)
  3155. Is @var{s1} a prefix of @var{s2}, ignoring character case?
  3156. @end deffn
  3157. @deffn {Scheme Procedure} string-suffix? s1 s2 [start1 [end1 [start2 [end2]]]]
  3158. @deffnx {C Function} scm_string_suffix_p (s1, s2, start1, end1, start2, end2)
  3159. Is @var{s1} a suffix of @var{s2}?
  3160. @end deffn
  3161. @deffn {Scheme Procedure} string-suffix-ci? s1 s2 [start1 [end1 [start2 [end2]]]]
  3162. @deffnx {C Function} scm_string_suffix_ci_p (s1, s2, start1, end1, start2, end2)
  3163. Is @var{s1} a suffix of @var{s2}, ignoring character case?
  3164. @end deffn
  3165. @deffn {Scheme Procedure} string-index-right s char_pred [start [end]]
  3166. @deffnx {C Function} scm_string_index_right (s, char_pred, start, end)
  3167. Search through the string @var{s} from right to left, returning
  3168. the index of the last occurrence of a character which
  3169. @itemize @bullet
  3170. @item
  3171. equals @var{char_pred}, if it is character,
  3172. @item
  3173. satisfies the predicate @var{char_pred}, if it is a procedure,
  3174. @item
  3175. is in the set if @var{char_pred} is a character set.
  3176. @end itemize
  3177. Return @code{#f} if no match is found.
  3178. @end deffn
  3179. @deffn {Scheme Procedure} string-skip s char_pred [start [end]]
  3180. @deffnx {C Function} scm_string_skip (s, char_pred, start, end)
  3181. Search through the string @var{s} from left to right, returning
  3182. the index of the first occurrence of a character which
  3183. @itemize @bullet
  3184. @item
  3185. does not equal @var{char_pred}, if it is character,
  3186. @item
  3187. does not satisfy the predicate @var{char_pred}, if it is a
  3188. procedure,
  3189. @item
  3190. is not in the set if @var{char_pred} is a character set.
  3191. @end itemize
  3192. @end deffn
  3193. @deffn {Scheme Procedure} string-skip-right s char_pred [start [end]]
  3194. @deffnx {C Function} scm_string_skip_right (s, char_pred, start, end)
  3195. Search through the string @var{s} from right to left, returning
  3196. the index of the last occurrence of a character which
  3197. @itemize @bullet
  3198. @item
  3199. does not equal @var{char_pred}, if it is character,
  3200. @item
  3201. does not satisfy the predicate @var{char_pred}, if it is a
  3202. procedure,
  3203. @item
  3204. is not in the set if @var{char_pred} is a character set.
  3205. @end itemize
  3206. @end deffn
  3207. @deffn {Scheme Procedure} string-count s char_pred [start [end]]
  3208. @deffnx {C Function} scm_string_count (s, char_pred, start, end)
  3209. Return the count of the number of characters in the string
  3210. @var{s} which
  3211. @itemize @bullet
  3212. @item
  3213. equals @var{char_pred}, if it is character,
  3214. @item
  3215. satisfies the predicate @var{char_pred}, if it is a procedure.
  3216. @item
  3217. is in the set @var{char_pred}, if it is a character set.
  3218. @end itemize
  3219. @end deffn
  3220. @deffn {Scheme Procedure} string-contains s1 s2 [start1 [end1 [start2 [end2]]]]
  3221. @deffnx {C Function} scm_string_contains (s1, s2, start1, end1, start2, end2)
  3222. Does string @var{s1} contain string @var{s2}? Return the index
  3223. in @var{s1} where @var{s2} occurs as a substring, or false.
  3224. The optional start/end indices restrict the operation to the
  3225. indicated substrings.
  3226. @end deffn
  3227. @deffn {Scheme Procedure} string-contains-ci s1 s2 [start1 [end1 [start2 [end2]]]]
  3228. @deffnx {C Function} scm_string_contains_ci (s1, s2, start1, end1, start2, end2)
  3229. Does string @var{s1} contain string @var{s2}? Return the index
  3230. in @var{s1} where @var{s2} occurs as a substring, or false.
  3231. The optional start/end indices restrict the operation to the
  3232. indicated substrings. Character comparison is done
  3233. case-insensitively.
  3234. @end deffn
  3235. @node Alphabetic Case Mapping
  3236. @subsubsection Alphabetic Case Mapping
  3237. These are procedures for mapping strings to their upper- or lower-case
  3238. equivalents, respectively, or for capitalizing strings.
  3239. They use the basic case mapping rules for Unicode characters. No
  3240. special language or context rules are considered. The resulting strings
  3241. are guaranteed to be the same length as the input strings.
  3242. @xref{Character Case Mapping, the @code{(ice-9
  3243. i18n)} module}, for locale-dependent case conversions.
  3244. @deffn {Scheme Procedure} string-upcase str [start [end]]
  3245. @deffnx {C Function} scm_substring_upcase (str, start, end)
  3246. @deffnx {C Function} scm_string_upcase (str)
  3247. Upcase every character in @code{str}.
  3248. @end deffn
  3249. @deffn {Scheme Procedure} string-upcase! str [start [end]]
  3250. @deffnx {C Function} scm_substring_upcase_x (str, start, end)
  3251. @deffnx {C Function} scm_string_upcase_x (str)
  3252. Destructively upcase every character in @code{str}.
  3253. @lisp
  3254. (string-upcase! y)
  3255. @result{} "ARRDEFG"
  3256. y
  3257. @result{} "ARRDEFG"
  3258. @end lisp
  3259. @end deffn
  3260. @deffn {Scheme Procedure} string-downcase str [start [end]]
  3261. @deffnx {C Function} scm_substring_downcase (str, start, end)
  3262. @deffnx {C Function} scm_string_downcase (str)
  3263. Downcase every character in @var{str}.
  3264. @end deffn
  3265. @deffn {Scheme Procedure} string-downcase! str [start [end]]
  3266. @deffnx {C Function} scm_substring_downcase_x (str, start, end)
  3267. @deffnx {C Function} scm_string_downcase_x (str)
  3268. Destructively downcase every character in @var{str}.
  3269. @lisp
  3270. y
  3271. @result{} "ARRDEFG"
  3272. (string-downcase! y)
  3273. @result{} "arrdefg"
  3274. y
  3275. @result{} "arrdefg"
  3276. @end lisp
  3277. @end deffn
  3278. @deffn {Scheme Procedure} string-capitalize str
  3279. @deffnx {C Function} scm_string_capitalize (str)
  3280. Return a freshly allocated string with the characters in
  3281. @var{str}, where the first character of every word is
  3282. capitalized.
  3283. @end deffn
  3284. @deffn {Scheme Procedure} string-capitalize! str
  3285. @deffnx {C Function} scm_string_capitalize_x (str)
  3286. Upcase the first character of every word in @var{str}
  3287. destructively and return @var{str}.
  3288. @lisp
  3289. y @result{} "hello world"
  3290. (string-capitalize! y) @result{} "Hello World"
  3291. y @result{} "Hello World"
  3292. @end lisp
  3293. @end deffn
  3294. @deffn {Scheme Procedure} string-titlecase str [start [end]]
  3295. @deffnx {C Function} scm_string_titlecase (str, start, end)
  3296. Titlecase every first character in a word in @var{str}.
  3297. @end deffn
  3298. @deffn {Scheme Procedure} string-titlecase! str [start [end]]
  3299. @deffnx {C Function} scm_string_titlecase_x (str, start, end)
  3300. Destructively titlecase every first character in a word in
  3301. @var{str}.
  3302. @end deffn
  3303. @node Reversing and Appending Strings
  3304. @subsubsection Reversing and Appending Strings
  3305. @deffn {Scheme Procedure} string-reverse str [start [end]]
  3306. @deffnx {C Function} scm_string_reverse (str, start, end)
  3307. Reverse the string @var{str}. The optional arguments
  3308. @var{start} and @var{end} delimit the region of @var{str} to
  3309. operate on.
  3310. @end deffn
  3311. @deffn {Scheme Procedure} string-reverse! str [start [end]]
  3312. @deffnx {C Function} scm_string_reverse_x (str, start, end)
  3313. Reverse the string @var{str} in-place. The optional arguments
  3314. @var{start} and @var{end} delimit the region of @var{str} to
  3315. operate on. The return value is unspecified.
  3316. @end deffn
  3317. @rnindex string-append
  3318. @deffn {Scheme Procedure} string-append arg @dots{}
  3319. @deffnx {C Function} scm_string_append (args)
  3320. Return a newly allocated string whose characters form the
  3321. concatenation of the given strings, @var{arg} @enddots{}.
  3322. @example
  3323. (let ((h "hello "))
  3324. (string-append h "world"))
  3325. @result{} "hello world"
  3326. @end example
  3327. @end deffn
  3328. @deffn {Scheme Procedure} string-append/shared arg @dots{}
  3329. @deffnx {C Function} scm_string_append_shared (args)
  3330. Like @code{string-append}, but the result may share memory
  3331. with the argument strings.
  3332. @end deffn
  3333. @deffn {Scheme Procedure} string-concatenate ls
  3334. @deffnx {C Function} scm_string_concatenate (ls)
  3335. Append the elements (which must be strings) of @var{ls} together into a
  3336. single string. Guaranteed to return a freshly allocated string.
  3337. @end deffn
  3338. @deffn {Scheme Procedure} string-concatenate-reverse ls [final_string [end]]
  3339. @deffnx {C Function} scm_string_concatenate_reverse (ls, final_string, end)
  3340. Without optional arguments, this procedure is equivalent to
  3341. @lisp
  3342. (string-concatenate (reverse ls))
  3343. @end lisp
  3344. If the optional argument @var{final_string} is specified, it is
  3345. consed onto the beginning to @var{ls} before performing the
  3346. list-reverse and string-concatenate operations. If @var{end}
  3347. is given, only the characters of @var{final_string} up to index
  3348. @var{end} are used.
  3349. Guaranteed to return a freshly allocated string.
  3350. @end deffn
  3351. @deffn {Scheme Procedure} string-concatenate/shared ls
  3352. @deffnx {C Function} scm_string_concatenate_shared (ls)
  3353. Like @code{string-concatenate}, but the result may share memory
  3354. with the strings in the list @var{ls}.
  3355. @end deffn
  3356. @deffn {Scheme Procedure} string-concatenate-reverse/shared ls [final_string [end]]
  3357. @deffnx {C Function} scm_string_concatenate_reverse_shared (ls, final_string, end)
  3358. Like @code{string-concatenate-reverse}, but the result may
  3359. share memory with the strings in the @var{ls} arguments.
  3360. @end deffn
  3361. @node Mapping Folding and Unfolding
  3362. @subsubsection Mapping, Folding, and Unfolding
  3363. @deffn {Scheme Procedure} string-map proc s [start [end]]
  3364. @deffnx {C Function} scm_string_map (proc, s, start, end)
  3365. @var{proc} is a char->char procedure, it is mapped over
  3366. @var{s}. The order in which the procedure is applied to the
  3367. string elements is not specified.
  3368. @end deffn
  3369. @deffn {Scheme Procedure} string-map! proc s [start [end]]
  3370. @deffnx {C Function} scm_string_map_x (proc, s, start, end)
  3371. @var{proc} is a char->char procedure, it is mapped over
  3372. @var{s}. The order in which the procedure is applied to the
  3373. string elements is not specified. The string @var{s} is
  3374. modified in-place, the return value is not specified.
  3375. @end deffn
  3376. @deffn {Scheme Procedure} string-for-each proc s [start [end]]
  3377. @deffnx {C Function} scm_string_for_each (proc, s, start, end)
  3378. @var{proc} is mapped over @var{s} in left-to-right order. The
  3379. return value is not specified.
  3380. @end deffn
  3381. @deffn {Scheme Procedure} string-for-each-index proc s [start [end]]
  3382. @deffnx {C Function} scm_string_for_each_index (proc, s, start, end)
  3383. Call @code{(@var{proc} i)} for each index i in @var{s}, from left to
  3384. right.
  3385. For example, to change characters to alternately upper and lower case,
  3386. @example
  3387. (define str (string-copy "studly"))
  3388. (string-for-each-index
  3389. (lambda (i)
  3390. (string-set! str i
  3391. ((if (even? i) char-upcase char-downcase)
  3392. (string-ref str i))))
  3393. str)
  3394. str @result{} "StUdLy"
  3395. @end example
  3396. @end deffn
  3397. @deffn {Scheme Procedure} string-fold kons knil s [start [end]]
  3398. @deffnx {C Function} scm_string_fold (kons, knil, s, start, end)
  3399. Fold @var{kons} over the characters of @var{s}, with @var{knil}
  3400. as the terminating element, from left to right. @var{kons}
  3401. must expect two arguments: The actual character and the last
  3402. result of @var{kons}' application.
  3403. @end deffn
  3404. @deffn {Scheme Procedure} string-fold-right kons knil s [start [end]]
  3405. @deffnx {C Function} scm_string_fold_right (kons, knil, s, start, end)
  3406. Fold @var{kons} over the characters of @var{s}, with @var{knil}
  3407. as the terminating element, from right to left. @var{kons}
  3408. must expect two arguments: The actual character and the last
  3409. result of @var{kons}' application.
  3410. @end deffn
  3411. @deffn {Scheme Procedure} string-unfold p f g seed [base [make_final]]
  3412. @deffnx {C Function} scm_string_unfold (p, f, g, seed, base, make_final)
  3413. @itemize @bullet
  3414. @item @var{g} is used to generate a series of @emph{seed}
  3415. values from the initial @var{seed}: @var{seed}, (@var{g}
  3416. @var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),
  3417. @dots{}
  3418. @item @var{p} tells us when to stop -- when it returns true
  3419. when applied to one of these seed values.
  3420. @item @var{f} maps each seed value to the corresponding
  3421. character in the result string. These chars are assembled
  3422. into the string in a left-to-right order.
  3423. @item @var{base} is the optional initial/leftmost portion
  3424. of the constructed string; it default to the empty
  3425. string.
  3426. @item @var{make_final} is applied to the terminal seed
  3427. value (on which @var{p} returns true) to produce
  3428. the final/rightmost portion of the constructed string.
  3429. The default is nothing extra.
  3430. @end itemize
  3431. @end deffn
  3432. @deffn {Scheme Procedure} string-unfold-right p f g seed [base [make_final]]
  3433. @deffnx {C Function} scm_string_unfold_right (p, f, g, seed, base, make_final)
  3434. @itemize @bullet
  3435. @item @var{g} is used to generate a series of @emph{seed}
  3436. values from the initial @var{seed}: @var{seed}, (@var{g}
  3437. @var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),
  3438. @dots{}
  3439. @item @var{p} tells us when to stop -- when it returns true
  3440. when applied to one of these seed values.
  3441. @item @var{f} maps each seed value to the corresponding
  3442. character in the result string. These chars are assembled
  3443. into the string in a right-to-left order.
  3444. @item @var{base} is the optional initial/rightmost portion
  3445. of the constructed string; it default to the empty
  3446. string.
  3447. @item @var{make_final} is applied to the terminal seed
  3448. value (on which @var{p} returns true) to produce
  3449. the final/leftmost portion of the constructed string.
  3450. It defaults to @code{(lambda (x) )}.
  3451. @end itemize
  3452. @end deffn
  3453. @node Miscellaneous String Operations
  3454. @subsubsection Miscellaneous String Operations
  3455. @deffn {Scheme Procedure} xsubstring s from [to [start [end]]]
  3456. @deffnx {C Function} scm_xsubstring (s, from, to, start, end)
  3457. This is the @emph{extended substring} procedure that implements
  3458. replicated copying of a substring of some string.
  3459. @var{s} is a string, @var{start} and @var{end} are optional
  3460. arguments that demarcate a substring of @var{s}, defaulting to
  3461. 0 and the length of @var{s}. Replicate this substring up and
  3462. down index space, in both the positive and negative directions.
  3463. @code{xsubstring} returns the substring of this string
  3464. beginning at index @var{from}, and ending at @var{to}, which
  3465. defaults to @var{from} + (@var{end} - @var{start}).
  3466. @end deffn
  3467. @deffn {Scheme Procedure} string-xcopy! target tstart s sfrom [sto [start [end]]]
  3468. @deffnx {C Function} scm_string_xcopy_x (target, tstart, s, sfrom, sto, start, end)
  3469. Exactly the same as @code{xsubstring}, but the extracted text
  3470. is written into the string @var{target} starting at index
  3471. @var{tstart}. The operation is not defined if @code{(eq?
  3472. @var{target} @var{s})} or these arguments share storage -- you
  3473. cannot copy a string on top of itself.
  3474. @end deffn
  3475. @deffn {Scheme Procedure} string-replace s1 s2 [start1 [end1 [start2 [end2]]]]
  3476. @deffnx {C Function} scm_string_replace (s1, s2, start1, end1, start2, end2)
  3477. Return the string @var{s1}, but with the characters
  3478. @var{start1} @dots{} @var{end1} replaced by the characters
  3479. @var{start2} @dots{} @var{end2} from @var{s2}.
  3480. @end deffn
  3481. @deffn {Scheme Procedure} string-tokenize s [token_set [start [end]]]
  3482. @deffnx {C Function} scm_string_tokenize (s, token_set, start, end)
  3483. Split the string @var{s} into a list of substrings, where each
  3484. substring is a maximal non-empty contiguous sequence of
  3485. characters from the character set @var{token_set}, which
  3486. defaults to @code{char-set:graphic}.
  3487. If @var{start} or @var{end} indices are provided, they restrict
  3488. @code{string-tokenize} to operating on the indicated substring
  3489. of @var{s}.
  3490. @end deffn
  3491. @deffn {Scheme Procedure} string-filter char_pred s [start [end]]
  3492. @deffnx {C Function} scm_string_filter (char_pred, s, start, end)
  3493. Filter the string @var{s}, retaining only those characters which
  3494. satisfy @var{char_pred}.
  3495. If @var{char_pred} is a procedure, it is applied to each character as
  3496. a predicate, if it is a character, it is tested for equality and if it
  3497. is a character set, it is tested for membership.
  3498. @end deffn
  3499. @deffn {Scheme Procedure} string-delete char_pred s [start [end]]
  3500. @deffnx {C Function} scm_string_delete (char_pred, s, start, end)
  3501. Delete characters satisfying @var{char_pred} from @var{s}.
  3502. If @var{char_pred} is a procedure, it is applied to each character as
  3503. a predicate, if it is a character, it is tested for equality and if it
  3504. is a character set, it is tested for membership.
  3505. @end deffn
  3506. The following additional functions are available in the module @code{(ice-9 string-fun)}. They can be used with:
  3507. @example
  3508. (use-modules (ice-9 string-fun))
  3509. @end example
  3510. @deffn {Scheme Procedure} string-replace-substring str substring replacement
  3511. Return a new string where every instance of @var{substring} in string
  3512. @var{str} has been replaced by @var{replacement}. For example:
  3513. @lisp
  3514. (string-replace-substring "a ring of strings" "ring" "rut")
  3515. @result{} "a rut of struts"
  3516. @end lisp
  3517. @end deffn
  3518. @node Representing Strings as Bytes
  3519. @subsubsection Representing Strings as Bytes
  3520. Out in the cold world outside of Guile, not all strings are treated in
  3521. the same way. Out there there are only bytes, and there are many ways
  3522. of representing a strings (sequences of characters) as binary data
  3523. (sequences of bytes).
  3524. As a user, usually you don't have to think about this very much. When
  3525. you type on your keyboard, your system encodes your keystrokes as bytes
  3526. according to the locale that you have configured on your computer.
  3527. Guile uses the locale to decode those bytes back into characters --
  3528. hopefully the same characters that you typed in.
  3529. All is not so clear when dealing with a system with multiple users, such
  3530. as a web server. Your web server might get a request from one user for
  3531. data encoded in the ISO-8859-1 character set, and then another request
  3532. from a different user for UTF-8 data.
  3533. @cindex iconv
  3534. @cindex character encoding
  3535. Guile provides an @dfn{iconv} module for converting between strings and
  3536. sequences of bytes. @xref{Bytevectors}, for more on how Guile
  3537. represents raw byte sequences. This module gets its name from the
  3538. common @sc{unix} command of the same name.
  3539. Note that often it is sufficient to just read and write strings from
  3540. ports instead of using these functions. To do this, specify the port
  3541. encoding using @code{set-port-encoding!}. @xref{Ports}, for more on
  3542. ports and character encodings.
  3543. Unlike the rest of the procedures in this section, you have to load the
  3544. @code{iconv} module before having access to these procedures:
  3545. @example
  3546. (use-modules (ice-9 iconv))
  3547. @end example
  3548. @deffn {Scheme Procedure} string->bytevector string encoding [conversion-strategy]
  3549. Encode @var{string} as a sequence of bytes.
  3550. The string will be encoded in the character set specified by the
  3551. @var{encoding} string. If the string has characters that cannot be
  3552. represented in the encoding, by default this procedure raises an
  3553. @code{encoding-error}. Pass a @var{conversion-strategy} argument to
  3554. specify other behaviors.
  3555. The return value is a bytevector. @xref{Bytevectors}, for more on
  3556. bytevectors. @xref{Ports}, for more on character encodings and
  3557. conversion strategies.
  3558. @end deffn
  3559. @deffn {Scheme Procedure} bytevector->string bytevector encoding [conversion-strategy]
  3560. Decode @var{bytevector} into a string.
  3561. The bytes will be decoded from the character set by the @var{encoding}
  3562. string. If the bytes do not form a valid encoding, by default this
  3563. procedure raises an @code{decoding-error}. As with
  3564. @code{string->bytevector}, pass the optional @var{conversion-strategy}
  3565. argument to modify this behavior. @xref{Ports}, for more on character
  3566. encodings and conversion strategies.
  3567. @end deffn
  3568. @deffn {Scheme Procedure} call-with-output-encoded-string encoding proc [conversion-strategy]
  3569. Like @code{call-with-output-string}, but instead of returning a string,
  3570. returns a encoding of the string according to @var{encoding}, as a
  3571. bytevector. This procedure can be more efficient than collecting a
  3572. string and then converting it via @code{string->bytevector}.
  3573. @end deffn
  3574. @node Conversion to/from C
  3575. @subsubsection Conversion to/from C
  3576. When creating a Scheme string from a C string or when converting a
  3577. Scheme string to a C string, the concept of character encoding becomes
  3578. important.
  3579. In C, a string is just a sequence of bytes, and the character encoding
  3580. describes the relation between these bytes and the actual characters
  3581. that make up the string. For Scheme strings, character encoding is not
  3582. an issue (most of the time), since in Scheme you usually treat strings
  3583. as character sequences, not byte sequences.
  3584. Converting to C and converting from C each have their own challenges.
  3585. When converting from C to Scheme, it is important that the sequence of
  3586. bytes in the C string be valid with respect to its encoding. ASCII
  3587. strings, for example, can't have any bytes greater than 127. An ASCII
  3588. byte greater than 127 is considered @emph{ill-formed} and cannot be
  3589. converted into a Scheme character.
  3590. Problems can occur in the reverse operation as well. Not all character
  3591. encodings can hold all possible Scheme characters. Some encodings, like
  3592. ASCII for example, can only describe a small subset of all possible
  3593. characters. So, when converting to C, one must first decide what to do
  3594. with Scheme characters that can't be represented in the C string.
  3595. Converting a Scheme string to a C string will often allocate fresh
  3596. memory to hold the result. You must take care that this memory is
  3597. properly freed eventually. In many cases, this can be achieved by
  3598. using @code{scm_dynwind_free} inside an appropriate dynwind context,
  3599. @xref{Dynamic Wind}.
  3600. @deftypefn {C Function} SCM scm_from_locale_string (const char *str)
  3601. @deftypefnx {C Function} SCM scm_from_locale_stringn (const char *str, size_t len)
  3602. Creates a new Scheme string that has the same contents as @var{str} when
  3603. interpreted in the character encoding of the current locale.
  3604. For @code{scm_from_locale_string}, @var{str} must be null-terminated.
  3605. For @code{scm_from_locale_stringn}, @var{len} specifies the length of
  3606. @var{str} in bytes, and @var{str} does not need to be null-terminated.
  3607. If @var{len} is @code{(size_t)-1}, then @var{str} does need to be
  3608. null-terminated and the real length will be found with @code{strlen}.
  3609. If the C string is ill-formed, an error will be raised.
  3610. Note that these functions should @emph{not} be used to convert C string
  3611. constants, because there is no guarantee that the current locale will
  3612. match that of the execution character set, used for string and character
  3613. constants. Most modern C compilers use UTF-8 by default, so to convert
  3614. C string constants we recommend @code{scm_from_utf8_string}.
  3615. @end deftypefn
  3616. @deftypefn {C Function} SCM scm_take_locale_string (char *str)
  3617. @deftypefnx {C Function} SCM scm_take_locale_stringn (char *str, size_t len)
  3618. Like @code{scm_from_locale_string} and @code{scm_from_locale_stringn},
  3619. respectively, but also frees @var{str} with @code{free} eventually.
  3620. Thus, you can use this function when you would free @var{str} anyway
  3621. immediately after creating the Scheme string. In certain cases, Guile
  3622. can then use @var{str} directly as its internal representation.
  3623. @end deftypefn
  3624. @deftypefn {C Function} {char *} scm_to_locale_string (SCM str)
  3625. @deftypefnx {C Function} {char *} scm_to_locale_stringn (SCM str, size_t *lenp)
  3626. Returns a C string with the same contents as @var{str} in the character
  3627. encoding of the current locale. The C string must be freed with
  3628. @code{free} eventually, maybe by using @code{scm_dynwind_free},
  3629. @xref{Dynamic Wind}.
  3630. For @code{scm_to_locale_string}, the returned string is
  3631. null-terminated and an error is signaled when @var{str} contains
  3632. @code{#\nul} characters.
  3633. For @code{scm_to_locale_stringn} and @var{lenp} not @code{NULL},
  3634. @var{str} might contain @code{#\nul} characters and the length of the
  3635. returned string in bytes is stored in @code{*@var{lenp}}. The
  3636. returned string will not be null-terminated in this case. If
  3637. @var{lenp} is @code{NULL}, @code{scm_to_locale_stringn} behaves like
  3638. @code{scm_to_locale_string}.
  3639. If a character in @var{str} cannot be represented in the character
  3640. encoding of the current locale, the default port conversion strategy is
  3641. used. @xref{Ports}, for more on conversion strategies.
  3642. If the conversion strategy is @code{error}, an error will be raised. If
  3643. it is @code{substitute}, a replacement character, such as a question
  3644. mark, will be inserted in its place. If it is @code{escape}, a hex
  3645. escape will be inserted in its place.
  3646. @end deftypefn
  3647. @deftypefn {C Function} size_t scm_to_locale_stringbuf (SCM str, char *buf, size_t max_len)
  3648. Puts @var{str} as a C string in the current locale encoding into the
  3649. memory pointed to by @var{buf}. The buffer at @var{buf} has room for
  3650. @var{max_len} bytes and @code{scm_to_local_stringbuf} will never store
  3651. more than that. No terminating @code{'\0'} will be stored.
  3652. The return value of @code{scm_to_locale_stringbuf} is the number of
  3653. bytes that are needed for all of @var{str}, regardless of whether
  3654. @var{buf} was large enough to hold them. Thus, when the return value
  3655. is larger than @var{max_len}, only @var{max_len} bytes have been
  3656. stored and you probably need to try again with a larger buffer.
  3657. @end deftypefn
  3658. For most situations, string conversion should occur using the current
  3659. locale, such as with the functions above. But there may be cases where
  3660. one wants to convert strings from a character encoding other than the
  3661. locale's character encoding. For these cases, the lower-level functions
  3662. @code{scm_to_stringn} and @code{scm_from_stringn} are provided. These
  3663. functions should seldom be necessary if one is properly using locales.
  3664. @deftp {C Type} scm_t_string_failed_conversion_handler
  3665. This is an enumerated type that can take one of three values:
  3666. @code{SCM_FAILED_CONVERSION_ERROR},
  3667. @code{SCM_FAILED_CONVERSION_QUESTION_MARK}, and
  3668. @code{SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE}. They are used to indicate
  3669. a strategy for handling characters that cannot be converted to or from a
  3670. given character encoding. @code{SCM_FAILED_CONVERSION_ERROR} indicates
  3671. that a conversion should throw an error if some characters cannot be
  3672. converted. @code{SCM_FAILED_CONVERSION_QUESTION_MARK} indicates that a
  3673. conversion should replace unconvertable characters with the question
  3674. mark character. And, @code{SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE}
  3675. requests that a conversion should replace an unconvertable character
  3676. with an escape sequence.
  3677. While all three strategies apply when converting Scheme strings to C,
  3678. only @code{SCM_FAILED_CONVERSION_ERROR} and
  3679. @code{SCM_FAILED_CONVERSION_QUESTION_MARK} can be used when converting C
  3680. strings to Scheme.
  3681. @end deftp
  3682. @deftypefn {C Function} char *scm_to_stringn (SCM str, size_t *lenp, const char *encoding, scm_t_string_failed_conversion_handler handler)
  3683. This function returns a newly allocated C string from the Guile string
  3684. @var{str}. The length of the returned string in bytes will be returned in
  3685. @var{lenp}. The character encoding of the C string is passed as the ASCII,
  3686. null-terminated C string @var{encoding}. The @var{handler} parameter
  3687. gives a strategy for dealing with characters that cannot be converted
  3688. into @var{encoding}.
  3689. If @var{lenp} is @code{NULL}, this function will return a null-terminated C
  3690. string. It will throw an error if the string contains a null
  3691. character.
  3692. The Scheme interface to this function is @code{string->bytevector}, from the
  3693. @code{ice-9 iconv} module. @xref{Representing Strings as Bytes}.
  3694. @end deftypefn
  3695. @deftypefn {C Function} SCM scm_from_stringn (const char *str, size_t len, const char *encoding, scm_t_string_failed_conversion_handler handler)
  3696. This function returns a scheme string from the C string @var{str}. The
  3697. length in bytes of the C string is input as @var{len}. The encoding of the C
  3698. string is passed as the ASCII, null-terminated C string @code{encoding}.
  3699. The @var{handler} parameters suggests a strategy for dealing with
  3700. unconvertable characters.
  3701. The Scheme interface to this function is @code{bytevector->string}.
  3702. @xref{Representing Strings as Bytes}.
  3703. @end deftypefn
  3704. The following conversion functions are provided as a convenience for the
  3705. most commonly used encodings.
  3706. @deftypefn {C Function} SCM scm_from_latin1_string (const char *str)
  3707. @deftypefnx {C Function} SCM scm_from_utf8_string (const char *str)
  3708. @deftypefnx {C Function} SCM scm_from_utf32_string (const scm_t_wchar *str)
  3709. Return a scheme string from the null-terminated C string @var{str},
  3710. which is ISO-8859-1-, UTF-8-, or UTF-32-encoded. These functions should
  3711. be used to convert hard-coded C string constants into Scheme strings.
  3712. @end deftypefn
  3713. @deftypefn {C Function} SCM scm_from_latin1_stringn (const char *str, size_t len)
  3714. @deftypefnx {C Function} SCM scm_from_utf8_stringn (const char *str, size_t len)
  3715. @deftypefnx {C Function} SCM scm_from_utf32_stringn (const scm_t_wchar *str, size_t len)
  3716. Return a scheme string from C string @var{str}, which is ISO-8859-1-,
  3717. UTF-8-, or UTF-32-encoded, of length @var{len}. @var{len} is the number
  3718. of bytes pointed to by @var{str} for @code{scm_from_latin1_stringn} and
  3719. @code{scm_from_utf8_stringn}; it is the number of elements (code points)
  3720. in @var{str} in the case of @code{scm_from_utf32_stringn}.
  3721. @end deftypefn
  3722. @deftypefn {C function} char *scm_to_latin1_stringn (SCM str, size_t *lenp)
  3723. @deftypefnx {C function} char *scm_to_utf8_stringn (SCM str, size_t *lenp)
  3724. @deftypefnx {C function} scm_t_wchar *scm_to_utf32_stringn (SCM str, size_t *lenp)
  3725. Return a newly allocated, ISO-8859-1-, UTF-8-, or UTF-32-encoded C string
  3726. from Scheme string @var{str}. An error is thrown when @var{str}
  3727. cannot be converted to the specified encoding. If @var{lenp} is
  3728. @code{NULL}, the returned C string will be null terminated, and an error
  3729. will be thrown if the C string would otherwise contain null
  3730. characters. If @var{lenp} is not @code{NULL}, the string is not null terminated,
  3731. and the length of the returned string is returned in @var{lenp}. The length
  3732. returned is the number of bytes for @code{scm_to_latin1_stringn} and
  3733. @code{scm_to_utf8_stringn}; it is the number of elements (code points)
  3734. for @code{scm_to_utf32_stringn}.
  3735. @end deftypefn
  3736. It is not often the case, but sometimes when you are dealing with the
  3737. implementation details of a port, you need to encode and decode strings
  3738. according to the encoding and conversion strategy of the port. There
  3739. are some convenience functions for that purpose as well.
  3740. @deftypefn {C Function} SCM scm_from_port_string (const char *str, SCM port)
  3741. @deftypefnx {C Function} SCM scm_from_port_stringn (const char *str, size_t len, SCM port)
  3742. @deftypefnx {C Function} char* scm_to_port_string (SCM str, SCM port)
  3743. @deftypefnx {C Function} char* scm_to_port_stringn (SCM str, size_t *lenp, SCM port)
  3744. Like @code{scm_from_stringn} and friends, except they take their
  3745. encoding and conversion strategy from a given port object.
  3746. @end deftypefn
  3747. @node String Internals
  3748. @subsubsection String Internals
  3749. Guile stores each string in memory as a contiguous array of Unicode code
  3750. points along with an associated set of attributes. If all of the code
  3751. points of a string have an integer range between 0 and 255 inclusive,
  3752. the code point array is stored as one byte per code point: it is stored
  3753. as an ISO-8859-1 (aka Latin-1) string. If any of the code points of the
  3754. string has an integer value greater that 255, the code point array is
  3755. stored as four bytes per code point: it is stored as a UTF-32 string.
  3756. Conversion between the one-byte-per-code-point and
  3757. four-bytes-per-code-point representations happens automatically as
  3758. necessary.
  3759. No API is provided to set the internal representation of strings;
  3760. however, there are pair of procedures available to query it. These are
  3761. debugging procedures. Using them in production code is discouraged,
  3762. since the details of Guile's internal representation of strings may
  3763. change from release to release.
  3764. @deffn {Scheme Procedure} string-bytes-per-char str
  3765. @deffnx {C Function} scm_string_bytes_per_char (str)
  3766. Return the number of bytes used to encode a Unicode code point in string
  3767. @var{str}. The result is one or four.
  3768. @end deffn
  3769. @deffn {Scheme Procedure} %string-dump str
  3770. @deffnx {C Function} scm_sys_string_dump (str)
  3771. Returns an association list containing debugging information for
  3772. @var{str}. The association list has the following entries.
  3773. @table @code
  3774. @item string
  3775. The string itself.
  3776. @item start
  3777. The start index of the string into its stringbuf
  3778. @item length
  3779. The length of the string
  3780. @item shared
  3781. If this string is a substring, it returns its
  3782. parent string. Otherwise, it returns @code{#f}
  3783. @item read-only
  3784. @code{#t} if the string is read-only
  3785. @item stringbuf-chars
  3786. A new string containing this string's stringbuf's characters
  3787. @item stringbuf-length
  3788. The number of characters in this stringbuf
  3789. @item stringbuf-shared
  3790. @code{#t} if this stringbuf is shared
  3791. @item stringbuf-wide
  3792. @code{#t} if this stringbuf's characters are stored in a 32-bit buffer,
  3793. or @code{#f} if they are stored in an 8-bit buffer
  3794. @end table
  3795. @end deffn
  3796. @node Symbols
  3797. @subsection Symbols
  3798. @tpindex Symbols
  3799. Symbols in Scheme are widely used in three ways: as items of discrete
  3800. data, as lookup keys for alists and hash tables, and to denote variable
  3801. references.
  3802. A @dfn{symbol} is similar to a string in that it is defined by a
  3803. sequence of characters. The sequence of characters is known as the
  3804. symbol's @dfn{name}. In the usual case --- that is, where the symbol's
  3805. name doesn't include any characters that could be confused with other
  3806. elements of Scheme syntax --- a symbol is written in a Scheme program by
  3807. writing the sequence of characters that make up the name, @emph{without}
  3808. any quotation marks or other special syntax. For example, the symbol
  3809. whose name is ``multiply-by-2'' is written, simply:
  3810. @lisp
  3811. multiply-by-2
  3812. @end lisp
  3813. Notice how this differs from a @emph{string} with contents
  3814. ``multiply-by-2'', which is written with double quotation marks, like
  3815. this:
  3816. @lisp
  3817. "multiply-by-2"
  3818. @end lisp
  3819. Looking beyond how they are written, symbols are different from strings
  3820. in two important respects.
  3821. The first important difference is uniqueness. If the same-looking
  3822. string is read twice from two different places in a program, the result
  3823. is two @emph{different} string objects whose contents just happen to be
  3824. the same. If, on the other hand, the same-looking symbol is read twice
  3825. from two different places in a program, the result is the @emph{same}
  3826. symbol object both times.
  3827. Given two read symbols, you can use @code{eq?} to test whether they are
  3828. the same (that is, have the same name). @code{eq?} is the most
  3829. efficient comparison operator in Scheme, and comparing two symbols like
  3830. this is as fast as comparing, for example, two numbers. Given two
  3831. strings, on the other hand, you must use @code{equal?} or
  3832. @code{string=?}, which are much slower comparison operators, to
  3833. determine whether the strings have the same contents.
  3834. @lisp
  3835. (define sym1 (quote hello))
  3836. (define sym2 (quote hello))
  3837. (eq? sym1 sym2) @result{} #t
  3838. (define str1 "hello")
  3839. (define str2 "hello")
  3840. (eq? str1 str2) @result{} #f
  3841. (equal? str1 str2) @result{} #t
  3842. @end lisp
  3843. The second important difference is that symbols, unlike strings, are not
  3844. self-evaluating. This is why we need the @code{(quote @dots{})}s in the
  3845. example above: @code{(quote hello)} evaluates to the symbol named
  3846. "hello" itself, whereas an unquoted @code{hello} is @emph{read} as the
  3847. symbol named "hello" and evaluated as a variable reference @dots{} about
  3848. which more below (@pxref{Symbol Variables}).
  3849. @menu
  3850. * Symbol Data:: Symbols as discrete data.
  3851. * Symbol Keys:: Symbols as lookup keys.
  3852. * Symbol Variables:: Symbols as denoting variables.
  3853. * Symbol Primitives:: Operations related to symbols.
  3854. * Symbol Read Syntax:: Extended read syntax for symbols.
  3855. * Symbol Uninterned:: Uninterned symbols.
  3856. @end menu
  3857. @node Symbol Data
  3858. @subsubsection Symbols as Discrete Data
  3859. Numbers and symbols are similar to the extent that they both lend
  3860. themselves to @code{eq?} comparison. But symbols are more descriptive
  3861. than numbers, because a symbol's name can be used directly to describe
  3862. the concept for which that symbol stands.
  3863. For example, imagine that you need to represent some colours in a
  3864. computer program. Using numbers, you would have to choose arbitrarily
  3865. some mapping between numbers and colours, and then take care to use that
  3866. mapping consistently:
  3867. @lisp
  3868. ;; 1=red, 2=green, 3=purple
  3869. (if (eq? (colour-of vehicle) 1)
  3870. ...)
  3871. @end lisp
  3872. @noindent
  3873. You can make the mapping more explicit and the code more readable by
  3874. defining constants:
  3875. @lisp
  3876. (define red 1)
  3877. (define green 2)
  3878. (define purple 3)
  3879. (if (eq? (colour-of vehicle) red)
  3880. ...)
  3881. @end lisp
  3882. @noindent
  3883. But the simplest and clearest approach is not to use numbers at all, but
  3884. symbols whose names specify the colours that they refer to:
  3885. @lisp
  3886. (if (eq? (colour-of vehicle) 'red)
  3887. ...)
  3888. @end lisp
  3889. The descriptive advantages of symbols over numbers increase as the set
  3890. of concepts that you want to describe grows. Suppose that a car object
  3891. can have other properties as well, such as whether it has or uses:
  3892. @itemize @bullet
  3893. @item
  3894. automatic or manual transmission
  3895. @item
  3896. leaded or unleaded fuel
  3897. @item
  3898. power steering (or not).
  3899. @end itemize
  3900. @noindent
  3901. Then a car's combined property set could be naturally represented and
  3902. manipulated as a list of symbols:
  3903. @lisp
  3904. (properties-of vehicle1)
  3905. @result{}
  3906. (red manual unleaded power-steering)
  3907. (if (memq 'power-steering (properties-of vehicle1))
  3908. (display "Unfit people can drive this vehicle.\n")
  3909. (display "You'll need strong arms to drive this vehicle!\n"))
  3910. @print{}
  3911. Unfit people can drive this vehicle.
  3912. @end lisp
  3913. Remember, the fundamental property of symbols that we are relying on
  3914. here is that an occurrence of @code{'red} in one part of a program is an
  3915. @emph{indistinguishable} symbol from an occurrence of @code{'red} in
  3916. another part of a program; this means that symbols can usefully be
  3917. compared using @code{eq?}. At the same time, symbols have naturally
  3918. descriptive names. This combination of efficiency and descriptive power
  3919. makes them ideal for use as discrete data.
  3920. @node Symbol Keys
  3921. @subsubsection Symbols as Lookup Keys
  3922. Given their efficiency and descriptive power, it is natural to use
  3923. symbols as the keys in an association list or hash table.
  3924. To illustrate this, consider a more structured representation of the car
  3925. properties example from the preceding subsection. Rather than
  3926. mixing all the properties up together in a flat list, we could use an
  3927. association list like this:
  3928. @lisp
  3929. (define car1-properties '((colour . red)
  3930. (transmission . manual)
  3931. (fuel . unleaded)
  3932. (steering . power-assisted)))
  3933. @end lisp
  3934. Notice how this structure is more explicit and extensible than the flat
  3935. list. For example it makes clear that @code{manual} refers to the
  3936. transmission rather than, say, the windows or the locking of the car.
  3937. It also allows further properties to use the same symbols among their
  3938. possible values without becoming ambiguous:
  3939. @lisp
  3940. (define car1-properties '((colour . red)
  3941. (transmission . manual)
  3942. (fuel . unleaded)
  3943. (steering . power-assisted)
  3944. (seat-colour . red)
  3945. (locking . manual)))
  3946. @end lisp
  3947. With a representation like this, it is easy to use the efficient
  3948. @code{assq-XXX} family of procedures (@pxref{Association Lists}) to
  3949. extract or change individual pieces of information:
  3950. @lisp
  3951. (assq-ref car1-properties 'fuel) @result{} unleaded
  3952. (assq-ref car1-properties 'transmission) @result{} manual
  3953. (assq-set! car1-properties 'seat-colour 'black)
  3954. @result{}
  3955. ((colour . red)
  3956. (transmission . manual)
  3957. (fuel . unleaded)
  3958. (steering . power-assisted)
  3959. (seat-colour . black)
  3960. (locking . manual)))
  3961. @end lisp
  3962. Hash tables also have keys, and exactly the same arguments apply to the
  3963. use of symbols in hash tables as in association lists. The hash value
  3964. that Guile uses to decide where to add a symbol-keyed entry to a hash
  3965. table can be obtained by calling the @code{symbol-hash} procedure:
  3966. @deffn {Scheme Procedure} symbol-hash symbol
  3967. @deffnx {C Function} scm_symbol_hash (symbol)
  3968. Return a hash value for @var{symbol}.
  3969. @end deffn
  3970. See @ref{Hash Tables} for information about hash tables in general, and
  3971. for why you might choose to use a hash table rather than an association
  3972. list.
  3973. @node Symbol Variables
  3974. @subsubsection Symbols as Denoting Variables
  3975. When an unquoted symbol in a Scheme program is evaluated, it is
  3976. interpreted as a variable reference, and the result of the evaluation is
  3977. the appropriate variable's value.
  3978. For example, when the expression @code{(string-length "abcd")} is read
  3979. and evaluated, the sequence of characters @code{string-length} is read
  3980. as the symbol whose name is "string-length". This symbol is associated
  3981. with a variable whose value is the procedure that implements string
  3982. length calculation. Therefore evaluation of the @code{string-length}
  3983. symbol results in that procedure.
  3984. The details of the connection between an unquoted symbol and the
  3985. variable to which it refers are explained elsewhere. See @ref{Binding
  3986. Constructs}, for how associations between symbols and variables are
  3987. created, and @ref{Modules}, for how those associations are affected by
  3988. Guile's module system.
  3989. @node Symbol Primitives
  3990. @subsubsection Operations Related to Symbols
  3991. Given any Scheme value, you can determine whether it is a symbol using
  3992. the @code{symbol?} primitive:
  3993. @rnindex symbol?
  3994. @deffn {Scheme Procedure} symbol? obj
  3995. @deffnx {C Function} scm_symbol_p (obj)
  3996. Return @code{#t} if @var{obj} is a symbol, otherwise return
  3997. @code{#f}.
  3998. @end deffn
  3999. @deftypefn {C Function} int scm_is_symbol (SCM val)
  4000. Equivalent to @code{scm_is_true (scm_symbol_p (val))}.
  4001. @end deftypefn
  4002. Once you know that you have a symbol, you can obtain its name as a
  4003. string by calling @code{symbol->string}. Note that Guile differs by
  4004. default from R5RS on the details of @code{symbol->string} as regards
  4005. case-sensitivity:
  4006. @rnindex symbol->string
  4007. @deffn {Scheme Procedure} symbol->string s
  4008. @deffnx {C Function} scm_symbol_to_string (s)
  4009. Return the name of symbol @var{s} as a string. By default, Guile reads
  4010. symbols case-sensitively, so the string returned will have the same case
  4011. variation as the sequence of characters that caused @var{s} to be
  4012. created.
  4013. If Guile is set to read symbols case-insensitively (as specified by
  4014. R5RS), and @var{s} comes into being as part of a literal expression
  4015. (@pxref{Literal expressions,,,r5rs, The Revised^5 Report on Scheme}) or
  4016. by a call to the @code{read} or @code{string-ci->symbol} procedures,
  4017. Guile converts any alphabetic characters in the symbol's name to
  4018. lower case before creating the symbol object, so the string returned
  4019. here will be in lower case.
  4020. If @var{s} was created by @code{string->symbol}, the case of characters
  4021. in the string returned will be the same as that in the string that was
  4022. passed to @code{string->symbol}, regardless of Guile's case-sensitivity
  4023. setting at the time @var{s} was created.
  4024. It is an error to apply mutation procedures like @code{string-set!} to
  4025. strings returned by this procedure.
  4026. @end deffn
  4027. Most symbols are created by writing them literally in code. However it
  4028. is also possible to create symbols programmatically using the following
  4029. procedures:
  4030. @deffn {Scheme Procedure} symbol char@dots{}
  4031. @rnindex symbol
  4032. Return a newly allocated symbol made from the given character arguments.
  4033. @example
  4034. (symbol #\x #\y #\z) @result{} xyz
  4035. @end example
  4036. @end deffn
  4037. @deffn {Scheme Procedure} list->symbol lst
  4038. @rnindex list->symbol
  4039. Return a newly allocated symbol made from a list of characters.
  4040. @example
  4041. (list->symbol '(#\a #\b #\c)) @result{} abc
  4042. @end example
  4043. @end deffn
  4044. @rnindex symbol-append
  4045. @deffn {Scheme Procedure} symbol-append arg @dots{}
  4046. Return a newly allocated symbol whose characters form the
  4047. concatenation of the given symbols, @var{arg} @enddots{}.
  4048. @example
  4049. (let ((h 'hello))
  4050. (symbol-append h 'world))
  4051. @result{} helloworld
  4052. @end example
  4053. @end deffn
  4054. @rnindex string->symbol
  4055. @deffn {Scheme Procedure} string->symbol string
  4056. @deffnx {C Function} scm_string_to_symbol (string)
  4057. Return the symbol whose name is @var{string}. This procedure can create
  4058. symbols with names containing special characters or letters in the
  4059. non-standard case, but it is usually a bad idea to create such symbols
  4060. because in some implementations of Scheme they cannot be read as
  4061. themselves.
  4062. @end deffn
  4063. @deffn {Scheme Procedure} string-ci->symbol str
  4064. @deffnx {C Function} scm_string_ci_to_symbol (str)
  4065. Return the symbol whose name is @var{str}. If Guile is currently
  4066. reading symbols case-insensitively, @var{str} is converted to lowercase
  4067. before the returned symbol is looked up or created.
  4068. @end deffn
  4069. The following examples illustrate Guile's detailed behaviour as regards
  4070. the case-sensitivity of symbols:
  4071. @lisp
  4072. (read-enable 'case-insensitive) ; R5RS compliant behaviour
  4073. (symbol->string 'flying-fish) @result{} "flying-fish"
  4074. (symbol->string 'Martin) @result{} "martin"
  4075. (symbol->string
  4076. (string->symbol "Malvina")) @result{} "Malvina"
  4077. (eq? 'mISSISSIppi 'mississippi) @result{} #t
  4078. (string->symbol "mISSISSIppi") @result{} mISSISSIppi
  4079. (eq? 'bitBlt (string->symbol "bitBlt")) @result{} #f
  4080. (eq? 'LolliPop
  4081. (string->symbol (symbol->string 'LolliPop))) @result{} #t
  4082. (string=? "K. Harper, M.D."
  4083. (symbol->string
  4084. (string->symbol "K. Harper, M.D."))) @result{} #t
  4085. (read-disable 'case-insensitive) ; Guile default behaviour
  4086. (symbol->string 'flying-fish) @result{} "flying-fish"
  4087. (symbol->string 'Martin) @result{} "Martin"
  4088. (symbol->string
  4089. (string->symbol "Malvina")) @result{} "Malvina"
  4090. (eq? 'mISSISSIppi 'mississippi) @result{} #f
  4091. (string->symbol "mISSISSIppi") @result{} mISSISSIppi
  4092. (eq? 'bitBlt (string->symbol "bitBlt")) @result{} #t
  4093. (eq? 'LolliPop
  4094. (string->symbol (symbol->string 'LolliPop))) @result{} #t
  4095. (string=? "K. Harper, M.D."
  4096. (symbol->string
  4097. (string->symbol "K. Harper, M.D."))) @result{} #t
  4098. @end lisp
  4099. From C, there are lower level functions that construct a Scheme symbol
  4100. from a C string in the current locale encoding.
  4101. When you want to do more from C, you should convert between symbols
  4102. and strings using @code{scm_symbol_to_string} and
  4103. @code{scm_string_to_symbol} and work with the strings.
  4104. @deftypefn {C Function} SCM scm_from_latin1_symbol (const char *name)
  4105. @deftypefnx {C Function} SCM scm_from_utf8_symbol (const char *name)
  4106. Construct and return a Scheme symbol whose name is specified by the
  4107. null-terminated C string @var{name}. These are appropriate when
  4108. the C string is hard-coded in the source code.
  4109. @end deftypefn
  4110. @deftypefn {C Function} SCM scm_from_locale_symbol (const char *name)
  4111. @deftypefnx {C Function} SCM scm_from_locale_symboln (const char *name, size_t len)
  4112. Construct and return a Scheme symbol whose name is specified by
  4113. @var{name}. For @code{scm_from_locale_symbol}, @var{name} must be null
  4114. terminated; for @code{scm_from_locale_symboln} the length of @var{name} is
  4115. specified explicitly by @var{len}.
  4116. Note that these functions should @emph{not} be used when @var{name} is a
  4117. C string constant, because there is no guarantee that the current locale
  4118. will match that of the execution character set, used for string and
  4119. character constants. Most modern C compilers use UTF-8 by default, so
  4120. in such cases we recommend @code{scm_from_utf8_symbol}.
  4121. @end deftypefn
  4122. @deftypefn {C Function} SCM scm_take_locale_symbol (char *str)
  4123. @deftypefnx {C Function} SCM scm_take_locale_symboln (char *str, size_t len)
  4124. Like @code{scm_from_locale_symbol} and @code{scm_from_locale_symboln},
  4125. respectively, but also frees @var{str} with @code{free} eventually.
  4126. Thus, you can use this function when you would free @var{str} anyway
  4127. immediately after creating the Scheme string. In certain cases, Guile
  4128. can then use @var{str} directly as its internal representation.
  4129. @end deftypefn
  4130. The size of a symbol can also be obtained from C:
  4131. @deftypefn {C Function} size_t scm_c_symbol_length (SCM sym)
  4132. Return the number of characters in @var{sym}.
  4133. @end deftypefn
  4134. Finally, some applications, especially those that generate new Scheme
  4135. code dynamically, need to generate symbols for use in the generated
  4136. code. The @code{gensym} primitive meets this need:
  4137. @deffn {Scheme Procedure} gensym [prefix]
  4138. @deffnx {C Function} scm_gensym (prefix)
  4139. Create a new symbol with a name constructed from a prefix and a counter
  4140. value. The string @var{prefix} can be specified as an optional
  4141. argument. Default prefix is @samp{@w{ g}}. The counter is increased by 1
  4142. at each call. There is no provision for resetting the counter.
  4143. @end deffn
  4144. The symbols generated by @code{gensym} are @emph{likely} to be unique,
  4145. since their names begin with a space and it is only otherwise possible
  4146. to generate such symbols if a programmer goes out of their way to do
  4147. so. Uniqueness can be guaranteed by instead using uninterned symbols
  4148. (@pxref{Symbol Uninterned}), though they can't be usefully written out
  4149. and read back in.
  4150. @node Symbol Read Syntax
  4151. @subsubsection Extended Read Syntax for Symbols
  4152. @cindex r7rs-symbols
  4153. The read syntax for a symbol is a sequence of letters, digits, and
  4154. @dfn{extended alphabetic characters}, beginning with a character that
  4155. cannot begin a number. In addition, the special cases of @code{+},
  4156. @code{-}, and @code{...} are read as symbols even though numbers can
  4157. begin with @code{+}, @code{-} or @code{.}.
  4158. Extended alphabetic characters may be used within identifiers as if
  4159. they were letters. The set of extended alphabetic characters is:
  4160. @example
  4161. ! $ % & * + - . / : < = > ? @@ ^ _ ~
  4162. @end example
  4163. In addition to the standard read syntax defined above (which is taken
  4164. from R5RS (@pxref{Formal syntax,,,r5rs,The Revised^5 Report on
  4165. Scheme})), Guile provides an extended symbol read syntax that allows the
  4166. inclusion of unusual characters such as space characters, newlines and
  4167. parentheses. If (for whatever reason) you need to write a symbol
  4168. containing characters not mentioned above, you can do so as follows.
  4169. @itemize @bullet
  4170. @item
  4171. Begin the symbol with the characters @code{#@{},
  4172. @item
  4173. write the characters of the symbol and
  4174. @item
  4175. finish the symbol with the characters @code{@}#}.
  4176. @end itemize
  4177. Here are a few examples of this form of read syntax. The first symbol
  4178. needs to use extended syntax because it contains a space character, the
  4179. second because it contains a line break, and the last because it looks
  4180. like a number.
  4181. @lisp
  4182. #@{foo bar@}#
  4183. #@{what
  4184. ever@}#
  4185. #@{4242@}#
  4186. @end lisp
  4187. Although Guile provides this extended read syntax for symbols,
  4188. widespread usage of it is discouraged because it is not portable and not
  4189. very readable.
  4190. Alternatively, if you enable the @code{r7rs-symbols} read option (see
  4191. @pxref{Scheme Read}), you can write arbitrary symbols using the same
  4192. notation used for strings, except delimited by vertical bars instead of
  4193. double quotes.
  4194. @example
  4195. |foo bar|
  4196. |\x3BB; is a greek lambda|
  4197. |\| is a vertical bar|
  4198. @end example
  4199. Note that there's also an @code{r7rs-symbols} print option
  4200. (@pxref{Scheme Write}). To enable the use of this notation, evaluate
  4201. one or both of the following expressions:
  4202. @example
  4203. (read-enable 'r7rs-symbols)
  4204. (print-enable 'r7rs-symbols)
  4205. @end example
  4206. @node Symbol Uninterned
  4207. @subsubsection Uninterned Symbols
  4208. What makes symbols useful is that they are automatically kept unique.
  4209. There are no two symbols that are distinct objects but have the same
  4210. name. But of course, there is no rule without exception. In addition
  4211. to the normal symbols that have been discussed up to now, you can also
  4212. create special @dfn{uninterned} symbols that behave slightly
  4213. differently.
  4214. To understand what is different about them and why they might be useful,
  4215. we look at how normal symbols are actually kept unique.
  4216. Whenever Guile wants to find the symbol with a specific name, for
  4217. example during @code{read} or when executing @code{string->symbol}, it
  4218. first looks into a table of all existing symbols to find out whether a
  4219. symbol with the given name already exists. When this is the case, Guile
  4220. just returns that symbol. When not, a new symbol with the name is
  4221. created and entered into the table so that it can be found later.
  4222. Sometimes you might want to create a symbol that is guaranteed `fresh',
  4223. i.e.@: a symbol that did not exist previously. You might also want to
  4224. somehow guarantee that no one else will ever unintentionally stumble
  4225. across your symbol in the future. These properties of a symbol are
  4226. often needed when generating code during macro expansion. When
  4227. introducing new temporary variables, you want to guarantee that they
  4228. don't conflict with variables in other people's code.
  4229. The simplest way to arrange for this is to create a new symbol but
  4230. not enter it into the global table of all symbols. That way, no one
  4231. will ever get access to your symbol by chance. Symbols that are not in
  4232. the table are called @dfn{uninterned}. Of course, symbols that
  4233. @emph{are} in the table are called @dfn{interned}.
  4234. You create new uninterned symbols with the function @code{make-symbol}.
  4235. You can test whether a symbol is interned or not with
  4236. @code{symbol-interned?}.
  4237. Uninterned symbols break the rule that the name of a symbol uniquely
  4238. identifies the symbol object. Because of this, they can not be written
  4239. out and read back in like interned symbols. Currently, Guile has no
  4240. support for reading uninterned symbols. Note that the function
  4241. @code{gensym} does not return uninterned symbols for this reason.
  4242. @deffn {Scheme Procedure} make-symbol name
  4243. @deffnx {C Function} scm_make_symbol (name)
  4244. Return a new uninterned symbol with the name @var{name}. The returned
  4245. symbol is guaranteed to be unique and future calls to
  4246. @code{string->symbol} will not return it.
  4247. @end deffn
  4248. @deffn {Scheme Procedure} symbol-interned? symbol
  4249. @deffnx {C Function} scm_symbol_interned_p (symbol)
  4250. Return @code{#t} if @var{symbol} is interned, otherwise return
  4251. @code{#f}.
  4252. @end deffn
  4253. For example:
  4254. @lisp
  4255. (define foo-1 (string->symbol "foo"))
  4256. (define foo-2 (string->symbol "foo"))
  4257. (define foo-3 (make-symbol "foo"))
  4258. (define foo-4 (make-symbol "foo"))
  4259. (eq? foo-1 foo-2)
  4260. @result{} #t
  4261. ; Two interned symbols with the same name are the same object,
  4262. (eq? foo-1 foo-3)
  4263. @result{} #f
  4264. ; but a call to make-symbol with the same name returns a
  4265. ; distinct object.
  4266. (eq? foo-3 foo-4)
  4267. @result{} #f
  4268. ; A call to make-symbol always returns a new object, even for
  4269. ; the same name.
  4270. foo-3
  4271. @result{} #<uninterned-symbol foo 8085290>
  4272. ; Uninterned symbols print differently from interned symbols,
  4273. (symbol? foo-3)
  4274. @result{} #t
  4275. ; but they are still symbols,
  4276. (symbol-interned? foo-3)
  4277. @result{} #f
  4278. ; just not interned.
  4279. @end lisp
  4280. @node Keywords
  4281. @subsection Keywords
  4282. @tpindex Keywords
  4283. Keywords are self-evaluating objects with a convenient read syntax that
  4284. makes them easy to type.
  4285. Guile's keyword support conforms to R5RS, and adds a (switchable) read
  4286. syntax extension to permit keywords to begin with @code{:} as well as
  4287. @code{#:}, or to end with @code{:}.
  4288. @menu
  4289. * Why Use Keywords?:: Motivation for keyword usage.
  4290. * Coding With Keywords:: How to use keywords.
  4291. * Keyword Read Syntax:: Read syntax for keywords.
  4292. * Keyword Procedures:: Procedures for dealing with keywords.
  4293. @end menu
  4294. @node Why Use Keywords?
  4295. @subsubsection Why Use Keywords?
  4296. Keywords are useful in contexts where a program or procedure wants to be
  4297. able to accept a large number of optional arguments without making its
  4298. interface unmanageable.
  4299. To illustrate this, consider a hypothetical @code{make-window}
  4300. procedure, which creates a new window on the screen for drawing into
  4301. using some graphical toolkit. There are many parameters that the caller
  4302. might like to specify, but which could also be sensibly defaulted, for
  4303. example:
  4304. @itemize @bullet
  4305. @item
  4306. color depth -- Default: the color depth for the screen
  4307. @item
  4308. background color -- Default: white
  4309. @item
  4310. width -- Default: 600
  4311. @item
  4312. height -- Default: 400
  4313. @end itemize
  4314. If @code{make-window} did not use keywords, the caller would have to
  4315. pass in a value for each possible argument, remembering the correct
  4316. argument order and using a special value to indicate the default value
  4317. for that argument:
  4318. @lisp
  4319. (make-window 'default ;; Color depth
  4320. 'default ;; Background color
  4321. 800 ;; Width
  4322. 100 ;; Height
  4323. @dots{}) ;; More make-window arguments
  4324. @end lisp
  4325. With keywords, on the other hand, defaulted arguments are omitted, and
  4326. non-default arguments are clearly tagged by the appropriate keyword. As
  4327. a result, the invocation becomes much clearer:
  4328. @lisp
  4329. (make-window #:width 800 #:height 100)
  4330. @end lisp
  4331. On the other hand, for a simpler procedure with few arguments, the use
  4332. of keywords would be a hindrance rather than a help. The primitive
  4333. procedure @code{cons}, for example, would not be improved if it had to
  4334. be invoked as
  4335. @lisp
  4336. (cons #:car x #:cdr y)
  4337. @end lisp
  4338. So the decision whether to use keywords or not is purely pragmatic: use
  4339. them if they will clarify the procedure invocation at point of call.
  4340. @node Coding With Keywords
  4341. @subsubsection Coding With Keywords
  4342. If a procedure wants to support keywords, it should take a rest argument
  4343. and then use whatever means is convenient to extract keywords and their
  4344. corresponding arguments from the contents of that rest argument.
  4345. The following example illustrates the principle: the code for
  4346. @code{make-window} uses a helper procedure called
  4347. @code{get-keyword-value} to extract individual keyword arguments from
  4348. the rest argument.
  4349. @lisp
  4350. (define (get-keyword-value args keyword default)
  4351. (let ((kv (memq keyword args)))
  4352. (if (and kv (>= (length kv) 2))
  4353. (cadr kv)
  4354. default)))
  4355. (define (make-window . args)
  4356. (let ((depth (get-keyword-value args #:depth screen-depth))
  4357. (bg (get-keyword-value args #:bg "white"))
  4358. (width (get-keyword-value args #:width 800))
  4359. (height (get-keyword-value args #:height 100))
  4360. @dots{})
  4361. @dots{}))
  4362. @end lisp
  4363. But you don't need to write @code{get-keyword-value}. The @code{(ice-9
  4364. optargs)} module provides a set of powerful macros that you can use to
  4365. implement keyword-supporting procedures like this:
  4366. @lisp
  4367. (use-modules (ice-9 optargs))
  4368. (define (make-window . args)
  4369. (let-keywords args #f ((depth screen-depth)
  4370. (bg "white")
  4371. (width 800)
  4372. (height 100))
  4373. ...))
  4374. @end lisp
  4375. @noindent
  4376. Or, even more economically, like this:
  4377. @lisp
  4378. (use-modules (ice-9 optargs))
  4379. (define* (make-window #:key (depth screen-depth)
  4380. (bg "white")
  4381. (width 800)
  4382. (height 100))
  4383. ...)
  4384. @end lisp
  4385. For further details on @code{let-keywords}, @code{define*} and other
  4386. facilities provided by the @code{(ice-9 optargs)} module, see
  4387. @ref{Optional Arguments}.
  4388. To handle keyword arguments from procedures implemented in C,
  4389. use @code{scm_c_bind_keyword_arguments} (@pxref{Keyword Procedures}).
  4390. @node Keyword Read Syntax
  4391. @subsubsection Keyword Read Syntax
  4392. Guile, by default, only recognizes a keyword syntax that is compatible
  4393. with R5RS. A token of the form @code{#:NAME}, where @code{NAME} has the
  4394. same syntax as a Scheme symbol (@pxref{Symbol Read Syntax}), is the
  4395. external representation of the keyword named @code{NAME}. Keyword
  4396. objects print using this syntax as well, so values containing keyword
  4397. objects can be read back into Guile. When used in an expression,
  4398. keywords are self-quoting objects.
  4399. If the @code{keywords} read option is set to @code{'prefix}, Guile also
  4400. recognizes the alternative read syntax @code{:NAME}. Otherwise, tokens
  4401. of the form @code{:NAME} are read as symbols, as required by R5RS.
  4402. @cindex SRFI-88 keyword syntax
  4403. If the @code{keywords} read option is set to @code{'postfix}, Guile
  4404. recognizes the SRFI-88 read syntax @code{NAME:} (@pxref{SRFI-88}).
  4405. Otherwise, tokens of this form are read as symbols.
  4406. To enable and disable the alternative non-R5RS keyword syntax, you use
  4407. the @code{read-set!} procedure documented @ref{Scheme Read}. Note that
  4408. the @code{prefix} and @code{postfix} syntax are mutually exclusive.
  4409. @lisp
  4410. (read-set! keywords 'prefix)
  4411. #:type
  4412. @result{}
  4413. #:type
  4414. :type
  4415. @result{}
  4416. #:type
  4417. (read-set! keywords 'postfix)
  4418. type:
  4419. @result{}
  4420. #:type
  4421. :type
  4422. @result{}
  4423. :type
  4424. (read-set! keywords #f)
  4425. #:type
  4426. @result{}
  4427. #:type
  4428. :type
  4429. @print{}
  4430. ERROR: In expression :type:
  4431. ERROR: Unbound variable: :type
  4432. ABORT: (unbound-variable)
  4433. @end lisp
  4434. @node Keyword Procedures
  4435. @subsubsection Keyword Procedures
  4436. @deffn {Scheme Procedure} keyword? obj
  4437. @deffnx {C Function} scm_keyword_p (obj)
  4438. Return @code{#t} if the argument @var{obj} is a keyword, else
  4439. @code{#f}.
  4440. @end deffn
  4441. @deffn {Scheme Procedure} keyword->symbol keyword
  4442. @deffnx {C Function} scm_keyword_to_symbol (keyword)
  4443. Return the symbol with the same name as @var{keyword}.
  4444. @end deffn
  4445. @deffn {Scheme Procedure} symbol->keyword symbol
  4446. @deffnx {C Function} scm_symbol_to_keyword (symbol)
  4447. Return the keyword with the same name as @var{symbol}.
  4448. @end deffn
  4449. @deftypefn {C Function} int scm_is_keyword (SCM obj)
  4450. Equivalent to @code{scm_is_true (scm_keyword_p (@var{obj}))}.
  4451. @end deftypefn
  4452. @deftypefn {C Function} SCM scm_from_locale_keyword (const char *name)
  4453. @deftypefnx {C Function} SCM scm_from_locale_keywordn (const char *name, size_t len)
  4454. Equivalent to @code{scm_symbol_to_keyword (scm_from_locale_symbol
  4455. (@var{name}))} and @code{scm_symbol_to_keyword (scm_from_locale_symboln
  4456. (@var{name}, @var{len}))}, respectively.
  4457. Note that these functions should @emph{not} be used when @var{name} is a
  4458. C string constant, because there is no guarantee that the current locale
  4459. will match that of the execution character set, used for string and
  4460. character constants. Most modern C compilers use UTF-8 by default, so
  4461. in such cases we recommend @code{scm_from_utf8_keyword}.
  4462. @end deftypefn
  4463. @deftypefn {C Function} SCM scm_from_latin1_keyword (const char *name)
  4464. @deftypefnx {C Function} SCM scm_from_utf8_keyword (const char *name)
  4465. Equivalent to @code{scm_symbol_to_keyword (scm_from_latin1_symbol
  4466. (@var{name}))} and @code{scm_symbol_to_keyword (scm_from_utf8_symbol
  4467. (@var{name}))}, respectively.
  4468. @end deftypefn
  4469. @deftypefn {C Function} void scm_c_bind_keyword_arguments (const char *subr, @
  4470. SCM rest, scm_t_keyword_arguments_flags flags, @
  4471. SCM keyword1, SCM *argp1, @
  4472. @dots{}, @
  4473. SCM keywordN, SCM *argpN, @
  4474. @nicode{SCM_UNDEFINED})
  4475. Extract the specified keyword arguments from @var{rest}, which is not
  4476. modified. If the keyword argument @var{keyword1} is present in
  4477. @var{rest} with an associated value, that value is stored in the
  4478. variable pointed to by @var{argp1}, otherwise the variable is left
  4479. unchanged. Similarly for the other keywords and argument pointers up to
  4480. @var{keywordN} and @var{argpN}. The argument list to
  4481. @code{scm_c_bind_keyword_arguments} must be terminated by
  4482. @code{SCM_UNDEFINED}.
  4483. Note that since the variables pointed to by @var{argp1} through
  4484. @var{argpN} are left unchanged if the associated keyword argument is not
  4485. present, they should be initialized to their default values before
  4486. calling @code{scm_c_bind_keyword_arguments}. Alternatively, you can
  4487. initialize them to @code{SCM_UNDEFINED} before the call, and then use
  4488. @code{SCM_UNBNDP} after the call to see which ones were provided.
  4489. If an unrecognized keyword argument is present in @var{rest} and
  4490. @var{flags} does not contain @code{SCM_ALLOW_OTHER_KEYS}, or if
  4491. non-keyword arguments are present and @var{flags} does not contain
  4492. @code{SCM_ALLOW_NON_KEYWORD_ARGUMENTS}, an exception is raised.
  4493. @var{subr} should be the name of the procedure receiving the keyword
  4494. arguments, for purposes of error reporting.
  4495. For example:
  4496. @example
  4497. SCM k_delimiter;
  4498. SCM k_grammar;
  4499. SCM sym_infix;
  4500. SCM my_string_join (SCM strings, SCM rest)
  4501. @{
  4502. SCM delimiter = SCM_UNDEFINED;
  4503. SCM grammar = sym_infix;
  4504. scm_c_bind_keyword_arguments ("my-string-join", rest, 0,
  4505. k_delimiter, &delimiter,
  4506. k_grammar, &grammar,
  4507. SCM_UNDEFINED);
  4508. if (SCM_UNBNDP (delimiter))
  4509. delimiter = scm_from_utf8_string (" ");
  4510. return scm_string_join (strings, delimiter, grammar);
  4511. @}
  4512. void my_init ()
  4513. @{
  4514. k_delimiter = scm_from_utf8_keyword ("delimiter");
  4515. k_grammar = scm_from_utf8_keyword ("grammar");
  4516. sym_infix = scm_from_utf8_symbol ("infix");
  4517. scm_c_define_gsubr ("my-string-join", 1, 0, 1, my_string_join);
  4518. @}
  4519. @end example
  4520. @end deftypefn
  4521. @node Pairs
  4522. @subsection Pairs
  4523. @tpindex Pairs
  4524. Pairs are used to combine two Scheme objects into one compound object.
  4525. Hence the name: A pair stores a pair of objects.
  4526. The data type @dfn{pair} is extremely important in Scheme, just like in
  4527. any other Lisp dialect. The reason is that pairs are not only used to
  4528. make two values available as one object, but that pairs are used for
  4529. constructing lists of values. Because lists are so important in Scheme,
  4530. they are described in a section of their own (@pxref{Lists}).
  4531. Pairs can literally get entered in source code or at the REPL, in the
  4532. so-called @dfn{dotted list} syntax. This syntax consists of an opening
  4533. parentheses, the first element of the pair, a dot, the second element
  4534. and a closing parentheses. The following example shows how a pair
  4535. consisting of the two numbers 1 and 2, and a pair containing the symbols
  4536. @code{foo} and @code{bar} can be entered. It is very important to write
  4537. the whitespace before and after the dot, because otherwise the Scheme
  4538. parser would not be able to figure out where to split the tokens.
  4539. @lisp
  4540. (1 . 2)
  4541. (foo . bar)
  4542. @end lisp
  4543. But beware, if you want to try out these examples, you have to
  4544. @dfn{quote} the expressions. More information about quotation is
  4545. available in the section @ref{Expression Syntax}. The correct way
  4546. to try these examples is as follows.
  4547. @lisp
  4548. '(1 . 2)
  4549. @result{}
  4550. (1 . 2)
  4551. '(foo . bar)
  4552. @result{}
  4553. (foo . bar)
  4554. @end lisp
  4555. A new pair is made by calling the procedure @code{cons} with two
  4556. arguments. Then the argument values are stored into a newly allocated
  4557. pair, and the pair is returned. The name @code{cons} stands for
  4558. "construct". Use the procedure @code{pair?} to test whether a
  4559. given Scheme object is a pair or not.
  4560. @rnindex cons
  4561. @deffn {Scheme Procedure} cons x y
  4562. @deffnx {C Function} scm_cons (x, y)
  4563. Return a newly allocated pair whose car is @var{x} and whose
  4564. cdr is @var{y}. The pair is guaranteed to be different (in the
  4565. sense of @code{eq?}) from every previously existing object.
  4566. @end deffn
  4567. @rnindex pair?
  4568. @deffn {Scheme Procedure} pair? x
  4569. @deffnx {C Function} scm_pair_p (x)
  4570. Return @code{#t} if @var{x} is a pair; otherwise return
  4571. @code{#f}.
  4572. @end deffn
  4573. @deftypefn {C Function} int scm_is_pair (SCM x)
  4574. Return 1 when @var{x} is a pair; otherwise return 0.
  4575. @end deftypefn
  4576. The two parts of a pair are traditionally called @dfn{car} and
  4577. @dfn{cdr}. They can be retrieved with procedures of the same name
  4578. (@code{car} and @code{cdr}), and can be modified with the procedures
  4579. @code{set-car!} and @code{set-cdr!}.
  4580. Since a very common operation in Scheme programs is to access the car of
  4581. a car of a pair, or the car of the cdr of a pair, etc., the procedures
  4582. called @code{caar}, @code{cadr} and so on are also predefined. However,
  4583. using these procedures is often detrimental to readability, and
  4584. error-prone. Thus, accessing the contents of a list is usually better
  4585. achieved using pattern matching techniques (@pxref{Pattern Matching}).
  4586. @rnindex car
  4587. @rnindex cdr
  4588. @deffn {Scheme Procedure} car pair
  4589. @deffnx {Scheme Procedure} cdr pair
  4590. @deffnx {C Function} scm_car (pair)
  4591. @deffnx {C Function} scm_cdr (pair)
  4592. Return the car or the cdr of @var{pair}, respectively.
  4593. @end deffn
  4594. @deftypefn {C Macro} SCM SCM_CAR (SCM pair)
  4595. @deftypefnx {C Macro} SCM SCM_CDR (SCM pair)
  4596. These two macros are the fastest way to access the car or cdr of a
  4597. pair; they can be thought of as compiling into a single memory
  4598. reference.
  4599. These macros do no checking at all. The argument @var{pair} must be a
  4600. valid pair.
  4601. @end deftypefn
  4602. @deffn {Scheme Procedure} cddr pair
  4603. @deffnx {Scheme Procedure} cdar pair
  4604. @deffnx {Scheme Procedure} cadr pair
  4605. @deffnx {Scheme Procedure} caar pair
  4606. @deffnx {Scheme Procedure} cdddr pair
  4607. @deffnx {Scheme Procedure} cddar pair
  4608. @deffnx {Scheme Procedure} cdadr pair
  4609. @deffnx {Scheme Procedure} cdaar pair
  4610. @deffnx {Scheme Procedure} caddr pair
  4611. @deffnx {Scheme Procedure} cadar pair
  4612. @deffnx {Scheme Procedure} caadr pair
  4613. @deffnx {Scheme Procedure} caaar pair
  4614. @deffnx {Scheme Procedure} cddddr pair
  4615. @deffnx {Scheme Procedure} cdddar pair
  4616. @deffnx {Scheme Procedure} cddadr pair
  4617. @deffnx {Scheme Procedure} cddaar pair
  4618. @deffnx {Scheme Procedure} cdaddr pair
  4619. @deffnx {Scheme Procedure} cdadar pair
  4620. @deffnx {Scheme Procedure} cdaadr pair
  4621. @deffnx {Scheme Procedure} cdaaar pair
  4622. @deffnx {Scheme Procedure} cadddr pair
  4623. @deffnx {Scheme Procedure} caddar pair
  4624. @deffnx {Scheme Procedure} cadadr pair
  4625. @deffnx {Scheme Procedure} cadaar pair
  4626. @deffnx {Scheme Procedure} caaddr pair
  4627. @deffnx {Scheme Procedure} caadar pair
  4628. @deffnx {Scheme Procedure} caaadr pair
  4629. @deffnx {Scheme Procedure} caaaar pair
  4630. @deffnx {C Function} scm_cddr (pair)
  4631. @deffnx {C Function} scm_cdar (pair)
  4632. @deffnx {C Function} scm_cadr (pair)
  4633. @deffnx {C Function} scm_caar (pair)
  4634. @deffnx {C Function} scm_cdddr (pair)
  4635. @deffnx {C Function} scm_cddar (pair)
  4636. @deffnx {C Function} scm_cdadr (pair)
  4637. @deffnx {C Function} scm_cdaar (pair)
  4638. @deffnx {C Function} scm_caddr (pair)
  4639. @deffnx {C Function} scm_cadar (pair)
  4640. @deffnx {C Function} scm_caadr (pair)
  4641. @deffnx {C Function} scm_caaar (pair)
  4642. @deffnx {C Function} scm_cddddr (pair)
  4643. @deffnx {C Function} scm_cdddar (pair)
  4644. @deffnx {C Function} scm_cddadr (pair)
  4645. @deffnx {C Function} scm_cddaar (pair)
  4646. @deffnx {C Function} scm_cdaddr (pair)
  4647. @deffnx {C Function} scm_cdadar (pair)
  4648. @deffnx {C Function} scm_cdaadr (pair)
  4649. @deffnx {C Function} scm_cdaaar (pair)
  4650. @deffnx {C Function} scm_cadddr (pair)
  4651. @deffnx {C Function} scm_caddar (pair)
  4652. @deffnx {C Function} scm_cadadr (pair)
  4653. @deffnx {C Function} scm_cadaar (pair)
  4654. @deffnx {C Function} scm_caaddr (pair)
  4655. @deffnx {C Function} scm_caadar (pair)
  4656. @deffnx {C Function} scm_caaadr (pair)
  4657. @deffnx {C Function} scm_caaaar (pair)
  4658. These procedures are compositions of @code{car} and @code{cdr}, where
  4659. for example @code{caddr} could be defined by
  4660. @lisp
  4661. (define caddr (lambda (x) (car (cdr (cdr x)))))
  4662. @end lisp
  4663. @code{cadr}, @code{caddr} and @code{cadddr} pick out the second, third
  4664. or fourth elements of a list, respectively. SRFI-1 provides the same
  4665. under the names @code{second}, @code{third} and @code{fourth}
  4666. (@pxref{SRFI-1 Selectors}).
  4667. @end deffn
  4668. @rnindex set-car!
  4669. @deffn {Scheme Procedure} set-car! pair value
  4670. @deffnx {C Function} scm_set_car_x (pair, value)
  4671. Stores @var{value} in the car field of @var{pair}. The value returned
  4672. by @code{set-car!} is unspecified.
  4673. @end deffn
  4674. @rnindex set-cdr!
  4675. @deffn {Scheme Procedure} set-cdr! pair value
  4676. @deffnx {C Function} scm_set_cdr_x (pair, value)
  4677. Stores @var{value} in the cdr field of @var{pair}. The value returned
  4678. by @code{set-cdr!} is unspecified.
  4679. @end deffn
  4680. @node Lists
  4681. @subsection Lists
  4682. @tpindex Lists
  4683. A very important data type in Scheme---as well as in all other Lisp
  4684. dialects---is the data type @dfn{list}.@footnote{Strictly speaking,
  4685. Scheme does not have a real datatype @dfn{list}. Lists are made up of
  4686. @dfn{chained pairs}, and only exist by definition---a list is a chain
  4687. of pairs which looks like a list.}
  4688. This is the short definition of what a list is:
  4689. @itemize @bullet
  4690. @item
  4691. Either the empty list @code{()},
  4692. @item
  4693. or a pair which has a list in its cdr.
  4694. @end itemize
  4695. @c FIXME::martin: Describe the pair chaining in more detail.
  4696. @c FIXME::martin: What is a proper, what an improper list?
  4697. @c What is a circular list?
  4698. @c FIXME::martin: Maybe steal some graphics from the Elisp reference
  4699. @c manual?
  4700. @menu
  4701. * List Syntax:: Writing literal lists.
  4702. * List Predicates:: Testing lists.
  4703. * List Constructors:: Creating new lists.
  4704. * List Selection:: Selecting from lists, getting their length.
  4705. * Append/Reverse:: Appending and reversing lists.
  4706. * List Modification:: Modifying existing lists.
  4707. * List Searching:: Searching for list elements
  4708. * List Mapping:: Applying procedures to lists.
  4709. @end menu
  4710. @node List Syntax
  4711. @subsubsection List Read Syntax
  4712. The syntax for lists is an opening parentheses, then all the elements of
  4713. the list (separated by whitespace) and finally a closing
  4714. parentheses.@footnote{Note that there is no separation character between
  4715. the list elements, like a comma or a semicolon.}.
  4716. @lisp
  4717. (1 2 3) ; @r{a list of the numbers 1, 2 and 3}
  4718. ("foo" bar 3.1415) ; @r{a string, a symbol and a real number}
  4719. () ; @r{the empty list}
  4720. @end lisp
  4721. The last example needs a bit more explanation. A list with no elements,
  4722. called the @dfn{empty list}, is special in some ways. It is used for
  4723. terminating lists by storing it into the cdr of the last pair that makes
  4724. up a list. An example will clear that up:
  4725. @lisp
  4726. (car '(1))
  4727. @result{}
  4728. 1
  4729. (cdr '(1))
  4730. @result{}
  4731. ()
  4732. @end lisp
  4733. This example also shows that lists have to be quoted when written
  4734. (@pxref{Expression Syntax}), because they would otherwise be
  4735. mistakingly taken as procedure applications (@pxref{Simple
  4736. Invocation}).
  4737. @node List Predicates
  4738. @subsubsection List Predicates
  4739. Often it is useful to test whether a given Scheme object is a list or
  4740. not. List-processing procedures could use this information to test
  4741. whether their input is valid, or they could do different things
  4742. depending on the datatype of their arguments.
  4743. @rnindex list?
  4744. @deffn {Scheme Procedure} list? x
  4745. @deffnx {C Function} scm_list_p (x)
  4746. Return @code{#t} if @var{x} is a proper list, else @code{#f}.
  4747. @end deffn
  4748. The predicate @code{null?} is often used in list-processing code to
  4749. tell whether a given list has run out of elements. That is, a loop
  4750. somehow deals with the elements of a list until the list satisfies
  4751. @code{null?}. Then, the algorithm terminates.
  4752. @rnindex null?
  4753. @deffn {Scheme Procedure} null? x
  4754. @deffnx {C Function} scm_null_p (x)
  4755. Return @code{#t} if @var{x} is the empty list, else @code{#f}.
  4756. @end deffn
  4757. @deftypefn {C Function} int scm_is_null (SCM x)
  4758. Return 1 when @var{x} is the empty list; otherwise return 0.
  4759. @end deftypefn
  4760. @node List Constructors
  4761. @subsubsection List Constructors
  4762. This section describes the procedures for constructing new lists.
  4763. @code{list} simply returns a list where the elements are the arguments,
  4764. @code{cons*} is similar, but the last argument is stored in the cdr of
  4765. the last pair of the list.
  4766. @c C Function scm_list(rest) used to be documented here, but it's a
  4767. @c no-op since it does nothing but return the list the caller must
  4768. @c have already created.
  4769. @c
  4770. @deffn {Scheme Procedure} list elem @dots{}
  4771. @deffnx {C Function} scm_list_1 (elem1)
  4772. @deffnx {C Function} scm_list_2 (elem1, elem2)
  4773. @deffnx {C Function} scm_list_3 (elem1, elem2, elem3)
  4774. @deffnx {C Function} scm_list_4 (elem1, elem2, elem3, elem4)
  4775. @deffnx {C Function} scm_list_5 (elem1, elem2, elem3, elem4, elem5)
  4776. @deffnx {C Function} scm_list_n (elem1, @dots{}, elemN, @nicode{SCM_UNDEFINED})
  4777. @rnindex list
  4778. Return a new list containing elements @var{elem} @enddots{}.
  4779. @code{scm_list_n} takes a variable number of arguments, terminated by
  4780. the special @code{SCM_UNDEFINED}. That final @code{SCM_UNDEFINED} is
  4781. not included in the list. None of @var{elem} @dots{} can
  4782. themselves be @code{SCM_UNDEFINED}, or @code{scm_list_n} will
  4783. terminate at that point.
  4784. @end deffn
  4785. @c C Function scm_cons_star(arg1,rest) used to be documented here,
  4786. @c but it's not really a useful interface, since it expects the
  4787. @c caller to have already consed up all but the first argument
  4788. @c already.
  4789. @c
  4790. @deffn {Scheme Procedure} cons* arg1 arg2 @dots{}
  4791. Like @code{list}, but the last arg provides the tail of the
  4792. constructed list, returning @code{(cons @var{arg1} (cons
  4793. @var{arg2} (cons @dots{} @var{argn})))}. Requires at least one
  4794. argument. If given one argument, that argument is returned as
  4795. result. This function is called @code{list*} in some other
  4796. Schemes and in Common LISP.
  4797. @end deffn
  4798. @deffn {Scheme Procedure} list-copy lst
  4799. @deffnx {C Function} scm_list_copy (lst)
  4800. Return a (newly-created) copy of @var{lst}.
  4801. @end deffn
  4802. @deffn {Scheme Procedure} make-list n [init]
  4803. Create a list containing of @var{n} elements, where each element is
  4804. initialized to @var{init}. @var{init} defaults to the empty list
  4805. @code{()} if not given.
  4806. @end deffn
  4807. Note that @code{list-copy} only makes a copy of the pairs which make up
  4808. the spine of the lists. The list elements are not copied, which means
  4809. that modifying the elements of the new list also modifies the elements
  4810. of the old list. On the other hand, applying procedures like
  4811. @code{set-cdr!} or @code{delv!} to the new list will not alter the old
  4812. list. If you also need to copy the list elements (making a deep copy),
  4813. use the procedure @code{copy-tree} from @code{(ice-9 copy-tree)}
  4814. (@pxref{Copying}).
  4815. @node List Selection
  4816. @subsubsection List Selection
  4817. These procedures are used to get some information about a list, or to
  4818. retrieve one or more elements of a list.
  4819. @rnindex length
  4820. @deffn {Scheme Procedure} length lst
  4821. @deffnx {C Function} scm_length (lst)
  4822. Return the number of elements in list @var{lst}.
  4823. @end deffn
  4824. @deffn {Scheme Procedure} last-pair lst
  4825. @deffnx {C Function} scm_last_pair (lst)
  4826. Return the last pair in @var{lst}, signalling an error if
  4827. @var{lst} is circular.
  4828. @end deffn
  4829. @rnindex list-ref
  4830. @deffn {Scheme Procedure} list-ref list k
  4831. @deffnx {C Function} scm_list_ref (list, k)
  4832. Return the @var{k}th element from @var{list}.
  4833. @end deffn
  4834. @rnindex list-tail
  4835. @deffn {Scheme Procedure} list-tail lst k
  4836. @deffnx {Scheme Procedure} list-cdr-ref lst k
  4837. @deffnx {C Function} scm_list_tail (lst, k)
  4838. Return the "tail" of @var{lst} beginning with its @var{k}th element.
  4839. The first element of the list is considered to be element 0.
  4840. @code{list-tail} and @code{list-cdr-ref} are identical. It may help to
  4841. think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,
  4842. or returning the results of cdring @var{k} times down @var{lst}.
  4843. @end deffn
  4844. @deffn {Scheme Procedure} list-head lst k
  4845. @deffnx {C Function} scm_list_head (lst, k)
  4846. Copy the first @var{k} elements from @var{lst} into a new list, and
  4847. return it.
  4848. @end deffn
  4849. @node Append/Reverse
  4850. @subsubsection Append and Reverse
  4851. @code{append} and @code{append!} are used to concatenate two or more
  4852. lists in order to form a new list. @code{reverse} and @code{reverse!}
  4853. return lists with the same elements as their arguments, but in reverse
  4854. order. The procedure variants with an @code{!} directly modify the
  4855. pairs which form the list, whereas the other procedures create new
  4856. pairs. This is why you should be careful when using the side-effecting
  4857. variants.
  4858. @rnindex append
  4859. @deffn {Scheme Procedure} append lst @dots{} obj
  4860. @deffnx {Scheme Procedure} append
  4861. @deffnx {Scheme Procedure} append! lst @dots{} obj
  4862. @deffnx {Scheme Procedure} append!
  4863. @deffnx {C Function} scm_append (lstlst)
  4864. @deffnx {C Function} scm_append_x (lstlst)
  4865. Return a list comprising all the elements of lists @var{lst} @dots{}
  4866. @var{obj}. If called with no arguments, return the empty list.
  4867. @lisp
  4868. (append '(x) '(y)) @result{} (x y)
  4869. (append '(a) '(b c d)) @result{} (a b c d)
  4870. (append '(a (b)) '((c))) @result{} (a (b) (c))
  4871. @end lisp
  4872. The last argument @var{obj} may actually be any object; an improper
  4873. list results if the last argument is not a proper list.
  4874. @lisp
  4875. (append '(a b) '(c . d)) @result{} (a b c . d)
  4876. (append '() 'a) @result{} a
  4877. @end lisp
  4878. @code{append} doesn't modify the given lists, but the return may share
  4879. structure with the final @var{obj}. @code{append!} is permitted, but
  4880. not required, to modify the given lists to form its return.
  4881. For @code{scm_append} and @code{scm_append_x}, @var{lstlst} is a list
  4882. of the list operands @var{lst} @dots{} @var{obj}. That @var{lstlst}
  4883. itself is not modified or used in the return.
  4884. @end deffn
  4885. @rnindex reverse
  4886. @deffn {Scheme Procedure} reverse lst
  4887. @deffnx {Scheme Procedure} reverse! lst [newtail]
  4888. @deffnx {C Function} scm_reverse (lst)
  4889. @deffnx {C Function} scm_reverse_x (lst, newtail)
  4890. Return a list comprising the elements of @var{lst}, in reverse order.
  4891. @code{reverse} constructs a new list. @code{reverse!} is permitted, but
  4892. not required, to modify @var{lst} in constructing its return.
  4893. For @code{reverse!}, the optional @var{newtail} is appended to the
  4894. result. @var{newtail} isn't reversed, it simply becomes the list
  4895. tail. For @code{scm_reverse_x}, the @var{newtail} parameter is
  4896. mandatory, but can be @code{SCM_EOL} if no further tail is required.
  4897. @end deffn
  4898. @node List Modification
  4899. @subsubsection List Modification
  4900. The following procedures modify an existing list, either by changing
  4901. elements of the list, or by changing the list structure itself.
  4902. @deffn {Scheme Procedure} list-set! list k val
  4903. @deffnx {C Function} scm_list_set_x (list, k, val)
  4904. Set the @var{k}th element of @var{list} to @var{val}.
  4905. @end deffn
  4906. @deffn {Scheme Procedure} list-cdr-set! list k val
  4907. @deffnx {C Function} scm_list_cdr_set_x (list, k, val)
  4908. Set the @var{k}th cdr of @var{list} to @var{val}.
  4909. @end deffn
  4910. @deffn {Scheme Procedure} delq item lst
  4911. @deffnx {C Function} scm_delq (item, lst)
  4912. Return a newly-created copy of @var{lst} with elements
  4913. @code{eq?} to @var{item} removed. This procedure mirrors
  4914. @code{memq}: @code{delq} compares elements of @var{lst} against
  4915. @var{item} with @code{eq?}.
  4916. @end deffn
  4917. @deffn {Scheme Procedure} delv item lst
  4918. @deffnx {C Function} scm_delv (item, lst)
  4919. Return a newly-created copy of @var{lst} with elements
  4920. @code{eqv?} to @var{item} removed. This procedure mirrors
  4921. @code{memv}: @code{delv} compares elements of @var{lst} against
  4922. @var{item} with @code{eqv?}.
  4923. @end deffn
  4924. @deffn {Scheme Procedure} delete item lst
  4925. @deffnx {C Function} scm_delete (item, lst)
  4926. Return a newly-created copy of @var{lst} with elements
  4927. @code{equal?} to @var{item} removed. This procedure mirrors
  4928. @code{member}: @code{delete} compares elements of @var{lst}
  4929. against @var{item} with @code{equal?}.
  4930. See also SRFI-1 which has an extended @code{delete} (@ref{SRFI-1
  4931. Deleting}), and also an @code{lset-difference} which can delete
  4932. multiple @var{item}s in one call (@ref{SRFI-1 Set Operations}).
  4933. @end deffn
  4934. @deffn {Scheme Procedure} delq! item lst
  4935. @deffnx {Scheme Procedure} delv! item lst
  4936. @deffnx {Scheme Procedure} delete! item lst
  4937. @deffnx {C Function} scm_delq_x (item, lst)
  4938. @deffnx {C Function} scm_delv_x (item, lst)
  4939. @deffnx {C Function} scm_delete_x (item, lst)
  4940. These procedures are destructive versions of @code{delq}, @code{delv}
  4941. and @code{delete}: they modify the pointers in the existing @var{lst}
  4942. rather than creating a new list. Caveat evaluator: Like other
  4943. destructive list functions, these functions cannot modify the binding of
  4944. @var{lst}, and so cannot be used to delete the first element of
  4945. @var{lst} destructively.
  4946. @end deffn
  4947. @deffn {Scheme Procedure} delq1! item lst
  4948. @deffnx {C Function} scm_delq1_x (item, lst)
  4949. Like @code{delq!}, but only deletes the first occurrence of
  4950. @var{item} from @var{lst}. Tests for equality using
  4951. @code{eq?}. See also @code{delv1!} and @code{delete1!}.
  4952. @end deffn
  4953. @deffn {Scheme Procedure} delv1! item lst
  4954. @deffnx {C Function} scm_delv1_x (item, lst)
  4955. Like @code{delv!}, but only deletes the first occurrence of
  4956. @var{item} from @var{lst}. Tests for equality using
  4957. @code{eqv?}. See also @code{delq1!} and @code{delete1!}.
  4958. @end deffn
  4959. @deffn {Scheme Procedure} delete1! item lst
  4960. @deffnx {C Function} scm_delete1_x (item, lst)
  4961. Like @code{delete!}, but only deletes the first occurrence of
  4962. @var{item} from @var{lst}. Tests for equality using
  4963. @code{equal?}. See also @code{delq1!} and @code{delv1!}.
  4964. @end deffn
  4965. @deffn {Scheme Procedure} filter pred lst
  4966. @deffnx {Scheme Procedure} filter! pred lst
  4967. Return a list containing all elements from @var{lst} which satisfy the
  4968. predicate @var{pred}. The elements in the result list have the same
  4969. order as in @var{lst}. The order in which @var{pred} is applied to
  4970. the list elements is not specified.
  4971. @code{filter} does not change @var{lst}, but the result may share a
  4972. tail with it. @code{filter!} may modify @var{lst} to construct its
  4973. return.
  4974. @end deffn
  4975. @node List Searching
  4976. @subsubsection List Searching
  4977. The following procedures search lists for particular elements. They use
  4978. different comparison predicates for comparing list elements with the
  4979. object to be searched. When they fail, they return @code{#f}, otherwise
  4980. they return the sublist whose car is equal to the search object, where
  4981. equality depends on the equality predicate used.
  4982. @rnindex memq
  4983. @deffn {Scheme Procedure} memq x lst
  4984. @deffnx {C Function} scm_memq (x, lst)
  4985. Return the first sublist of @var{lst} whose car is @code{eq?}
  4986. to @var{x} where the sublists of @var{lst} are the non-empty
  4987. lists returned by @code{(list-tail @var{lst} @var{k})} for
  4988. @var{k} less than the length of @var{lst}. If @var{x} does not
  4989. occur in @var{lst}, then @code{#f} (not the empty list) is
  4990. returned.
  4991. @end deffn
  4992. @rnindex memv
  4993. @deffn {Scheme Procedure} memv x lst
  4994. @deffnx {C Function} scm_memv (x, lst)
  4995. Return the first sublist of @var{lst} whose car is @code{eqv?}
  4996. to @var{x} where the sublists of @var{lst} are the non-empty
  4997. lists returned by @code{(list-tail @var{lst} @var{k})} for
  4998. @var{k} less than the length of @var{lst}. If @var{x} does not
  4999. occur in @var{lst}, then @code{#f} (not the empty list) is
  5000. returned.
  5001. @end deffn
  5002. @rnindex member
  5003. @deffn {Scheme Procedure} member x lst
  5004. @deffnx {C Function} scm_member (x, lst)
  5005. Return the first sublist of @var{lst} whose car is
  5006. @code{equal?} to @var{x} where the sublists of @var{lst} are
  5007. the non-empty lists returned by @code{(list-tail @var{lst}
  5008. @var{k})} for @var{k} less than the length of @var{lst}. If
  5009. @var{x} does not occur in @var{lst}, then @code{#f} (not the
  5010. empty list) is returned.
  5011. See also SRFI-1 which has an extended @code{member} function
  5012. (@ref{SRFI-1 Searching}).
  5013. @end deffn
  5014. @node List Mapping
  5015. @subsubsection List Mapping
  5016. List processing is very convenient in Scheme because the process of
  5017. iterating over the elements of a list can be highly abstracted. The
  5018. procedures in this section are the most basic iterating procedures for
  5019. lists. They take a procedure and one or more lists as arguments, and
  5020. apply the procedure to each element of the list. They differ in their
  5021. return value.
  5022. @rnindex map
  5023. @c begin (texi-doc-string "guile" "map")
  5024. @deffn {Scheme Procedure} map proc arg1 arg2 @dots{}
  5025. @deffnx {Scheme Procedure} map-in-order proc arg1 arg2 @dots{}
  5026. @deffnx {C Function} scm_map (proc, arg1, args)
  5027. Apply @var{proc} to each element of the list @var{arg1} (if only two
  5028. arguments are given), or to the corresponding elements of the argument
  5029. lists (if more than two arguments are given). The result(s) of the
  5030. procedure applications are saved and returned in a list. For
  5031. @code{map}, the order of procedure applications is not specified,
  5032. @code{map-in-order} applies the procedure from left to right to the list
  5033. elements.
  5034. @end deffn
  5035. @rnindex for-each
  5036. @c begin (texi-doc-string "guile" "for-each")
  5037. @deffn {Scheme Procedure} for-each proc arg1 arg2 @dots{}
  5038. Like @code{map}, but the procedure is always applied from left to right,
  5039. and the result(s) of the procedure applications are thrown away. The
  5040. return value is not specified.
  5041. @end deffn
  5042. See also SRFI-1 which extends these functions to take lists of unequal
  5043. lengths (@ref{SRFI-1 Fold and Map}).
  5044. @node Vectors
  5045. @subsection Vectors
  5046. @tpindex Vectors
  5047. Vectors are sequences of Scheme objects. Unlike lists, the length of a
  5048. vector, once the vector is created, cannot be changed. The advantage of
  5049. vectors over lists is that the time required to access one element of a vector
  5050. given its @dfn{position} (synonymous with @dfn{index}), a zero-origin number,
  5051. is constant, whereas lists have an access time linear to the position of the
  5052. accessed element in the list.
  5053. Vectors can contain any kind of Scheme object; it is even possible to
  5054. have different types of objects in the same vector. For vectors
  5055. containing vectors, you may wish to use @ref{Arrays} instead.
  5056. Note, too, that vectors are a special case of one dimensional
  5057. non-uniform arrays and that array procedures operate happily on vectors.
  5058. Also see @ref{SRFI-43}, @ref{R6RS Support}, or @ref{R7RS Support}, for
  5059. more comprehensive vector libraries.
  5060. @menu
  5061. * Vector Syntax:: Read syntax for vectors.
  5062. * Vector Creation:: Dynamic vector creation and validation.
  5063. * Vector Accessors:: Accessing and modifying vector contents.
  5064. * Vector Accessing from C:: Ways to work with vectors from C.
  5065. * Uniform Numeric Vectors:: Vectors of unboxed numeric values.
  5066. @end menu
  5067. @node Vector Syntax
  5068. @subsubsection Read Syntax for Vectors
  5069. Vectors can literally be entered in source code, just like strings,
  5070. characters or some of the other data types. The read syntax for vectors
  5071. is as follows: A sharp sign (@code{#}), followed by an opening
  5072. parentheses, all elements of the vector in their respective read syntax,
  5073. and finally a closing parentheses. Like strings, vectors do not have to
  5074. be quoted.
  5075. The following are examples of the read syntax for vectors; where the
  5076. first vector only contains numbers and the second three different object
  5077. types: a string, a symbol and a number in hexadecimal notation.
  5078. @lisp
  5079. #(1 2 3)
  5080. #("Hello" foo #xdeadbeef)
  5081. @end lisp
  5082. @node Vector Creation
  5083. @subsubsection Dynamic Vector Creation and Validation
  5084. Instead of creating a vector implicitly by using the read syntax just
  5085. described, you can create a vector dynamically by calling one of the
  5086. @code{vector} and @code{list->vector} primitives with the list of Scheme
  5087. values that you want to place into a vector. The size of the vector
  5088. thus created is determined implicitly by the number of arguments given.
  5089. @rnindex vector
  5090. @rnindex list->vector
  5091. @deffn {Scheme Procedure} vector arg @dots{}
  5092. @deffnx {Scheme Procedure} list->vector l
  5093. @deffnx {C Function} scm_vector (l)
  5094. Return a newly allocated vector composed of the
  5095. given arguments. Analogous to @code{list}.
  5096. @lisp
  5097. (vector 'a 'b 'c) @result{} #(a b c)
  5098. @end lisp
  5099. @end deffn
  5100. The inverse operation is @code{vector->list}:
  5101. @rnindex vector->list
  5102. @deffn {Scheme Procedure} vector->list v
  5103. @deffnx {C Function} scm_vector_to_list (v)
  5104. Return a newly allocated list composed of the elements of @var{v}.
  5105. @lisp
  5106. (vector->list #(dah dah didah)) @result{} (dah dah didah)
  5107. (list->vector '(dididit dah)) @result{} #(dididit dah)
  5108. @end lisp
  5109. @end deffn
  5110. To allocate a vector with an explicitly specified size, use
  5111. @code{make-vector}. With this primitive you can also specify an initial
  5112. value for the vector elements (the same value for all elements, that
  5113. is):
  5114. @rnindex make-vector
  5115. @deffn {Scheme Procedure} make-vector len [fill]
  5116. @deffnx {C Function} scm_make_vector (len, fill)
  5117. Return a newly allocated vector of @var{len} elements. If a
  5118. second argument is given, then each position is initialized to
  5119. @var{fill}. Otherwise the initial contents of each position is
  5120. unspecified.
  5121. @end deffn
  5122. @deftypefn {C Function} SCM scm_c_make_vector (size_t k, SCM fill)
  5123. Like @code{scm_make_vector}, but the length is given as a @code{size_t}.
  5124. @end deftypefn
  5125. To check whether an arbitrary Scheme value @emph{is} a vector, use the
  5126. @code{vector?} primitive:
  5127. @rnindex vector?
  5128. @deffn {Scheme Procedure} vector? obj
  5129. @deffnx {C Function} scm_vector_p (obj)
  5130. Return @code{#t} if @var{obj} is a vector, otherwise return
  5131. @code{#f}.
  5132. @end deffn
  5133. @deftypefn {C Function} int scm_is_vector (SCM obj)
  5134. Return non-zero when @var{obj} is a vector, otherwise return
  5135. @code{zero}.
  5136. @end deftypefn
  5137. @node Vector Accessors
  5138. @subsubsection Accessing and Modifying Vector Contents
  5139. @code{vector-length} and @code{vector-ref} return information about a
  5140. given vector, respectively its size and the elements that are contained
  5141. in the vector.
  5142. @rnindex vector-length
  5143. @deffn {Scheme Procedure} vector-length vector
  5144. @deffnx {C Function} scm_vector_length (vector)
  5145. Return the number of elements in @var{vector} as an exact integer.
  5146. @end deffn
  5147. @deftypefn {C Function} size_t scm_c_vector_length (SCM vec)
  5148. Return the number of elements in @var{vec} as a @code{size_t}.
  5149. @end deftypefn
  5150. @rnindex vector-ref
  5151. @deffn {Scheme Procedure} vector-ref vec k
  5152. @deffnx {C Function} scm_vector_ref (vec, k)
  5153. Return the contents of position @var{k} of @var{vec}.
  5154. @var{k} must be a valid index of @var{vec}.
  5155. @lisp
  5156. (vector-ref #(1 1 2 3 5 8 13 21) 5) @result{} 8
  5157. (vector-ref #(1 1 2 3 5 8 13 21)
  5158. (let ((i (round (* 2 (acos -1)))))
  5159. (if (inexact? i)
  5160. (inexact->exact i)
  5161. i))) @result{} 13
  5162. @end lisp
  5163. @end deffn
  5164. @anchor{x-scm_c_vector_ref}
  5165. @deftypefn {C Function} SCM scm_c_vector_ref (SCM vec, size_t k)
  5166. Return the contents of position @var{k} (a @code{size_t}) of
  5167. @var{vec}.
  5168. @end deftypefn
  5169. A vector created by one of the dynamic vector constructor procedures
  5170. (@pxref{Vector Creation}) can be modified using the following
  5171. procedures.
  5172. @emph{NOTE:} According to R5RS, it is an error to use any of these
  5173. procedures on a literally read vector, because such vectors should be
  5174. considered as constants. Currently, however, Guile does not detect this
  5175. error.
  5176. @rnindex vector-set!
  5177. @deffn {Scheme Procedure} vector-set! vec k obj
  5178. @deffnx {C Function} scm_vector_set_x (vec, k, obj)
  5179. Store @var{obj} in position @var{k} of @var{vec}.
  5180. @var{k} must be a valid index of @var{vec}.
  5181. The value returned by @samp{vector-set!} is unspecified.
  5182. @lisp
  5183. (let ((vec (vector 0 '(2 2 2 2) "Anna")))
  5184. (vector-set! vec 1 '("Sue" "Sue"))
  5185. vec) @result{} #(0 ("Sue" "Sue") "Anna")
  5186. @end lisp
  5187. @end deffn
  5188. @anchor{x-scm_c_vector_set_x}
  5189. @deftypefn {C Function} void scm_c_vector_set_x (SCM vec, size_t k, SCM obj)
  5190. Store @var{obj} in position @var{k} (a @code{size_t}) of @var{vec}.
  5191. @end deftypefn
  5192. @rnindex vector-fill!
  5193. @anchor{x-vector-fill!}
  5194. @deffn {Scheme Procedure} vector-fill! vec fill [start [end]]
  5195. @deffnx {C Function} scm_vector_fill_x (vec, fill)
  5196. Store @var{fill} in every position of @var{vec} in the range
  5197. [@var{start} ... @var{end}). @var{start} defaults to 0 and @var{end}
  5198. defaults to the length of @var{vec}.
  5199. The value returned by @code{vector-fill!} is unspecified.
  5200. @end deffn
  5201. @rnindex vector-copy
  5202. @anchor{x-vector-copy}
  5203. @deffn {Scheme Procedure} vector-copy vec [start [end]]
  5204. @deffnx {C Function} scm_vector_copy (vec)
  5205. Returns a freshly allocated vector containing the elements of @var{vec}
  5206. in the range [@var{start} ... @var{end}). @var{start} defaults to 0 and
  5207. @var{end} defaults to the length of @var{vec}.
  5208. @end deffn
  5209. @rnindex vector-copy!
  5210. @anchor{x-vector-copy!}
  5211. @deffn {Scheme Procedure} vector-copy! dst at src [start [end]]
  5212. Copy the block of elements from vector @var{src} in the range
  5213. [@var{start} ... @var{end}) into vector @var{dst}, starting at position
  5214. @var{at}. @var{at} and @var{start} default to 0 and @var{end} defaults
  5215. to the length of @var{src}.
  5216. It is an error for @var{dst} to have a length less than @var{at} +
  5217. (@var{end} - @var{start}).
  5218. The order in which elements are copied is unspecified, except that if the
  5219. source and destination overlap, copying takes place as if the source is
  5220. first copied into a temporary vector and then into the destination.
  5221. The value returned by @code{vector-copy!} is unspecified.
  5222. @end deffn
  5223. @deffn {Scheme Procedure} vector-move-left! vec1 start1 end1 vec2 start2
  5224. @deffnx {C Function} scm_vector_move_left_x (vec1, start1, end1, vec2, start2)
  5225. Copy elements from @var{vec1}, positions @var{start1} to @var{end1},
  5226. to @var{vec2} starting at position @var{start2}. @var{start1} and
  5227. @var{start2} are inclusive indices; @var{end1} is exclusive.
  5228. @code{vector-move-left!} copies elements in leftmost order.
  5229. Therefore, in the case where @var{vec1} and @var{vec2} refer to the
  5230. same vector, @code{vector-move-left!} is usually appropriate when
  5231. @var{start1} is greater than @var{start2}.
  5232. The value returned by @code{vector-move-left!} is unspecified.
  5233. @end deffn
  5234. @deffn {Scheme Procedure} vector-move-right! vec1 start1 end1 vec2 start2
  5235. @deffnx {C Function} scm_vector_move_right_x (vec1, start1, end1, vec2, start2)
  5236. Copy elements from @var{vec1}, positions @var{start1} to @var{end1},
  5237. to @var{vec2} starting at position @var{start2}. @var{start1} and
  5238. @var{start2} are inclusive indices; @var{end1} is exclusive.
  5239. @code{vector-move-right!} copies elements in rightmost order.
  5240. Therefore, in the case where @var{vec1} and @var{vec2} refer to the
  5241. same vector, @code{vector-move-right!} is usually appropriate when
  5242. @var{start1} is less than @var{start2}.
  5243. The value returned by @code{vector-move-right!} is unspecified.
  5244. @end deffn
  5245. @node Vector Accessing from C
  5246. @subsubsection Vector Accessing from C
  5247. A vector can be read and modified from C with the functions
  5248. @ref{x-scm_c_vector_ref,@code{scm_c_vector_ref}} and
  5249. @ref{x-scm_c_vector_set_x,@code{scm_c_vector_set_x}}. In addition to
  5250. these functions, there are two other ways to access vectors from C that
  5251. might be more efficient in certain situations: you can use the unsafe
  5252. @emph{vector macros}; or you can use the general framework for accessing
  5253. all kinds of arrays (@pxref{Accessing Arrays from C}), which is more
  5254. verbose, but can deal efficiently with all kinds of vectors (and
  5255. arrays). For arrays of rank 1 whose backing store is a vector, you can
  5256. use the @code{scm_vector_elements} and
  5257. @code{scm_vector_writable_elements} functions as shortcuts.
  5258. @deftypefn {C Macro} size_t SCM_SIMPLE_VECTOR_LENGTH (SCM vec)
  5259. Evaluates to the length of the vector @var{vec}. No type
  5260. checking is done.
  5261. @end deftypefn
  5262. @deftypefn {C Macro} SCM SCM_SIMPLE_VECTOR_REF (SCM vec, size_t idx)
  5263. Evaluates to the element at position @var{idx} in the vector @var{vec}.
  5264. No type or range checking is done.
  5265. @end deftypefn
  5266. @deftypefn {C Macro} void SCM_SIMPLE_VECTOR_SET (SCM vec, size_t idx, SCM val)
  5267. Sets the element at position @var{idx} in the vector @var{vec} to
  5268. @var{val}. No type or range checking is done.
  5269. @end deftypefn
  5270. @deftypefn {C Function} {const SCM *} scm_vector_elements (SCM array, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  5271. Acquire a @ref{Accessing Arrays from C,handle} for @var{array} and
  5272. return a read-only pointer to its elements. @var{array} must be either
  5273. a vector, or an array of rank 1 whose backing store is a vector;
  5274. otherwise an error is signaled. The handle must eventually be released
  5275. with @ref{x-scm_array_handle_release,@code{scm_array_handle_release}}.
  5276. The variables pointed to by @var{lenp} and @var{incp} are filled with
  5277. the number of elements of the array and the increment (number of
  5278. elements) between successive elements, respectively. Successive
  5279. elements of @var{array} need not be contiguous in their underlying
  5280. ``root vector'' returned here; hence the increment is not necessarily
  5281. equal to 1 and may well be negative too (@pxref{Shared Arrays}).
  5282. The following example shows the typical way to use this function. It
  5283. creates a list of all elements of @var{array} (in reverse order).
  5284. @example
  5285. scm_t_array_handle handle;
  5286. size_t i, len;
  5287. ssize_t inc;
  5288. const SCM *elt;
  5289. SCM list;
  5290. elt = scm_vector_elements (array, &handle, &len, &inc);
  5291. list = SCM_EOL;
  5292. for (i = 0; i < len; i++, elt += inc)
  5293. list = scm_cons (*elt, list);
  5294. scm_array_handle_release (&handle);
  5295. @end example
  5296. @end deftypefn
  5297. @deftypefn {C Function} {SCM *} scm_vector_writable_elements (SCM array, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  5298. Like @code{scm_vector_elements} but the pointer can be used to modify
  5299. the array.
  5300. The following example shows the typical way to use this function. It
  5301. fills an array with @code{#t}.
  5302. @example
  5303. scm_t_array_handle handle;
  5304. size_t i, len;
  5305. ssize_t inc;
  5306. SCM *elt;
  5307. elt = scm_vector_writable_elements (array, &handle, &len, &inc);
  5308. for (i = 0; i < len; i++, elt += inc)
  5309. *elt = SCM_BOOL_T;
  5310. scm_array_handle_release (&handle);
  5311. @end example
  5312. @end deftypefn
  5313. @node Uniform Numeric Vectors
  5314. @subsubsection Uniform Numeric Vectors
  5315. A uniform numeric vector is a vector whose elements are all of a single
  5316. numeric type. Guile offers uniform numeric vectors for signed and
  5317. unsigned 8-bit, 16-bit, 32-bit, and 64-bit integers, two sizes of
  5318. floating point values, and complex floating-point numbers of these two
  5319. sizes. @xref{SRFI-4}, for more information.
  5320. For many purposes, bytevectors work just as well as uniform vectors, and have
  5321. the advantage that they integrate well with binary input and output.
  5322. @xref{Bytevectors}, for more information on bytevectors.
  5323. @node Bit Vectors
  5324. @subsection Bit Vectors
  5325. @noindent
  5326. Bit vectors are zero-origin, one-dimensional arrays of booleans. They
  5327. are displayed as a sequence of @code{0}s and @code{1}s prefixed by
  5328. @code{#*}, e.g.,
  5329. @example
  5330. (make-bitvector 8 #f) @result{}
  5331. #*00000000
  5332. @end example
  5333. Bit vectors are the special case of one dimensional bit arrays, and can
  5334. thus be used with the array procedures, @xref{Arrays}.
  5335. @deffn {Scheme Procedure} bitvector? obj
  5336. Return @code{#t} when @var{obj} is a bitvector, else
  5337. return @code{#f}.
  5338. @end deffn
  5339. @deffn {Scheme Procedure} make-bitvector len [fill]
  5340. Create a new bitvector of length @var{len} and
  5341. optionally initialize all elements to @var{fill}.
  5342. @end deffn
  5343. @deffn {Scheme Procedure} bitvector bit @dots{}
  5344. Create a new bitvector with the arguments as elements.
  5345. @end deffn
  5346. @deffn {Scheme Procedure} bitvector-length vec
  5347. Return the length of the bitvector @var{vec}.
  5348. @end deffn
  5349. @deffn {Scheme Procedure} bitvector-bit-set? vec idx
  5350. @deffnx {Scheme Procedure} bitvector-bit-clear? vec idx
  5351. Return @code{#t} if the bit at index @var{idx} of the bitvector
  5352. @var{vec} is set (for @code{bitvector-bit-set?}) or clear (for
  5353. @code{bitvector-bit-clear?}).
  5354. @end deffn
  5355. @deffn {Scheme Procedure} bitvector-set-bit! vec idx
  5356. @deffnx {Scheme Procedure} bitvector-clear-bit! vec idx
  5357. Set (for @code{bitvector-set-bit!}) or clear (for
  5358. @code{bitvector-clear-bit!}) the bit at index @var{idx} of the bitvector
  5359. @var{vec}.
  5360. @end deffn
  5361. @deffn {Scheme Procedure} bitvector-set-all-bits! vec
  5362. @deffnx {Scheme Procedure} bitvector-clear-all-bits! vec
  5363. @deffnx {Scheme Procedure} bitvector-flip-all-bits! vec
  5364. Set, clear, or flip all bits of @var{vec}.
  5365. @end deffn
  5366. @deffn {Scheme Procedure} list->bitvector list
  5367. @deffnx {C Function} scm_list_to_bitvector (list)
  5368. Return a new bitvector initialized with the elements
  5369. of @var{list}.
  5370. @end deffn
  5371. @deffn {Scheme Procedure} bitvector->list vec
  5372. @deffnx {C Function} scm_bitvector_to_list (vec)
  5373. Return a new list initialized with the elements
  5374. of the bitvector @var{vec}.
  5375. @end deffn
  5376. @deffn {Scheme Procedure} bitvector-copy bitvector [start [end]]
  5377. @deffnx {C Function} scm_bitvector_copy (bitvector, start, end)
  5378. Returns a freshly allocated bitvector containing the elements of @var{bitvector}
  5379. in the range [@var{start} ... @var{end}). @var{start} defaults to 0 and
  5380. @var{end} defaults to the length of @var{bitvector}.
  5381. @end deffn
  5382. @deffn {Scheme Procedure} bitvector-count bitvector
  5383. Return a count of how many entries in @var{bitvector} are set.
  5384. @example
  5385. (bitvector-count #*000111000) @result{} 3
  5386. @end example
  5387. @end deffn
  5388. @deffn {Scheme Procedure} bitvector-count-bits bitvector bits
  5389. Return a count of how many entries in @var{bitvector} are set, with the
  5390. bitvector @var{bits} selecting the entries to consider. @var{bitvector}
  5391. must be at least as long as @var{bits}.
  5392. For example,
  5393. @example
  5394. (bitvector-count-bits #*01110111 #*11001101) @result{} 3
  5395. @end example
  5396. @end deffn
  5397. @deffn {Scheme Procedure} bitvector-position bitvector bool start
  5398. @deffnx {C Function} scm_bitvector_position (bitvector, bool, start)
  5399. Return the index of the first occurrence of @var{bool} in
  5400. @var{bitvector}, starting from @var{start}. If there is no @var{bool}
  5401. entry between @var{start} and the end of @var{bitvector}, then return
  5402. @code{#f}. For example,
  5403. @example
  5404. (bitvector-position #*000101 #t 0) @result{} 3
  5405. (bitvector-position #*0001111 #f 3) @result{} #f
  5406. @end example
  5407. @end deffn
  5408. @deffn {Scheme Procedure} bitvector-set-bits! bitvector bits
  5409. Set entries of @var{bitvector} to @code{#t}, with @var{bits} selecting
  5410. the bits to set. The return value is unspecified. @var{bitvector} must
  5411. be at least as long as @var{bits}.
  5412. @example
  5413. (define bv (bitvector-copy #*11000010))
  5414. (bitvector-set-bits! bv #*10010001)
  5415. bv
  5416. @result{} #*11010011
  5417. @end example
  5418. @end deffn
  5419. @deffn {Scheme Procedure} bitvector-clear-bits! bitvector bits
  5420. Set entries of @var{bitvector} to @code{#f}, with @var{bits} selecting
  5421. the bits to clear. The return value is unspecified. @var{bitvector}
  5422. must be at least as long as @var{bits}.
  5423. @example
  5424. (define bv (bitvector-copy #*11000010))
  5425. (bitvector-clear-bits! bv #*10010001)
  5426. bv
  5427. @result{} #*01000010
  5428. @end example
  5429. @end deffn
  5430. @deftypefn {C Function} int scm_is_bitvector (SCM obj)
  5431. @deftypefnx {C Function} SCM scm_c_make_bitvector (size_t len, SCM fill)
  5432. @deftypefnx {C Function} int scm_bitvector_bit_is_set (SCM vec, size_t idx)
  5433. @deftypefnx {C Function} int scm_bitvector_bit_is_clear (SCM vec, size_t idx)
  5434. @deftypefnx {C Function} void scm_c_bitvector_set_bit_x (SCM vec, size_t idx)
  5435. @deftypefnx {C Function} void scm_c_bitvector_clear_bit_x (SCM vec, size_t idx)
  5436. @deftypefnx {C Function} void scm_c_bitvector_set_bits_x (SCM vec, SCM bits)
  5437. @deftypefnx {C Function} void scm_c_bitvector_clear_bits_x (SCM vec, SCM bits)
  5438. @deftypefnx {C Function} void scm_c_bitvector_set_all_bits_x (SCM vec)
  5439. @deftypefnx {C Function} void scm_c_bitvector_clear_all_bits_x (SCM vec)
  5440. @deftypefnx {C Function} void scm_c_bitvector_flip_all_bits_x (SCM vec)
  5441. @deftypefnx {C Function} size_t scm_c_bitvector_length (SCM bitvector)
  5442. @deftypefnx {C Function} size_t scm_c_bitvector_count (SCM bitvector)
  5443. @deftypefnx {C Function} size_t scm_c_bitvector_count_bits (SCM bitvector, SCM bits)
  5444. C API for the corresponding Scheme bitvector interfaces.
  5445. @end deftypefn
  5446. @deftypefn {C Function} {const scm_t_uint32 *} scm_bitvector_elements (SCM vec, scm_t_array_handle *handle, size_t *offp, size_t *lenp, ssize_t *incp)
  5447. Like @code{scm_vector_elements} (@pxref{Vector Accessing from C}), but
  5448. for bitvectors. The variable pointed to by @var{offp} is set to the
  5449. value returned by @code{scm_array_handle_bit_elements_offset}. See
  5450. @code{scm_array_handle_bit_elements} for how to use the returned
  5451. pointer and the offset.
  5452. @end deftypefn
  5453. @deftypefn {C Function} {scm_t_uint32 *} scm_bitvector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *offp, size_t *lenp, ssize_t *incp)
  5454. Like @code{scm_bitvector_elements}, but the pointer is good for reading
  5455. and writing.
  5456. @end deftypefn
  5457. @node Bytevectors
  5458. @subsection Bytevectors
  5459. @cindex bytevector
  5460. @cindex R6RS
  5461. A @dfn{bytevector} is a raw byte string. The @code{(rnrs bytevectors)}
  5462. module provides the programming interface specified by the
  5463. @uref{http://www.r6rs.org/, Revised^6 Report on the Algorithmic Language
  5464. Scheme (R6RS)}. It contains procedures to manipulate bytevectors and
  5465. interpret their contents in a number of ways: as signed or unsigned
  5466. integer of various sizes and endianness, as IEEE-754 floating point
  5467. numbers, or as strings. It is a useful tool to encode and decode binary
  5468. data. The @ref{R7RS Support,R7RS} offers its own set of bytevector
  5469. procedures (@pxref{Bytevector Procedures in R7RS}).
  5470. The R6RS (Section 4.3.4) specifies an external representation for
  5471. bytevectors, whereby the octets (integers in the range 0--255) contained
  5472. in the bytevector are represented as a list prefixed by @code{#vu8}:
  5473. @lisp
  5474. #vu8(1 53 204)
  5475. @end lisp
  5476. denotes a 3-byte bytevector containing the octets 1, 53, and 204. Like
  5477. string literals, booleans, etc., bytevectors are ``self-quoting'', i.e.,
  5478. they do not need to be quoted:
  5479. @lisp
  5480. #vu8(1 53 204)
  5481. @result{} #vu8(1 53 204)
  5482. @end lisp
  5483. Bytevectors can be used with the binary input/output primitives
  5484. (@pxref{Binary I/O}).
  5485. @menu
  5486. * Bytevector Endianness:: Dealing with byte order.
  5487. * Bytevector Manipulation:: Creating, copying, manipulating bytevectors.
  5488. * Bytevectors as Integers:: Interpreting bytes as integers.
  5489. * Bytevectors and Integer Lists:: Converting to/from an integer list.
  5490. * Bytevectors as Floats:: Interpreting bytes as real numbers.
  5491. * Bytevectors as Strings:: Interpreting bytes as Unicode strings.
  5492. * Bytevectors as Arrays:: Guile extension to the bytevector API.
  5493. * Bytevectors as Uniform Vectors:: Bytevectors and SRFI-4.
  5494. * Bytevector Procedures in R7RS:: R7RS interface for bytevectors.
  5495. * Bytevector Slices:: Aliases for parts of a bytevector.
  5496. @end menu
  5497. @node Bytevector Endianness
  5498. @subsubsection Endianness
  5499. @cindex endianness
  5500. @cindex byte order
  5501. @cindex word order
  5502. Some of the following procedures take an @var{endianness} parameter.
  5503. The @dfn{endianness} is defined as the order of bytes in multi-byte
  5504. numbers: numbers encoded in @dfn{big endian} have their most
  5505. significant bytes written first, whereas numbers encoded in
  5506. @dfn{little endian} have their least significant bytes
  5507. first@footnote{Big-endian and little-endian are the most common
  5508. ``endiannesses'', but others do exist. For instance, the GNU MP
  5509. library allows @dfn{word order} to be specified independently of
  5510. @dfn{byte order} (@pxref{Integer Import and Export,,, gmp, The GNU
  5511. Multiple Precision Arithmetic Library Manual}).}.
  5512. Little-endian is the native endianness of the IA32 architecture and
  5513. its derivatives, while big-endian is native to SPARC and PowerPC,
  5514. among others. The @code{native-endianness} procedure returns the
  5515. native endianness of the machine it runs on.
  5516. @deffn {Scheme Procedure} native-endianness
  5517. @deffnx {C Function} scm_native_endianness ()
  5518. Return a value denoting the native endianness of the host machine.
  5519. @end deffn
  5520. @deffn {Scheme Macro} endianness symbol
  5521. Return an object denoting the endianness specified by @var{symbol}. If
  5522. @var{symbol} is neither @code{big} nor @code{little} then an error is
  5523. raised at expand-time.
  5524. @end deffn
  5525. @defvr {C Variable} scm_endianness_big
  5526. @defvrx {C Variable} scm_endianness_little
  5527. The objects denoting big- and little-endianness, respectively.
  5528. @end defvr
  5529. @node Bytevector Manipulation
  5530. @subsubsection Manipulating Bytevectors
  5531. Bytevectors can be created, copied, and analyzed with the following
  5532. procedures and C functions.
  5533. @anchor{x-make-bytevector}
  5534. @deffn {Scheme Procedure} make-bytevector len [fill]
  5535. @deffnx {C Function} scm_make_bytevector (len, fill)
  5536. @deffnx {C Function} scm_c_make_bytevector (size_t len)
  5537. Return a new bytevector of @var{len} bytes. Optionally, if @var{fill}
  5538. is given, fill it with @var{fill}; @var{fill} must be in the range
  5539. [-128,255].
  5540. @end deffn
  5541. @anchor{x-bytevector?}
  5542. @deffn {Scheme Procedure} bytevector? obj
  5543. @deffnx {C Function} scm_bytevector_p (obj)
  5544. Return true if @var{obj} is a bytevector.
  5545. @end deffn
  5546. @deftypefn {C Function} int scm_is_bytevector (SCM obj)
  5547. Equivalent to @code{scm_is_true (scm_bytevector_p (obj))}.
  5548. @end deftypefn
  5549. @anchor{x-bytevector-length}
  5550. @deffn {Scheme Procedure} bytevector-length bv
  5551. @deffnx {C Function} scm_bytevector_length (bv)
  5552. Return the length in bytes of bytevector @var{bv}.
  5553. @end deffn
  5554. @deftypefn {C Function} size_t scm_c_bytevector_length (SCM bv)
  5555. Likewise, return the length in bytes of bytevector @var{bv}.
  5556. @end deftypefn
  5557. @deffn {Scheme Procedure} bytevector=? bv1 bv2
  5558. @deffnx {C Function} scm_bytevector_eq_p (bv1, bv2)
  5559. Return @code{#t} if @var{bv1} equals @var{bv2}---i.e., if they have the same
  5560. length and contents.
  5561. @end deffn
  5562. @deffn {Scheme Procedure} bytevector-fill! bv fill [start [end]]
  5563. @deffnx {C Function} scm_bytevector_fill_x (bv, fill)
  5564. Fill positions [@var{start} ... @var{end}) of bytevector @var{bv} with
  5565. byte @var{fill}. @var{start} defaults to 0 and @var{end} defaults to the
  5566. length of @var{bv}.@footnote{R6RS only defines @code{(bytevector-fill! bv
  5567. fill)}. Arguments @var{start} and @var{end} are a Guile extension
  5568. (cf. @ref{x-vector-fill!,@code{vector-fill!}},
  5569. @ref{x-string-fill!,@code{string-fill!}}).}
  5570. @end deffn
  5571. @anchor{x-r6:bytevector-copy!}
  5572. @deffn {Scheme Procedure} bytevector-copy! source source-start target target-start len
  5573. @deffnx {C Function} scm_bytevector_copy_x (source, source_start, target, target_start, len)
  5574. Copy @var{len} bytes from @var{source} into @var{target}, starting
  5575. reading from @var{source-start} (an index index within @var{source})
  5576. and writing at @var{target-start}.
  5577. It is permitted for the @var{source} and @var{target} regions to
  5578. overlap. In that case, copying takes place as if the source is first
  5579. copied into a temporary bytevector and then into the destination.
  5580. @end deffn
  5581. @anchor{x-r6:bytevector-copy}
  5582. @deffn {Scheme Procedure} bytevector-copy bv
  5583. @deffnx {C Function} scm_bytevector_copy (bv)
  5584. Return a newly allocated copy of @var{bv}.
  5585. @end deffn
  5586. @deftypefn {C Function} scm_t_uint8 scm_c_bytevector_ref (SCM bv, size_t index)
  5587. Return the byte at @var{index} in bytevector @var{bv}.
  5588. @end deftypefn
  5589. @deftypefn {C Function} void scm_c_bytevector_set_x (SCM bv, size_t index, scm_t_uint8 value)
  5590. Set the byte at @var{index} in @var{bv} to @var{value}.
  5591. @end deftypefn
  5592. Low-level C macros are available. They do not perform any
  5593. type-checking; as such they should be used with care.
  5594. @deftypefn {C Macro} size_t SCM_BYTEVECTOR_LENGTH (bv)
  5595. Return the length in bytes of bytevector @var{bv}.
  5596. @end deftypefn
  5597. @deftypefn {C Macro} {signed char *} SCM_BYTEVECTOR_CONTENTS (bv)
  5598. Return a pointer to the contents of bytevector @var{bv}.
  5599. @end deftypefn
  5600. @node Bytevectors as Integers
  5601. @subsubsection Interpreting Bytevector Contents as Integers
  5602. The contents of a bytevector can be interpreted as a sequence of
  5603. integers of any given size, sign, and endianness.
  5604. @lisp
  5605. (let ((bv (make-bytevector 4)))
  5606. (bytevector-u8-set! bv 0 #x12)
  5607. (bytevector-u8-set! bv 1 #x34)
  5608. (bytevector-u8-set! bv 2 #x56)
  5609. (bytevector-u8-set! bv 3 #x78)
  5610. (map (lambda (number)
  5611. (number->string number 16))
  5612. (list (bytevector-u8-ref bv 0)
  5613. (bytevector-u16-ref bv 0 (endianness big))
  5614. (bytevector-u32-ref bv 0 (endianness little)))))
  5615. @result{} ("12" "1234" "78563412")
  5616. @end lisp
  5617. The most generic procedures to interpret bytevector contents as integers
  5618. are described below.
  5619. @deffn {Scheme Procedure} bytevector-uint-ref bv index endianness size
  5620. @deffnx {C Function} scm_bytevector_uint_ref (bv, index, endianness, size)
  5621. Return the @var{size}-byte long unsigned integer at index @var{index} in
  5622. @var{bv}, decoded according to @var{endianness}.
  5623. @end deffn
  5624. @deffn {Scheme Procedure} bytevector-sint-ref bv index endianness size
  5625. @deffnx {C Function} scm_bytevector_sint_ref (bv, index, endianness, size)
  5626. Return the @var{size}-byte long signed integer at index @var{index} in
  5627. @var{bv}, decoded according to @var{endianness}.
  5628. @end deffn
  5629. @deffn {Scheme Procedure} bytevector-uint-set! bv index value endianness size
  5630. @deffnx {C Function} scm_bytevector_uint_set_x (bv, index, value, endianness, size)
  5631. Set the @var{size}-byte long unsigned integer at @var{index} to
  5632. @var{value}, encoded according to @var{endianness}.
  5633. @end deffn
  5634. @deffn {Scheme Procedure} bytevector-sint-set! bv index value endianness size
  5635. @deffnx {C Function} scm_bytevector_sint_set_x (bv, index, value, endianness, size)
  5636. Set the @var{size}-byte long signed integer at @var{index} to
  5637. @var{value}, encoded according to @var{endianness}.
  5638. @end deffn
  5639. The following procedures are similar to the ones above, but specialized
  5640. to a given integer size:
  5641. @anchor{x-bytevector-u8-ref}
  5642. @deffn {Scheme Procedure} bytevector-u8-ref bv index
  5643. @deffnx {Scheme Procedure} bytevector-s8-ref bv index
  5644. @deffnx {Scheme Procedure} bytevector-u16-ref bv index endianness
  5645. @deffnx {Scheme Procedure} bytevector-s16-ref bv index endianness
  5646. @deffnx {Scheme Procedure} bytevector-u32-ref bv index endianness
  5647. @deffnx {Scheme Procedure} bytevector-s32-ref bv index endianness
  5648. @deffnx {Scheme Procedure} bytevector-u64-ref bv index endianness
  5649. @deffnx {Scheme Procedure} bytevector-s64-ref bv index endianness
  5650. @deffnx {C Function} scm_bytevector_u8_ref (bv, index)
  5651. @deffnx {C Function} scm_bytevector_s8_ref (bv, index)
  5652. @deffnx {C Function} scm_bytevector_u16_ref (bv, index, endianness)
  5653. @deffnx {C Function} scm_bytevector_s16_ref (bv, index, endianness)
  5654. @deffnx {C Function} scm_bytevector_u32_ref (bv, index, endianness)
  5655. @deffnx {C Function} scm_bytevector_s32_ref (bv, index, endianness)
  5656. @deffnx {C Function} scm_bytevector_u64_ref (bv, index, endianness)
  5657. @deffnx {C Function} scm_bytevector_s64_ref (bv, index, endianness)
  5658. Return the unsigned @var{n}-bit (signed) integer (where @var{n} is 8,
  5659. 16, 32 or 64) from @var{bv} at @var{index}, decoded according to
  5660. @var{endianness}.
  5661. @end deffn
  5662. @anchor{x-bytevector-u8-set!}
  5663. @deffn {Scheme Procedure} bytevector-u8-set! bv index value
  5664. @deffnx {Scheme Procedure} bytevector-s8-set! bv index value
  5665. @deffnx {Scheme Procedure} bytevector-u16-set! bv index value endianness
  5666. @deffnx {Scheme Procedure} bytevector-s16-set! bv index value endianness
  5667. @deffnx {Scheme Procedure} bytevector-u32-set! bv index value endianness
  5668. @deffnx {Scheme Procedure} bytevector-s32-set! bv index value endianness
  5669. @deffnx {Scheme Procedure} bytevector-u64-set! bv index value endianness
  5670. @deffnx {Scheme Procedure} bytevector-s64-set! bv index value endianness
  5671. @deffnx {C Function} scm_bytevector_u8_set_x (bv, index, value)
  5672. @deffnx {C Function} scm_bytevector_s8_set_x (bv, index, value)
  5673. @deffnx {C Function} scm_bytevector_u16_set_x (bv, index, value, endianness)
  5674. @deffnx {C Function} scm_bytevector_s16_set_x (bv, index, value, endianness)
  5675. @deffnx {C Function} scm_bytevector_u32_set_x (bv, index, value, endianness)
  5676. @deffnx {C Function} scm_bytevector_s32_set_x (bv, index, value, endianness)
  5677. @deffnx {C Function} scm_bytevector_u64_set_x (bv, index, value, endianness)
  5678. @deffnx {C Function} scm_bytevector_s64_set_x (bv, index, value, endianness)
  5679. Store @var{value} as an @var{n}-bit (signed) integer (where @var{n} is
  5680. 8, 16, 32 or 64) in @var{bv} at @var{index}, encoded according to
  5681. @var{endianness}.
  5682. @end deffn
  5683. Finally, a variant specialized for the host's endianness is available
  5684. for each of these functions (with the exception of the @code{u8} and
  5685. @code{s8} accessors, as endianness is about byte order and there is only
  5686. 1 byte):
  5687. @deffn {Scheme Procedure} bytevector-u16-native-ref bv index
  5688. @deffnx {Scheme Procedure} bytevector-s16-native-ref bv index
  5689. @deffnx {Scheme Procedure} bytevector-u32-native-ref bv index
  5690. @deffnx {Scheme Procedure} bytevector-s32-native-ref bv index
  5691. @deffnx {Scheme Procedure} bytevector-u64-native-ref bv index
  5692. @deffnx {Scheme Procedure} bytevector-s64-native-ref bv index
  5693. @deffnx {C Function} scm_bytevector_u16_native_ref (bv, index)
  5694. @deffnx {C Function} scm_bytevector_s16_native_ref (bv, index)
  5695. @deffnx {C Function} scm_bytevector_u32_native_ref (bv, index)
  5696. @deffnx {C Function} scm_bytevector_s32_native_ref (bv, index)
  5697. @deffnx {C Function} scm_bytevector_u64_native_ref (bv, index)
  5698. @deffnx {C Function} scm_bytevector_s64_native_ref (bv, index)
  5699. Return the unsigned @var{n}-bit (signed) integer (where @var{n} is 8,
  5700. 16, 32 or 64) from @var{bv} at @var{index}, decoded according to the
  5701. host's native endianness.
  5702. @end deffn
  5703. @deffn {Scheme Procedure} bytevector-u16-native-set! bv index value
  5704. @deffnx {Scheme Procedure} bytevector-s16-native-set! bv index value
  5705. @deffnx {Scheme Procedure} bytevector-u32-native-set! bv index value
  5706. @deffnx {Scheme Procedure} bytevector-s32-native-set! bv index value
  5707. @deffnx {Scheme Procedure} bytevector-u64-native-set! bv index value
  5708. @deffnx {Scheme Procedure} bytevector-s64-native-set! bv index value
  5709. @deffnx {C Function} scm_bytevector_u16_native_set_x (bv, index, value)
  5710. @deffnx {C Function} scm_bytevector_s16_native_set_x (bv, index, value)
  5711. @deffnx {C Function} scm_bytevector_u32_native_set_x (bv, index, value)
  5712. @deffnx {C Function} scm_bytevector_s32_native_set_x (bv, index, value)
  5713. @deffnx {C Function} scm_bytevector_u64_native_set_x (bv, index, value)
  5714. @deffnx {C Function} scm_bytevector_s64_native_set_x (bv, index, value)
  5715. Store @var{value} as an @var{n}-bit (signed) integer (where @var{n} is
  5716. 8, 16, 32 or 64) in @var{bv} at @var{index}, encoded according to the
  5717. host's native endianness.
  5718. @end deffn
  5719. @node Bytevectors and Integer Lists
  5720. @subsubsection Converting Bytevectors to/from Integer Lists
  5721. Bytevector contents can readily be converted to/from lists of signed or
  5722. unsigned integers:
  5723. @lisp
  5724. (bytevector->sint-list (u8-list->bytevector (make-list 4 255))
  5725. (endianness little) 2)
  5726. @result{} (-1 -1)
  5727. @end lisp
  5728. @deffn {Scheme Procedure} bytevector->u8-list bv
  5729. @deffnx {C Function} scm_bytevector_to_u8_list (bv)
  5730. Return a newly allocated list of unsigned 8-bit integers from the
  5731. contents of @var{bv}.
  5732. @end deffn
  5733. @anchor{x-u8-list->bytevector}
  5734. @deffn {Scheme Procedure} u8-list->bytevector lst
  5735. @deffnx {C Function} scm_u8_list_to_bytevector (lst)
  5736. Return a newly allocated bytevector consisting of the unsigned 8-bit
  5737. integers listed in @var{lst}.
  5738. @end deffn
  5739. @deffn {Scheme Procedure} bytevector->uint-list bv endianness size
  5740. @deffnx {C Function} scm_bytevector_to_uint_list (bv, endianness, size)
  5741. Return a list of unsigned integers of @var{size} bytes representing the
  5742. contents of @var{bv}, decoded according to @var{endianness}.
  5743. @end deffn
  5744. @deffn {Scheme Procedure} bytevector->sint-list bv endianness size
  5745. @deffnx {C Function} scm_bytevector_to_sint_list (bv, endianness, size)
  5746. Return a list of signed integers of @var{size} bytes representing the
  5747. contents of @var{bv}, decoded according to @var{endianness}.
  5748. @end deffn
  5749. @deffn {Scheme Procedure} uint-list->bytevector lst endianness size
  5750. @deffnx {C Function} scm_uint_list_to_bytevector (lst, endianness, size)
  5751. Return a new bytevector containing the unsigned integers listed in
  5752. @var{lst} and encoded on @var{size} bytes according to @var{endianness}.
  5753. @end deffn
  5754. @deffn {Scheme Procedure} sint-list->bytevector lst endianness size
  5755. @deffnx {C Function} scm_sint_list_to_bytevector (lst, endianness, size)
  5756. Return a new bytevector containing the signed integers listed in
  5757. @var{lst} and encoded on @var{size} bytes according to @var{endianness}.
  5758. @end deffn
  5759. @node Bytevectors as Floats
  5760. @subsubsection Interpreting Bytevector Contents as Floating Point Numbers
  5761. @cindex IEEE-754 floating point numbers
  5762. Bytevector contents can also be accessed as IEEE-754 single- or
  5763. double-precision floating point numbers (respectively 32 and 64-bit
  5764. long) using the procedures described here.
  5765. @deffn {Scheme Procedure} bytevector-ieee-single-ref bv index endianness
  5766. @deffnx {Scheme Procedure} bytevector-ieee-double-ref bv index endianness
  5767. @deffnx {C Function} scm_bytevector_ieee_single_ref (bv, index, endianness)
  5768. @deffnx {C Function} scm_bytevector_ieee_double_ref (bv, index, endianness)
  5769. Return the IEEE-754 single-precision floating point number from @var{bv}
  5770. at @var{index} according to @var{endianness}.
  5771. @end deffn
  5772. @deffn {Scheme Procedure} bytevector-ieee-single-set! bv index value endianness
  5773. @deffnx {Scheme Procedure} bytevector-ieee-double-set! bv index value endianness
  5774. @deffnx {C Function} scm_bytevector_ieee_single_set_x (bv, index, value, endianness)
  5775. @deffnx {C Function} scm_bytevector_ieee_double_set_x (bv, index, value, endianness)
  5776. Store real number @var{value} in @var{bv} at @var{index} according to
  5777. @var{endianness}.
  5778. @end deffn
  5779. Specialized procedures are also available:
  5780. @deffn {Scheme Procedure} bytevector-ieee-single-native-ref bv index
  5781. @deffnx {Scheme Procedure} bytevector-ieee-double-native-ref bv index
  5782. @deffnx {C Function} scm_bytevector_ieee_single_native_ref (bv, index)
  5783. @deffnx {C Function} scm_bytevector_ieee_double_native_ref (bv, index)
  5784. Return the IEEE-754 single-precision floating point number from @var{bv}
  5785. at @var{index} according to the host's native endianness.
  5786. @end deffn
  5787. @deffn {Scheme Procedure} bytevector-ieee-single-native-set! bv index value
  5788. @deffnx {Scheme Procedure} bytevector-ieee-double-native-set! bv index value
  5789. @deffnx {C Function} scm_bytevector_ieee_single_native_set_x (bv, index, value)
  5790. @deffnx {C Function} scm_bytevector_ieee_double_native_set_x (bv, index, value)
  5791. Store real number @var{value} in @var{bv} at @var{index} according to
  5792. the host's native endianness.
  5793. @end deffn
  5794. @node Bytevectors as Strings
  5795. @subsubsection Interpreting Bytevector Contents as Unicode Strings
  5796. @cindex Unicode string encoding
  5797. Bytevector contents can also be interpreted as Unicode strings encoded
  5798. in one of the most commonly available encoding formats.
  5799. @xref{Representing Strings as Bytes}, for a more generic interface.
  5800. @lisp
  5801. (utf8->string (u8-list->bytevector '(99 97 102 101)))
  5802. @result{} "cafe"
  5803. (string->utf8 "caf@'e") ;; SMALL LATIN LETTER E WITH ACUTE ACCENT
  5804. @result{} #vu8(99 97 102 195 169)
  5805. @end lisp
  5806. @deftypefn {Scheme Procedure} {} string-utf8-length str
  5807. @deftypefnx {C function} SCM scm_string_utf8_length (str)
  5808. @deftypefnx {C function} size_t scm_c_string_utf8_length (str)
  5809. Return the number of bytes in the UTF-8 representation of @var{str}.
  5810. @end deftypefn
  5811. @deffn {Scheme Procedure} string->utf8 str
  5812. @deffnx {Scheme Procedure} string->utf16 str [endianness]
  5813. @deffnx {Scheme Procedure} string->utf32 str [endianness]
  5814. @deffnx {C Function} scm_string_to_utf8 (str)
  5815. @deffnx {C Function} scm_string_to_utf16 (str, endianness)
  5816. @deffnx {C Function} scm_string_to_utf32 (str, endianness)
  5817. Return a newly allocated bytevector that contains the UTF-8, UTF-16, or
  5818. UTF-32 (aka. UCS-4) encoding of @var{str}. For UTF-16 and UTF-32,
  5819. @var{endianness} should be the symbol @code{big} or @code{little}; when omitted,
  5820. it defaults to big endian.
  5821. @end deffn
  5822. @deffn {Scheme Procedure} utf8->string utf
  5823. @deffnx {Scheme Procedure} utf16->string utf [endianness]
  5824. @deffnx {Scheme Procedure} utf32->string utf [endianness]
  5825. @deffnx {C Function} scm_utf8_to_string (utf)
  5826. @deffnx {C Function} scm_utf16_to_string (utf, endianness)
  5827. @deffnx {C Function} scm_utf32_to_string (utf, endianness)
  5828. Return a newly allocated string that contains from the UTF-8-, UTF-16-,
  5829. or UTF-32-decoded contents of bytevector @var{utf}. For UTF-16 and UTF-32,
  5830. @var{endianness} should be the symbol @code{big} or @code{little}; when omitted,
  5831. it defaults to big endian.
  5832. @end deffn
  5833. @node Bytevectors as Arrays
  5834. @subsubsection Accessing Bytevectors with the Array API
  5835. As an extension to the R6RS, Guile allows bytevectors to be manipulated
  5836. with the @dfn{array} procedures (@pxref{Arrays}). When using these
  5837. APIs, bytes are accessed one at a time as 8-bit unsigned integers:
  5838. @example
  5839. (define bv #vu8(0 1 2 3))
  5840. (array? bv)
  5841. @result{} #t
  5842. (array-rank bv)
  5843. @result{} 1
  5844. (array-ref bv 2)
  5845. @result{} 2
  5846. ;; Note the different argument order on array-set!.
  5847. (array-set! bv 77 2)
  5848. (array-ref bv 2)
  5849. @result{} 77
  5850. (array-type bv)
  5851. @result{} vu8
  5852. @end example
  5853. @node Bytevectors as Uniform Vectors
  5854. @subsubsection Accessing Bytevectors with the SRFI-4 API
  5855. Bytevectors may also be accessed with the SRFI-4 API. @xref{SRFI-4 and
  5856. Bytevectors}, for more information.
  5857. @node Bytevector Procedures in R7RS
  5858. @subsubsection Bytevector Procedures in R7RS
  5859. The @ref{R7RS Support,R7RS} (Section 6.9) defines a set of
  5860. bytevector manipulation procedures, accessible with
  5861. @example
  5862. (use-modules (scheme base))
  5863. @end example
  5864. Of these, @ref{x-make-bytevector,@code{make-bytevector}},
  5865. @ref{x-bytevector?,@code{bytevector?}},
  5866. @ref{x-bytevector-length,@code{bytevector-length}},
  5867. @ref{x-bytevector-u8-ref,@code{bytevector-u8-ref}} and
  5868. @ref{x-bytevector-u8-set!,@code{bytevector-u8-set!}} have the same
  5869. definition as in R6RS. The procedures listed below either have a
  5870. different definition in R7RS and R6RS, or are not defined in R6RS.
  5871. @deffn {Scheme Procedure} bytevector arg @dots{}
  5872. Return a newly allocated bytevector composed of the given arguments.
  5873. Analogous to @code{list}.
  5874. @lisp
  5875. (bytevector 2 3 4) @result{} #vu8(2 3 4)
  5876. @end lisp
  5877. See also @ref{x-u8-list->bytevector,@code{u8-list->bytevector}}.
  5878. @end deffn
  5879. @anchor{x-r7:bytevector-copy}
  5880. @deffn {Scheme Procedure} bytevector-copy bv [start [end]]
  5881. Returns a newly allocated bytevector containing the elements of @var{bv}
  5882. in the range [@var{start} ... @var{end}). @var{start} defaults to 0 and
  5883. @var{end} defaults to the length of @var{bv}.
  5884. @lisp
  5885. (define bv #vu8(0 1 2 3 4 5))
  5886. (bytevector-copy bv) @result{} #vu8(0 1 2 3 4 5)
  5887. (bytevector-copy bv 2) @result{} #vu8(2 3 4 5)
  5888. (bytevector-copy bv 2 4) @result{} #vu8(2 3)
  5889. @end lisp
  5890. See also @ref{x-r6:bytevector-copy,the R6RS version}.
  5891. @end deffn
  5892. @anchor{x-r7:bytevector-copy!}
  5893. @deffn {Scheme Procedure} bytevector-copy! dst at src [start [end]]
  5894. Copy the block of elements from bytevector @var{src} in the range
  5895. [@var{start} ... @var{end}) into bytevector @var{dst}, starting at
  5896. position @var{at}. @var{start} defaults to 0 and @var{end} defaults to
  5897. the length of @var{src}. It is an error for @var{dst}
  5898. to have a length less than @var{at} + (@var{end} - @var{start}).
  5899. See also @ref{x-r6:bytevector-copy!,the R6RS version}. With
  5900. @lisp
  5901. (use-modules ((rnrs bytevectors) #:prefix r6:)
  5902. ((scheme base) #:prefix r7:))
  5903. @end lisp
  5904. the following calls are equivalent:
  5905. @lisp
  5906. (r6:bytevector-copy! source source-start target target-start len)
  5907. (r7:bytevector-copy! target target-start source source-start (+ source-start len))
  5908. @end lisp
  5909. @end deffn
  5910. @rnindex bytevector-append
  5911. @deffn {Scheme Procedure} bytevector-append arg @dots{}
  5912. Return a newly allocated bytevector whose characters form the
  5913. concatenation of the given bytevectors @var{arg} @enddots{}
  5914. @lisp
  5915. (bytevector-append #vu8(0 1 2) #vu8(3 4 5))
  5916. @result{} #vu8(0 1 2 3 4 5)
  5917. @end lisp
  5918. @end deffn
  5919. @node Bytevector Slices
  5920. @subsubsection Bytevector Slices
  5921. @cindex subset, of a bytevector
  5922. @cindex slice, of a bytevector
  5923. @cindex slice, of a uniform vector
  5924. As an extension to the R6RS specification, the @code{(rnrs bytevectors
  5925. gnu)} module provides the @code{bytevector-slice} procedure, which
  5926. returns a bytevector aliasing part of an existing bytevector.
  5927. @deffn {Scheme Procedure} bytevector-slice @var{bv} @var{offset} [@var{size}]
  5928. @deffnx {C Function} scm_bytevector_slice (@var{bv}, @var{offset}, @var{size})
  5929. Return the slice of @var{bv} starting at @var{offset} and counting
  5930. @var{size} bytes. When @var{size} is omitted, the slice covers all
  5931. of @var{bv} starting from @var{offset}. The returned slice shares
  5932. storage with @var{bv}: changes to the slice are visible in @var{bv}
  5933. and vice-versa.
  5934. When @var{bv} is actually a SRFI-4 uniform vector, its element
  5935. type is preserved unless @var{offset} and @var{size} are not aligned
  5936. on its element type size.
  5937. @end deffn
  5938. Here is an example showing how to use it:
  5939. @lisp
  5940. (use-modules (rnrs bytevectors)
  5941. (rnrs bytevectors gnu))
  5942. (define bv (u8-list->bytevector (iota 10)))
  5943. (define slice (bytevector-slice bv 2 3))
  5944. slice
  5945. @result{} #vu8(2 3 4)
  5946. (bytevector-u8-set! slice 0 77)
  5947. slice
  5948. @result{} #vu8(77 3 4)
  5949. bv
  5950. @result{} #vu8(0 1 77 3 4 5 6 7 8 9)
  5951. @end lisp
  5952. @node Arrays
  5953. @subsection Arrays
  5954. @tpindex Arrays
  5955. @dfn{Arrays} are a collection of cells organized into an arbitrary
  5956. number of dimensions. Each cell can be accessed in constant time by
  5957. supplying an index for each dimension.
  5958. In the current implementation, an array uses a vector of some kind for
  5959. the actual storage of its elements. Any kind of vector will do, so you
  5960. can have arrays of uniform numeric values, arrays of characters, arrays
  5961. of bits, and of course, arrays of arbitrary Scheme values. For example,
  5962. arrays with an underlying @code{c64vector} might be nice for digital
  5963. signal processing, while arrays made from a @code{u8vector} might be
  5964. used to hold gray-scale images.
  5965. The number of dimensions of an array is called its @dfn{rank}. Thus,
  5966. a matrix is an array of rank 2, while a vector has rank 1. When
  5967. accessing an array element, you have to specify one exact integer for
  5968. each dimension. These integers are called the @dfn{indices} of the
  5969. element. An array specifies the allowed range of indices for each
  5970. dimension via an inclusive lower and upper bound. These bounds can
  5971. well be negative, but the upper bound must be greater than or equal to
  5972. the lower bound minus one. When all lower bounds of an array are
  5973. zero, it is called a @dfn{zero-origin} array.
  5974. Arrays can be of rank 0, which could be interpreted as a scalar.
  5975. Thus, a zero-rank array can store exactly one object and the list of
  5976. indices of this element is the empty list.
  5977. Arrays contain zero elements when one of their dimensions has a zero
  5978. length. These empty arrays maintain information about their shape: a
  5979. matrix with zero columns and 3 rows is different from a matrix with 3
  5980. columns and zero rows, which again is different from a vector of
  5981. length zero.
  5982. The array procedures are all polymorphic, treating strings, uniform
  5983. numeric vectors, bytevectors, bit vectors and ordinary vectors as one
  5984. dimensional arrays.
  5985. @menu
  5986. * Array Syntax::
  5987. * Array Procedures::
  5988. * Shared Arrays::
  5989. * Arrays as arrays of arrays::
  5990. * Accessing Arrays from C::
  5991. @end menu
  5992. @node Array Syntax
  5993. @subsubsection Array Syntax
  5994. An array is displayed as @code{#} followed by its rank, followed by a
  5995. tag that describes the underlying vector, optionally followed by
  5996. information about its shape, and finally followed by the cells,
  5997. organized into dimensions using parentheses.
  5998. In more words, the array tag is of the form
  5999. @example
  6000. #<rank><vectag><@@lower><:len><@@lower><:len>...
  6001. @end example
  6002. where @code{<rank>} is a positive integer in decimal giving the rank of
  6003. the array. It is omitted when the rank is 1 and the array is non-shared
  6004. and has zero-origin (see below). For shared arrays and for a non-zero
  6005. origin, the rank is always printed even when it is 1 to distinguish
  6006. them from ordinary vectors.
  6007. The @code{<vectag>} part is the tag for a uniform numeric vector, like
  6008. @code{u8}, @code{s16}, etc, @code{b} for bitvectors, or @code{a} for
  6009. strings. It is empty for ordinary vectors.
  6010. The @code{<@@lower>} part is a @samp{@@} character followed by a signed
  6011. integer in decimal giving the lower bound of a dimension. There is one
  6012. @code{<@@lower>} for each dimension. When all lower bounds are zero,
  6013. all @code{<@@lower>} parts are omitted.
  6014. The @code{<:len>} part is a @samp{:} character followed by an unsigned
  6015. integer in decimal giving the length of a dimension. Like for the lower
  6016. bounds, there is one @code{<:len>} for each dimension, and the
  6017. @code{<:len>} part always follows the @code{<@@lower>} part for a
  6018. dimension. Lengths are only then printed when they can't be deduced
  6019. from the nested lists of elements of the array literal, which can happen
  6020. when at least one length is zero.
  6021. As a special case, an array of rank 0 is printed as
  6022. @code{#0<vectag>(<scalar>)}, where @code{<scalar>} is the result of
  6023. printing the single element of the array.
  6024. Thus,
  6025. @table @code
  6026. @item #(1 2 3)
  6027. is an ordinary array of rank 1 with lower bound 0 in dimension 0.
  6028. (I.e., a regular vector.)
  6029. @item #@@2(1 2 3)
  6030. is an ordinary array of rank 1 with lower bound 2 in dimension 0.
  6031. @item #2((1 2 3) (4 5 6))
  6032. is a non-uniform array of rank 2; a 2@cross{}3 matrix with index ranges 0..1
  6033. and 0..2.
  6034. @item #u8(0 1 2)
  6035. is a uniform u8 array of rank 1.
  6036. @item #2u32@@2@@3((1 2) (2 3))
  6037. is a uniform u32 array of rank 2 with index ranges 2..3 and 3..4.
  6038. @item #2()
  6039. is a two-dimensional array with index ranges 0..-1 and 0..-1, i.e.@:
  6040. both dimensions have length zero.
  6041. @item #2:0:2()
  6042. is a two-dimensional array with index ranges 0..-1 and 0..1, i.e.@: the
  6043. first dimension has length zero, but the second has length 2.
  6044. @item #0(12)
  6045. is a rank-zero array with contents 12.
  6046. @end table
  6047. In addition, bytevectors are also arrays, but use a different syntax
  6048. (@pxref{Bytevectors}):
  6049. @table @code
  6050. @item #vu8(1 2 3)
  6051. is a 3-byte long bytevector, with contents 1, 2, 3.
  6052. @end table
  6053. @node Array Procedures
  6054. @subsubsection Array Procedures
  6055. When an array is created, the range of each dimension must be
  6056. specified, e.g., to create a 2@cross{}3 array with a zero-based index:
  6057. @example
  6058. (make-array 'ho 2 3) @result{} #2((ho ho ho) (ho ho ho))
  6059. @end example
  6060. The range of each dimension can also be given explicitly, e.g., another
  6061. way to create the same array:
  6062. @example
  6063. (make-array 'ho '(0 1) '(0 2)) @result{} #2((ho ho ho) (ho ho ho))
  6064. @end example
  6065. The following procedures can be used with arrays (or vectors). An
  6066. argument shown as @var{idx}@dots{} means one parameter for each
  6067. dimension in the array. A @var{idxlist} argument means a list of such
  6068. values, one for each dimension.
  6069. @deffn {Scheme Procedure} array? obj
  6070. @deffnx {C Function} scm_array_p (obj, unused)
  6071. Return @code{#t} if the @var{obj} is an array, and @code{#f} if
  6072. not.
  6073. The second argument to scm_array_p is there for historical reasons,
  6074. but it is not used. You should always pass @code{SCM_UNDEFINED} as
  6075. its value.
  6076. @end deffn
  6077. @deffn {Scheme Procedure} typed-array? obj type
  6078. @deffnx {C Function} scm_typed_array_p (obj, type)
  6079. Return @code{#t} if the @var{obj} is an array of type @var{type}, and
  6080. @code{#f} if not.
  6081. @end deffn
  6082. @deftypefn {C Function} int scm_is_array (SCM obj)
  6083. Return @code{1} if the @var{obj} is an array and @code{0} if not.
  6084. @end deftypefn
  6085. @deftypefn {C Function} int scm_is_typed_array (SCM obj, SCM type)
  6086. Return @code{0} if the @var{obj} is an array of type @var{type}, and
  6087. @code{1} if not.
  6088. @end deftypefn
  6089. @deffn {Scheme Procedure} make-array fill bound @dots{}
  6090. @deffnx {C Function} scm_make_array (fill, bounds)
  6091. Equivalent to @code{(make-typed-array #t @var{fill} @var{bound} ...)}.
  6092. @end deffn
  6093. @deffn {Scheme Procedure} make-typed-array type fill bound @dots{}
  6094. @deffnx {C Function} scm_make_typed_array (type, fill, bounds)
  6095. Create and return an array that has as many dimensions as there are
  6096. @var{bound}s and (maybe) fill it with @var{fill}.
  6097. The underlying storage vector is created according to @var{type},
  6098. which must be a symbol whose name is the `vectag' of the array as
  6099. explained above, or @code{#t} for ordinary, non-specialized arrays.
  6100. For example, using the symbol @code{f64} for @var{type} will create an
  6101. array that uses a @code{f64vector} for storing its elements, and
  6102. @code{a} will use a string.
  6103. When @var{fill} is not the special @emph{unspecified} value, the new
  6104. array is filled with @var{fill}. Otherwise, the initial contents of
  6105. the array is unspecified. The special @emph{unspecified} value is
  6106. stored in the variable @code{*unspecified*} so that for example
  6107. @code{(make-typed-array 'u32 *unspecified* 4)} creates a uninitialized
  6108. @code{u32} vector of length 4.
  6109. Each @var{bound} may be a positive non-zero integer @var{n}, in which
  6110. case the index for that dimension can range from 0 through @var{n}-1; or
  6111. an explicit index range specifier in the form @code{(LOWER UPPER)},
  6112. where both @var{lower} and @var{upper} are integers, possibly less than
  6113. zero, and possibly the same number (however, @var{lower} cannot be
  6114. greater than @var{upper}).
  6115. @end deffn
  6116. @deffn {Scheme Procedure} list->array dimspec list
  6117. Equivalent to @code{(list->typed-array #t @var{dimspec}
  6118. @var{list})}.
  6119. @end deffn
  6120. @deffn {Scheme Procedure} list->typed-array type dimspec list
  6121. @deffnx {C Function} scm_list_to_typed_array (type, dimspec, list)
  6122. Return an array of the type indicated by @var{type} with elements the
  6123. same as those of @var{list}.
  6124. The argument @var{dimspec} determines the number of dimensions of the
  6125. array and their lower bounds. When @var{dimspec} is an exact integer,
  6126. it gives the number of dimensions directly and all lower bounds are
  6127. zero. When it is a list of exact integers, then each element is the
  6128. lower index bound of a dimension, and there will be as many dimensions
  6129. as elements in the list.
  6130. @end deffn
  6131. @deffn {Scheme Procedure} array-type array
  6132. @deffnx {C Function} scm_array_type (array)
  6133. Return the type of @var{array}. This is the `vectag' used for
  6134. printing @var{array} (or @code{#t} for ordinary arrays) and can be
  6135. used with @code{make-typed-array} to create an array of the same kind
  6136. as @var{array}.
  6137. @end deffn
  6138. @deffn {Scheme Procedure} array-ref array idx @dots{}
  6139. @deffnx {C Function} scm_array_ref (array, idxlist)
  6140. Return the element at @code{(idx @dots{})} in @var{array}.
  6141. @example
  6142. (define a (make-array 999 '(1 2) '(3 4)))
  6143. (array-ref a 2 4) @result{} 999
  6144. @end example
  6145. @end deffn
  6146. @deffn {Scheme Procedure} array-in-bounds? array idx @dots{}
  6147. @deffnx {C Function} scm_array_in_bounds_p (array, idxlist)
  6148. Return @code{#t} if the given indices would be acceptable to
  6149. @code{array-ref}.
  6150. @example
  6151. (define a (make-array #f '(1 2) '(3 4)))
  6152. (array-in-bounds? a 2 3) @result{} #t
  6153. (array-in-bounds? a 0 0) @result{} #f
  6154. @end example
  6155. @end deffn
  6156. @deffn {Scheme Procedure} array-set! array obj idx @dots{}
  6157. @deffnx {C Function} scm_array_set_x (array, obj, idxlist)
  6158. Set the element at @code{(idx @dots{})} in @var{array} to @var{obj}.
  6159. The return value is unspecified.
  6160. @example
  6161. (define a (make-array #f '(0 1) '(0 1)))
  6162. (array-set! a #t 1 1)
  6163. a @result{} #2((#f #f) (#f #t))
  6164. @end example
  6165. @end deffn
  6166. @deffn {Scheme Procedure} array-shape array
  6167. @deffnx {Scheme Procedure} array-dimensions array
  6168. @deffnx {C Function} scm_array_dimensions (array)
  6169. Return a list of the bounds for each dimension of @var{array}.
  6170. @code{array-shape} gives @code{(@var{lower} @var{upper})} for each
  6171. dimension. @code{array-dimensions} instead returns just
  6172. @math{@var{upper}+1} for dimensions with a 0 lower bound. Both are
  6173. suitable as input to @code{make-array}.
  6174. For example,
  6175. @example
  6176. (define a (make-array 'foo '(-1 3) 5))
  6177. (array-shape a) @result{} ((-1 3) (0 4))
  6178. (array-dimensions a) @result{} ((-1 3) 5)
  6179. @end example
  6180. @end deffn
  6181. @deffn {Scheme Procedure} array-length array
  6182. @deffnx {C Function} scm_array_length (array)
  6183. @deffnx {C Function} size_t scm_c_array_length (array)
  6184. Return the length of an array: its first dimension. It is an error to
  6185. ask for the length of an array of rank 0.
  6186. @end deffn
  6187. @deffn {Scheme Procedure} array-rank array
  6188. @deffnx {C Function} scm_array_rank (array)
  6189. Return the rank of @var{array}.
  6190. @end deffn
  6191. @deftypefn {C Function} size_t scm_c_array_rank (SCM array)
  6192. Return the rank of @var{array} as a @code{size_t}.
  6193. @end deftypefn
  6194. @deffn {Scheme Procedure} array->list array
  6195. @deffnx {C Function} scm_array_to_list (array)
  6196. Return a list consisting of all the elements, in order, of
  6197. @var{array}.
  6198. @end deffn
  6199. @c FIXME: Describe how the order affects the copying (it matters for
  6200. @c shared arrays with the same underlying root vector, presumably).
  6201. @c
  6202. @deffn {Scheme Procedure} array-copy! src dst
  6203. @deffnx {Scheme Procedure} array-copy-in-order! src dst
  6204. @deffnx {C Function} scm_array_copy_x (src, dst)
  6205. Copy every element from vector or array @var{src} to the corresponding
  6206. element of @var{dst}. @var{dst} must have the same rank as @var{src},
  6207. and be at least as large in each dimension. The return value is
  6208. unspecified.
  6209. @end deffn
  6210. @deffn {Scheme Procedure} array-fill! array fill
  6211. @deffnx {C Function} scm_array_fill_x (array, fill)
  6212. Store @var{fill} in every element of @var{array}. The value returned
  6213. is unspecified.
  6214. @end deffn
  6215. @c begin (texi-doc-string "guile" "array-equal?")
  6216. @deffn {Scheme Procedure} array-equal? array @dots{}
  6217. Return @code{#t} if all arguments are arrays with the same shape, the
  6218. same type, and have corresponding elements which are either
  6219. @code{equal?} or @code{array-equal?}. This function differs from
  6220. @code{equal?} (@pxref{Equality}) in that all arguments must be arrays.
  6221. @end deffn
  6222. @c FIXME: array-for-each doesn't say what happens if the sources have
  6223. @c different index ranges. The code currently iterates over the
  6224. @c indices of the first and expects the others to cover those. That
  6225. @c at least vaguely matches array-map!, but is it meant to be a
  6226. @c documented feature?
  6227. @deffn {Scheme Procedure} array-map! dst proc src @dots{}
  6228. @deffnx {Scheme Procedure} array-map-in-order! dst proc src @dots{}
  6229. @deffnx {C Function} scm_array_map_x (dst, proc, srclist)
  6230. Set each element of the @var{dst} array to values obtained from calls to
  6231. @var{proc}. The list of @var{src} arguments may be empty. The value
  6232. returned is unspecified.
  6233. Each call is @code{(@var{proc} @var{elem} @dots{})}, where each
  6234. @var{elem} is from the corresponding @var{src} array, at the
  6235. @var{dst} index. @code{array-map-in-order!} makes the calls in
  6236. row-major order, @code{array-map!} makes them in an unspecified order.
  6237. The @var{src} arrays must have the same number of dimensions as
  6238. @var{dst}, and must have a range for each dimension which covers the
  6239. range in @var{dst}. This ensures all @var{dst} indices are valid in
  6240. each @var{src}.
  6241. @end deffn
  6242. @deffn {Scheme Procedure} array-for-each proc src1 src2 @dots{}
  6243. @deffnx {C Function} scm_array_for_each (proc, src1, srclist)
  6244. Apply @var{proc} to each tuple of elements of @var{src1} @var{src2}
  6245. @dots{}, in row-major order. The value returned is unspecified.
  6246. @end deffn
  6247. @deffn {Scheme Procedure} array-index-map! dst proc
  6248. @deffnx {C Function} scm_array_index_map_x (dst, proc)
  6249. Set each element of the @var{dst} array to values returned by calls to
  6250. @var{proc}. The value returned is unspecified.
  6251. Each call is @code{(@var{proc} @var{i1} @dots{} @var{iN})}, where
  6252. @var{i1}@dots{}@var{iN} is the destination index, one parameter for
  6253. each dimension. The order in which the calls are made is unspecified.
  6254. For example, to create a @m{4\times4, 4x4} matrix representing a
  6255. cyclic group,
  6256. @tex
  6257. \advance\leftskip by 2\lispnarrowing {
  6258. $\left(\matrix{%
  6259. 0 & 1 & 2 & 3 \cr
  6260. 1 & 2 & 3 & 0 \cr
  6261. 2 & 3 & 0 & 1 \cr
  6262. 3 & 0 & 1 & 2 \cr
  6263. }\right)$} \par
  6264. @end tex
  6265. @ifnottex
  6266. @example
  6267. / 0 1 2 3 \
  6268. | 1 2 3 0 |
  6269. | 2 3 0 1 |
  6270. \ 3 0 1 2 /
  6271. @end example
  6272. @end ifnottex
  6273. @example
  6274. (define a (make-array #f 4 4))
  6275. (array-index-map! a (lambda (i j)
  6276. (modulo (+ i j) 4)))
  6277. @end example
  6278. @end deffn
  6279. An additional array function is available in the module
  6280. @code{(ice-9 arrays)}. It can be used with:
  6281. @example
  6282. (use-modules (ice-9 arrays))
  6283. @end example
  6284. @deffn {Scheme Procedure} array-copy src
  6285. Return a new array with the same elements, type and shape as
  6286. @var{src}. However, the array increments may not be the same as those of
  6287. @var{src}. In the current implementation, the returned array will be in
  6288. row-major order, but that might change in the future. Use
  6289. @code{array-copy!} on an array of known order if that is a concern.
  6290. @end deffn
  6291. @node Shared Arrays
  6292. @subsubsection Shared Arrays
  6293. @deffn {Scheme Procedure} make-shared-array oldarray mapfunc bound @dots{}
  6294. @deffnx {C Function} scm_make_shared_array (oldarray, mapfunc, boundlist)
  6295. Return a new array which shares the storage of @var{oldarray}.
  6296. Changes made through either affect the same underlying storage. The
  6297. @var{bound} @dots{} arguments are the shape of the new array, the same
  6298. as @code{make-array} (@pxref{Array Procedures}).
  6299. @var{mapfunc} translates coordinates from the new array to the
  6300. @var{oldarray}. It's called as @code{(@var{mapfunc} newidx1 @dots{})}
  6301. with one parameter for each dimension of the new array, and should
  6302. return a list of indices for @var{oldarray}, one for each dimension of
  6303. @var{oldarray}.
  6304. @var{mapfunc} must be affine linear, meaning that each @var{oldarray}
  6305. index must be formed by adding integer multiples (possibly negative)
  6306. of some or all of @var{newidx1} etc, plus a possible integer offset.
  6307. The multiples and offset must be the same in each call.
  6308. @sp 1
  6309. One good use for a shared array is to restrict the range of some
  6310. dimensions, so as to apply say @code{array-for-each} or
  6311. @code{array-fill!} to only part of an array. The plain @code{list}
  6312. function can be used for @var{mapfunc} in this case, making no changes
  6313. to the index values. For example,
  6314. @example
  6315. (make-shared-array #2((a b c) (d e f) (g h i)) list 3 2)
  6316. @result{} #2((a b) (d e) (g h))
  6317. @end example
  6318. The new array can have fewer dimensions than @var{oldarray}, for
  6319. example to take a column from an array.
  6320. @example
  6321. (make-shared-array #2((a b c) (d e f) (g h i))
  6322. (lambda (i) (list i 2))
  6323. '(0 2))
  6324. @result{} #1(c f i)
  6325. @end example
  6326. A diagonal can be taken by using the single new array index for both
  6327. row and column in the old array. For example,
  6328. @example
  6329. (make-shared-array #2((a b c) (d e f) (g h i))
  6330. (lambda (i) (list i i))
  6331. '(0 2))
  6332. @result{} #1(a e i)
  6333. @end example
  6334. Dimensions can be increased by for instance considering portions of a
  6335. one dimensional array as rows in a two dimensional array.
  6336. (@code{array-contents} below can do the opposite, flattening an
  6337. array.)
  6338. @example
  6339. (make-shared-array #1(a b c d e f g h i j k l)
  6340. (lambda (i j) (list (+ (* i 3) j)))
  6341. 4 3)
  6342. @result{} #2((a b c) (d e f) (g h i) (j k l))
  6343. @end example
  6344. By negating an index the order that elements appear can be reversed.
  6345. The following just reverses the column order,
  6346. @example
  6347. (make-shared-array #2((a b c) (d e f) (g h i))
  6348. (lambda (i j) (list i (- 2 j)))
  6349. 3 3)
  6350. @result{} #2((c b a) (f e d) (i h g))
  6351. @end example
  6352. A fixed offset on indexes allows for instance a change from a 0 based
  6353. to a 1 based array,
  6354. @example
  6355. (define x #2((a b c) (d e f) (g h i)))
  6356. (define y (make-shared-array x
  6357. (lambda (i j) (list (1- i) (1- j)))
  6358. '(1 3) '(1 3)))
  6359. (array-ref x 0 0) @result{} a
  6360. (array-ref y 1 1) @result{} a
  6361. @end example
  6362. A multiple on an index allows every Nth element of an array to be
  6363. taken. The following is every third element,
  6364. @example
  6365. (make-shared-array #1(a b c d e f g h i j k l)
  6366. (lambda (i) (list (* i 3)))
  6367. 4)
  6368. @result{} #1(a d g j)
  6369. @end example
  6370. The above examples can be combined to make weird and wonderful
  6371. selections from an array, but it's important to note that because
  6372. @var{mapfunc} must be affine linear, arbitrary permutations are not
  6373. possible.
  6374. In the current implementation, @var{mapfunc} is not called for every
  6375. access to the new array but only on some sample points to establish a
  6376. base and stride for new array indices in @var{oldarray} data. A few
  6377. sample points are enough because @var{mapfunc} is linear.
  6378. @end deffn
  6379. @deffn {Scheme Procedure} shared-array-increments array
  6380. @deffnx {C Function} scm_shared_array_increments (array)
  6381. For each dimension, return the distance between elements in the root vector.
  6382. @end deffn
  6383. @deffn {Scheme Procedure} shared-array-offset array
  6384. @deffnx {C Function} scm_shared_array_offset (array)
  6385. Return the root vector index of the first element in the array.
  6386. @end deffn
  6387. @deffn {Scheme Procedure} shared-array-root array
  6388. @deffnx {C Function} scm_shared_array_root (array)
  6389. Return the root vector of a shared array.
  6390. @end deffn
  6391. @deffn {Scheme Procedure} array-contents array [strict]
  6392. @deffnx {C Function} scm_array_contents (array, strict)
  6393. If @var{array} may be @dfn{unrolled} into a one dimensional shared array
  6394. without changing their order (last subscript changing fastest), then
  6395. @code{array-contents} returns that shared array, otherwise it returns
  6396. @code{#f}. All arrays made by @code{make-array} and
  6397. @code{make-typed-array} may be unrolled, some arrays made by
  6398. @code{make-shared-array} may not be.
  6399. If the optional argument @var{strict} is provided, a shared array will
  6400. be returned only if its elements are stored internally contiguous in
  6401. memory.
  6402. @end deffn
  6403. @deffn {Scheme Procedure} transpose-array array dim1 dim2 @dots{}
  6404. @deffnx {C Function} scm_transpose_array (array, dimlist)
  6405. Return an array sharing contents with @var{array}, but with
  6406. dimensions arranged in a different order. There must be one
  6407. @var{dim} argument for each dimension of @var{array}.
  6408. @var{dim1}, @var{dim2}, @dots{} should be integers between 0
  6409. and the rank of the array to be returned. Each integer in that
  6410. range must appear at least once in the argument list.
  6411. The values of @var{dim1}, @var{dim2}, @dots{} correspond to
  6412. dimensions in the array to be returned, and their positions in the
  6413. argument list to dimensions of @var{array}. Several @var{dim}s
  6414. may have the same value, in which case the returned array will
  6415. have smaller rank than @var{array}.
  6416. @lisp
  6417. (transpose-array '#2((a b) (c d)) 1 0) @result{} #2((a c) (b d))
  6418. (transpose-array '#2((a b) (c d)) 0 0) @result{} #1(a d)
  6419. (transpose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 1 0) @result{}
  6420. #2((a 4) (b 5) (c 6))
  6421. @end lisp
  6422. @end deffn
  6423. @node Arrays as arrays of arrays
  6424. @subsubsection Arrays as arrays of arrays
  6425. @cindex array cell
  6426. One can see an array of rank @math{n} (an
  6427. @math{n}-array) as an array of lower rank where the elements are
  6428. themselves arrays (`cells').
  6429. @cindex array frame
  6430. @cindex frame rank
  6431. We speak of the first @math{n-k} dimensions of the array as the
  6432. @math{n-k}-`frame' of the array, while the last @math{k} dimensions are
  6433. the dimensions of the @math{k}-`cells'. For example, a 3-array can be
  6434. seen as a 2-array of vectors (1-arrays) or as a 1-array of matrices
  6435. (2-arrays). In each case, the vectors or matrices are the 1-cells or
  6436. 2-cells of the array. This terminology originates in the J language.
  6437. @cindex array slice
  6438. @cindex prefix slice
  6439. The more vague concept of a `slice' refers to a subset of the array
  6440. where some indices are fixed and others are left free. As a Guile data
  6441. object, a cell is the same as a `prefix slice' (the first @math{n-k}
  6442. indices into the original array are fixed), except that a 0-cell is not
  6443. a shared array of the original array, but a 0-slice (where all the
  6444. indices into the original array are fixed) is.
  6445. @cindex enclosed array
  6446. Before @w{version 2.0}, Guile had a feature called `enclosed arrays' to
  6447. create special `array of arrays' objects. The functions in this section
  6448. do not need special types; instead, the frame rank is stated in each
  6449. function call, either implicitly or explicitly.
  6450. @deffn {Scheme Procedure} array-cell-ref array idx @dots{}
  6451. @deffnx {C Function} scm_array_cell_ref (array, idxlist)
  6452. If the length of @var{idxlist} equals the rank @math{n} of @var{array},
  6453. return the element at @code{(idx @dots{})}, just like @code{(array-ref
  6454. array idx @dots{})}. If, however, the length @math{k} of @var{idxlist}
  6455. is smaller than @math{n}, then return the @math{(n-k)}-cell of
  6456. @var{array} given by @var{idxlist}, as a shared array.
  6457. For example:
  6458. @lisp
  6459. (array-cell-ref #2((a b) (c d)) 0) @result{} #(a b)
  6460. (array-cell-ref #2((a b) (c d)) 1) @result{} #(c d)
  6461. (array-cell-ref #2((a b) (c d)) 1 1) @result{} d
  6462. (array-cell-ref #2((a b) (c d))) @result{} #2((a b) (c d))
  6463. @end lisp
  6464. @code{(apply array-cell-ref array indices)} is equivalent to
  6465. @lisp
  6466. (let ((len (length indices)))
  6467. (if (= (array-rank a) len)
  6468. (apply array-ref a indices)
  6469. (apply make-shared-array a
  6470. (lambda t (append indices t))
  6471. (drop (array-dimensions a) len))))
  6472. @end lisp
  6473. @end deffn
  6474. @deffn {Scheme Procedure} array-slice array idx @dots{}
  6475. @deffnx {C Function} scm_array_slice (array, idxlist)
  6476. Like @code{(array-cell-ref array idx @dots{})}, but return a 0-rank
  6477. shared array into @var{ARRAY} if the length of @var{idxlist} matches the
  6478. rank of @var{array}. This can be useful when using @var{ARRAY} as a
  6479. place to write to.
  6480. Compare:
  6481. @lisp
  6482. (array-cell-ref #2((a b) (c d)) 1 1) @result{} d
  6483. (array-slice #2((a b) (c d)) 1 1) @result{} #0(d)
  6484. (define a (make-array 'a 2 2))
  6485. (array-fill! (array-slice a 1 1) 'b)
  6486. a @result{} #2((a a) (a b)).
  6487. (array-fill! (array-cell-ref a 1 1) 'b) @result{} error: not an array
  6488. @end lisp
  6489. @code{(apply array-slice array indices)} is equivalent to
  6490. @lisp
  6491. (apply make-shared-array a
  6492. (lambda t (append indices t))
  6493. (drop (array-dimensions a) (length indices)))
  6494. @end lisp
  6495. @end deffn
  6496. @deffn {Scheme Procedure} array-cell-set! array x idx @dots{}
  6497. @deffnx {C Function} scm_array_cell_set_x (array, x, idxlist)
  6498. If the length of @var{idxlist} equals the rank @math{n} of
  6499. @var{array}, set the element at @code{(idx @dots{})} of @var{array} to
  6500. @var{x}, just like @code{(array-set! array x idx @dots{})}. If,
  6501. however, the length @math{k} of @var{idxlist} is smaller than
  6502. @math{n}, then copy the @math{(n-k)}-rank array @var{x}
  6503. into the @math{(n-k)}-cell of @var{array} given by
  6504. @var{idxlist}. In this case, the last @math{(n-k)} dimensions of
  6505. @var{array} and the dimensions of @var{x} must match exactly.
  6506. This function returns the modified @var{array}.
  6507. For example:
  6508. @lisp
  6509. (array-cell-set! (make-array 'a 2 2) b 1 1)
  6510. @result{} #2((a a) (a b))
  6511. (array-cell-set! (make-array 'a 2 2) #(x y) 1)
  6512. @result{} #2((a a) (x y))
  6513. @end lisp
  6514. Note that @code{array-cell-set!} will expect elements, not arrays, when
  6515. the destination has rank 0. Use @code{array-slice} for the opposite
  6516. behavior.
  6517. @lisp
  6518. (array-cell-set! (make-array 'a 2 2) #0(b) 1 1)
  6519. @result{} #2((a a) (a #0(b)))
  6520. (let ((a (make-array 'a 2 2)))
  6521. (array-copy! #0(b) (array-slice a 1 1)) a)
  6522. @result{} #2((a a) (a b))
  6523. @end lisp
  6524. @code{(apply array-cell-set! array x indices)} is equivalent to
  6525. @lisp
  6526. (let ((len (length indices)))
  6527. (if (= (array-rank array) len)
  6528. (apply array-set! array x indices)
  6529. (array-copy! x (apply array-cell-ref array indices)))
  6530. array)
  6531. @end lisp
  6532. @end deffn
  6533. @deffn {Scheme Procedure} array-slice-for-each frame-rank op x @dots{}
  6534. @deffnx {C Function} scm_array_slice_for_each (array, frame_rank, op, xlist)
  6535. Each @var{x} must be an array of rank ≥ @var{frame-rank}, and
  6536. the first @var{frame-rank} dimensions of each @var{x} must all be the
  6537. same. @var{array-slice-for-each} calls @var{op} with each set of
  6538. (rank(@var{x}) - @var{frame-rank})-cells from @var{x}, in unspecified order.
  6539. @var{array-slice-for-each} allows you to loop over cells of any rank
  6540. without having to carry an index list or construct shared arrays
  6541. manually. The slices passed to @var{op} are always shared arrays of
  6542. @var{X}, even if they are of rank 0, so it is possible to write to them.
  6543. This function returns an unspecified value.
  6544. For example, to sort the rows of rank-2 array @code{a}:
  6545. @lisp
  6546. (array-slice-for-each 1 (lambda (x) (sort! x <)) a)
  6547. @end lisp
  6548. As another example, let @code{a} be a rank-2 array where each row is a
  6549. 2-element vector @math{(x,y)}. Let's compute the arguments of these
  6550. vectors and store them in rank-1 array @code{b}.
  6551. @lisp
  6552. (array-slice-for-each 1
  6553. (lambda (a b)
  6554. (array-set! b (atan (array-ref a 1) (array-ref a 0))))
  6555. a b)
  6556. @end lisp
  6557. @code{(apply array-slice-for-each frame-rank op x)} is equivalent to
  6558. @lisp
  6559. (let ((frame (take (array-dimensions (car x)) frank)))
  6560. (unless (every (lambda (x)
  6561. (equal? frame (take (array-dimensions x) frank)))
  6562. (cdr x))
  6563. (error))
  6564. (array-index-map!
  6565. (apply make-shared-array (make-array #t) (const '()) frame)
  6566. (lambda i (apply op (map (lambda (x) (apply array-slice x i)) x)))))
  6567. @end lisp
  6568. @end deffn
  6569. @deffn {Scheme Procedure} array-slice-for-each-in-order frame-rank op x @dots{}
  6570. @deffnx {C Function} scm_array_slice_for_each_in_order (array, frame_rank, op, xlist)
  6571. Same as @code{array-slice-for-each}, but the arguments are traversed
  6572. sequentially and in row-major order.
  6573. @end deffn
  6574. @node Accessing Arrays from C
  6575. @subsubsection Accessing Arrays from C
  6576. For interworking with external C code, Guile provides an API to allow C
  6577. code to access the elements of a Scheme array. In particular, for
  6578. uniform numeric arrays, the API exposes the underlying uniform data as a
  6579. C array of numbers of the relevant type.
  6580. While pointers to the elements of an array are in use, the array itself
  6581. must be protected so that the pointer remains valid. Such a protected
  6582. array is said to be @dfn{reserved}. A reserved array can be read but
  6583. modifications to it that would cause the pointer to its elements to
  6584. become invalid are prevented. When you attempt such a modification, an
  6585. error is signaled.
  6586. (This is similar to locking the array while it is in use, but without
  6587. the danger of a deadlock. In a multi-threaded program, you will need
  6588. additional synchronization to avoid modifying reserved arrays.)
  6589. You must take care to always unreserve an array after reserving it,
  6590. even in the presence of non-local exits. If a non-local exit can
  6591. happen between these two calls, you should install a dynwind context
  6592. that releases the array when it is left (@pxref{Dynamic Wind}).
  6593. In addition, array reserving and unreserving must be properly
  6594. paired. For instance, when reserving two or more arrays in a certain
  6595. order, you need to unreserve them in the opposite order.
  6596. Once you have reserved an array and have retrieved the pointer to its
  6597. elements, you must figure out the layout of the elements in memory.
  6598. Guile allows slices to be taken out of arrays without actually making a
  6599. copy, such as making an alias for the diagonal of a matrix that can be
  6600. treated as a vector. Arrays that result from such an operation are not
  6601. stored contiguously in memory and when working with their elements
  6602. directly, you need to take this into account.
  6603. The layout of array elements in memory can be defined via a
  6604. @emph{mapping function} that computes a scalar position from a vector of
  6605. indices. The scalar position then is the offset of the element with the
  6606. given indices from the start of the storage block of the array.
  6607. In Guile, this mapping function is restricted to be @dfn{affine}: all
  6608. mapping functions of Guile arrays can be written as @code{p = b +
  6609. c[0]*i[0] + c[1]*i[1] + ... + c[n-1]*i[n-1]} where @code{i[k]} is the
  6610. @nicode{k}th index and @code{n} is the rank of the array. For
  6611. example, a matrix of size 3x3 would have @code{b == 0}, @code{c[0] ==
  6612. 3} and @code{c[1] == 1}. When you transpose this matrix (with
  6613. @code{transpose-array}, say), you will get an array whose mapping
  6614. function has @code{b == 0}, @code{c[0] == 1} and @code{c[1] == 3}.
  6615. The function @code{scm_array_handle_dims} gives you (indirect) access to
  6616. the coefficients @code{c[k]}.
  6617. @c XXX
  6618. Note that there are no functions for accessing the elements of a
  6619. character array yet. Once the string implementation of Guile has been
  6620. changed to use Unicode, we will provide them.
  6621. @deftp {C Type} scm_t_array_handle
  6622. This is a structure type that holds all information necessary to manage
  6623. the reservation of arrays as explained above. Structures of this type
  6624. must be allocated on the stack and must only be accessed by the
  6625. functions listed below.
  6626. @end deftp
  6627. @deftypefn {C Function} void scm_array_get_handle (SCM array, scm_t_array_handle *handle)
  6628. Reserve @var{array}, which must be an array, and prepare @var{handle} to
  6629. be used with the functions below. You must eventually call
  6630. @code{scm_array_handle_release} on @var{handle}, and do this in a
  6631. properly nested fashion, as explained above. The structure pointed to
  6632. by @var{handle} does not need to be initialized before calling this
  6633. function.
  6634. @end deftypefn
  6635. @anchor{x-scm_array_handle_release}
  6636. @deftypefn {C Function} void scm_array_handle_release (scm_t_array_handle *handle)
  6637. End the array reservation represented by @var{handle}. After a call to
  6638. this function, @var{handle} might be used for another reservation.
  6639. @end deftypefn
  6640. @deftypefn {C Function} size_t scm_array_handle_rank (scm_t_array_handle *handle)
  6641. Return the rank of the array represented by @var{handle}.
  6642. @end deftypefn
  6643. @deftp {C Type} scm_t_array_dim
  6644. This structure type holds information about the layout of one dimension
  6645. of an array. It includes the following fields:
  6646. @table @code
  6647. @item ssize_t lbnd
  6648. @itemx ssize_t ubnd
  6649. The lower and upper bounds (both inclusive) of the permissible index
  6650. range for the given dimension. Both values can be negative, but
  6651. @var{lbnd} is always less than or equal to @var{ubnd}.
  6652. @item ssize_t inc
  6653. The distance from one element of this dimension to the next. Note, too,
  6654. that this can be negative.
  6655. @end table
  6656. @end deftp
  6657. @deftypefn {C Function} {const scm_t_array_dim *} scm_array_handle_dims (scm_t_array_handle *handle)
  6658. Return a pointer to a C vector of information about the dimensions of
  6659. the array represented by @var{handle}. This pointer is valid as long as
  6660. the array remains reserved. As explained above, the
  6661. @code{scm_t_array_dim} structures returned by this function can be used
  6662. calculate the position of an element in the storage block of the array
  6663. from its indices.
  6664. This position can then be used as an index into the C array pointer
  6665. returned by the various @code{scm_array_handle_<foo>_elements}
  6666. functions, or with @code{scm_array_handle_ref} and
  6667. @code{scm_array_handle_set}.
  6668. Here is how one can compute the position @var{pos} of an element given
  6669. its indices in the vector @var{indices}:
  6670. @example
  6671. ssize_t indices[RANK];
  6672. scm_t_array_dim *dims;
  6673. ssize_t pos;
  6674. size_t i;
  6675. pos = 0;
  6676. for (i = 0; i < RANK; i++)
  6677. @{
  6678. if (indices[i] < dims[i].lbnd || indices[i] > dims[i].ubnd)
  6679. out_of_range ();
  6680. pos += (indices[i] - dims[i].lbnd) * dims[i].inc;
  6681. @}
  6682. @end example
  6683. @end deftypefn
  6684. @deftypefn {C Function} ssize_t scm_array_handle_pos (scm_t_array_handle *handle, SCM indices)
  6685. Compute the position corresponding to @var{indices}, a list of
  6686. indices. The position is computed as described above for
  6687. @code{scm_array_handle_dims}. The number of the indices and their
  6688. range is checked and an appropriate error is signaled for invalid
  6689. indices.
  6690. @end deftypefn
  6691. @deftypefn {C Function} SCM scm_array_handle_ref (scm_t_array_handle *handle, ssize_t pos)
  6692. Return the element at position @var{pos} in the storage block of the
  6693. array represented by @var{handle}. Any kind of array is acceptable. No
  6694. range checking is done on @var{pos}.
  6695. @end deftypefn
  6696. @deftypefn {C Function} void scm_array_handle_set (scm_t_array_handle *handle, ssize_t pos, SCM val)
  6697. Set the element at position @var{pos} in the storage block of the array
  6698. represented by @var{handle} to @var{val}. Any kind of array is
  6699. acceptable. No range checking is done on @var{pos}. An error is
  6700. signaled when the array can not store @var{val}.
  6701. @end deftypefn
  6702. @deftypefn {C Function} {const SCM *} scm_array_handle_elements (scm_t_array_handle *handle)
  6703. Return a pointer to the elements of a ordinary array of general Scheme
  6704. values (i.e., a non-uniform array) for reading. This pointer is valid
  6705. as long as the array remains reserved.
  6706. @end deftypefn
  6707. @deftypefn {C Function} {SCM *} scm_array_handle_writable_elements (scm_t_array_handle *handle)
  6708. Like @code{scm_array_handle_elements}, but the pointer is good for
  6709. reading and writing.
  6710. @end deftypefn
  6711. @deftypefn {C Function} {const void *} scm_array_handle_uniform_elements (scm_t_array_handle *handle)
  6712. Return a pointer to the elements of a uniform numeric array for reading.
  6713. This pointer is valid as long as the array remains reserved. The size
  6714. of each element is given by @code{scm_array_handle_uniform_element_size}.
  6715. @end deftypefn
  6716. @deftypefn {C Function} {void *} scm_array_handle_uniform_writable_elements (scm_t_array_handle *handle)
  6717. Like @code{scm_array_handle_uniform_elements}, but the pointer is good
  6718. reading and writing.
  6719. @end deftypefn
  6720. @deftypefn {C Function} size_t scm_array_handle_uniform_element_size (scm_t_array_handle *handle)
  6721. Return the size of one element of the uniform numeric array represented
  6722. by @var{handle}.
  6723. @end deftypefn
  6724. @deftypefn {C Function} {const scm_t_uint8 *} scm_array_handle_u8_elements (scm_t_array_handle *handle)
  6725. @deftypefnx {C Function} {const scm_t_int8 *} scm_array_handle_s8_elements (scm_t_array_handle *handle)
  6726. @deftypefnx {C Function} {const scm_t_uint16 *} scm_array_handle_u16_elements (scm_t_array_handle *handle)
  6727. @deftypefnx {C Function} {const scm_t_int16 *} scm_array_handle_s16_elements (scm_t_array_handle *handle)
  6728. @deftypefnx {C Function} {const scm_t_uint32 *} scm_array_handle_u32_elements (scm_t_array_handle *handle)
  6729. @deftypefnx {C Function} {const scm_t_int32 *} scm_array_handle_s32_elements (scm_t_array_handle *handle)
  6730. @deftypefnx {C Function} {const scm_t_uint64 *} scm_array_handle_u64_elements (scm_t_array_handle *handle)
  6731. @deftypefnx {C Function} {const scm_t_int64 *} scm_array_handle_s64_elements (scm_t_array_handle *handle)
  6732. @deftypefnx {C Function} {const float *} scm_array_handle_f32_elements (scm_t_array_handle *handle)
  6733. @deftypefnx {C Function} {const double *} scm_array_handle_f64_elements (scm_t_array_handle *handle)
  6734. @deftypefnx {C Function} {const float *} scm_array_handle_c32_elements (scm_t_array_handle *handle)
  6735. @deftypefnx {C Function} {const double *} scm_array_handle_c64_elements (scm_t_array_handle *handle)
  6736. Return a pointer to the elements of a uniform numeric array of the
  6737. indicated kind for reading. This pointer is valid as long as the array
  6738. remains reserved.
  6739. The pointers for @code{c32} and @code{c64} uniform numeric arrays point
  6740. to pairs of floating point numbers. The even index holds the real part,
  6741. the odd index the imaginary part of the complex number.
  6742. @end deftypefn
  6743. @deftypefn {C Function} {scm_t_uint8 *} scm_array_handle_u8_writable_elements (scm_t_array_handle *handle)
  6744. @deftypefnx {C Function} {scm_t_int8 *} scm_array_handle_s8_writable_elements (scm_t_array_handle *handle)
  6745. @deftypefnx {C Function} {scm_t_uint16 *} scm_array_handle_u16_writable_elements (scm_t_array_handle *handle)
  6746. @deftypefnx {C Function} {scm_t_int16 *} scm_array_handle_s16_writable_elements (scm_t_array_handle *handle)
  6747. @deftypefnx {C Function} {scm_t_uint32 *} scm_array_handle_u32_writable_elements (scm_t_array_handle *handle)
  6748. @deftypefnx {C Function} {scm_t_int32 *} scm_array_handle_s32_writable_elements (scm_t_array_handle *handle)
  6749. @deftypefnx {C Function} {scm_t_uint64 *} scm_array_handle_u64_writable_elements (scm_t_array_handle *handle)
  6750. @deftypefnx {C Function} {scm_t_int64 *} scm_array_handle_s64_writable_elements (scm_t_array_handle *handle)
  6751. @deftypefnx {C Function} {float *} scm_array_handle_f32_writable_elements (scm_t_array_handle *handle)
  6752. @deftypefnx {C Function} {double *} scm_array_handle_f64_writable_elements (scm_t_array_handle *handle)
  6753. @deftypefnx {C Function} {float *} scm_array_handle_c32_writable_elements (scm_t_array_handle *handle)
  6754. @deftypefnx {C Function} {double *} scm_array_handle_c64_writable_elements (scm_t_array_handle *handle)
  6755. Like @code{scm_array_handle_<kind>_elements}, but the pointer is good
  6756. for reading and writing.
  6757. @end deftypefn
  6758. @deftypefn {C Function} {const scm_t_uint32 *} scm_array_handle_bit_elements (scm_t_array_handle *handle)
  6759. Return a pointer to the words that store the bits of the represented
  6760. array, which must be a bit array.
  6761. Unlike other arrays, bit arrays have an additional offset that must be
  6762. figured into index calculations. That offset is returned by
  6763. @code{scm_array_handle_bit_elements_offset}.
  6764. To find a certain bit you first need to calculate its position as
  6765. explained above for @code{scm_array_handle_dims} and then add the
  6766. offset. This gives the absolute position of the bit, which is always a
  6767. non-negative integer.
  6768. Each word of the bit array storage block contains exactly 32 bits, with
  6769. the least significant bit in that word having the lowest absolute
  6770. position number. The next word contains the next 32 bits.
  6771. Thus, the following code can be used to access a bit whose position
  6772. according to @code{scm_array_handle_dims} is given in @var{pos}:
  6773. @example
  6774. SCM bit_array;
  6775. scm_t_array_handle handle;
  6776. scm_t_uint32 *bits;
  6777. ssize_t pos;
  6778. size_t abs_pos;
  6779. size_t word_pos, mask;
  6780. scm_array_get_handle (&bit_array, &handle);
  6781. bits = scm_array_handle_bit_elements (&handle);
  6782. pos = ...
  6783. abs_pos = pos + scm_array_handle_bit_elements_offset (&handle);
  6784. word_pos = abs_pos / 32;
  6785. mask = 1L << (abs_pos % 32);
  6786. if (bits[word_pos] & mask)
  6787. /* bit is set. */
  6788. scm_array_handle_release (&handle);
  6789. @end example
  6790. @end deftypefn
  6791. @deftypefn {C Function} {scm_t_uint32 *} scm_array_handle_bit_writable_elements (scm_t_array_handle *handle)
  6792. Like @code{scm_array_handle_bit_elements} but the pointer is good for
  6793. reading and writing. You must take care not to modify bits outside of
  6794. the allowed index range of the array, even for contiguous arrays.
  6795. @end deftypefn
  6796. @node VLists
  6797. @subsection VLists
  6798. @cindex vlist
  6799. The @code{(ice-9 vlist)} module provides an implementation of the @dfn{VList}
  6800. data structure designed by Phil Bagwell in 2002. VLists are immutable lists,
  6801. which can contain any Scheme object. They improve on standard Scheme linked
  6802. lists in several areas:
  6803. @itemize
  6804. @item
  6805. Random access has typically constant-time complexity.
  6806. @item
  6807. Computing the length of a VList has time complexity logarithmic in the number of
  6808. elements.
  6809. @item
  6810. VLists use less storage space than standard lists.
  6811. @item
  6812. VList elements are stored in contiguous regions, which improves memory locality
  6813. and leads to more efficient use of hardware caches.
  6814. @end itemize
  6815. The idea behind VLists is to store vlist elements in increasingly large
  6816. contiguous blocks (implemented as vectors here). These blocks are linked to one
  6817. another using a pointer to the next block and an offset within that block. The
  6818. size of these blocks form a geometric series with ratio
  6819. @code{block-growth-factor} (2 by default).
  6820. The VList structure also serves as the basis for the @dfn{VList-based hash
  6821. lists} or ``vhashes'', an immutable dictionary type (@pxref{VHashes}).
  6822. However, the current implementation in @code{(ice-9 vlist)} has several
  6823. noteworthy shortcomings:
  6824. @itemize
  6825. @item
  6826. It is @emph{not} thread-safe. Although operations on vlists are all
  6827. @dfn{referentially transparent} (i.e., purely functional), adding elements to a
  6828. vlist with @code{vlist-cons} mutates part of its internal structure, which makes
  6829. it non-thread-safe. This could be fixed, but it would slow down
  6830. @code{vlist-cons}.
  6831. @item
  6832. @code{vlist-cons} always allocates at least as much memory as @code{cons}.
  6833. Again, Phil Bagwell describes how to fix it, but that would require tuning the
  6834. garbage collector in a way that may not be generally beneficial.
  6835. @item
  6836. @code{vlist-cons} is a Scheme procedure compiled to bytecode, and it does not
  6837. compete with the straightforward C implementation of @code{cons}, and with the
  6838. fact that the VM has a special @code{cons} instruction.
  6839. @end itemize
  6840. We hope to address these in the future.
  6841. The programming interface exported by @code{(ice-9 vlist)} is defined below.
  6842. Most of it is the same as SRFI-1 with an added @code{vlist-} prefix to function
  6843. names.
  6844. @deffn {Scheme Procedure} vlist? obj
  6845. Return true if @var{obj} is a VList.
  6846. @end deffn
  6847. @defvr {Scheme Variable} vlist-null
  6848. The empty VList. Note that it's possible to create an empty VList not
  6849. @code{eq?} to @code{vlist-null}; thus, callers should always use
  6850. @code{vlist-null?} when testing whether a VList is empty.
  6851. @end defvr
  6852. @deffn {Scheme Procedure} vlist-null? vlist
  6853. Return true if @var{vlist} is empty.
  6854. @end deffn
  6855. @deffn {Scheme Procedure} vlist-cons item vlist
  6856. Return a new vlist with @var{item} as its head and @var{vlist} as its tail.
  6857. @end deffn
  6858. @deffn {Scheme Procedure} vlist-head vlist
  6859. Return the head of @var{vlist}.
  6860. @end deffn
  6861. @deffn {Scheme Procedure} vlist-tail vlist
  6862. Return the tail of @var{vlist}.
  6863. @end deffn
  6864. @defvr {Scheme Variable} block-growth-factor
  6865. A fluid that defines the growth factor of VList blocks, 2 by default.
  6866. @end defvr
  6867. The functions below provide the usual set of higher-level list operations.
  6868. @deffn {Scheme Procedure} vlist-fold proc init vlist
  6869. @deffnx {Scheme Procedure} vlist-fold-right proc init vlist
  6870. Fold over @var{vlist}, calling @var{proc} for each element, as for SRFI-1
  6871. @code{fold} and @code{fold-right} (@pxref{SRFI-1, @code{fold}}).
  6872. @end deffn
  6873. @deffn {Scheme Procedure} vlist-ref vlist index
  6874. Return the element at index @var{index} in @var{vlist}. This is typically a
  6875. constant-time operation.
  6876. @end deffn
  6877. @deffn {Scheme Procedure} vlist-length vlist
  6878. Return the length of @var{vlist}. This is typically logarithmic in the number
  6879. of elements in @var{vlist}.
  6880. @end deffn
  6881. @deffn {Scheme Procedure} vlist-reverse vlist
  6882. Return a new @var{vlist} whose content are those of @var{vlist} in reverse
  6883. order.
  6884. @end deffn
  6885. @deffn {Scheme Procedure} vlist-map proc vlist
  6886. Map @var{proc} over the elements of @var{vlist} and return a new vlist.
  6887. @end deffn
  6888. @deffn {Scheme Procedure} vlist-for-each proc vlist
  6889. Call @var{proc} on each element of @var{vlist}. The result is unspecified.
  6890. @end deffn
  6891. @deffn {Scheme Procedure} vlist-drop vlist count
  6892. Return a new vlist that does not contain the @var{count} first elements of
  6893. @var{vlist}. This is typically a constant-time operation.
  6894. @end deffn
  6895. @deffn {Scheme Procedure} vlist-take vlist count
  6896. Return a new vlist that contains only the @var{count} first elements of
  6897. @var{vlist}.
  6898. @end deffn
  6899. @deffn {Scheme Procedure} vlist-filter pred vlist
  6900. Return a new vlist containing all the elements from @var{vlist} that satisfy
  6901. @var{pred}.
  6902. @end deffn
  6903. @deffn {Scheme Procedure} vlist-delete x vlist [equal?]
  6904. Return a new vlist corresponding to @var{vlist} without the elements
  6905. @var{equal?} to @var{x}.
  6906. @end deffn
  6907. @deffn {Scheme Procedure} vlist-unfold p f g seed [tail-gen]
  6908. @deffnx {Scheme Procedure} vlist-unfold-right p f g seed [tail]
  6909. Return a new vlist, as for SRFI-1 @code{unfold} and @code{unfold-right}
  6910. (@pxref{SRFI-1, @code{unfold}}).
  6911. @end deffn
  6912. @deffn {Scheme Procedure} vlist-append vlist @dots{}
  6913. Append the given vlists and return the resulting vlist.
  6914. @end deffn
  6915. @deffn {Scheme Procedure} list->vlist lst
  6916. Return a new vlist whose contents correspond to @var{lst}.
  6917. @end deffn
  6918. @deffn {Scheme Procedure} vlist->list vlist
  6919. Return a new list whose contents match those of @var{vlist}.
  6920. @end deffn
  6921. @node Record Overview
  6922. @subsection Record Overview
  6923. @cindex record
  6924. @cindex structure
  6925. @dfn{Records}, also called @dfn{structures}, are Scheme's primary
  6926. mechanism to define new disjoint types. A @dfn{record type} defines a
  6927. list of @dfn{fields} that instances of the type consist of. This is like
  6928. C's @code{struct}.
  6929. Historically, Guile has offered several different ways to define record
  6930. types and to create records, offering different features, and making
  6931. different trade-offs. Over the years, each ``standard'' has also come
  6932. with its own new record interface, leading to a maze of record APIs.
  6933. At the highest level is SRFI-9, a high-level record interface
  6934. implemented by most Scheme implementations (@pxref{SRFI-9 Records}). It
  6935. defines a simple and efficient syntactic abstraction of record types and
  6936. their associated type predicate, fields, and field accessors. SRFI-9 is
  6937. suitable for most uses, and this is the recommended way to create record
  6938. types in Guile. Similar high-level record APIs include SRFI-35
  6939. (@pxref{SRFI-35}) and R6RS records (@pxref{rnrs records syntactic}).
  6940. Then comes Guile's historical ``records'' API (@pxref{Records}). Record
  6941. types defined this way are first-class objects. Introspection
  6942. facilities are available, allowing users to query the list of fields or
  6943. the value of a specific field at run-time, without prior knowledge of
  6944. the type.
  6945. Finally, the common denominator of these interfaces is Guile's
  6946. @dfn{structure} API (@pxref{Structures}). Guile's structures are the
  6947. low-level building block for all other record APIs. Application writers
  6948. will normally not need to use it.
  6949. Records created with these APIs may all be pattern-matched using Guile's
  6950. standard pattern matcher (@pxref{Pattern Matching}).
  6951. @node SRFI-9 Records
  6952. @subsection SRFI-9 Records
  6953. @cindex SRFI-9
  6954. @cindex record
  6955. SRFI-9 standardizes a syntax for defining new record types and creating
  6956. predicate, constructor, and field getter and setter functions. In Guile
  6957. this is the recommended option to create new record types (@pxref{Record
  6958. Overview}). It can be used with:
  6959. @example
  6960. (use-modules (srfi srfi-9))
  6961. @end example
  6962. @deffn {Scheme Syntax} define-record-type type @* (constructor fieldname @dots{}) @* predicate @* (fieldname accessor [modifier]) @dots{}
  6963. @sp 1
  6964. Create a new record type, and make various @code{define}s for using
  6965. it. This syntax can only occur at the top-level, not nested within
  6966. some other form.
  6967. @var{type} is bound to the record type, which is as per the return
  6968. from the core @code{make-record-type}. @var{type} also provides the
  6969. name for the record, as per @code{record-type-name}.
  6970. @var{constructor} is bound to a function to be called as
  6971. @code{(@var{constructor} fieldval @dots{})} to create a new record of
  6972. this type. The arguments are initial values for the fields, one
  6973. argument for each field, in the order they appear in the
  6974. @code{define-record-type} form.
  6975. The @var{fieldname}s provide the names for the record fields, as per
  6976. the core @code{record-type-fields} etc, and are referred to in the
  6977. subsequent accessor/modifier forms.
  6978. @var{predicate} is bound to a function to be called as
  6979. @code{(@var{predicate} obj)}. It returns @code{#t} or @code{#f}
  6980. according to whether @var{obj} is a record of this type.
  6981. Each @var{accessor} is bound to a function to be called
  6982. @code{(@var{accessor} record)} to retrieve the respective field from a
  6983. @var{record}. Similarly each @var{modifier} is bound to a function to
  6984. be called @code{(@var{modifier} record val)} to set the respective
  6985. field in a @var{record}.
  6986. @end deffn
  6987. @noindent
  6988. An example will illustrate typical usage,
  6989. @example
  6990. (define-record-type <employee>
  6991. (make-employee name age salary)
  6992. employee?
  6993. (name employee-name)
  6994. (age employee-age set-employee-age!)
  6995. (salary employee-salary set-employee-salary!))
  6996. @end example
  6997. This creates a new employee data type, with name, age and salary
  6998. fields. Accessor functions are created for each field, but no
  6999. modifier function for the name (the intention in this example being
  7000. that it's established only when an employee object is created). These
  7001. can all then be used as for example,
  7002. @example
  7003. <employee> @result{} #<record-type <employee>>
  7004. (define fred (make-employee "Fred" 45 20000.00))
  7005. (employee? fred) @result{} #t
  7006. (employee-age fred) @result{} 45
  7007. (set-employee-salary! fred 25000.00) ;; pay rise
  7008. @end example
  7009. The functions created by @code{define-record-type} are ordinary
  7010. top-level @code{define}s. They can be redefined or @code{set!} as
  7011. desired, exported from a module, etc.
  7012. @unnumberedsubsubsec Non-toplevel Record Definitions
  7013. The SRFI-9 specification explicitly disallows record definitions in a
  7014. non-toplevel context, such as inside @code{lambda} body or inside a
  7015. @var{let} block. However, Guile's implementation does not enforce that
  7016. restriction.
  7017. @unnumberedsubsubsec Custom Printers
  7018. You may use @code{set-record-type-printer!} to customize the default printing
  7019. behavior of records. This is a Guile extension and is not part of SRFI-9. It
  7020. is located in the @nicode{(srfi srfi-9 gnu)} module.
  7021. @deffn {Scheme Syntax} set-record-type-printer! type proc
  7022. Where @var{type} corresponds to the first argument of @code{define-record-type},
  7023. and @var{proc} is a procedure accepting two arguments, the record to print, and
  7024. an output port.
  7025. @end deffn
  7026. @noindent
  7027. This example prints the employee's name in brackets, for instance @code{[Fred]}.
  7028. @example
  7029. (set-record-type-printer! <employee>
  7030. (lambda (record port)
  7031. (write-char #\[ port)
  7032. (display (employee-name record) port)
  7033. (write-char #\] port)))
  7034. @end example
  7035. @unnumberedsubsubsec Functional ``Setters''
  7036. @cindex functional setters
  7037. When writing code in a functional style, it is desirable to never alter
  7038. the contents of records. For such code, a simple way to return new
  7039. record instances based on existing ones is highly desirable.
  7040. The @code{(srfi srfi-9 gnu)} module extends SRFI-9 with facilities to
  7041. return new record instances based on existing ones, only with one or
  7042. more field values changed---@dfn{functional setters}. First, the
  7043. @code{define-immutable-record-type} works like
  7044. @code{define-record-type}, except that fields are immutable and setters
  7045. are defined as functional setters.
  7046. @deffn {Scheme Syntax} define-immutable-record-type type @* (constructor fieldname @dots{}) @* predicate @* (fieldname accessor [modifier]) @dots{}
  7047. Define @var{type} as a new record type, like @code{define-record-type}.
  7048. However, the record type is made @emph{immutable} (records may not be
  7049. mutated, even with @code{struct-set!}), and any @var{modifier} is
  7050. defined to be a functional setter---a procedure that returns a new
  7051. record instance with the specified field changed, and leaves the
  7052. original unchanged (see example below.)
  7053. @end deffn
  7054. @noindent
  7055. In addition, the generic @code{set-field} and @code{set-fields} macros
  7056. may be applied to any SRFI-9 record.
  7057. @deffn {Scheme Syntax} set-field record (field sub-fields ...) value
  7058. Return a new record of @var{record}'s type whose fields are equal to
  7059. the corresponding fields of @var{record} except for the one specified by
  7060. @var{field}.
  7061. @var{field} must be the name of the getter corresponding to the field of
  7062. @var{record} being ``set''. Subsequent @var{sub-fields} must be record
  7063. getters designating sub-fields within that field value to be set (see
  7064. example below.)
  7065. @end deffn
  7066. @deffn {Scheme Syntax} set-fields record ((field sub-fields ...) value) ...
  7067. Like @code{set-field}, but can be used to set more than one field at a
  7068. time. This expands to code that is more efficient than a series of
  7069. single @code{set-field} calls.
  7070. @end deffn
  7071. To illustrate the use of functional setters, let's assume these two
  7072. record type definitions:
  7073. @example
  7074. (define-record-type <address>
  7075. (address street city country)
  7076. address?
  7077. (street address-street)
  7078. (city address-city)
  7079. (country address-country))
  7080. (define-immutable-record-type <person>
  7081. (person age email address)
  7082. person?
  7083. (age person-age set-person-age)
  7084. (email person-email set-person-email)
  7085. (address person-address set-person-address))
  7086. @end example
  7087. @noindent
  7088. First, note that the @code{<person>} record type definition introduces
  7089. named functional setters. These may be used like this:
  7090. @example
  7091. (define fsf-address
  7092. (address "Franklin Street" "Boston" "USA"))
  7093. (define rms
  7094. (person 30 "rms@@gnu.org" fsf-address))
  7095. (and (equal? (set-person-age rms 60)
  7096. (person 60 "rms@@gnu.org" fsf-address))
  7097. (= (person-age rms) 30))
  7098. @result{} #t
  7099. @end example
  7100. @noindent
  7101. Here, the original @code{<person>} record, to which @var{rms} is bound,
  7102. is left unchanged.
  7103. Now, suppose we want to change both the street and age of @var{rms}.
  7104. This can be achieved using @code{set-fields}:
  7105. @example
  7106. (set-fields rms
  7107. ((person-age) 60)
  7108. ((person-address address-street) "Temple Place"))
  7109. @result{} #<<person> age: 60 email: "rms@@gnu.org"
  7110. address: #<<address> street: "Temple Place" city: "Boston" country: "USA">>
  7111. @end example
  7112. @noindent
  7113. Notice how the above changed two fields of @var{rms}, including the
  7114. @code{street} field of its @code{address} field, in a concise way. Also
  7115. note that @code{set-fields} works equally well for types defined with
  7116. just @code{define-record-type}.
  7117. @node Records
  7118. @subsection Records
  7119. A @dfn{record type} is a first class object representing a user-defined
  7120. data type. A @dfn{record} is an instance of a record type.
  7121. Note that in many ways, this interface is too low-level for every-day
  7122. use. Most uses of records are better served by SRFI-9 records.
  7123. @xref{SRFI-9 Records}.
  7124. @deffn {Scheme Procedure} record? obj
  7125. Return @code{#t} if @var{obj} is a record of any type and @code{#f}
  7126. otherwise.
  7127. Note that @code{record?} may be true of any Scheme value; there is no
  7128. promise that records are disjoint with other Scheme types.
  7129. @end deffn
  7130. @deffn {Scheme Procedure} make-record-type type-name field-names [print] @
  7131. [#:parent=@code{#f}] [#:uid=@code{#f}] @
  7132. [#:extensible?=@code{#f}] [#:opaque?=@code{#f}] @
  7133. [#:allow-duplicate-field-names?=@code{#t}]
  7134. Create and return a new @dfn{record-type descriptor}.
  7135. @var{type-name} is a string naming the type. Currently it's only used
  7136. in the printed representation of records, and in diagnostics.
  7137. @var{field-names} is a list of elements of the form @code{(immutable
  7138. @var{name})}, @code{(mutable @var{name})}, or @var{name}, where
  7139. @var{name} are symbols naming the fields of a record of the type.
  7140. Duplicates are not allowed among these symbols, unless
  7141. @var{allow-duplicate-field-names?} is true.
  7142. @example
  7143. (make-record-type "employee" '(name age salary))
  7144. @end example
  7145. The optional @var{print} argument is a function used by
  7146. @code{display}, @code{write}, etc, for printing a record of the new
  7147. type. It's called as @code{(@var{print} record port)} and should look
  7148. at @var{record} and write to @var{port}.
  7149. Pass the @code{#:parent} keyword to derive a record type from a
  7150. supertype. A derived record type has the fields from its parent type,
  7151. followed by fields declared in the @code{make-record-type} call. Record
  7152. predicates and field accessors for instance of a parent type will also
  7153. work on any instance of a subtype.
  7154. @cindex extensible record types
  7155. @cindex record types, extensible
  7156. Allowing record subtyping has a small amount of overhead. To avoid this
  7157. overhead, prevent extensibility by passing @code{#:extensible? #f}.
  7158. By default, record types in Guile are not extensible.
  7159. @cindex prefab record types
  7160. @cindex record types, prefab
  7161. @cindex record types, nongenerative
  7162. Generally speaking, calling @code{make-record-type} returns a fresh
  7163. record type; it @emph{generates} new record types. However sometimes
  7164. you only want to define a record type if one hasn't been defined
  7165. already. For a @emph{nongenerative} record type definition, pass a
  7166. symbol as the @code{#:uid} keyword parameter. If a record with the
  7167. given @var{uid} was already defined, it will be returned instead. The
  7168. type name, fields, parent (if any), and so on for the previously-defined
  7169. type must be compatible.
  7170. @cindex record types, opaque
  7171. R6RS defines a notion of ``opaque'' record types. Given an instance of
  7172. an opaque record type, one cannot obtain a run-time representation of
  7173. the record type. @xref{rnrs records procedural}, for full details. The
  7174. @code{#:opaque?} flag is used by Guile's R6RS layer to record this
  7175. information. The default is determined by whether the parent type, if
  7176. any, was opaque.
  7177. Fields are mutable by default, meaning that @code{record-modifier} will
  7178. return a procedure that can update a record in place. Specifying a
  7179. field using the form @code{(immutable @var{name})} instead marks a field
  7180. as immutable.
  7181. @end deffn
  7182. @deffn {Scheme Procedure} record-constructor rtd
  7183. Return a procedure for constructing new members of the type represented
  7184. by @var{rtd}. The result will be a procedure accepting exactly as many
  7185. arguments as there are fields in the record type.
  7186. @end deffn
  7187. @deffn {Scheme Procedure} record-predicate rtd
  7188. Return a procedure for testing membership in the type represented by
  7189. @var{rtd}. The returned procedure accepts exactly one argument and
  7190. returns a true value if the argument is a member of the indicated record
  7191. type; it returns a false value otherwise.
  7192. @end deffn
  7193. @deffn {Scheme Procedure} record-accessor rtd field-name
  7194. Return a procedure for reading the value of a particular field of a
  7195. member of the type represented by @var{rtd}. The returned procedure
  7196. accepts exactly one argument which must be a record of the appropriate
  7197. type; it returns the current value of the field named by the symbol
  7198. @var{field-name} in that record.
  7199. If @var{field-name} is a symbol, it must be a member of the list of
  7200. field-names in the call to @code{make-record-type} that created the type
  7201. represented by @var{rtd}. If multiple fields in @var{rtd} have the same
  7202. name, @code{record-accessor} returns the first one.
  7203. If @var{field-name} is an integer, it should be an index into
  7204. @code{(record-type-fields @var{rtd})}. This allows accessing fields
  7205. with duplicate names.
  7206. @end deffn
  7207. @deffn {Scheme Procedure} record-modifier rtd field-name
  7208. Return a procedure for writing the value of a particular field of a
  7209. member of the type represented by @var{rtd}. The returned procedure
  7210. accepts exactly two arguments: first, a record of the appropriate type,
  7211. and second, an arbitrary Scheme value; it modifies the field named by
  7212. the symbol @var{field-name} in that record to contain the given value.
  7213. The returned value of the modifier procedure is unspecified. The symbol
  7214. @var{field-name} is a field name or a field index, as in
  7215. @code{record-modifier}.
  7216. @end deffn
  7217. @deffn {Scheme Procedure} record-type-descriptor record
  7218. Return a record-type descriptor representing the type of the given
  7219. record. That is, for example, if the returned descriptor were passed to
  7220. @code{record-predicate}, the resulting predicate would return a true
  7221. value when passed the given record. Note that it is not necessarily the
  7222. case that the returned descriptor is the one that was passed to
  7223. @code{record-constructor} in the call that created the constructor
  7224. procedure that created the given record.
  7225. @end deffn
  7226. @deffn {Scheme Procedure} record-type-name rtd
  7227. Return the type-name associated with the type represented by rtd. The
  7228. returned value is @code{eqv?} to the @var{type-name} argument given in
  7229. the call to @code{make-record-type} that created the type represented by
  7230. @var{rtd}.
  7231. @end deffn
  7232. @deffn {Scheme Procedure} record-type-fields rtd
  7233. Return a list of the symbols naming the fields in members of the type
  7234. represented by @var{rtd}. The returned value is @code{equal?} to the
  7235. field-names argument given in the call to @code{make-record-type} that
  7236. created the type represented by @var{rtd}.
  7237. @end deffn
  7238. @node Structures
  7239. @subsection Structures
  7240. @tpindex Structures
  7241. A @dfn{structure} is a first class data type which holds Scheme values
  7242. or C words in fields numbered 0 upwards. A @dfn{vtable} is a structure
  7243. that represents a structure type, giving field types and permissions,
  7244. and an optional print function for @code{write} etc.
  7245. Structures are lower level than records (@pxref{Records}). Usually,
  7246. when you need to represent structured data, you just want to use
  7247. records. But sometimes you need to implement new kinds of structured
  7248. data abstractions, and for that purpose structures are useful. Indeed,
  7249. records in Guile are implemented with structures.
  7250. @menu
  7251. * Vtables::
  7252. * Structure Basics::
  7253. * Vtable Contents::
  7254. * Meta-Vtables::
  7255. * Vtable Example::
  7256. @end menu
  7257. @node Vtables
  7258. @subsubsection Vtables
  7259. A vtable is a structure type, specifying its layout, and other
  7260. information. A vtable is actually itself a structure, but there's no
  7261. need to worry about that initially (@pxref{Vtable Contents}.)
  7262. @deffn {Scheme Procedure} make-vtable fields [print]
  7263. Create a new vtable.
  7264. @var{fields} is a string describing the fields in the structures to be
  7265. created. Each field is represented by two characters, a type letter
  7266. and a permissions letter, for example @code{"pw"}. The types are as
  7267. follows.
  7268. @itemize @bullet{}
  7269. @item
  7270. @code{p} -- a Scheme value. ``p'' stands for ``protected'' meaning
  7271. it's protected against garbage collection.
  7272. @item
  7273. @code{u} -- an arbitrary word of data (an @code{scm_t_bits}). At the
  7274. Scheme level it's read and written as an unsigned integer. ``u'' stands
  7275. for ``unboxed'', as it's stored as a raw value without additional type
  7276. annotations.
  7277. @end itemize
  7278. It used to be that the second letter for each field was a permission
  7279. code, such as @code{w} for writable or @code{r} for read-only. However
  7280. over time structs have become more of a raw low-level facility; access
  7281. control is better implemented as a layer on top. After all,
  7282. @code{struct-set!} is a cross-cutting operator that can bypass
  7283. abstractions made by higher-level record facilities; it's not generally
  7284. safe (in the sense of abstraction-preserving) to expose
  7285. @code{struct-set!} to ``untrusted'' code, even if the fields happen to
  7286. be writable. Additionally, permission checks added overhead to every
  7287. structure access in a way that couldn't be optimized out, hampering the
  7288. ability of structs to act as a low-level building block. For all of
  7289. these reasons, all fields in Guile structs are now writable; attempting
  7290. to make a read-only field will now issue a deprecation warning, and the
  7291. field will be writable regardless.
  7292. @example
  7293. (make-vtable "pw") ;; one scheme field
  7294. (make-vtable "pwuwuw") ;; one scheme and two unboxed fields
  7295. @end example
  7296. The optional @var{print} argument is a function called by
  7297. @code{display} and @code{write} (etc) to give a printed representation
  7298. of a structure created from this vtable. It's called
  7299. @code{(@var{print} struct port)} and should look at @var{struct} and
  7300. write to @var{port}. The default print merely gives a form like
  7301. @samp{#<struct ADDR:ADDR>} with a pair of machine addresses.
  7302. The following print function for example shows the two fields of its
  7303. structure.
  7304. @example
  7305. (make-vtable "pwpw"
  7306. (lambda (struct port)
  7307. (format port "#<~a and ~a>"
  7308. (struct-ref struct 0)
  7309. (struct-ref struct 1))))
  7310. @end example
  7311. @end deffn
  7312. @node Structure Basics
  7313. @subsubsection Structure Basics
  7314. This section describes the basic procedures for working with structures.
  7315. @code{make-struct/no-tail} creates a structure, and @code{struct-ref}
  7316. and @code{struct-set!} access its fields.
  7317. @deffn {Scheme Procedure} make-struct/no-tail vtable init @dots{}
  7318. Create a new structure, with layout per the given @var{vtable}
  7319. (@pxref{Vtables}).
  7320. The optional @var{init}@dots{} arguments are initial values for the
  7321. fields of the structure. This is the only way to
  7322. put values in read-only fields. If there are fewer @var{init}
  7323. arguments than fields then the defaults are @code{#f} for a Scheme
  7324. field (type @code{p}) or 0 for an unboxed field (type @code{u}).
  7325. The name is a bit strange, we admit. The reason for it is that Guile
  7326. used to have a @code{make-struct} that took an additional argument;
  7327. while we deprecate that old interface, @code{make-struct/no-tail} is the
  7328. new name for this functionality.
  7329. For example,
  7330. @example
  7331. (define v (make-vtable "pwpwpw"))
  7332. (define s (make-struct/no-tail v 123 "abc" 456))
  7333. (struct-ref s 0) @result{} 123
  7334. (struct-ref s 1) @result{} "abc"
  7335. @end example
  7336. @end deffn
  7337. @deftypefn {C Function} SCM scm_make_struct (SCM vtable, SCM tail_size, SCM init_list)
  7338. @deftypefnx {C Function} SCM scm_c_make_struct (SCM vtable, SCM tail_size, SCM init, ...)
  7339. @deftypefnx {C Function} SCM scm_c_make_structv (SCM vtable, SCM tail_size, size_t n_inits, scm_t_bits init[])
  7340. There are a few ways to make structures from C. @code{scm_make_struct}
  7341. takes a list, @code{scm_c_make_struct} takes variable arguments
  7342. terminated with SCM_UNDEFINED, and @code{scm_c_make_structv} takes a
  7343. packed array.
  7344. For all of these, @var{tail_size} should be zero (as a SCM value).
  7345. @end deftypefn
  7346. @deffn {Scheme Procedure} struct? obj
  7347. @deffnx {C Function} scm_struct_p (obj)
  7348. Return @code{#t} if @var{obj} is a structure, or @code{#f} if not.
  7349. @end deffn
  7350. @deffn {Scheme Procedure} struct-ref struct n
  7351. @deffnx {C Function} scm_struct_ref (struct, n)
  7352. Return the contents of field number @var{n} in @var{struct}. The
  7353. first field is number 0.
  7354. An error is thrown if @var{n} is out of range.
  7355. @end deffn
  7356. @deffn {Scheme Procedure} struct-set! struct n value
  7357. @deffnx {C Function} scm_struct_set_x (struct, n, value)
  7358. Set field number @var{n} in @var{struct} to @var{value}. The first
  7359. field is number 0.
  7360. An error is thrown if @var{n} is out of range, or if the field cannot
  7361. be written because it's @code{r} read-only.
  7362. @end deffn
  7363. Unboxed fields (those with type @code{u}) need to be accessed with
  7364. special procedures.
  7365. @deffn {Scheme Procedure} struct-ref/unboxed struct n
  7366. @deffnx {Scheme Procedure} struct-set!/unboxed struct n value
  7367. @deffnx {C Function} scm_struct_ref_unboxed (struct, n)
  7368. @deffnx {C Function} scm_struct_set_x_unboxed (struct, n, value)
  7369. Like @code{struct-ref} and @code{struct-set!}, except that these may
  7370. only be used on unboxed fields. @code{struct-ref/unboxed} will always
  7371. return a positive integer. Likewise, @code{struct-set!/unboxed} takes
  7372. an unsigned integer as the @var{value} argument, and will signal an
  7373. error otherwise.
  7374. @end deffn
  7375. @deffn {Scheme Procedure} struct-vtable struct
  7376. @deffnx {C Function} scm_struct_vtable (struct)
  7377. Return the vtable that describes @var{struct}.
  7378. The vtable is effectively the type of the structure. See @ref{Vtable
  7379. Contents}, for more on vtables.
  7380. @end deffn
  7381. @node Vtable Contents
  7382. @subsubsection Vtable Contents
  7383. A vtable is itself a structure. It has a specific set of fields
  7384. describing various aspects of its @dfn{instances}: the structures
  7385. created from a vtable. Some of the fields are internal to Guile, some
  7386. of them are part of the public interface, and there may be additional
  7387. fields added on by the user.
  7388. Every vtable has a field for the layout of their instances, a field for
  7389. the procedure used to print its instances, and a field for the name of
  7390. the vtable itself. Access to the layout and printer is exposed directly
  7391. via field indexes. Access to the vtable name is exposed via accessor
  7392. procedures.
  7393. @defvr {Scheme Variable} vtable-index-layout
  7394. @defvrx {C Macro} scm_vtable_index_layout
  7395. The field number of the layout specification in a vtable. The layout
  7396. specification is a symbol like @code{pwpw} formed from the fields
  7397. string passed to @code{make-vtable}, or created by
  7398. @code{make-struct-layout} (@pxref{Meta-Vtables}).
  7399. @example
  7400. (define v (make-vtable "pwpw" 0))
  7401. (struct-ref v vtable-index-layout) @result{} pwpw
  7402. @end example
  7403. This field is read-only, since the layout of structures using a vtable
  7404. cannot be changed.
  7405. @end defvr
  7406. @defvr {Scheme Variable} vtable-index-printer
  7407. @defvrx {C Macro} scm_vtable_index_printer
  7408. The field number of the printer function. This field contains @code{#f}
  7409. if the default print function should be used.
  7410. @example
  7411. (define (my-print-func struct port)
  7412. ...)
  7413. (define v (make-vtable "pwpw" my-print-func))
  7414. (struct-ref v vtable-index-printer) @result{} my-print-func
  7415. @end example
  7416. This field is writable, allowing the print function to be changed
  7417. dynamically.
  7418. @end defvr
  7419. @deffn {Scheme Procedure} struct-vtable-name vtable
  7420. @deffnx {Scheme Procedure} set-struct-vtable-name! vtable name
  7421. @deffnx {C Function} scm_struct_vtable_name (vtable)
  7422. @deffnx {C Function} scm_set_struct_vtable_name_x (vtable, name)
  7423. Get or set the name of @var{vtable}. @var{name} is a symbol and is
  7424. used in the default print function when printing structures created
  7425. from @var{vtable}.
  7426. @example
  7427. (define v (make-vtable "pw"))
  7428. (set-struct-vtable-name! v 'my-name)
  7429. (define s (make-struct v 0))
  7430. (display s) @print{} #<my-name b7ab3ae0:b7ab3730>
  7431. @end example
  7432. @end deffn
  7433. @node Meta-Vtables
  7434. @subsubsection Meta-Vtables
  7435. As a structure, a vtable also has a vtable, which is also a structure.
  7436. Structures, their vtables, the vtables of the vtables, and so on form a
  7437. tree of structures. Making a new structure adds a leaf to the tree, and
  7438. if that structure is a vtable, it may be used to create other leaves.
  7439. If you traverse up the tree of vtables, via calling
  7440. @code{struct-vtable}, eventually you reach a root which is the vtable of
  7441. itself:
  7442. @example
  7443. scheme@@(guile-user)> (current-module)
  7444. $1 = #<directory (guile-user) 221b090>
  7445. scheme@@(guile-user)> (struct-vtable $1)
  7446. $2 = #<record-type module>
  7447. scheme@@(guile-user)> (struct-vtable $2)
  7448. $3 = #<<standard-vtable> 12c30a0>
  7449. scheme@@(guile-user)> (struct-vtable $3)
  7450. $4 = #<<standard-vtable> 12c3fa0>
  7451. scheme@@(guile-user)> (struct-vtable $4)
  7452. $5 = #<<standard-vtable> 12c3fa0>
  7453. scheme@@(guile-user)> <standard-vtable>
  7454. $6 = #<<standard-vtable> 12c3fa0>
  7455. @end example
  7456. In this example, we can say that @code{$1} is an instance of @code{$2},
  7457. @code{$2} is an instance of @code{$3}, @code{$3} is an instance of
  7458. @code{$4}, and @code{$4}, strangely enough, is an instance of itself.
  7459. The value bound to @code{$4} in this console session also bound to
  7460. @code{<standard-vtable>} in the default environment.
  7461. @defvr {Scheme Variable} <standard-vtable>
  7462. A meta-vtable, useful for making new vtables.
  7463. @end defvr
  7464. All of these values are structures. All but @code{$1} are vtables. As
  7465. @code{$2} is an instance of @code{$3}, and @code{$3} is a vtable, we can
  7466. say that @code{$3} is a @dfn{meta-vtable}: a vtable that can create
  7467. vtables.
  7468. With this definition, we can specify more precisely what a vtable is: a
  7469. vtable is a structure made from a meta-vtable. Making a structure from
  7470. a meta-vtable runs some special checks to ensure that the first field of
  7471. the structure is a valid layout. Additionally, if these checks see that
  7472. the layout of the child vtable contains all the required fields of a
  7473. vtable, in the correct order, then the child vtable will also be a
  7474. meta-table, inheriting a magical bit from the parent.
  7475. @deffn {Scheme Procedure} struct-vtable? obj
  7476. @deffnx {C Function} scm_struct_vtable_p (obj)
  7477. Return @code{#t} if @var{obj} is a vtable structure: an instance of a
  7478. meta-vtable.
  7479. @end deffn
  7480. @code{<standard-vtable>} is a root of the vtable tree. (Normally there
  7481. is only one root in a given Guile process, but due to some legacy
  7482. interfaces there may be more than one.)
  7483. The set of required fields of a vtable is the set of fields in the
  7484. @code{<standard-vtable>}, and is bound to @code{standard-vtable-fields}
  7485. in the default environment. It is possible to create a meta-vtable that
  7486. with additional fields in its layout, which can be used to create
  7487. vtables with additional data:
  7488. @example
  7489. scheme@@(guile-user)> (struct-ref $3 vtable-index-layout)
  7490. $6 = pwuhuhpwphuhuhpwpwpw
  7491. scheme@@(guile-user)> (struct-ref $4 vtable-index-layout)
  7492. $7 = pwuhuhpwphuhuh
  7493. scheme@@(guile-user)> standard-vtable-fields
  7494. $8 = "pwuhuhpwphuhuh"
  7495. scheme@@(guile-user)> (struct-ref $2 vtable-offset-user)
  7496. $9 = module
  7497. @end example
  7498. In this continuation of our earlier example, @code{$2} is a vtable that
  7499. has extra fields, because its vtable, @code{$3}, was made from a
  7500. meta-vtable with an extended layout. @code{vtable-offset-user} is a
  7501. convenient definition that indicates the number of fields in
  7502. @code{standard-vtable-fields}.
  7503. @defvr {Scheme Variable} standard-vtable-fields
  7504. A string containing the ordered set of fields that a vtable must have.
  7505. @end defvr
  7506. @defvr {Scheme Variable} vtable-offset-user
  7507. The first index in a vtable that is available for a user.
  7508. @end defvr
  7509. @deffn {Scheme Procedure} make-struct-layout fields
  7510. @deffnx {C Function} scm_make_struct_layout (fields)
  7511. Return a structure layout symbol, from a @var{fields} string.
  7512. @var{fields} is as described under @code{make-vtable}
  7513. (@pxref{Vtables}). An invalid @var{fields} string is an error.
  7514. @end deffn
  7515. With these definitions, one can define @code{make-vtable} in this way:
  7516. @example
  7517. (define* (make-vtable fields #:optional printer)
  7518. (make-struct/no-tail <standard-vtable>
  7519. (make-struct-layout fields)
  7520. printer))
  7521. @end example
  7522. @node Vtable Example
  7523. @subsubsection Vtable Example
  7524. Let us bring these points together with an example. Consider a simple
  7525. object system with single inheritance. Objects will be normal
  7526. structures, and classes will be vtables with three extra class fields:
  7527. the name of the class, the parent class, and the list of fields.
  7528. So, first we need a meta-vtable that allocates instances with these
  7529. extra class fields.
  7530. @example
  7531. (define <class>
  7532. (make-vtable
  7533. (string-append standard-vtable-fields "pwpwpw")
  7534. (lambda (x port)
  7535. (format port "<<class> ~a>" (class-name x)))))
  7536. (define (class? x)
  7537. (and (struct? x)
  7538. (eq? (struct-vtable x) <class>)))
  7539. @end example
  7540. To make a structure with a specific meta-vtable, we will use
  7541. @code{make-struct/no-tail}, passing it the computed instance layout and
  7542. printer, as with @code{make-vtable}, and additionally the extra three
  7543. class fields.
  7544. @example
  7545. (define (make-class name parent fields)
  7546. (let* ((fields (compute-fields parent fields))
  7547. (layout (compute-layout fields)))
  7548. (make-struct/no-tail <class>
  7549. layout
  7550. (lambda (x port)
  7551. (print-instance x port))
  7552. name
  7553. parent
  7554. fields)))
  7555. @end example
  7556. Instances will store their associated data in slots in the structure: as
  7557. many slots as there are fields. The @code{compute-layout} procedure
  7558. below can compute a layout, and @code{field-index} returns the slot
  7559. corresponding to a field.
  7560. @example
  7561. (define-syntax-rule (define-accessor name n)
  7562. (define (name obj)
  7563. (struct-ref obj n)))
  7564. ;; Accessors for classes
  7565. (define-accessor class-name (+ vtable-offset-user 0))
  7566. (define-accessor class-parent (+ vtable-offset-user 1))
  7567. (define-accessor class-fields (+ vtable-offset-user 2))
  7568. (define (compute-fields parent fields)
  7569. (if parent
  7570. (append (class-fields parent) fields)
  7571. fields))
  7572. (define (compute-layout fields)
  7573. (make-struct-layout
  7574. (string-concatenate (make-list (length fields) "pw"))))
  7575. (define (field-index class field)
  7576. (list-index (class-fields class) field))
  7577. (define (print-instance x port)
  7578. (format port "<~a" (class-name (struct-vtable x)))
  7579. (for-each (lambda (field idx)
  7580. (format port " ~a: ~a" field (struct-ref x idx)))
  7581. (class-fields (struct-vtable x))
  7582. (iota (length (class-fields (struct-vtable x)))))
  7583. (format port ">"))
  7584. @end example
  7585. So, at this point we can actually make a few classes:
  7586. @example
  7587. (define-syntax-rule (define-class name parent field ...)
  7588. (define name (make-class 'name parent '(field ...))))
  7589. (define-class <surface> #f
  7590. width height)
  7591. (define-class <window> <surface>
  7592. x y)
  7593. @end example
  7594. And finally, make an instance:
  7595. @example
  7596. (make-struct/no-tail <window> 400 300 10 20)
  7597. @result{} <<window> width: 400 height: 300 x: 10 y: 20>
  7598. @end example
  7599. And that's that. Note that there are many possible optimizations and
  7600. feature enhancements that can be made to this object system, and the
  7601. included GOOPS system does make most of them. For more simple use
  7602. cases, the records facility is usually sufficient. But sometimes you
  7603. need to make new kinds of data abstractions, and for that purpose,
  7604. structs are here.
  7605. @node Dictionary Types
  7606. @subsection Dictionary Types
  7607. A @dfn{dictionary} object is a data structure used to index
  7608. information in a user-defined way. In standard Scheme, the main
  7609. aggregate data types are lists and vectors. Lists are not really
  7610. indexed at all, and vectors are indexed only by number
  7611. (e.g.@: @code{(vector-ref foo 5)}). Often you will find it useful
  7612. to index your data on some other type; for example, in a library
  7613. catalog you might want to look up a book by the name of its
  7614. author. Dictionaries are used to help you organize information in
  7615. such a way.
  7616. An @dfn{association list} (or @dfn{alist} for short) is a list of
  7617. key-value pairs. Each pair represents a single quantity or
  7618. object; the @code{car} of the pair is a key which is used to
  7619. identify the object, and the @code{cdr} is the object's value.
  7620. A @dfn{hash table} also permits you to index objects with
  7621. arbitrary keys, but in a way that makes looking up any one object
  7622. extremely fast. A well-designed hash system makes hash table
  7623. lookups almost as fast as conventional array or vector references.
  7624. Alists are popular among Lisp programmers because they use only
  7625. the language's primitive operations (lists, @dfn{car}, @dfn{cdr}
  7626. and the equality primitives). No changes to the language core are
  7627. necessary. Therefore, with Scheme's built-in list manipulation
  7628. facilities, it is very convenient to handle data stored in an
  7629. association list. Also, alists are highly portable and can be
  7630. easily implemented on even the most minimal Lisp systems.
  7631. However, alists are inefficient, especially for storing large
  7632. quantities of data. Because we want Guile to be useful for large
  7633. software systems as well as small ones, Guile provides a rich set
  7634. of tools for using either association lists or hash tables.
  7635. @node Association Lists
  7636. @subsection Association Lists
  7637. @tpindex Association Lists
  7638. @tpindex Alist
  7639. @cindex association List
  7640. @cindex alist
  7641. @cindex database
  7642. An association list is a conventional data structure that is often used
  7643. to implement simple key-value databases. It consists of a list of
  7644. entries in which each entry is a pair. The @dfn{key} of each entry is
  7645. the @code{car} of the pair and the @dfn{value} of each entry is the
  7646. @code{cdr}.
  7647. @example
  7648. ASSOCIATION LIST ::= '( (KEY1 . VALUE1)
  7649. (KEY2 . VALUE2)
  7650. (KEY3 . VALUE3)
  7651. @dots{}
  7652. )
  7653. @end example
  7654. @noindent
  7655. Association lists are also known, for short, as @dfn{alists}.
  7656. The structure of an association list is just one example of the infinite
  7657. number of possible structures that can be built using pairs and lists.
  7658. As such, the keys and values in an association list can be manipulated
  7659. using the general list structure procedures @code{cons}, @code{car},
  7660. @code{cdr}, @code{set-car!}, @code{set-cdr!} and so on. However,
  7661. because association lists are so useful, Guile also provides specific
  7662. procedures for manipulating them.
  7663. @menu
  7664. * Alist Key Equality::
  7665. * Adding or Setting Alist Entries::
  7666. * Retrieving Alist Entries::
  7667. * Removing Alist Entries::
  7668. * Sloppy Alist Functions::
  7669. * Alist Example::
  7670. @end menu
  7671. @node Alist Key Equality
  7672. @subsubsection Alist Key Equality
  7673. All of Guile's dedicated association list procedures, apart from
  7674. @code{acons}, come in three flavours, depending on the level of equality
  7675. that is required to decide whether an existing key in the association
  7676. list is the same as the key that the procedure call uses to identify the
  7677. required entry.
  7678. @itemize @bullet
  7679. @item
  7680. Procedures with @dfn{assq} in their name use @code{eq?} to determine key
  7681. equality.
  7682. @item
  7683. Procedures with @dfn{assv} in their name use @code{eqv?} to determine
  7684. key equality.
  7685. @item
  7686. Procedures with @dfn{assoc} in their name use @code{equal?} to
  7687. determine key equality.
  7688. @end itemize
  7689. @code{acons} is an exception because it is used to build association
  7690. lists which do not require their entries' keys to be unique.
  7691. @node Adding or Setting Alist Entries
  7692. @subsubsection Adding or Setting Alist Entries
  7693. @code{acons} adds a new entry to an association list and returns the
  7694. combined association list. The combined alist is formed by consing the
  7695. new entry onto the head of the alist specified in the @code{acons}
  7696. procedure call. So the specified alist is not modified, but its
  7697. contents become shared with the tail of the combined alist that
  7698. @code{acons} returns.
  7699. In the most common usage of @code{acons}, a variable holding the
  7700. original association list is updated with the combined alist:
  7701. @example
  7702. (set! address-list (acons name address address-list))
  7703. @end example
  7704. In such cases, it doesn't matter that the old and new values of
  7705. @code{address-list} share some of their contents, since the old value is
  7706. usually no longer independently accessible.
  7707. Note that @code{acons} adds the specified new entry regardless of
  7708. whether the alist may already contain entries with keys that are, in
  7709. some sense, the same as that of the new entry. Thus @code{acons} is
  7710. ideal for building alists where there is no concept of key uniqueness.
  7711. @example
  7712. (set! task-list (acons 3 "pay gas bill" '()))
  7713. task-list
  7714. @result{}
  7715. ((3 . "pay gas bill"))
  7716. (set! task-list (acons 3 "tidy bedroom" task-list))
  7717. task-list
  7718. @result{}
  7719. ((3 . "tidy bedroom") (3 . "pay gas bill"))
  7720. @end example
  7721. @code{assq-set!}, @code{assv-set!} and @code{assoc-set!} are used to add
  7722. or replace an entry in an association list where there @emph{is} a
  7723. concept of key uniqueness. If the specified association list already
  7724. contains an entry whose key is the same as that specified in the
  7725. procedure call, the existing entry is replaced by the new one.
  7726. Otherwise, the new entry is consed onto the head of the old association
  7727. list to create the combined alist. In all cases, these procedures
  7728. return the combined alist.
  7729. @code{assq-set!} and friends @emph{may} destructively modify the
  7730. structure of the old association list in such a way that an existing
  7731. variable is correctly updated without having to @code{set!} it to the
  7732. value returned:
  7733. @example
  7734. address-list
  7735. @result{}
  7736. (("mary" . "34 Elm Road") ("james" . "16 Bow Street"))
  7737. (assoc-set! address-list "james" "1a London Road")
  7738. @result{}
  7739. (("mary" . "34 Elm Road") ("james" . "1a London Road"))
  7740. address-list
  7741. @result{}
  7742. (("mary" . "34 Elm Road") ("james" . "1a London Road"))
  7743. @end example
  7744. Or they may not:
  7745. @example
  7746. (assoc-set! address-list "bob" "11 Newington Avenue")
  7747. @result{}
  7748. (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road")
  7749. ("james" . "1a London Road"))
  7750. address-list
  7751. @result{}
  7752. (("mary" . "34 Elm Road") ("james" . "1a London Road"))
  7753. @end example
  7754. The only safe way to update an association list variable when adding or
  7755. replacing an entry like this is to @code{set!} the variable to the
  7756. returned value:
  7757. @example
  7758. (set! address-list
  7759. (assoc-set! address-list "bob" "11 Newington Avenue"))
  7760. address-list
  7761. @result{}
  7762. (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road")
  7763. ("james" . "1a London Road"))
  7764. @end example
  7765. Because of this slight inconvenience, you may find it more convenient to
  7766. use hash tables to store dictionary data. If your application will not
  7767. be modifying the contents of an alist very often, this may not make much
  7768. difference to you.
  7769. If you need to keep the old value of an association list in a form
  7770. independent from the list that results from modification by
  7771. @code{acons}, @code{assq-set!}, @code{assv-set!} or @code{assoc-set!},
  7772. use @code{list-copy} to copy the old association list before modifying
  7773. it.
  7774. @deffn {Scheme Procedure} acons key value alist
  7775. @deffnx {C Function} scm_acons (key, value, alist)
  7776. Add a new key-value pair to @var{alist}. A new pair is
  7777. created whose car is @var{key} and whose cdr is @var{value}, and the
  7778. pair is consed onto @var{alist}, and the new list is returned. This
  7779. function is @emph{not} destructive; @var{alist} is not modified.
  7780. @end deffn
  7781. @deffn {Scheme Procedure} assq-set! alist key val
  7782. @deffnx {Scheme Procedure} assv-set! alist key value
  7783. @deffnx {Scheme Procedure} assoc-set! alist key value
  7784. @deffnx {C Function} scm_assq_set_x (alist, key, val)
  7785. @deffnx {C Function} scm_assv_set_x (alist, key, val)
  7786. @deffnx {C Function} scm_assoc_set_x (alist, key, val)
  7787. Reassociate @var{key} in @var{alist} with @var{value}: find any existing
  7788. @var{alist} entry for @var{key} and associate it with the new
  7789. @var{value}. If @var{alist} does not contain an entry for @var{key},
  7790. add a new one. Return the (possibly new) alist.
  7791. These functions do not attempt to verify the structure of @var{alist},
  7792. and so may cause unusual results if passed an object that is not an
  7793. association list.
  7794. @end deffn
  7795. @node Retrieving Alist Entries
  7796. @subsubsection Retrieving Alist Entries
  7797. @rnindex assq
  7798. @rnindex assv
  7799. @rnindex assoc
  7800. @code{assq}, @code{assv} and @code{assoc} find the entry in an alist
  7801. for a given key, and return the @code{(@var{key} . @var{value})} pair.
  7802. @code{assq-ref}, @code{assv-ref} and @code{assoc-ref} do a similar
  7803. lookup, but return just the @var{value}.
  7804. @deffn {Scheme Procedure} assq key alist
  7805. @deffnx {Scheme Procedure} assv key alist
  7806. @deffnx {Scheme Procedure} assoc key alist
  7807. @deffnx {C Function} scm_assq (key, alist)
  7808. @deffnx {C Function} scm_assv (key, alist)
  7809. @deffnx {C Function} scm_assoc (key, alist)
  7810. Return the first entry in @var{alist} with the given @var{key}. The
  7811. return is the pair @code{(KEY . VALUE)} from @var{alist}. If there's
  7812. no matching entry the return is @code{#f}.
  7813. @code{assq} compares keys with @code{eq?}, @code{assv} uses
  7814. @code{eqv?} and @code{assoc} uses @code{equal?}. See also SRFI-1
  7815. which has an extended @code{assoc} (@ref{SRFI-1 Association Lists}).
  7816. @end deffn
  7817. @deffn {Scheme Procedure} assq-ref alist key
  7818. @deffnx {Scheme Procedure} assv-ref alist key
  7819. @deffnx {Scheme Procedure} assoc-ref alist key
  7820. @deffnx {C Function} scm_assq_ref (alist, key)
  7821. @deffnx {C Function} scm_assv_ref (alist, key)
  7822. @deffnx {C Function} scm_assoc_ref (alist, key)
  7823. Return the value from the first entry in @var{alist} with the given
  7824. @var{key}, or @code{#f} if there's no such entry.
  7825. @code{assq-ref} compares keys with @code{eq?}, @code{assv-ref} uses
  7826. @code{eqv?} and @code{assoc-ref} uses @code{equal?}.
  7827. Notice these functions have the @var{key} argument last, like other
  7828. @code{-ref} functions, but this is opposite to what @code{assq}
  7829. etc above use.
  7830. When the return is @code{#f} it can be either @var{key} not found, or
  7831. an entry which happens to have value @code{#f} in the @code{cdr}. Use
  7832. @code{assq} etc above if you need to differentiate these cases.
  7833. @end deffn
  7834. @node Removing Alist Entries
  7835. @subsubsection Removing Alist Entries
  7836. To remove the element from an association list whose key matches a
  7837. specified key, use @code{assq-remove!}, @code{assv-remove!} or
  7838. @code{assoc-remove!} (depending, as usual, on the level of equality
  7839. required between the key that you specify and the keys in the
  7840. association list).
  7841. As with @code{assq-set!} and friends, the specified alist may or may not
  7842. be modified destructively, and the only safe way to update a variable
  7843. containing the alist is to @code{set!} it to the value that
  7844. @code{assq-remove!} and friends return.
  7845. @example
  7846. address-list
  7847. @result{}
  7848. (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road")
  7849. ("james" . "1a London Road"))
  7850. (set! address-list (assoc-remove! address-list "mary"))
  7851. address-list
  7852. @result{}
  7853. (("bob" . "11 Newington Avenue") ("james" . "1a London Road"))
  7854. @end example
  7855. Note that, when @code{assq/v/oc-remove!} is used to modify an
  7856. association list that has been constructed only using the corresponding
  7857. @code{assq/v/oc-set!}, there can be at most one matching entry in the
  7858. alist, so the question of multiple entries being removed in one go does
  7859. not arise. If @code{assq/v/oc-remove!} is applied to an association
  7860. list that has been constructed using @code{acons}, or an
  7861. @code{assq/v/oc-set!} with a different level of equality, or any mixture
  7862. of these, it removes only the first matching entry from the alist, even
  7863. if the alist might contain further matching entries. For example:
  7864. @example
  7865. (define address-list '())
  7866. (set! address-list (assq-set! address-list "mary" "11 Elm Street"))
  7867. (set! address-list (assq-set! address-list "mary" "57 Pine Drive"))
  7868. address-list
  7869. @result{}
  7870. (("mary" . "57 Pine Drive") ("mary" . "11 Elm Street"))
  7871. (set! address-list (assoc-remove! address-list "mary"))
  7872. address-list
  7873. @result{}
  7874. (("mary" . "11 Elm Street"))
  7875. @end example
  7876. In this example, the two instances of the string "mary" are not the same
  7877. when compared using @code{eq?}, so the two @code{assq-set!} calls add
  7878. two distinct entries to @code{address-list}. When compared using
  7879. @code{equal?}, both "mary"s in @code{address-list} are the same as the
  7880. "mary" in the @code{assoc-remove!} call, but @code{assoc-remove!} stops
  7881. after removing the first matching entry that it finds, and so one of the
  7882. "mary" entries is left in place.
  7883. @deffn {Scheme Procedure} assq-remove! alist key
  7884. @deffnx {Scheme Procedure} assv-remove! alist key
  7885. @deffnx {Scheme Procedure} assoc-remove! alist key
  7886. @deffnx {C Function} scm_assq_remove_x (alist, key)
  7887. @deffnx {C Function} scm_assv_remove_x (alist, key)
  7888. @deffnx {C Function} scm_assoc_remove_x (alist, key)
  7889. Delete the first entry in @var{alist} associated with @var{key}, and return
  7890. the resulting alist.
  7891. @end deffn
  7892. @node Sloppy Alist Functions
  7893. @subsubsection Sloppy Alist Functions
  7894. @code{sloppy-assq}, @code{sloppy-assv} and @code{sloppy-assoc} behave
  7895. like the corresponding non-@code{sloppy-} procedures, except that they
  7896. return @code{#f} when the specified association list is not well-formed,
  7897. where the non-@code{sloppy-} versions would signal an error.
  7898. Specifically, there are two conditions for which the non-@code{sloppy-}
  7899. procedures signal an error, which the @code{sloppy-} procedures handle
  7900. instead by returning @code{#f}. Firstly, if the specified alist as a
  7901. whole is not a proper list:
  7902. @example
  7903. (assoc "mary" '((1 . 2) ("key" . "door") . "open sesame"))
  7904. @result{}
  7905. ERROR: In procedure assoc in expression (assoc "mary" (quote #)):
  7906. ERROR: Wrong type argument in position 2 (expecting
  7907. association list): ((1 . 2) ("key" . "door") . "open sesame")
  7908. (sloppy-assoc "mary" '((1 . 2) ("key" . "door") . "open sesame"))
  7909. @result{}
  7910. #f
  7911. @end example
  7912. @noindent
  7913. Secondly, if one of the entries in the specified alist is not a pair:
  7914. @example
  7915. (assoc 2 '((1 . 1) 2 (3 . 9)))
  7916. @result{}
  7917. ERROR: In procedure assoc in expression (assoc 2 (quote #)):
  7918. ERROR: Wrong type argument in position 2 (expecting
  7919. association list): ((1 . 1) 2 (3 . 9))
  7920. (sloppy-assoc 2 '((1 . 1) 2 (3 . 9)))
  7921. @result{}
  7922. #f
  7923. @end example
  7924. Unless you are explicitly working with badly formed association lists,
  7925. it is much safer to use the non-@code{sloppy-} procedures, because they
  7926. help to highlight coding and data errors that the @code{sloppy-}
  7927. versions would silently cover up.
  7928. @deffn {Scheme Procedure} sloppy-assq key alist
  7929. @deffnx {C Function} scm_sloppy_assq (key, alist)
  7930. Behaves like @code{assq} but does not do any error checking.
  7931. Recommended only for use in Guile internals.
  7932. @end deffn
  7933. @deffn {Scheme Procedure} sloppy-assv key alist
  7934. @deffnx {C Function} scm_sloppy_assv (key, alist)
  7935. Behaves like @code{assv} but does not do any error checking.
  7936. Recommended only for use in Guile internals.
  7937. @end deffn
  7938. @deffn {Scheme Procedure} sloppy-assoc key alist
  7939. @deffnx {C Function} scm_sloppy_assoc (key, alist)
  7940. Behaves like @code{assoc} but does not do any error checking.
  7941. Recommended only for use in Guile internals.
  7942. @end deffn
  7943. @node Alist Example
  7944. @subsubsection Alist Example
  7945. Here is a longer example of how alists may be used in practice.
  7946. @lisp
  7947. (define capitals '(("New York" . "Albany")
  7948. ("Oregon" . "Salem")
  7949. ("Florida" . "Miami")))
  7950. ;; What's the capital of Oregon?
  7951. (assoc "Oregon" capitals) @result{} ("Oregon" . "Salem")
  7952. (assoc-ref capitals "Oregon") @result{} "Salem"
  7953. ;; We left out South Dakota.
  7954. (set! capitals
  7955. (assoc-set! capitals "South Dakota" "Pierre"))
  7956. capitals
  7957. @result{} (("South Dakota" . "Pierre")
  7958. ("New York" . "Albany")
  7959. ("Oregon" . "Salem")
  7960. ("Florida" . "Miami"))
  7961. ;; And we got Florida wrong.
  7962. (set! capitals
  7963. (assoc-set! capitals "Florida" "Tallahassee"))
  7964. capitals
  7965. @result{} (("South Dakota" . "Pierre")
  7966. ("New York" . "Albany")
  7967. ("Oregon" . "Salem")
  7968. ("Florida" . "Tallahassee"))
  7969. ;; After Oregon secedes, we can remove it.
  7970. (set! capitals
  7971. (assoc-remove! capitals "Oregon"))
  7972. capitals
  7973. @result{} (("South Dakota" . "Pierre")
  7974. ("New York" . "Albany")
  7975. ("Florida" . "Tallahassee"))
  7976. @end lisp
  7977. @node VHashes
  7978. @subsection VList-Based Hash Lists or ``VHashes''
  7979. @cindex VList-based hash lists
  7980. @cindex VHash
  7981. The @code{(ice-9 vlist)} module provides an implementation of @dfn{VList-based
  7982. hash lists} (@pxref{VLists}). VList-based hash lists, or @dfn{vhashes}, are an
  7983. immutable dictionary type similar to association lists that maps @dfn{keys} to
  7984. @dfn{values}. However, unlike association lists, accessing a value given its
  7985. key is typically a constant-time operation.
  7986. The VHash programming interface of @code{(ice-9 vlist)} is mostly the same as
  7987. that of association lists found in SRFI-1, with procedure names prefixed by
  7988. @code{vhash-} instead of @code{alist-} (@pxref{SRFI-1 Association Lists}).
  7989. In addition, vhashes can be manipulated using VList operations:
  7990. @example
  7991. (vlist-head (vhash-consq 'a 1 vlist-null))
  7992. @result{} (a . 1)
  7993. (define vh1 (vhash-consq 'b 2 (vhash-consq 'a 1 vlist-null)))
  7994. (define vh2 (vhash-consq 'c 3 (vlist-tail vh1)))
  7995. (vhash-assq 'a vh2)
  7996. @result{} (a . 1)
  7997. (vhash-assq 'b vh2)
  7998. @result{} #f
  7999. (vhash-assq 'c vh2)
  8000. @result{} (c . 3)
  8001. (vlist->list vh2)
  8002. @result{} ((c . 3) (a . 1))
  8003. @end example
  8004. However, keep in mind that procedures that construct new VLists
  8005. (@code{vlist-map}, @code{vlist-filter}, etc.) return raw VLists, not vhashes:
  8006. @example
  8007. (define vh (alist->vhash '((a . 1) (b . 2) (c . 3)) hashq))
  8008. (vhash-assq 'a vh)
  8009. @result{} (a . 1)
  8010. (define vl
  8011. ;; This will create a raw vlist.
  8012. (vlist-filter (lambda (key+value) (odd? (cdr key+value))) vh))
  8013. (vhash-assq 'a vl)
  8014. @result{} ERROR: Wrong type argument in position 2
  8015. (vlist->list vl)
  8016. @result{} ((a . 1) (c . 3))
  8017. @end example
  8018. @deffn {Scheme Procedure} vhash? obj
  8019. Return true if @var{obj} is a vhash.
  8020. @end deffn
  8021. @deffn {Scheme Procedure} vhash-cons key value vhash [hash-proc]
  8022. @deffnx {Scheme Procedure} vhash-consq key value vhash
  8023. @deffnx {Scheme Procedure} vhash-consv key value vhash
  8024. Return a new hash list based on @var{vhash} where @var{key} is associated with
  8025. @var{value}, using @var{hash-proc} to compute the hash of @var{key}.
  8026. @var{vhash} must be either @code{vlist-null} or a vhash returned by a previous
  8027. call to @code{vhash-cons}. @var{hash-proc} defaults to @code{hash} (@pxref{Hash
  8028. Table Reference, @code{hash} procedure}). With @code{vhash-consq}, the
  8029. @code{hashq} hash function is used; with @code{vhash-consv} the @code{hashv}
  8030. hash function is used.
  8031. All @code{vhash-cons} calls made to construct a vhash should use the same
  8032. @var{hash-proc}. Failing to do that, the result is undefined.
  8033. @end deffn
  8034. @deffn {Scheme Procedure} vhash-assoc key vhash [equal? [hash-proc]]
  8035. @deffnx {Scheme Procedure} vhash-assq key vhash
  8036. @deffnx {Scheme Procedure} vhash-assv key vhash
  8037. Return the first key/value pair from @var{vhash} whose key is equal to @var{key}
  8038. according to the @var{equal?} equality predicate (which defaults to
  8039. @code{equal?}), and using @var{hash-proc} (which defaults to @code{hash}) to
  8040. compute the hash of @var{key}. The second form uses @code{eq?} as the equality
  8041. predicate and @code{hashq} as the hash function; the last form uses @code{eqv?}
  8042. and @code{hashv}.
  8043. Note that it is important to consistently use the same hash function for
  8044. @var{hash-proc} as was passed to @code{vhash-cons}. Failing to do that, the
  8045. result is unpredictable.
  8046. @end deffn
  8047. @deffn {Scheme Procedure} vhash-delete key vhash [equal? [hash-proc]]
  8048. @deffnx {Scheme Procedure} vhash-delq key vhash
  8049. @deffnx {Scheme Procedure} vhash-delv key vhash
  8050. Remove all associations from @var{vhash} with @var{key}, comparing keys with
  8051. @var{equal?} (which defaults to @code{equal?}), and computing the hash of
  8052. @var{key} using @var{hash-proc} (which defaults to @code{hash}). The second
  8053. form uses @code{eq?} as the equality predicate and @code{hashq} as the hash
  8054. function; the last one uses @code{eqv?} and @code{hashv}.
  8055. Again the choice of @var{hash-proc} must be consistent with previous calls to
  8056. @code{vhash-cons}.
  8057. @end deffn
  8058. @deffn {Scheme Procedure} vhash-fold proc init vhash
  8059. @deffnx {Scheme Procedure} vhash-fold-right proc init vhash
  8060. Fold over the key/value elements of @var{vhash} in the given direction,
  8061. with each call to @var{proc} having the form @code{(@var{proc} key value
  8062. result)}, where @var{result} is the result of the previous call to
  8063. @var{proc} and @var{init} the value of @var{result} for the first call
  8064. to @var{proc}.
  8065. @end deffn
  8066. @deffn {Scheme Procedure} vhash-fold* proc init key vhash [equal? [hash]]
  8067. @deffnx {Scheme Procedure} vhash-foldq* proc init key vhash
  8068. @deffnx {Scheme Procedure} vhash-foldv* proc init key vhash
  8069. Fold over all the values associated with @var{key} in @var{vhash}, with each
  8070. call to @var{proc} having the form @code{(proc value result)}, where
  8071. @var{result} is the result of the previous call to @var{proc} and @var{init} the
  8072. value of @var{result} for the first call to @var{proc}.
  8073. Keys in @var{vhash} are hashed using @var{hash} are compared using @var{equal?}.
  8074. The second form uses @code{eq?} as the equality predicate and @code{hashq} as
  8075. the hash function; the third one uses @code{eqv?} and @code{hashv}.
  8076. Example:
  8077. @example
  8078. (define vh
  8079. (alist->vhash '((a . 1) (a . 2) (z . 0) (a . 3))))
  8080. (vhash-fold* cons '() 'a vh)
  8081. @result{} (3 2 1)
  8082. (vhash-fold* cons '() 'z vh)
  8083. @result{} (0)
  8084. @end example
  8085. @end deffn
  8086. @deffn {Scheme Procedure} alist->vhash alist [hash-proc]
  8087. Return the vhash corresponding to @var{alist}, an association list, using
  8088. @var{hash-proc} to compute key hashes. When omitted, @var{hash-proc} defaults
  8089. to @code{hash}.
  8090. @end deffn
  8091. @node Hash Tables
  8092. @subsection Hash Tables
  8093. @tpindex Hash Tables
  8094. Hash tables are dictionaries which offer similar functionality as
  8095. association lists: They provide a mapping from keys to values. The
  8096. difference is that association lists need time linear in the size of
  8097. elements when searching for entries, whereas hash tables can normally
  8098. search in constant time. The drawback is that hash tables require a
  8099. little bit more memory, and that you can not use the normal list
  8100. procedures (@pxref{Lists}) for working with them.
  8101. @menu
  8102. * Hash Table Examples:: Demonstration of hash table usage.
  8103. * Hash Table Reference:: Hash table procedure descriptions.
  8104. @end menu
  8105. @node Hash Table Examples
  8106. @subsubsection Hash Table Examples
  8107. For demonstration purposes, this section gives a few usage examples of
  8108. some hash table procedures, together with some explanation what they do.
  8109. First we start by creating a new hash table with 31 slots, and
  8110. populate it with two key/value pairs.
  8111. @lisp
  8112. (define h (make-hash-table 31))
  8113. ;; This is an opaque object
  8114. h
  8115. @result{}
  8116. #<hash-table 0/31>
  8117. ;; Inserting into a hash table can be done with hashq-set!
  8118. (hashq-set! h 'foo "bar")
  8119. @result{}
  8120. "bar"
  8121. (hashq-set! h 'braz "zonk")
  8122. @result{}
  8123. "zonk"
  8124. ;; Or with hash-create-handle!
  8125. (hashq-create-handle! h 'frob #f)
  8126. @result{}
  8127. (frob . #f)
  8128. @end lisp
  8129. You can get the value for a given key with the procedure
  8130. @code{hashq-ref}, but the problem with this procedure is that you
  8131. cannot reliably determine whether a key does exists in the table. The
  8132. reason is that the procedure returns @code{#f} if the key is not in
  8133. the table, but it will return the same value if the key is in the
  8134. table and just happens to have the value @code{#f}, as you can see in
  8135. the following examples.
  8136. @lisp
  8137. (hashq-ref h 'foo)
  8138. @result{}
  8139. "bar"
  8140. (hashq-ref h 'frob)
  8141. @result{}
  8142. #f
  8143. (hashq-ref h 'not-there)
  8144. @result{}
  8145. #f
  8146. @end lisp
  8147. It is often better is to use the procedure @code{hashq-get-handle},
  8148. which makes a distinction between the two cases. Just like @code{assq},
  8149. this procedure returns a key/value-pair on success, and @code{#f} if the
  8150. key is not found.
  8151. @lisp
  8152. (hashq-get-handle h 'foo)
  8153. @result{}
  8154. (foo . "bar")
  8155. (hashq-get-handle h 'not-there)
  8156. @result{}
  8157. #f
  8158. @end lisp
  8159. Interesting results can be computed by using @code{hash-fold} to work
  8160. through each element. This example will count the total number of
  8161. elements:
  8162. @lisp
  8163. (hash-fold (lambda (key value seed) (+ 1 seed)) 0 h)
  8164. @result{}
  8165. 3
  8166. @end lisp
  8167. The same thing can be done with the procedure @code{hash-count}, which
  8168. can also count the number of elements matching a particular predicate.
  8169. For example, count the number of elements with string values:
  8170. @lisp
  8171. (hash-count (lambda (key value) (string? value)) h)
  8172. @result{}
  8173. 2
  8174. @end lisp
  8175. Counting all the elements is a simple task using @code{const}:
  8176. @lisp
  8177. (hash-count (const #t) h)
  8178. @result{}
  8179. 3
  8180. @end lisp
  8181. @node Hash Table Reference
  8182. @subsubsection Hash Table Reference
  8183. @c FIXME: Describe in broad terms what happens for resizing, and what
  8184. @c the initial size means for this.
  8185. Like the association list functions, the hash table functions come in
  8186. several varieties, according to the equality test used for the keys.
  8187. Plain @code{hash-} functions use @code{equal?}, @code{hashq-}
  8188. functions use @code{eq?}, @code{hashv-} functions use @code{eqv?}, and
  8189. the @code{hashx-} functions use an application supplied test.
  8190. A single @code{make-hash-table} creates a hash table suitable for use
  8191. with any set of functions, but it's imperative that just one set is
  8192. then used consistently, or results will be unpredictable.
  8193. Hash tables are implemented as a vector indexed by a hash value formed
  8194. from the key, with an association list of key/value pairs for each
  8195. bucket in case distinct keys hash together. Direct access to the
  8196. pairs in those lists is provided by the @code{-handle-} functions.
  8197. When the number of entries in a hash table goes above a threshold, the
  8198. vector is made larger and the entries are rehashed, to prevent the
  8199. bucket lists from becoming too long and slowing down accesses. When the
  8200. number of entries goes below a threshold, the vector is shrunk to save
  8201. space.
  8202. For the @code{hashx-} ``extended'' routines, an application supplies a
  8203. @var{hash} function producing an integer index like @code{hashq} etc
  8204. below, and an @var{assoc} alist search function like @code{assq} etc
  8205. (@pxref{Retrieving Alist Entries}). Here's an example of such
  8206. functions implementing case-insensitive hashing of string keys,
  8207. @example
  8208. (use-modules (srfi srfi-1)
  8209. (srfi srfi-13))
  8210. (define (my-hash str size)
  8211. (remainder (string-hash-ci str) size))
  8212. (define (my-assoc str alist)
  8213. (find (lambda (pair) (string-ci=? str (car pair))) alist))
  8214. (define my-table (make-hash-table))
  8215. (hashx-set! my-hash my-assoc my-table "foo" 123)
  8216. (hashx-ref my-hash my-assoc my-table "FOO")
  8217. @result{} 123
  8218. @end example
  8219. In a @code{hashx-} @var{hash} function the aim is to spread keys
  8220. across the vector, so bucket lists don't become long. But the actual
  8221. values are arbitrary as long as they're in the range 0 to
  8222. @math{@var{size}-1}. Helpful functions for forming a hash value, in
  8223. addition to @code{hashq} etc below, include @code{symbol-hash}
  8224. (@pxref{Symbol Keys}), @code{string-hash} and @code{string-hash-ci}
  8225. (@pxref{String Comparison}), and @code{char-set-hash}
  8226. (@pxref{Character Set Predicates/Comparison}).
  8227. @sp 1
  8228. @deffn {Scheme Procedure} make-hash-table [size]
  8229. Create a new hash table object, with an optional minimum
  8230. vector @var{size}.
  8231. When @var{size} is given, the table vector will still grow and shrink
  8232. automatically, as described above, but with @var{size} as a minimum.
  8233. If an application knows roughly how many entries the table will hold
  8234. then it can use @var{size} to avoid rehashing when initial entries are
  8235. added.
  8236. @end deffn
  8237. @deffn {Scheme Procedure} alist->hash-table alist
  8238. @deffnx {Scheme Procedure} alist->hashq-table alist
  8239. @deffnx {Scheme Procedure} alist->hashv-table alist
  8240. @deffnx {Scheme Procedure} alist->hashx-table hash assoc alist
  8241. Convert @var{alist} into a hash table. When keys are repeated in
  8242. @var{alist}, the leftmost association takes precedence.
  8243. @example
  8244. (use-modules (ice-9 hash-table))
  8245. (alist->hash-table '((foo . 1) (bar . 2)))
  8246. @end example
  8247. When converting to an extended hash table, custom @var{hash} and
  8248. @var{assoc} procedures must be provided.
  8249. @example
  8250. (alist->hashx-table hash assoc '((foo . 1) (bar . 2)))
  8251. @end example
  8252. @end deffn
  8253. @deffn {Scheme Procedure} hash-table? obj
  8254. @deffnx {C Function} scm_hash_table_p (obj)
  8255. Return @code{#t} if @var{obj} is a abstract hash table object.
  8256. @end deffn
  8257. @deffn {Scheme Procedure} hash-clear! table
  8258. @deffnx {C Function} scm_hash_clear_x (table)
  8259. Remove all items from @var{table} (without triggering a resize).
  8260. @end deffn
  8261. @deffn {Scheme Procedure} hash-ref table key [dflt]
  8262. @deffnx {Scheme Procedure} hashq-ref table key [dflt]
  8263. @deffnx {Scheme Procedure} hashv-ref table key [dflt]
  8264. @deffnx {Scheme Procedure} hashx-ref hash assoc table key [dflt]
  8265. @deffnx {C Function} scm_hash_ref (table, key, dflt)
  8266. @deffnx {C Function} scm_hashq_ref (table, key, dflt)
  8267. @deffnx {C Function} scm_hashv_ref (table, key, dflt)
  8268. @deffnx {C Function} scm_hashx_ref (hash, assoc, table, key, dflt)
  8269. Lookup @var{key} in the given hash @var{table}, and return the
  8270. associated value. If @var{key} is not found, return @var{dflt}, or
  8271. @code{#f} if @var{dflt} is not given.
  8272. @end deffn
  8273. @deffn {Scheme Procedure} hash-set! table key val
  8274. @deffnx {Scheme Procedure} hashq-set! table key val
  8275. @deffnx {Scheme Procedure} hashv-set! table key val
  8276. @deffnx {Scheme Procedure} hashx-set! hash assoc table key val
  8277. @deffnx {C Function} scm_hash_set_x (table, key, val)
  8278. @deffnx {C Function} scm_hashq_set_x (table, key, val)
  8279. @deffnx {C Function} scm_hashv_set_x (table, key, val)
  8280. @deffnx {C Function} scm_hashx_set_x (hash, assoc, table, key, val)
  8281. Associate @var{val} with @var{key} in the given hash @var{table}. If
  8282. @var{key} is already present then it's associated value is changed.
  8283. If it's not present then a new entry is created.
  8284. @end deffn
  8285. @deffn {Scheme Procedure} hash-remove! table key
  8286. @deffnx {Scheme Procedure} hashq-remove! table key
  8287. @deffnx {Scheme Procedure} hashv-remove! table key
  8288. @deffnx {Scheme Procedure} hashx-remove! hash assoc table key
  8289. @deffnx {C Function} scm_hash_remove_x (table, key)
  8290. @deffnx {C Function} scm_hashq_remove_x (table, key)
  8291. @deffnx {C Function} scm_hashv_remove_x (table, key)
  8292. @deffnx {C Function} scm_hashx_remove_x (hash, assoc, table, key)
  8293. Remove any association for @var{key} in the given hash @var{table}.
  8294. If @var{key} is not in @var{table} then nothing is done.
  8295. @end deffn
  8296. @deffn {Scheme Procedure} hash key size
  8297. @deffnx {Scheme Procedure} hashq key size
  8298. @deffnx {Scheme Procedure} hashv key size
  8299. @deffnx {C Function} scm_hash (key, size)
  8300. @deffnx {C Function} scm_hashq (key, size)
  8301. @deffnx {C Function} scm_hashv (key, size)
  8302. Return a hash value for @var{key}. This is a number in the range
  8303. @math{0} to @math{@var{size}-1}, which is suitable for use in a hash
  8304. table of the given @var{size}.
  8305. Note that @code{hashq} and @code{hashv} may use internal addresses of
  8306. objects, so if an object is garbage collected and re-created it can
  8307. have a different hash value, even when the two are notionally
  8308. @code{eq?}. For instance with symbols,
  8309. @example
  8310. (hashq 'something 123) @result{} 19
  8311. (gc)
  8312. (hashq 'something 123) @result{} 62
  8313. @end example
  8314. In normal use this is not a problem, since an object entered into a
  8315. hash table won't be garbage collected until removed. It's only if
  8316. hashing calculations are somehow separated from normal references that
  8317. its lifetime needs to be considered.
  8318. @end deffn
  8319. @deffn {Scheme Procedure} hash-get-handle table key
  8320. @deffnx {Scheme Procedure} hashq-get-handle table key
  8321. @deffnx {Scheme Procedure} hashv-get-handle table key
  8322. @deffnx {Scheme Procedure} hashx-get-handle hash assoc table key
  8323. @deffnx {C Function} scm_hash_get_handle (table, key)
  8324. @deffnx {C Function} scm_hashq_get_handle (table, key)
  8325. @deffnx {C Function} scm_hashv_get_handle (table, key)
  8326. @deffnx {C Function} scm_hashx_get_handle (hash, assoc, table, key)
  8327. Return the @code{(@var{key} . @var{value})} pair for @var{key} in the
  8328. given hash @var{table}, or @code{#f} if @var{key} is not in
  8329. @var{table}.
  8330. @end deffn
  8331. @deffn {Scheme Procedure} hash-create-handle! table key init
  8332. @deffnx {Scheme Procedure} hashq-create-handle! table key init
  8333. @deffnx {Scheme Procedure} hashv-create-handle! table key init
  8334. @deffnx {Scheme Procedure} hashx-create-handle! hash assoc table key init
  8335. @deffnx {C Function} scm_hash_create_handle_x (table, key, init)
  8336. @deffnx {C Function} scm_hashq_create_handle_x (table, key, init)
  8337. @deffnx {C Function} scm_hashv_create_handle_x (table, key, init)
  8338. @deffnx {C Function} scm_hashx_create_handle_x (hash, assoc, table, key, init)
  8339. Return the @code{(@var{key} . @var{value})} pair for @var{key} in the
  8340. given hash @var{table}. If @var{key} is not in @var{table} then
  8341. create an entry for it with @var{init} as the value, and return that
  8342. pair.
  8343. @end deffn
  8344. @deffn {Scheme Procedure} hash-map->list proc table
  8345. @deffnx {Scheme Procedure} hash-for-each proc table
  8346. @deffnx {C Function} scm_hash_map_to_list (proc, table)
  8347. @deffnx {C Function} scm_hash_for_each (proc, table)
  8348. Apply @var{proc} to the entries in the given hash @var{table}. Each
  8349. call is @code{(@var{proc} @var{key} @var{value})}. @code{hash-map->list}
  8350. returns a list of the results from these calls, @code{hash-for-each}
  8351. discards the results and returns an unspecified value.
  8352. Calls are made over the table entries in an unspecified order, and for
  8353. @code{hash-map->list} the order of the values in the returned list is
  8354. unspecified. Results will be unpredictable if @var{table} is modified
  8355. while iterating.
  8356. For example the following returns a new alist comprising all the
  8357. entries from @code{mytable}, in no particular order.
  8358. @example
  8359. (hash-map->list cons mytable)
  8360. @end example
  8361. @end deffn
  8362. @deffn {Scheme Procedure} hash-for-each-handle proc table
  8363. @deffnx {C Function} scm_hash_for_each_handle (proc, table)
  8364. Apply @var{proc} to the entries in the given hash @var{table}. Each
  8365. call is @code{(@var{proc} @var{handle})}, where @var{handle} is a
  8366. @code{(@var{key} . @var{value})} pair. Return an unspecified value.
  8367. @code{hash-for-each-handle} differs from @code{hash-for-each} only in
  8368. the argument list of @var{proc}.
  8369. @end deffn
  8370. @deffn {Scheme Procedure} hash-fold proc init table
  8371. @deffnx {C Function} scm_hash_fold (proc, init, table)
  8372. Accumulate a result by applying @var{proc} to the elements of the
  8373. given hash @var{table}. Each call is @code{(@var{proc} @var{key}
  8374. @var{value} @var{prior-result})}, where @var{key} and @var{value} are
  8375. from the @var{table} and @var{prior-result} is the return from the
  8376. previous @var{proc} call. For the first call, @var{prior-result} is
  8377. the given @var{init} value.
  8378. Calls are made over the table entries in an unspecified order.
  8379. Results will be unpredictable if @var{table} is modified while
  8380. @code{hash-fold} is running.
  8381. For example, the following returns a count of how many keys in
  8382. @code{mytable} are strings.
  8383. @example
  8384. (hash-fold (lambda (key value prior)
  8385. (if (string? key) (1+ prior) prior))
  8386. 0 mytable)
  8387. @end example
  8388. @end deffn
  8389. @deffn {Scheme Procedure} hash-count pred table
  8390. @deffnx {C Function} scm_hash_count (pred, table)
  8391. Return the number of elements in the given hash @var{table} that cause
  8392. @code{(@var{pred} @var{key} @var{value})} to return true. To quickly
  8393. determine the total number of elements, use @code{(const #t)} for
  8394. @var{pred}.
  8395. @end deffn
  8396. @node Other Types
  8397. @subsection Other Types
  8398. Procedures are documented in their own section. @xref{Procedures}.
  8399. Variable objects are documented as part of the description of Guile's
  8400. module system: see @ref{Variables}.
  8401. @xref{Scheduling}, for discussion of threads, mutexes, and so on.
  8402. Ports are described in the section on I/O: see @ref{Input and Output}.
  8403. Regular expressions are described in their own section: see @ref{Regular
  8404. Expressions}.
  8405. There are quite a number of additional data types documented in this
  8406. manual; if you feel a link is missing here, please file a bug.
  8407. @c Local Variables:
  8408. @c TeX-master: "guile.texi"
  8409. @c End: