api-data.texi 370 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294
  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 an error message, as one might
  451. expect. 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 approach 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
  3044. a non-negative exact integer specifying the range of the hash function.
  3045. A positive value restricts the return value to the range [0,bound).
  3046. @end deffn
  3047. @deffn {Scheme Procedure} string-hash-ci s [bound [start [end]]]
  3048. @deffnx {C Function} scm_substring_hash_ci (s, bound, start, end)
  3049. Compute a hash value for @var{s}. The optional argument @var{bound} is
  3050. a non-negative exact integer specifying the range of the hash function.
  3051. A positive value restricts the return value to the range [0,bound).
  3052. @end deffn
  3053. Because the same visual appearance of an abstract Unicode character can
  3054. be obtained via multiple sequences of Unicode characters, even the
  3055. case-insensitive string comparison functions described above may return
  3056. @code{#f} when presented with strings containing different
  3057. representations of the same character. For example, the Unicode
  3058. character ``LATIN SMALL LETTER S WITH DOT BELOW AND DOT ABOVE'' can be
  3059. represented with a single character (U+1E69) or by the character ``LATIN
  3060. SMALL LETTER S'' (U+0073) followed by the combining marks ``COMBINING
  3061. DOT BELOW'' (U+0323) and ``COMBINING DOT ABOVE'' (U+0307).
  3062. For this reason, it is often desirable to ensure that the strings
  3063. to be compared are using a mutually consistent representation for every
  3064. character. The Unicode standard defines two methods of normalizing the
  3065. contents of strings: Decomposition, which breaks composite characters
  3066. into a set of constituent characters with an ordering defined by the
  3067. Unicode Standard; and composition, which performs the converse.
  3068. There are two decomposition operations. ``Canonical decomposition''
  3069. produces character sequences that share the same visual appearance as
  3070. the original characters, while ``compatibility decomposition'' produces
  3071. ones whose visual appearances may differ from the originals but which
  3072. represent the same abstract character.
  3073. These operations are encapsulated in the following set of normalization
  3074. forms:
  3075. @table @dfn
  3076. @item NFD
  3077. Characters are decomposed to their canonical forms.
  3078. @item NFKD
  3079. Characters are decomposed to their compatibility forms.
  3080. @item NFC
  3081. Characters are decomposed to their canonical forms, then composed.
  3082. @item NFKC
  3083. Characters are decomposed to their compatibility forms, then composed.
  3084. @end table
  3085. The functions below put their arguments into one of the forms described
  3086. above.
  3087. @deffn {Scheme Procedure} string-normalize-nfd s
  3088. @deffnx {C Function} scm_string_normalize_nfd (s)
  3089. Return the @code{NFD} normalized form of @var{s}.
  3090. @end deffn
  3091. @deffn {Scheme Procedure} string-normalize-nfkd s
  3092. @deffnx {C Function} scm_string_normalize_nfkd (s)
  3093. Return the @code{NFKD} normalized form of @var{s}.
  3094. @end deffn
  3095. @deffn {Scheme Procedure} string-normalize-nfc s
  3096. @deffnx {C Function} scm_string_normalize_nfc (s)
  3097. Return the @code{NFC} normalized form of @var{s}.
  3098. @end deffn
  3099. @deffn {Scheme Procedure} string-normalize-nfkc s
  3100. @deffnx {C Function} scm_string_normalize_nfkc (s)
  3101. Return the @code{NFKC} normalized form of @var{s}.
  3102. @end deffn
  3103. @node String Searching
  3104. @subsubsection String Searching
  3105. @deffn {Scheme Procedure} string-index s char_pred [start [end]]
  3106. @deffnx {C Function} scm_string_index (s, char_pred, start, end)
  3107. Search through the string @var{s} from left to right, returning
  3108. the index of the first occurrence of a character which
  3109. @itemize @bullet
  3110. @item
  3111. equals @var{char_pred}, if it is character,
  3112. @item
  3113. satisfies the predicate @var{char_pred}, if it is a procedure,
  3114. @item
  3115. is in the set @var{char_pred}, if it is a character set.
  3116. @end itemize
  3117. Return @code{#f} if no match is found.
  3118. @end deffn
  3119. @deffn {Scheme Procedure} string-rindex s char_pred [start [end]]
  3120. @deffnx {C Function} scm_string_rindex (s, char_pred, start, end)
  3121. Search through the string @var{s} from right to left, returning
  3122. the index of the last occurrence of a character which
  3123. @itemize @bullet
  3124. @item
  3125. equals @var{char_pred}, if it is character,
  3126. @item
  3127. satisfies the predicate @var{char_pred}, if it is a procedure,
  3128. @item
  3129. is in the set if @var{char_pred} is a character set.
  3130. @end itemize
  3131. Return @code{#f} if no match is found.
  3132. @end deffn
  3133. @deffn {Scheme Procedure} string-prefix-length s1 s2 [start1 [end1 [start2 [end2]]]]
  3134. @deffnx {C Function} scm_string_prefix_length (s1, s2, start1, end1, start2, end2)
  3135. Return the length of the longest common prefix of the two
  3136. strings.
  3137. @end deffn
  3138. @deffn {Scheme Procedure} string-prefix-length-ci s1 s2 [start1 [end1 [start2 [end2]]]]
  3139. @deffnx {C Function} scm_string_prefix_length_ci (s1, s2, start1, end1, start2, end2)
  3140. Return the length of the longest common prefix of the two
  3141. strings, ignoring character case.
  3142. @end deffn
  3143. @deffn {Scheme Procedure} string-suffix-length s1 s2 [start1 [end1 [start2 [end2]]]]
  3144. @deffnx {C Function} scm_string_suffix_length (s1, s2, start1, end1, start2, end2)
  3145. Return the length of the longest common suffix of the two
  3146. strings.
  3147. @end deffn
  3148. @deffn {Scheme Procedure} string-suffix-length-ci s1 s2 [start1 [end1 [start2 [end2]]]]
  3149. @deffnx {C Function} scm_string_suffix_length_ci (s1, s2, start1, end1, start2, end2)
  3150. Return the length of the longest common suffix of the two
  3151. strings, ignoring character case.
  3152. @end deffn
  3153. @deffn {Scheme Procedure} string-prefix? s1 s2 [start1 [end1 [start2 [end2]]]]
  3154. @deffnx {C Function} scm_string_prefix_p (s1, s2, start1, end1, start2, end2)
  3155. Is @var{s1} a prefix of @var{s2}?
  3156. @end deffn
  3157. @deffn {Scheme Procedure} string-prefix-ci? s1 s2 [start1 [end1 [start2 [end2]]]]
  3158. @deffnx {C Function} scm_string_prefix_ci_p (s1, s2, start1, end1, start2, end2)
  3159. Is @var{s1} a prefix of @var{s2}, ignoring character case?
  3160. @end deffn
  3161. @deffn {Scheme Procedure} string-suffix? s1 s2 [start1 [end1 [start2 [end2]]]]
  3162. @deffnx {C Function} scm_string_suffix_p (s1, s2, start1, end1, start2, end2)
  3163. Is @var{s1} a suffix of @var{s2}?
  3164. @end deffn
  3165. @deffn {Scheme Procedure} string-suffix-ci? s1 s2 [start1 [end1 [start2 [end2]]]]
  3166. @deffnx {C Function} scm_string_suffix_ci_p (s1, s2, start1, end1, start2, end2)
  3167. Is @var{s1} a suffix of @var{s2}, ignoring character case?
  3168. @end deffn
  3169. @deffn {Scheme Procedure} string-index-right s char_pred [start [end]]
  3170. @deffnx {C Function} scm_string_index_right (s, char_pred, start, end)
  3171. Search through the string @var{s} from right to left, returning
  3172. the index of the last occurrence of a character which
  3173. @itemize @bullet
  3174. @item
  3175. equals @var{char_pred}, if it is character,
  3176. @item
  3177. satisfies the predicate @var{char_pred}, if it is a procedure,
  3178. @item
  3179. is in the set if @var{char_pred} is a character set.
  3180. @end itemize
  3181. Return @code{#f} if no match is found.
  3182. @end deffn
  3183. @deffn {Scheme Procedure} string-skip s char_pred [start [end]]
  3184. @deffnx {C Function} scm_string_skip (s, char_pred, start, end)
  3185. Search through the string @var{s} from left to right, returning
  3186. the index of the first occurrence of a character which
  3187. @itemize @bullet
  3188. @item
  3189. does not equal @var{char_pred}, if it is character,
  3190. @item
  3191. does not satisfy the predicate @var{char_pred}, if it is a
  3192. procedure,
  3193. @item
  3194. is not in the set if @var{char_pred} is a character set.
  3195. @end itemize
  3196. @end deffn
  3197. @deffn {Scheme Procedure} string-skip-right s char_pred [start [end]]
  3198. @deffnx {C Function} scm_string_skip_right (s, char_pred, start, end)
  3199. Search through the string @var{s} from right to left, returning
  3200. the index of the last occurrence of a character which
  3201. @itemize @bullet
  3202. @item
  3203. does not equal @var{char_pred}, if it is character,
  3204. @item
  3205. does not satisfy the predicate @var{char_pred}, if it is a
  3206. procedure,
  3207. @item
  3208. is not in the set if @var{char_pred} is a character set.
  3209. @end itemize
  3210. @end deffn
  3211. @deffn {Scheme Procedure} string-count s char_pred [start [end]]
  3212. @deffnx {C Function} scm_string_count (s, char_pred, start, end)
  3213. Return the count of the number of characters in the string
  3214. @var{s} which
  3215. @itemize @bullet
  3216. @item
  3217. equals @var{char_pred}, if it is character,
  3218. @item
  3219. satisfies the predicate @var{char_pred}, if it is a procedure.
  3220. @item
  3221. is in the set @var{char_pred}, if it is a character set.
  3222. @end itemize
  3223. @end deffn
  3224. @deffn {Scheme Procedure} string-contains s1 s2 [start1 [end1 [start2 [end2]]]]
  3225. @deffnx {C Function} scm_string_contains (s1, s2, start1, end1, start2, end2)
  3226. Does string @var{s1} contain string @var{s2}? Return the index
  3227. in @var{s1} where @var{s2} occurs as a substring, or false.
  3228. The optional start/end indices restrict the operation to the
  3229. indicated substrings.
  3230. @end deffn
  3231. @deffn {Scheme Procedure} string-contains-ci s1 s2 [start1 [end1 [start2 [end2]]]]
  3232. @deffnx {C Function} scm_string_contains_ci (s1, s2, start1, end1, start2, end2)
  3233. Does string @var{s1} contain string @var{s2}? Return the index
  3234. in @var{s1} where @var{s2} occurs as a substring, or false.
  3235. The optional start/end indices restrict the operation to the
  3236. indicated substrings. Character comparison is done
  3237. case-insensitively.
  3238. @end deffn
  3239. @node Alphabetic Case Mapping
  3240. @subsubsection Alphabetic Case Mapping
  3241. These are procedures for mapping strings to their upper- or lower-case
  3242. equivalents, respectively, or for capitalizing strings.
  3243. They use the basic case mapping rules for Unicode characters. No
  3244. special language or context rules are considered. The resulting strings
  3245. are guaranteed to be the same length as the input strings.
  3246. @xref{Character Case Mapping, the @code{(ice-9
  3247. i18n)} module}, for locale-dependent case conversions.
  3248. @deffn {Scheme Procedure} string-upcase str [start [end]]
  3249. @deffnx {C Function} scm_substring_upcase (str, start, end)
  3250. @deffnx {C Function} scm_string_upcase (str)
  3251. Upcase every character in @code{str}.
  3252. @end deffn
  3253. @deffn {Scheme Procedure} string-upcase! str [start [end]]
  3254. @deffnx {C Function} scm_substring_upcase_x (str, start, end)
  3255. @deffnx {C Function} scm_string_upcase_x (str)
  3256. Destructively upcase every character in @code{str}.
  3257. @lisp
  3258. (string-upcase! y)
  3259. @result{} "ARRDEFG"
  3260. y
  3261. @result{} "ARRDEFG"
  3262. @end lisp
  3263. @end deffn
  3264. @deffn {Scheme Procedure} string-downcase str [start [end]]
  3265. @deffnx {C Function} scm_substring_downcase (str, start, end)
  3266. @deffnx {C Function} scm_string_downcase (str)
  3267. Downcase every character in @var{str}.
  3268. @end deffn
  3269. @deffn {Scheme Procedure} string-downcase! str [start [end]]
  3270. @deffnx {C Function} scm_substring_downcase_x (str, start, end)
  3271. @deffnx {C Function} scm_string_downcase_x (str)
  3272. Destructively downcase every character in @var{str}.
  3273. @lisp
  3274. y
  3275. @result{} "ARRDEFG"
  3276. (string-downcase! y)
  3277. @result{} "arrdefg"
  3278. y
  3279. @result{} "arrdefg"
  3280. @end lisp
  3281. @end deffn
  3282. @deffn {Scheme Procedure} string-capitalize str
  3283. @deffnx {C Function} scm_string_capitalize (str)
  3284. Return a freshly allocated string with the characters in
  3285. @var{str}, where the first character of every word is
  3286. capitalized.
  3287. @end deffn
  3288. @deffn {Scheme Procedure} string-capitalize! str
  3289. @deffnx {C Function} scm_string_capitalize_x (str)
  3290. Upcase the first character of every word in @var{str}
  3291. destructively and return @var{str}.
  3292. @lisp
  3293. y @result{} "hello world"
  3294. (string-capitalize! y) @result{} "Hello World"
  3295. y @result{} "Hello World"
  3296. @end lisp
  3297. @end deffn
  3298. @deffn {Scheme Procedure} string-titlecase str [start [end]]
  3299. @deffnx {C Function} scm_string_titlecase (str, start, end)
  3300. Titlecase every first character in a word in @var{str}.
  3301. @end deffn
  3302. @deffn {Scheme Procedure} string-titlecase! str [start [end]]
  3303. @deffnx {C Function} scm_string_titlecase_x (str, start, end)
  3304. Destructively titlecase every first character in a word in
  3305. @var{str}.
  3306. @end deffn
  3307. @node Reversing and Appending Strings
  3308. @subsubsection Reversing and Appending Strings
  3309. @deffn {Scheme Procedure} string-reverse str [start [end]]
  3310. @deffnx {C Function} scm_string_reverse (str, start, end)
  3311. Reverse the string @var{str}. The optional arguments
  3312. @var{start} and @var{end} delimit the region of @var{str} to
  3313. operate on.
  3314. @end deffn
  3315. @deffn {Scheme Procedure} string-reverse! str [start [end]]
  3316. @deffnx {C Function} scm_string_reverse_x (str, start, end)
  3317. Reverse the string @var{str} in-place. The optional arguments
  3318. @var{start} and @var{end} delimit the region of @var{str} to
  3319. operate on. The return value is unspecified.
  3320. @end deffn
  3321. @rnindex string-append
  3322. @deffn {Scheme Procedure} string-append arg @dots{}
  3323. @deffnx {C Function} scm_string_append (args)
  3324. Return a newly allocated string whose characters form the
  3325. concatenation of the given strings, @var{arg} @enddots{}.
  3326. @example
  3327. (let ((h "hello "))
  3328. (string-append h "world"))
  3329. @result{} "hello world"
  3330. @end example
  3331. @end deffn
  3332. @deffn {Scheme Procedure} string-append/shared arg @dots{}
  3333. @deffnx {C Function} scm_string_append_shared (args)
  3334. Like @code{string-append}, but the result may share memory
  3335. with the argument strings.
  3336. @end deffn
  3337. @deffn {Scheme Procedure} string-concatenate ls
  3338. @deffnx {C Function} scm_string_concatenate (ls)
  3339. Append the elements (which must be strings) of @var{ls} together into a
  3340. single string. Guaranteed to return a freshly allocated string.
  3341. @end deffn
  3342. @deffn {Scheme Procedure} string-concatenate-reverse ls [final_string [end]]
  3343. @deffnx {C Function} scm_string_concatenate_reverse (ls, final_string, end)
  3344. Without optional arguments, this procedure is equivalent to
  3345. @lisp
  3346. (string-concatenate (reverse ls))
  3347. @end lisp
  3348. If the optional argument @var{final_string} is specified, it is
  3349. consed onto the beginning to @var{ls} before performing the
  3350. list-reverse and string-concatenate operations. If @var{end}
  3351. is given, only the characters of @var{final_string} up to index
  3352. @var{end} are used.
  3353. Guaranteed to return a freshly allocated string.
  3354. @end deffn
  3355. @deffn {Scheme Procedure} string-concatenate/shared ls
  3356. @deffnx {C Function} scm_string_concatenate_shared (ls)
  3357. Like @code{string-concatenate}, but the result may share memory
  3358. with the strings in the list @var{ls}.
  3359. @end deffn
  3360. @deffn {Scheme Procedure} string-concatenate-reverse/shared ls [final_string [end]]
  3361. @deffnx {C Function} scm_string_concatenate_reverse_shared (ls, final_string, end)
  3362. Like @code{string-concatenate-reverse}, but the result may
  3363. share memory with the strings in the @var{ls} arguments.
  3364. @end deffn
  3365. @node Mapping Folding and Unfolding
  3366. @subsubsection Mapping, Folding, and Unfolding
  3367. @deffn {Scheme Procedure} string-map proc s [start [end]]
  3368. @deffnx {C Function} scm_string_map (proc, s, start, end)
  3369. @var{proc} is a char->char procedure, it is mapped over
  3370. @var{s}. The order in which the procedure is applied to the
  3371. string elements is not specified.
  3372. @end deffn
  3373. @deffn {Scheme Procedure} string-map! proc s [start [end]]
  3374. @deffnx {C Function} scm_string_map_x (proc, s, start, end)
  3375. @var{proc} is a char->char procedure, it is mapped over
  3376. @var{s}. The order in which the procedure is applied to the
  3377. string elements is not specified. The string @var{s} is
  3378. modified in-place, the return value is not specified.
  3379. @end deffn
  3380. @deffn {Scheme Procedure} string-for-each proc s [start [end]]
  3381. @deffnx {C Function} scm_string_for_each (proc, s, start, end)
  3382. @var{proc} is mapped over @var{s} in left-to-right order. The
  3383. return value is not specified.
  3384. @end deffn
  3385. @deffn {Scheme Procedure} string-for-each-index proc s [start [end]]
  3386. @deffnx {C Function} scm_string_for_each_index (proc, s, start, end)
  3387. Call @code{(@var{proc} i)} for each index i in @var{s}, from left to
  3388. right.
  3389. For example, to change characters to alternately upper and lower case,
  3390. @example
  3391. (define str (string-copy "studly"))
  3392. (string-for-each-index
  3393. (lambda (i)
  3394. (string-set! str i
  3395. ((if (even? i) char-upcase char-downcase)
  3396. (string-ref str i))))
  3397. str)
  3398. str @result{} "StUdLy"
  3399. @end example
  3400. @end deffn
  3401. @deffn {Scheme Procedure} string-fold kons knil s [start [end]]
  3402. @deffnx {C Function} scm_string_fold (kons, knil, s, start, end)
  3403. Fold @var{kons} over the characters of @var{s}, with @var{knil}
  3404. as the terminating element, from left to right. @var{kons}
  3405. must expect two arguments: The actual character and the last
  3406. result of @var{kons}' application.
  3407. @end deffn
  3408. @deffn {Scheme Procedure} string-fold-right kons knil s [start [end]]
  3409. @deffnx {C Function} scm_string_fold_right (kons, knil, s, start, end)
  3410. Fold @var{kons} over the characters of @var{s}, with @var{knil}
  3411. as the terminating element, from right to left. @var{kons}
  3412. must expect two arguments: The actual character and the last
  3413. result of @var{kons}' application.
  3414. @end deffn
  3415. @deffn {Scheme Procedure} string-unfold p f g seed [base [make_final]]
  3416. @deffnx {C Function} scm_string_unfold (p, f, g, seed, base, make_final)
  3417. @itemize @bullet
  3418. @item @var{g} is used to generate a series of @emph{seed}
  3419. values from the initial @var{seed}: @var{seed}, (@var{g}
  3420. @var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),
  3421. @dots{}
  3422. @item @var{p} tells us when to stop -- when it returns true
  3423. when applied to one of these seed values.
  3424. @item @var{f} maps each seed value to the corresponding
  3425. character in the result string. These chars are assembled
  3426. into the string in a left-to-right order.
  3427. @item @var{base} is the optional initial/leftmost portion
  3428. of the constructed string; it default to the empty
  3429. string.
  3430. @item @var{make_final} is applied to the terminal seed
  3431. value (on which @var{p} returns true) to produce
  3432. the final/rightmost portion of the constructed string.
  3433. The default is nothing extra.
  3434. @end itemize
  3435. @end deffn
  3436. @deffn {Scheme Procedure} string-unfold-right p f g seed [base [make_final]]
  3437. @deffnx {C Function} scm_string_unfold_right (p, f, g, seed, base, make_final)
  3438. @itemize @bullet
  3439. @item @var{g} is used to generate a series of @emph{seed}
  3440. values from the initial @var{seed}: @var{seed}, (@var{g}
  3441. @var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),
  3442. @dots{}
  3443. @item @var{p} tells us when to stop -- when it returns true
  3444. when applied to one of these seed values.
  3445. @item @var{f} maps each seed value to the corresponding
  3446. character in the result string. These chars are assembled
  3447. into the string in a right-to-left order.
  3448. @item @var{base} is the optional initial/rightmost portion
  3449. of the constructed string; it default to the empty
  3450. string.
  3451. @item @var{make_final} is applied to the terminal seed
  3452. value (on which @var{p} returns true) to produce
  3453. the final/leftmost portion of the constructed string.
  3454. It defaults to @code{(lambda (x) )}.
  3455. @end itemize
  3456. @end deffn
  3457. @node Miscellaneous String Operations
  3458. @subsubsection Miscellaneous String Operations
  3459. @deffn {Scheme Procedure} xsubstring s from [to [start [end]]]
  3460. @deffnx {C Function} scm_xsubstring (s, from, to, start, end)
  3461. This is the @emph{extended substring} procedure that implements
  3462. replicated copying of a substring of some string.
  3463. @var{s} is a string, @var{start} and @var{end} are optional
  3464. arguments that demarcate a substring of @var{s}, defaulting to
  3465. 0 and the length of @var{s}. Replicate this substring up and
  3466. down index space, in both the positive and negative directions.
  3467. @code{xsubstring} returns the substring of this string
  3468. beginning at index @var{from}, and ending at @var{to}, which
  3469. defaults to @var{from} + (@var{end} - @var{start}).
  3470. @end deffn
  3471. @deffn {Scheme Procedure} string-xcopy! target tstart s sfrom [sto [start [end]]]
  3472. @deffnx {C Function} scm_string_xcopy_x (target, tstart, s, sfrom, sto, start, end)
  3473. Exactly the same as @code{xsubstring}, but the extracted text
  3474. is written into the string @var{target} starting at index
  3475. @var{tstart}. The operation is not defined if @code{(eq?
  3476. @var{target} @var{s})} or these arguments share storage -- you
  3477. cannot copy a string on top of itself.
  3478. @end deffn
  3479. @deffn {Scheme Procedure} string-replace s1 s2 [start1 [end1 [start2 [end2]]]]
  3480. @deffnx {C Function} scm_string_replace (s1, s2, start1, end1, start2, end2)
  3481. Return the string @var{s1}, but with the characters
  3482. @var{start1} @dots{} @var{end1} replaced by the characters
  3483. @var{start2} @dots{} @var{end2} from @var{s2}.
  3484. @end deffn
  3485. @deffn {Scheme Procedure} string-tokenize s [token_set [start [end]]]
  3486. @deffnx {C Function} scm_string_tokenize (s, token_set, start, end)
  3487. Split the string @var{s} into a list of substrings, where each
  3488. substring is a maximal non-empty contiguous sequence of
  3489. characters from the character set @var{token_set}, which
  3490. defaults to @code{char-set:graphic}.
  3491. If @var{start} or @var{end} indices are provided, they restrict
  3492. @code{string-tokenize} to operating on the indicated substring
  3493. of @var{s}.
  3494. @end deffn
  3495. @deffn {Scheme Procedure} string-filter char_pred s [start [end]]
  3496. @deffnx {C Function} scm_string_filter (char_pred, s, start, end)
  3497. Filter the string @var{s}, retaining only those characters which
  3498. satisfy @var{char_pred}.
  3499. If @var{char_pred} is a procedure, it is applied to each character as
  3500. a predicate, if it is a character, it is tested for equality and if it
  3501. is a character set, it is tested for membership.
  3502. @end deffn
  3503. @deffn {Scheme Procedure} string-delete char_pred s [start [end]]
  3504. @deffnx {C Function} scm_string_delete (char_pred, s, start, end)
  3505. Delete characters satisfying @var{char_pred} from @var{s}.
  3506. If @var{char_pred} is a procedure, it is applied to each character as
  3507. a predicate, if it is a character, it is tested for equality and if it
  3508. is a character set, it is tested for membership.
  3509. @end deffn
  3510. The following additional functions are available in the module
  3511. @code{(ice-9 string-fun)}. They can be used with:
  3512. @example
  3513. (use-modules (ice-9 string-fun))
  3514. @end example
  3515. @deffn {Scheme Procedure} string-replace-substring str substring replacement
  3516. Return a new string where every instance of @var{substring} in string
  3517. @var{str} has been replaced by @var{replacement}. For example:
  3518. @lisp
  3519. (string-replace-substring "a ring of strings" "ring" "rut")
  3520. @result{} "a rut of struts"
  3521. @end lisp
  3522. @end deffn
  3523. @node Representing Strings as Bytes
  3524. @subsubsection Representing Strings as Bytes
  3525. In the cold world outside of Guile, not all strings are treated in
  3526. the same way. Out there there are only bytes, and there are many ways
  3527. of representing a strings (sequences of characters) as binary data
  3528. (sequences of bytes).
  3529. As a user, usually you don't have to think about this very much. When
  3530. you type on your keyboard, your system encodes your keystrokes as bytes
  3531. according to the locale that you have configured on your computer.
  3532. Guile uses the locale to decode those bytes back into characters --
  3533. hopefully the same characters that you typed in.
  3534. All is not so clear when dealing with a system with multiple users, such
  3535. as a web server. Your web server might get a request from one user for
  3536. data encoded in the ISO-8859-1 character set, and then another request
  3537. from a different user for UTF-8 data.
  3538. @cindex iconv
  3539. @cindex character encoding
  3540. Guile provides an @dfn{iconv} module for converting between strings and
  3541. sequences of bytes. @xref{Bytevectors}, for more on how Guile
  3542. represents raw byte sequences. This module gets its name from the
  3543. common @sc{unix} command of the same name.
  3544. Note that often it is sufficient to just read and write strings from
  3545. ports instead of using these functions. To do this, specify the port
  3546. encoding using @code{set-port-encoding!}. @xref{Ports}, for more on
  3547. ports and character encodings.
  3548. Unlike the rest of the procedures in this section, you have to load the
  3549. @code{iconv} module before having access to these procedures:
  3550. @example
  3551. (use-modules (ice-9 iconv))
  3552. @end example
  3553. @deffn {Scheme Procedure} string->bytevector string encoding [conversion-strategy]
  3554. Encode @var{string} as a sequence of bytes.
  3555. The string will be encoded in the character set specified by the
  3556. @var{encoding} string. If the string has characters that cannot be
  3557. represented in the encoding, by default this procedure raises an
  3558. @code{encoding-error}. Pass a @var{conversion-strategy} argument to
  3559. specify other behaviors.
  3560. The return value is a bytevector. @xref{Bytevectors}, for more on
  3561. bytevectors. @xref{Ports}, for more on character encodings and
  3562. conversion strategies.
  3563. @end deffn
  3564. @deffn {Scheme Procedure} bytevector->string bytevector encoding [conversion-strategy]
  3565. Decode @var{bytevector} into a string.
  3566. The bytes will be decoded from the character set by the @var{encoding}
  3567. string. If the bytes do not form a valid encoding, by default this
  3568. procedure raises an @code{decoding-error}. As with
  3569. @code{string->bytevector}, pass the optional @var{conversion-strategy}
  3570. argument to modify this behavior. @xref{Ports}, for more on character
  3571. encodings and conversion strategies.
  3572. @end deffn
  3573. @deffn {Scheme Procedure} call-with-output-encoded-string encoding proc [conversion-strategy]
  3574. Like @code{call-with-output-string}, but instead of returning a string,
  3575. returns a encoding of the string according to @var{encoding}, as a
  3576. bytevector. This procedure can be more efficient than collecting a
  3577. string and then converting it via @code{string->bytevector}.
  3578. @end deffn
  3579. @node Conversion to/from C
  3580. @subsubsection Conversion to/from C
  3581. When creating a Scheme string from a C string or when converting a
  3582. Scheme string to a C string, the concept of character encoding becomes
  3583. important.
  3584. In C, a string is just a sequence of bytes, and the character encoding
  3585. describes the relation between these bytes and the actual characters
  3586. that make up the string. For Scheme strings, character encoding is not
  3587. an issue (most of the time), since in Scheme you usually treat strings
  3588. as character sequences, not byte sequences.
  3589. Converting to C and converting from C each have their own challenges.
  3590. When converting from C to Scheme, it is important that the sequence of
  3591. bytes in the C string be valid with respect to its encoding. ASCII
  3592. strings, for example, can't have any bytes greater than 127. An ASCII
  3593. byte greater than 127 is considered @emph{ill-formed} and cannot be
  3594. converted into a Scheme character.
  3595. Problems can occur in the reverse operation as well. Not all character
  3596. encodings can hold all possible Scheme characters. Some encodings, like
  3597. ASCII for example, can only describe a small subset of all possible
  3598. characters. So, when converting to C, one must first decide what to do
  3599. with Scheme characters that can't be represented in the C string.
  3600. Converting a Scheme string to a C string will often allocate fresh
  3601. memory to hold the result. You must take care that this memory is
  3602. properly freed eventually. In many cases, this can be achieved by
  3603. using @code{scm_dynwind_free} inside an appropriate dynwind context,
  3604. @xref{Dynamic Wind}.
  3605. @deftypefn {C Function} SCM scm_from_locale_string (const char *str)
  3606. @deftypefnx {C Function} SCM scm_from_locale_stringn (const char *str, size_t len)
  3607. Creates a new Scheme string that has the same contents as @var{str} when
  3608. interpreted in the character encoding of the current locale.
  3609. For @code{scm_from_locale_string}, @var{str} must be null-terminated.
  3610. For @code{scm_from_locale_stringn}, @var{len} specifies the length of
  3611. @var{str} in bytes, and @var{str} does not need to be null-terminated.
  3612. If @var{len} is @code{(size_t)-1}, then @var{str} does need to be
  3613. null-terminated and the real length will be found with @code{strlen}.
  3614. If the C string is ill-formed, an error will be raised.
  3615. Note that these functions should @emph{not} be used to convert C string
  3616. constants, because there is no guarantee that the current locale will
  3617. match that of the execution character set, used for string and character
  3618. constants. Most modern C compilers use UTF-8 by default, so to convert
  3619. C string constants we recommend @code{scm_from_utf8_string}.
  3620. @end deftypefn
  3621. @deftypefn {C Function} SCM scm_take_locale_string (char *str)
  3622. @deftypefnx {C Function} SCM scm_take_locale_stringn (char *str, size_t len)
  3623. Like @code{scm_from_locale_string} and @code{scm_from_locale_stringn},
  3624. respectively, but also frees @var{str} with @code{free} eventually.
  3625. Thus, you can use this function when you would free @var{str} anyway
  3626. immediately after creating the Scheme string. In certain cases, Guile
  3627. can then use @var{str} directly as its internal representation.
  3628. @end deftypefn
  3629. @deftypefn {C Function} {char *} scm_to_locale_string (SCM str)
  3630. @deftypefnx {C Function} {char *} scm_to_locale_stringn (SCM str, size_t *lenp)
  3631. Returns a C string with the same contents as @var{str} in the character
  3632. encoding of the current locale. The C string must be freed with
  3633. @code{free} eventually, maybe by using @code{scm_dynwind_free},
  3634. @xref{Dynamic Wind}.
  3635. For @code{scm_to_locale_string}, the returned string is
  3636. null-terminated and an error is signaled when @var{str} contains
  3637. @code{#\nul} characters.
  3638. For @code{scm_to_locale_stringn} and @var{lenp} not @code{NULL},
  3639. @var{str} might contain @code{#\nul} characters and the length of the
  3640. returned string in bytes is stored in @code{*@var{lenp}}. The
  3641. returned string will not be null-terminated in this case. If
  3642. @var{lenp} is @code{NULL}, @code{scm_to_locale_stringn} behaves like
  3643. @code{scm_to_locale_string}.
  3644. If a character in @var{str} cannot be represented in the character
  3645. encoding of the current locale, the default port conversion strategy is
  3646. used. @xref{Ports}, for more on conversion strategies.
  3647. If the conversion strategy is @code{error}, an error will be raised. If
  3648. it is @code{substitute}, a replacement character, such as a question
  3649. mark, will be inserted in its place. If it is @code{escape}, a hex
  3650. escape will be inserted in its place.
  3651. @end deftypefn
  3652. @deftypefn {C Function} size_t scm_to_locale_stringbuf (SCM str, char *buf, size_t max_len)
  3653. Puts @var{str} as a C string in the current locale encoding into the
  3654. memory pointed to by @var{buf}. The buffer at @var{buf} has room for
  3655. @var{max_len} bytes and @code{scm_to_local_stringbuf} will never store
  3656. more than that. No terminating @code{'\0'} will be stored.
  3657. The return value of @code{scm_to_locale_stringbuf} is the number of
  3658. bytes that are needed for all of @var{str}, regardless of whether
  3659. @var{buf} was large enough to hold them. Thus, when the return value
  3660. is larger than @var{max_len}, only @var{max_len} bytes have been
  3661. stored and you probably need to try again with a larger buffer.
  3662. @end deftypefn
  3663. For most situations, string conversion should occur using the current
  3664. locale, such as with the functions above. But there may be cases where
  3665. one wants to convert strings from a character encoding other than the
  3666. locale's character encoding. For these cases, the lower-level functions
  3667. @code{scm_to_stringn} and @code{scm_from_stringn} are provided. These
  3668. functions should seldom be necessary if one is properly using locales.
  3669. @deftp {C Type} scm_t_string_failed_conversion_handler
  3670. This is an enumerated type that can take one of three values:
  3671. @code{SCM_FAILED_CONVERSION_ERROR},
  3672. @code{SCM_FAILED_CONVERSION_QUESTION_MARK}, and
  3673. @code{SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE}. They are used to indicate
  3674. a strategy for handling characters that cannot be converted to or from a
  3675. given character encoding. @code{SCM_FAILED_CONVERSION_ERROR} indicates
  3676. that a conversion should throw an error if some characters cannot be
  3677. converted. @code{SCM_FAILED_CONVERSION_QUESTION_MARK} indicates that a
  3678. conversion should replace unconvertable characters with the question
  3679. mark character. And, @code{SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE}
  3680. requests that a conversion should replace an unconvertable character
  3681. with an escape sequence.
  3682. While all three strategies apply when converting Scheme strings to C,
  3683. only @code{SCM_FAILED_CONVERSION_ERROR} and
  3684. @code{SCM_FAILED_CONVERSION_QUESTION_MARK} can be used when converting C
  3685. strings to Scheme.
  3686. @end deftp
  3687. @deftypefn {C Function} char *scm_to_stringn (SCM str, size_t *lenp, const char *encoding, scm_t_string_failed_conversion_handler handler)
  3688. This function returns a newly allocated C string from the Guile string
  3689. @var{str}. The length of the returned string in bytes will be returned in
  3690. @var{lenp}. The character encoding of the C string is passed as the ASCII,
  3691. null-terminated C string @var{encoding}. The @var{handler} parameter
  3692. gives a strategy for dealing with characters that cannot be converted
  3693. into @var{encoding}.
  3694. If @var{lenp} is @code{NULL}, this function will return a null-terminated C
  3695. string. It will throw an error if the string contains a null
  3696. character.
  3697. The Scheme interface to this function is @code{string->bytevector}, from the
  3698. @code{ice-9 iconv} module. @xref{Representing Strings as Bytes}.
  3699. @end deftypefn
  3700. @deftypefn {C Function} SCM scm_from_stringn (const char *str, size_t len, const char *encoding, scm_t_string_failed_conversion_handler handler)
  3701. This function returns a scheme string from the C string @var{str}. The
  3702. length in bytes of the C string is input as @var{len}. The encoding of the C
  3703. string is passed as the ASCII, null-terminated C string @code{encoding}.
  3704. The @var{handler} parameters suggests a strategy for dealing with
  3705. unconvertable characters.
  3706. The Scheme interface to this function is @code{bytevector->string}.
  3707. @xref{Representing Strings as Bytes}.
  3708. @end deftypefn
  3709. The following conversion functions are provided as a convenience for the
  3710. most commonly used encodings.
  3711. @deftypefn {C Function} SCM scm_from_latin1_string (const char *str)
  3712. @deftypefnx {C Function} SCM scm_from_utf8_string (const char *str)
  3713. @deftypefnx {C Function} SCM scm_from_utf32_string (const scm_t_wchar *str)
  3714. Return a scheme string from the null-terminated C string @var{str},
  3715. which is ISO-8859-1-, UTF-8-, or UTF-32-encoded. These functions should
  3716. be used to convert hard-coded C string constants into Scheme strings.
  3717. @end deftypefn
  3718. @deftypefn {C Function} SCM scm_from_latin1_stringn (const char *str, size_t len)
  3719. @deftypefnx {C Function} SCM scm_from_utf8_stringn (const char *str, size_t len)
  3720. @deftypefnx {C Function} SCM scm_from_utf32_stringn (const scm_t_wchar *str, size_t len)
  3721. Return a scheme string from C string @var{str}, which is ISO-8859-1-,
  3722. UTF-8-, or UTF-32-encoded, of length @var{len}. @var{len} is the number
  3723. of bytes pointed to by @var{str} for @code{scm_from_latin1_stringn} and
  3724. @code{scm_from_utf8_stringn}; it is the number of elements (code points)
  3725. in @var{str} in the case of @code{scm_from_utf32_stringn}.
  3726. @end deftypefn
  3727. @deftypefn {C function} char *scm_to_latin1_stringn (SCM str, size_t *lenp)
  3728. @deftypefnx {C function} char *scm_to_utf8_stringn (SCM str, size_t *lenp)
  3729. @deftypefnx {C function} scm_t_wchar *scm_to_utf32_stringn (SCM str, size_t *lenp)
  3730. Return a newly allocated, ISO-8859-1-, UTF-8-, or UTF-32-encoded C string
  3731. from Scheme string @var{str}. An error is thrown when @var{str}
  3732. cannot be converted to the specified encoding. If @var{lenp} is
  3733. @code{NULL}, the returned C string will be null terminated, and an error
  3734. will be thrown if the C string would otherwise contain null
  3735. characters. If @var{lenp} is not @code{NULL}, the string is not null terminated,
  3736. and the length of the returned string is returned in @var{lenp}. The length
  3737. returned is the number of bytes for @code{scm_to_latin1_stringn} and
  3738. @code{scm_to_utf8_stringn}; it is the number of elements (code points)
  3739. for @code{scm_to_utf32_stringn}.
  3740. @end deftypefn
  3741. It is not often the case, but sometimes when you are dealing with the
  3742. implementation details of a port, you need to encode and decode strings
  3743. according to the encoding and conversion strategy of the port. There
  3744. are some convenience functions for that purpose as well.
  3745. @deftypefn {C Function} SCM scm_from_port_string (const char *str, SCM port)
  3746. @deftypefnx {C Function} SCM scm_from_port_stringn (const char *str, size_t len, SCM port)
  3747. @deftypefnx {C Function} char* scm_to_port_string (SCM str, SCM port)
  3748. @deftypefnx {C Function} char* scm_to_port_stringn (SCM str, size_t *lenp, SCM port)
  3749. Like @code{scm_from_stringn} and friends, except they take their
  3750. encoding and conversion strategy from a given port object.
  3751. @end deftypefn
  3752. @node String Internals
  3753. @subsubsection String Internals
  3754. Guile stores each string in memory as a contiguous array of Unicode code
  3755. points along with an associated set of attributes. If all of the code
  3756. points of a string have an integer range between 0 and 255 inclusive,
  3757. the code point array is stored as one byte per code point: it is stored
  3758. as an ISO-8859-1 (aka Latin-1) string. If any of the code points of the
  3759. string has an integer value greater that 255, the code point array is
  3760. stored as four bytes per code point: it is stored as a UTF-32 string.
  3761. Conversion between the one-byte-per-code-point and
  3762. four-bytes-per-code-point representations happens automatically as
  3763. necessary.
  3764. No API is provided to set the internal representation of strings;
  3765. however, there are pair of procedures available to query it. These are
  3766. debugging procedures. Using them in production code is discouraged,
  3767. since the details of Guile's internal representation of strings may
  3768. change from release to release.
  3769. @deffn {Scheme Procedure} string-bytes-per-char str
  3770. @deffnx {C Function} scm_string_bytes_per_char (str)
  3771. Return the number of bytes used to encode a Unicode code point in string
  3772. @var{str}. The result is one or four.
  3773. @end deffn
  3774. @deffn {Scheme Procedure} %string-dump str
  3775. @deffnx {C Function} scm_sys_string_dump (str)
  3776. Returns an association list containing debugging information for
  3777. @var{str}. The association list has the following entries.
  3778. @table @code
  3779. @item string
  3780. The string itself.
  3781. @item start
  3782. The start index of the string into its stringbuf
  3783. @item length
  3784. The length of the string
  3785. @item shared
  3786. If this string is a substring, it returns its
  3787. parent string. Otherwise, it returns @code{#f}
  3788. @item read-only
  3789. @code{#t} if the string is read-only
  3790. @item stringbuf-chars
  3791. A new string containing this string's stringbuf's characters
  3792. @item stringbuf-length
  3793. The number of characters in this stringbuf
  3794. @item stringbuf-shared
  3795. @code{#t} if this stringbuf is shared
  3796. @item stringbuf-wide
  3797. @code{#t} if this stringbuf's characters are stored in a 32-bit buffer,
  3798. or @code{#f} if they are stored in an 8-bit buffer
  3799. @end table
  3800. @end deffn
  3801. @node Symbols
  3802. @subsection Symbols
  3803. @tpindex Symbols
  3804. Symbols in Scheme are widely used in three ways: as items of discrete
  3805. data, as lookup keys for alists and hash tables, and to denote variable
  3806. references.
  3807. A @dfn{symbol} is similar to a string in that it is defined by a
  3808. sequence of characters. The sequence of characters is known as the
  3809. symbol's @dfn{name}. In the usual case --- that is, where the symbol's
  3810. name doesn't include any characters that could be confused with other
  3811. elements of Scheme syntax --- a symbol is written in a Scheme program by
  3812. writing the sequence of characters that make up the name, @emph{without}
  3813. any quotation marks or other special syntax. For example, the symbol
  3814. whose name is ``multiply-by-2'' is written, simply:
  3815. @lisp
  3816. multiply-by-2
  3817. @end lisp
  3818. Notice how this differs from a @emph{string} with contents
  3819. ``multiply-by-2'', which is written with double quotation marks, like
  3820. this:
  3821. @lisp
  3822. "multiply-by-2"
  3823. @end lisp
  3824. Looking beyond how they are written, symbols are different from strings
  3825. in two important respects.
  3826. The first important difference is uniqueness. If the same-looking
  3827. string is read twice from two different places in a program, the result
  3828. is two @emph{different} string objects whose contents just happen to be
  3829. the same. If, on the other hand, the same-looking symbol is read twice
  3830. from two different places in a program, the result is the @emph{same}
  3831. symbol object both times.
  3832. Given two read symbols, you can use @code{eq?} to test whether they are
  3833. the same (that is, have the same name). @code{eq?} is the most
  3834. efficient comparison operator in Scheme, and comparing two symbols like
  3835. this is as fast as comparing, for example, two numbers. Given two
  3836. strings, on the other hand, you must use @code{equal?} or
  3837. @code{string=?}, which are much slower comparison operators, to
  3838. determine whether the strings have the same contents.
  3839. @lisp
  3840. (define sym1 (quote hello))
  3841. (define sym2 (quote hello))
  3842. (eq? sym1 sym2) @result{} #t
  3843. (define str1 "hello")
  3844. (define str2 "hello")
  3845. (eq? str1 str2) @result{} #f
  3846. (equal? str1 str2) @result{} #t
  3847. @end lisp
  3848. The second important difference is that symbols, unlike strings, are not
  3849. self-evaluating. This is why we need the @code{(quote @dots{})}s in the
  3850. example above: @code{(quote hello)} evaluates to the symbol named
  3851. "hello" itself, whereas an unquoted @code{hello} is @emph{read} as the
  3852. symbol named "hello" and evaluated as a variable reference @dots{} about
  3853. which more below (@pxref{Symbol Variables}).
  3854. @menu
  3855. * Symbol Data:: Symbols as discrete data.
  3856. * Symbol Keys:: Symbols as lookup keys.
  3857. * Symbol Variables:: Symbols as denoting variables.
  3858. * Symbol Primitives:: Operations related to symbols.
  3859. * Symbol Read Syntax:: Extended read syntax for symbols.
  3860. * Symbol Uninterned:: Uninterned symbols.
  3861. @end menu
  3862. @node Symbol Data
  3863. @subsubsection Symbols as Discrete Data
  3864. Numbers and symbols are similar to the extent that they both lend
  3865. themselves to @code{eq?} comparison. But symbols are more descriptive
  3866. than numbers, because a symbol's name can be used directly to describe
  3867. the concept for which that symbol stands.
  3868. For example, imagine that you need to represent some colors in a
  3869. computer program. Using numbers, you would have to choose arbitrarily
  3870. some mapping between numbers and colors, and then take care to use that
  3871. mapping consistently:
  3872. @lisp
  3873. ;; 1=red, 2=green, 3=purple
  3874. (if (eq? (color-of vehicle) 1)
  3875. ...)
  3876. @end lisp
  3877. @noindent
  3878. You can make the mapping more explicit and the code more readable by
  3879. defining constants:
  3880. @lisp
  3881. (define red 1)
  3882. (define green 2)
  3883. (define purple 3)
  3884. (if (eq? (color-of vehicle) red)
  3885. ...)
  3886. @end lisp
  3887. @noindent
  3888. But the simplest and clearest approach is not to use numbers at all, but
  3889. symbols whose names specify the colors that they refer to:
  3890. @lisp
  3891. (if (eq? (color-of vehicle) 'red)
  3892. ...)
  3893. @end lisp
  3894. The descriptive advantages of symbols over numbers increase as the set
  3895. of concepts that you want to describe grows. Suppose that a car object
  3896. can have other properties as well, such as whether it has or uses:
  3897. @itemize @bullet
  3898. @item
  3899. automatic or manual transmission
  3900. @item
  3901. leaded or unleaded fuel
  3902. @item
  3903. power steering (or not).
  3904. @end itemize
  3905. @noindent
  3906. Then a car's combined property set could be naturally represented and
  3907. manipulated as a list of symbols:
  3908. @lisp
  3909. (properties-of vehicle1)
  3910. @result{}
  3911. (red manual unleaded power-steering)
  3912. (if (memq 'power-steering (properties-of vehicle1))
  3913. (display "Unfit people can drive this vehicle.\n")
  3914. (display "You'll need strong arms to drive this vehicle!\n"))
  3915. @print{}
  3916. Unfit people can drive this vehicle.
  3917. @end lisp
  3918. Remember, the fundamental property of symbols that we are relying on
  3919. here is that an occurrence of @code{'red} in one part of a program is an
  3920. @emph{indistinguishable} symbol from an occurrence of @code{'red} in
  3921. another part of a program; this means that symbols can usefully be
  3922. compared using @code{eq?}. At the same time, symbols have naturally
  3923. descriptive names. This combination of efficiency and descriptive power
  3924. makes them ideal for use as discrete data.
  3925. @node Symbol Keys
  3926. @subsubsection Symbols as Lookup Keys
  3927. Given their efficiency and descriptive power, it is natural to use
  3928. symbols as the keys in an association list or hash table.
  3929. To illustrate this, consider a more structured representation of the car
  3930. properties example from the preceding subsection. Rather than
  3931. mixing all the properties up together in a flat list, we could use an
  3932. association list like this:
  3933. @lisp
  3934. (define car1-properties '((color . red)
  3935. (transmission . manual)
  3936. (fuel . unleaded)
  3937. (steering . power-assisted)))
  3938. @end lisp
  3939. Notice how this structure is more explicit and extensible than the flat
  3940. list. For example it makes clear that @code{manual} refers to the
  3941. transmission rather than, say, the windows or the locking of the car.
  3942. It also allows further properties to use the same symbols among their
  3943. possible values without becoming ambiguous:
  3944. @lisp
  3945. (define car1-properties '((color . red)
  3946. (transmission . manual)
  3947. (fuel . unleaded)
  3948. (steering . power-assisted)
  3949. (seat-color . red)
  3950. (locking . manual)))
  3951. @end lisp
  3952. With a representation like this, it is easy to use the efficient
  3953. @code{assq-XXX} family of procedures (@pxref{Association Lists}) to
  3954. extract or change individual pieces of information:
  3955. @lisp
  3956. (assq-ref car1-properties 'fuel) @result{} unleaded
  3957. (assq-ref car1-properties 'transmission) @result{} manual
  3958. (assq-set! car1-properties 'seat-color 'black)
  3959. @result{}
  3960. ((color . red)
  3961. (transmission . manual)
  3962. (fuel . unleaded)
  3963. (steering . power-assisted)
  3964. (seat-color . black)
  3965. (locking . manual)))
  3966. @end lisp
  3967. Hash tables also have keys, and exactly the same arguments apply to the
  3968. use of symbols in hash tables as in association lists. The hash value
  3969. that Guile uses to decide where to add a symbol-keyed entry to a hash
  3970. table can be obtained by calling the @code{symbol-hash} procedure:
  3971. @deffn {Scheme Procedure} symbol-hash symbol
  3972. @deffnx {C Function} scm_symbol_hash (symbol)
  3973. Return a hash value for @var{symbol}.
  3974. @end deffn
  3975. See @ref{Hash Tables} for information about hash tables in general, and
  3976. for why you might choose to use a hash table rather than an association
  3977. list.
  3978. @node Symbol Variables
  3979. @subsubsection Symbols as Denoting Variables
  3980. When an unquoted symbol in a Scheme program is evaluated, it is
  3981. interpreted as a variable reference, and the result of the evaluation is
  3982. the appropriate variable's value.
  3983. For example, when the expression @code{(string-length "abcd")} is read
  3984. and evaluated, the sequence of characters @code{string-length} is read
  3985. as the symbol whose name is "string-length". This symbol is associated
  3986. with a variable whose value is the procedure that implements string
  3987. length calculation. Therefore evaluation of the @code{string-length}
  3988. symbol results in that procedure.
  3989. The details of the connection between an unquoted symbol and the
  3990. variable to which it refers are explained elsewhere. See @ref{Binding
  3991. Constructs}, for how associations between symbols and variables are
  3992. created, and @ref{Modules}, for how those associations are affected by
  3993. Guile's module system.
  3994. @node Symbol Primitives
  3995. @subsubsection Operations Related to Symbols
  3996. Given any Scheme value, you can determine whether it is a symbol using
  3997. the @code{symbol?} primitive:
  3998. @rnindex symbol?
  3999. @deffn {Scheme Procedure} symbol? obj
  4000. @deffnx {C Function} scm_symbol_p (obj)
  4001. Return @code{#t} if @var{obj} is a symbol, otherwise return
  4002. @code{#f}.
  4003. @end deffn
  4004. @deftypefn {C Function} int scm_is_symbol (SCM val)
  4005. Equivalent to @code{scm_is_true (scm_symbol_p (val))}.
  4006. @end deftypefn
  4007. Once you know that you have a symbol, you can obtain its name as a
  4008. string by calling @code{symbol->string}. Note that Guile differs by
  4009. default from R5RS on the details of @code{symbol->string} as regards
  4010. case-sensitivity:
  4011. @rnindex symbol->string
  4012. @deffn {Scheme Procedure} symbol->string s
  4013. @deffnx {C Function} scm_symbol_to_string (s)
  4014. Return the name of symbol @var{s} as a string. By default, Guile reads
  4015. symbols case-sensitively, so the string returned will have the same case
  4016. variation as the sequence of characters that caused @var{s} to be
  4017. created.
  4018. If Guile is set to read symbols case-insensitively (as specified by
  4019. R5RS), and @var{s} comes into being as part of a literal expression
  4020. (@pxref{Literal expressions,,,r5rs, The Revised^5 Report on Scheme}) or
  4021. by a call to the @code{read} or @code{string-ci->symbol} procedures,
  4022. Guile converts any alphabetic characters in the symbol's name to
  4023. lower case before creating the symbol object, so the string returned
  4024. here will be in lower case.
  4025. If @var{s} was created by @code{string->symbol}, the case of characters
  4026. in the string returned will be the same as that in the string that was
  4027. passed to @code{string->symbol}, regardless of Guile's case-sensitivity
  4028. setting at the time @var{s} was created.
  4029. It is an error to apply mutation procedures like @code{string-set!} to
  4030. strings returned by this procedure.
  4031. @end deffn
  4032. Most symbols are created by writing them literally in code. However it
  4033. is also possible to create symbols programmatically using the following
  4034. procedures:
  4035. @deffn {Scheme Procedure} symbol char@dots{}
  4036. @rnindex symbol
  4037. Return a newly allocated symbol made from the given character arguments.
  4038. @example
  4039. (symbol #\x #\y #\z) @result{} xyz
  4040. @end example
  4041. @end deffn
  4042. @deffn {Scheme Procedure} list->symbol lst
  4043. @rnindex list->symbol
  4044. Return a newly allocated symbol made from a list of characters.
  4045. @example
  4046. (list->symbol '(#\a #\b #\c)) @result{} abc
  4047. @end example
  4048. @end deffn
  4049. @rnindex symbol-append
  4050. @deffn {Scheme Procedure} symbol-append arg @dots{}
  4051. Return a newly allocated symbol whose characters form the
  4052. concatenation of the given symbols, @var{arg} @enddots{}.
  4053. @example
  4054. (let ((h 'hello))
  4055. (symbol-append h 'world))
  4056. @result{} helloworld
  4057. @end example
  4058. @end deffn
  4059. @rnindex string->symbol
  4060. @deffn {Scheme Procedure} string->symbol string
  4061. @deffnx {C Function} scm_string_to_symbol (string)
  4062. Return the symbol whose name is @var{string}. This procedure can create
  4063. symbols with names containing special characters or letters in the
  4064. non-standard case, but it is usually a bad idea to create such symbols
  4065. because in some implementations of Scheme they cannot be read as
  4066. themselves.
  4067. @end deffn
  4068. @deffn {Scheme Procedure} string-ci->symbol str
  4069. @deffnx {C Function} scm_string_ci_to_symbol (str)
  4070. Return the symbol whose name is @var{str}. If Guile is currently
  4071. reading symbols case-insensitively, @var{str} is converted to lowercase
  4072. before the returned symbol is looked up or created.
  4073. @end deffn
  4074. The following examples illustrate Guile's detailed behavior as regards
  4075. the case-sensitivity of symbols:
  4076. @lisp
  4077. (read-enable 'case-insensitive) ; R5RS compliant behavior
  4078. (symbol->string 'flying-fish) @result{} "flying-fish"
  4079. (symbol->string 'Martin) @result{} "martin"
  4080. (symbol->string
  4081. (string->symbol "Malvina")) @result{} "Malvina"
  4082. (eq? 'mISSISSIppi 'mississippi) @result{} #t
  4083. (string->symbol "mISSISSIppi") @result{} mISSISSIppi
  4084. (eq? 'bitBlt (string->symbol "bitBlt")) @result{} #f
  4085. (eq? 'LolliPop
  4086. (string->symbol (symbol->string 'LolliPop))) @result{} #t
  4087. (string=? "K. Harper, M.D."
  4088. (symbol->string
  4089. (string->symbol "K. Harper, M.D."))) @result{} #t
  4090. (read-disable 'case-insensitive) ; Guile default behavior
  4091. (symbol->string 'flying-fish) @result{} "flying-fish"
  4092. (symbol->string 'Martin) @result{} "Martin"
  4093. (symbol->string
  4094. (string->symbol "Malvina")) @result{} "Malvina"
  4095. (eq? 'mISSISSIppi 'mississippi) @result{} #f
  4096. (string->symbol "mISSISSIppi") @result{} mISSISSIppi
  4097. (eq? 'bitBlt (string->symbol "bitBlt")) @result{} #t
  4098. (eq? 'LolliPop
  4099. (string->symbol (symbol->string 'LolliPop))) @result{} #t
  4100. (string=? "K. Harper, M.D."
  4101. (symbol->string
  4102. (string->symbol "K. Harper, M.D."))) @result{} #t
  4103. @end lisp
  4104. From C, there are lower level functions that construct a Scheme symbol
  4105. from a C string in the current locale encoding.
  4106. When you want to do more from C, you should convert between symbols
  4107. and strings using @code{scm_symbol_to_string} and
  4108. @code{scm_string_to_symbol} and work with the strings.
  4109. @deftypefn {C Function} SCM scm_from_latin1_symbol (const char *name)
  4110. @deftypefnx {C Function} SCM scm_from_utf8_symbol (const char *name)
  4111. Construct and return a Scheme symbol whose name is specified by the
  4112. null-terminated C string @var{name}. These are appropriate when
  4113. the C string is hard-coded in the source code.
  4114. @end deftypefn
  4115. @deftypefn {C Function} SCM scm_from_locale_symbol (const char *name)
  4116. @deftypefnx {C Function} SCM scm_from_locale_symboln (const char *name, size_t len)
  4117. Construct and return a Scheme symbol whose name is specified by
  4118. @var{name}. For @code{scm_from_locale_symbol}, @var{name} must be null
  4119. terminated; for @code{scm_from_locale_symboln} the length of @var{name} is
  4120. specified explicitly by @var{len}.
  4121. Note that these functions should @emph{not} be used when @var{name} is a
  4122. C string constant, because there is no guarantee that the current locale
  4123. will match that of the execution character set, used for string and
  4124. character constants. Most modern C compilers use UTF-8 by default, so
  4125. in such cases we recommend @code{scm_from_utf8_symbol}.
  4126. @end deftypefn
  4127. @deftypefn {C Function} SCM scm_take_locale_symbol (char *str)
  4128. @deftypefnx {C Function} SCM scm_take_locale_symboln (char *str, size_t len)
  4129. Like @code{scm_from_locale_symbol} and @code{scm_from_locale_symboln},
  4130. respectively, but also frees @var{str} with @code{free} eventually.
  4131. Thus, you can use this function when you would free @var{str} anyway
  4132. immediately after creating the Scheme string. In certain cases, Guile
  4133. can then use @var{str} directly as its internal representation.
  4134. @end deftypefn
  4135. The size of a symbol can also be obtained from C:
  4136. @deftypefn {C Function} size_t scm_c_symbol_length (SCM sym)
  4137. Return the number of characters in @var{sym}.
  4138. @end deftypefn
  4139. Finally, some applications, especially those that generate new Scheme
  4140. code dynamically, need to generate symbols for use in the generated
  4141. code. The @code{gensym} primitive meets this need:
  4142. @deffn {Scheme Procedure} gensym [prefix]
  4143. @deffnx {C Function} scm_gensym (prefix)
  4144. Create a new symbol with a name constructed from a prefix and a counter
  4145. value. The string @var{prefix} can be specified as an optional
  4146. argument. Default prefix is @samp{@w{ g}}. The counter is increased by 1
  4147. at each call. There is no provision for resetting the counter.
  4148. @end deffn
  4149. The symbols generated by @code{gensym} are @emph{likely} to be unique,
  4150. since their names begin with a space and it is only otherwise possible
  4151. to generate such symbols if a programmer goes out of their way to do
  4152. so. Uniqueness can be guaranteed by instead using uninterned symbols
  4153. (@pxref{Symbol Uninterned}), though they can't be usefully written out
  4154. and read back in.
  4155. @node Symbol Read Syntax
  4156. @subsubsection Extended Read Syntax for Symbols
  4157. @cindex r7rs-symbols
  4158. The read syntax for a symbol is a sequence of letters, digits, and
  4159. @dfn{extended alphabetic characters}, beginning with a character that
  4160. cannot begin a number. In addition, the special cases of @code{+},
  4161. @code{-}, and @code{...} are read as symbols even though numbers can
  4162. begin with @code{+}, @code{-} or @code{.}.
  4163. Extended alphabetic characters may be used within identifiers as if
  4164. they were letters. The set of extended alphabetic characters is:
  4165. @example
  4166. ! $ % & * + - . / : < = > ? @@ ^ _ ~
  4167. @end example
  4168. In addition to the standard read syntax defined above (which is taken
  4169. from R5RS (@pxref{Formal syntax,,,r5rs,The Revised^5 Report on
  4170. Scheme})), Guile provides an extended symbol read syntax that allows the
  4171. inclusion of unusual characters such as space characters, newlines and
  4172. parentheses. If (for whatever reason) you need to write a symbol
  4173. containing characters not mentioned above, you can do so as follows.
  4174. @itemize @bullet
  4175. @item
  4176. Begin the symbol with the characters @code{#@{},
  4177. @item
  4178. write the characters of the symbol and
  4179. @item
  4180. finish the symbol with the characters @code{@}#}.
  4181. @end itemize
  4182. Here are a few examples of this form of read syntax. The first symbol
  4183. needs to use extended syntax because it contains a space character, the
  4184. second because it contains a line break, and the last because it looks
  4185. like a number.
  4186. @lisp
  4187. #@{foo bar@}#
  4188. #@{what
  4189. ever@}#
  4190. #@{4242@}#
  4191. @end lisp
  4192. Although Guile provides this extended read syntax for symbols,
  4193. widespread usage of it is discouraged because it is not portable and not
  4194. very readable.
  4195. Alternatively, if you enable the @code{r7rs-symbols} read option (see
  4196. @pxref{Scheme Read}), you can write arbitrary symbols using the same
  4197. notation used for strings, except delimited by vertical bars instead of
  4198. double quotes.
  4199. @example
  4200. |foo bar|
  4201. |\x3BB; is a greek lambda|
  4202. |\| is a vertical bar|
  4203. @end example
  4204. Note that there's also an @code{r7rs-symbols} print option
  4205. (@pxref{Scheme Write}). To enable the use of this notation, evaluate
  4206. one or both of the following expressions:
  4207. @example
  4208. (read-enable 'r7rs-symbols)
  4209. (print-enable 'r7rs-symbols)
  4210. @end example
  4211. @node Symbol Uninterned
  4212. @subsubsection Uninterned Symbols
  4213. What makes symbols useful is that they are automatically kept unique.
  4214. There are no two symbols that are distinct objects but have the same
  4215. name. But of course, there is no rule without exception. In addition
  4216. to the normal symbols that have been discussed up to now, you can also
  4217. create special @dfn{uninterned} symbols that behave slightly
  4218. differently.
  4219. To understand what is different about them and why they might be useful,
  4220. we look at how normal symbols are actually kept unique.
  4221. Whenever Guile wants to find the symbol with a specific name, for
  4222. example during @code{read} or when executing @code{string->symbol}, it
  4223. first looks into a table of all existing symbols to find out whether a
  4224. symbol with the given name already exists. When this is the case, Guile
  4225. just returns that symbol. When not, a new symbol with the name is
  4226. created and entered into the table so that it can be found later.
  4227. Sometimes you might want to create a symbol that is guaranteed `fresh',
  4228. i.e.@: a symbol that did not exist previously. You might also want to
  4229. somehow guarantee that no one else will ever unintentionally stumble
  4230. across your symbol in the future. These properties of a symbol are
  4231. often needed when generating code during macro expansion. When
  4232. introducing new temporary variables, you want to guarantee that they
  4233. don't conflict with variables in other people's code.
  4234. The simplest way to arrange for this is to create a new symbol but
  4235. not enter it into the global table of all symbols. That way, no one
  4236. will ever get access to your symbol by chance. Symbols that are not in
  4237. the table are called @dfn{uninterned}. Of course, symbols that
  4238. @emph{are} in the table are called @dfn{interned}.
  4239. You create new uninterned symbols with the function @code{make-symbol}.
  4240. You can test whether a symbol is interned or not with
  4241. @code{symbol-interned?}.
  4242. Uninterned symbols break the rule that the name of a symbol uniquely
  4243. identifies the symbol object. Because of this, they can not be written
  4244. out and read back in like interned symbols. Currently, Guile has no
  4245. support for reading uninterned symbols. Note that the function
  4246. @code{gensym} does not return uninterned symbols for this reason.
  4247. @deffn {Scheme Procedure} make-symbol name
  4248. @deffnx {C Function} scm_make_symbol (name)
  4249. Return a new uninterned symbol with the name @var{name}. The returned
  4250. symbol is guaranteed to be unique and future calls to
  4251. @code{string->symbol} will not return it.
  4252. @end deffn
  4253. @deffn {Scheme Procedure} symbol-interned? symbol
  4254. @deffnx {C Function} scm_symbol_interned_p (symbol)
  4255. Return @code{#t} if @var{symbol} is interned, otherwise return
  4256. @code{#f}.
  4257. @end deffn
  4258. For example:
  4259. @lisp
  4260. (define foo-1 (string->symbol "foo"))
  4261. (define foo-2 (string->symbol "foo"))
  4262. (define foo-3 (make-symbol "foo"))
  4263. (define foo-4 (make-symbol "foo"))
  4264. (eq? foo-1 foo-2)
  4265. @result{} #t
  4266. ; Two interned symbols with the same name are the same object,
  4267. (eq? foo-1 foo-3)
  4268. @result{} #f
  4269. ; but a call to make-symbol with the same name returns a
  4270. ; distinct object.
  4271. (eq? foo-3 foo-4)
  4272. @result{} #f
  4273. ; A call to make-symbol always returns a new object, even for
  4274. ; the same name.
  4275. foo-3
  4276. @result{} #<uninterned-symbol foo 8085290>
  4277. ; Uninterned symbols print differently from interned symbols,
  4278. (symbol? foo-3)
  4279. @result{} #t
  4280. ; but they are still symbols,
  4281. (symbol-interned? foo-3)
  4282. @result{} #f
  4283. ; just not interned.
  4284. @end lisp
  4285. @node Keywords
  4286. @subsection Keywords
  4287. @tpindex Keywords
  4288. Keywords are self-evaluating objects with a convenient read syntax that
  4289. makes them easy to type.
  4290. Guile's keyword support conforms to R5RS, and adds a (switchable) read
  4291. syntax extension to permit keywords to begin with @code{:} as well as
  4292. @code{#:}, or to end with @code{:}.
  4293. @menu
  4294. * Why Use Keywords?:: Motivation for keyword usage.
  4295. * Coding With Keywords:: How to use keywords.
  4296. * Keyword Read Syntax:: Read syntax for keywords.
  4297. * Keyword Procedures:: Procedures for dealing with keywords.
  4298. @end menu
  4299. @node Why Use Keywords?
  4300. @subsubsection Why Use Keywords?
  4301. Keywords are useful in contexts where a program or procedure wants to be
  4302. able to accept a large number of optional arguments without making its
  4303. interface unmanageable.
  4304. To illustrate this, consider a hypothetical @code{make-window}
  4305. procedure, which creates a new window on the screen for drawing into
  4306. using some graphical toolkit. There are many parameters that the caller
  4307. might like to specify, but which could also be sensibly defaulted, for
  4308. example:
  4309. @itemize @bullet
  4310. @item
  4311. color depth -- Default: the color depth for the screen
  4312. @item
  4313. background color -- Default: white
  4314. @item
  4315. width -- Default: 600
  4316. @item
  4317. height -- Default: 400
  4318. @end itemize
  4319. If @code{make-window} did not use keywords, the caller would have to
  4320. pass in a value for each possible argument, remembering the correct
  4321. argument order and using a special value to indicate the default value
  4322. for that argument:
  4323. @lisp
  4324. (make-window 'default ;; Color depth
  4325. 'default ;; Background color
  4326. 800 ;; Width
  4327. 100 ;; Height
  4328. @dots{}) ;; More make-window arguments
  4329. @end lisp
  4330. With keywords, on the other hand, defaulted arguments are omitted, and
  4331. non-default arguments are clearly tagged by the appropriate keyword. As
  4332. a result, the invocation becomes much clearer:
  4333. @lisp
  4334. (make-window #:width 800 #:height 100)
  4335. @end lisp
  4336. On the other hand, for a simpler procedure with few arguments, the use
  4337. of keywords would be a hindrance rather than a help. The primitive
  4338. procedure @code{cons}, for example, would not be improved if it had to
  4339. be invoked as
  4340. @lisp
  4341. (cons #:car x #:cdr y)
  4342. @end lisp
  4343. So the decision whether to use keywords or not is purely pragmatic: use
  4344. them if they will clarify the procedure invocation at point of call.
  4345. @node Coding With Keywords
  4346. @subsubsection Coding With Keywords
  4347. If a procedure wants to support keywords, it should take a rest argument
  4348. and then use whatever means is convenient to extract keywords and their
  4349. corresponding arguments from the contents of that rest argument.
  4350. The following example illustrates the principle: the code for
  4351. @code{make-window} uses a helper procedure called
  4352. @code{get-keyword-value} to extract individual keyword arguments from
  4353. the rest argument.
  4354. @lisp
  4355. (define (get-keyword-value args keyword default)
  4356. (let ((kv (memq keyword args)))
  4357. (if (and kv (>= (length kv) 2))
  4358. (cadr kv)
  4359. default)))
  4360. (define (make-window . args)
  4361. (let ((depth (get-keyword-value args #:depth screen-depth))
  4362. (bg (get-keyword-value args #:bg "white"))
  4363. (width (get-keyword-value args #:width 800))
  4364. (height (get-keyword-value args #:height 100))
  4365. @dots{})
  4366. @dots{}))
  4367. @end lisp
  4368. But you don't need to write @code{get-keyword-value}. The @code{(ice-9
  4369. optargs)} module provides a set of powerful macros that you can use to
  4370. implement keyword-supporting procedures like this:
  4371. @lisp
  4372. (use-modules (ice-9 optargs))
  4373. (define (make-window . args)
  4374. (let-keywords args #f ((depth screen-depth)
  4375. (bg "white")
  4376. (width 800)
  4377. (height 100))
  4378. ...))
  4379. @end lisp
  4380. @noindent
  4381. Or, even more economically, like this:
  4382. @lisp
  4383. (use-modules (ice-9 optargs))
  4384. (define* (make-window #:key (depth screen-depth)
  4385. (bg "white")
  4386. (width 800)
  4387. (height 100))
  4388. ...)
  4389. @end lisp
  4390. For further details on @code{let-keywords}, @code{define*} and other
  4391. facilities provided by the @code{(ice-9 optargs)} module, see
  4392. @ref{Optional Arguments}.
  4393. To handle keyword arguments from procedures implemented in C,
  4394. use @code{scm_c_bind_keyword_arguments} (@pxref{Keyword Procedures}).
  4395. @node Keyword Read Syntax
  4396. @subsubsection Keyword Read Syntax
  4397. Guile, by default, only recognizes a keyword syntax that is compatible
  4398. with R5RS. A token of the form @code{#:NAME}, where @code{NAME} has the
  4399. same syntax as a Scheme symbol (@pxref{Symbol Read Syntax}), is the
  4400. external representation of the keyword named @code{NAME}. Keyword
  4401. objects print using this syntax as well, so values containing keyword
  4402. objects can be read back into Guile. When used in an expression,
  4403. keywords are self-quoting objects.
  4404. If the @code{keywords} read option is set to @code{'prefix}, Guile also
  4405. recognizes the alternative read syntax @code{:NAME}. Otherwise, tokens
  4406. of the form @code{:NAME} are read as symbols, as required by R5RS.
  4407. @cindex SRFI-88 keyword syntax
  4408. If the @code{keywords} read option is set to @code{'postfix}, Guile
  4409. recognizes the SRFI-88 read syntax @code{NAME:} (@pxref{SRFI-88}).
  4410. Otherwise, tokens of this form are read as symbols.
  4411. To enable and disable the alternative non-R5RS keyword syntax, you use
  4412. the @code{read-set!} procedure documented @ref{Scheme Read}. Note that
  4413. the @code{prefix} and @code{postfix} syntax are mutually exclusive.
  4414. @lisp
  4415. (read-set! keywords 'prefix)
  4416. #:type
  4417. @result{}
  4418. #:type
  4419. :type
  4420. @result{}
  4421. #:type
  4422. (read-set! keywords 'postfix)
  4423. type:
  4424. @result{}
  4425. #:type
  4426. :type
  4427. @result{}
  4428. :type
  4429. (read-set! keywords #f)
  4430. #:type
  4431. @result{}
  4432. #:type
  4433. :type
  4434. @print{}
  4435. ERROR: In expression :type:
  4436. ERROR: Unbound variable: :type
  4437. ABORT: (unbound-variable)
  4438. @end lisp
  4439. @node Keyword Procedures
  4440. @subsubsection Keyword Procedures
  4441. @deffn {Scheme Procedure} keyword? obj
  4442. @deffnx {C Function} scm_keyword_p (obj)
  4443. Return @code{#t} if the argument @var{obj} is a keyword, else
  4444. @code{#f}.
  4445. @end deffn
  4446. @deffn {Scheme Procedure} keyword->symbol keyword
  4447. @deffnx {C Function} scm_keyword_to_symbol (keyword)
  4448. Return the symbol with the same name as @var{keyword}.
  4449. @end deffn
  4450. @deffn {Scheme Procedure} symbol->keyword symbol
  4451. @deffnx {C Function} scm_symbol_to_keyword (symbol)
  4452. Return the keyword with the same name as @var{symbol}.
  4453. @end deffn
  4454. @deftypefn {C Function} int scm_is_keyword (SCM obj)
  4455. Equivalent to @code{scm_is_true (scm_keyword_p (@var{obj}))}.
  4456. @end deftypefn
  4457. @deftypefn {C Function} SCM scm_from_locale_keyword (const char *name)
  4458. @deftypefnx {C Function} SCM scm_from_locale_keywordn (const char *name, size_t len)
  4459. Equivalent to @code{scm_symbol_to_keyword (scm_from_locale_symbol
  4460. (@var{name}))} and @code{scm_symbol_to_keyword (scm_from_locale_symboln
  4461. (@var{name}, @var{len}))}, respectively.
  4462. Note that these functions should @emph{not} be used when @var{name} is a
  4463. C string constant, because there is no guarantee that the current locale
  4464. will match that of the execution character set, used for string and
  4465. character constants. Most modern C compilers use UTF-8 by default, so
  4466. in such cases we recommend @code{scm_from_utf8_keyword}.
  4467. @end deftypefn
  4468. @deftypefn {C Function} SCM scm_from_latin1_keyword (const char *name)
  4469. @deftypefnx {C Function} SCM scm_from_utf8_keyword (const char *name)
  4470. Equivalent to @code{scm_symbol_to_keyword (scm_from_latin1_symbol
  4471. (@var{name}))} and @code{scm_symbol_to_keyword (scm_from_utf8_symbol
  4472. (@var{name}))}, respectively.
  4473. @end deftypefn
  4474. @deftypefn {C Function} void scm_c_bind_keyword_arguments (const char *subr, @
  4475. SCM rest, scm_t_keyword_arguments_flags flags, @
  4476. SCM keyword1, SCM *argp1, @
  4477. @dots{}, @
  4478. SCM keywordN, SCM *argpN, @
  4479. @nicode{SCM_UNDEFINED})
  4480. Extract the specified keyword arguments from @var{rest}, which is not
  4481. modified. If the keyword argument @var{keyword1} is present in
  4482. @var{rest} with an associated value, that value is stored in the
  4483. variable pointed to by @var{argp1}, otherwise the variable is left
  4484. unchanged. Similarly for the other keywords and argument pointers up to
  4485. @var{keywordN} and @var{argpN}. The argument list to
  4486. @code{scm_c_bind_keyword_arguments} must be terminated by
  4487. @code{SCM_UNDEFINED}.
  4488. Note that since the variables pointed to by @var{argp1} through
  4489. @var{argpN} are left unchanged if the associated keyword argument is not
  4490. present, they should be initialized to their default values before
  4491. calling @code{scm_c_bind_keyword_arguments}. Alternatively, you can
  4492. initialize them to @code{SCM_UNDEFINED} before the call, and then use
  4493. @code{SCM_UNBNDP} after the call to see which ones were provided.
  4494. If an unrecognized keyword argument is present in @var{rest} and
  4495. @var{flags} does not contain @code{SCM_ALLOW_OTHER_KEYS}, or if
  4496. non-keyword arguments are present and @var{flags} does not contain
  4497. @code{SCM_ALLOW_NON_KEYWORD_ARGUMENTS}, an exception is raised.
  4498. @var{subr} should be the name of the procedure receiving the keyword
  4499. arguments, for purposes of error reporting.
  4500. For example:
  4501. @example
  4502. SCM k_delimiter;
  4503. SCM k_grammar;
  4504. SCM sym_infix;
  4505. SCM my_string_join (SCM strings, SCM rest)
  4506. @{
  4507. SCM delimiter = SCM_UNDEFINED;
  4508. SCM grammar = sym_infix;
  4509. scm_c_bind_keyword_arguments ("my-string-join", rest, 0,
  4510. k_delimiter, &delimiter,
  4511. k_grammar, &grammar,
  4512. SCM_UNDEFINED);
  4513. if (SCM_UNBNDP (delimiter))
  4514. delimiter = scm_from_utf8_string (" ");
  4515. return scm_string_join (strings, delimiter, grammar);
  4516. @}
  4517. void my_init ()
  4518. @{
  4519. k_delimiter = scm_from_utf8_keyword ("delimiter");
  4520. k_grammar = scm_from_utf8_keyword ("grammar");
  4521. sym_infix = scm_from_utf8_symbol ("infix");
  4522. scm_c_define_gsubr ("my-string-join", 1, 0, 1, my_string_join);
  4523. @}
  4524. @end example
  4525. @end deftypefn
  4526. @node Pairs
  4527. @subsection Pairs
  4528. @tpindex Pairs
  4529. Pairs are used to combine two Scheme objects into one compound object.
  4530. Hence the name: A pair stores a pair of objects.
  4531. The data type @dfn{pair} is extremely important in Scheme, just like in
  4532. any other Lisp dialect. The reason is that pairs are not only used to
  4533. make two values available as one object, but that pairs are used for
  4534. constructing lists of values. Because lists are so important in Scheme,
  4535. they are described in a section of their own (@pxref{Lists}).
  4536. Pairs can literally get entered in source code or at the REPL, in the
  4537. so-called @dfn{dotted list} syntax. This syntax consists of an opening
  4538. parentheses, the first element of the pair, a dot, the second element
  4539. and a closing parentheses. The following example shows how a pair
  4540. consisting of the two numbers 1 and 2, and a pair containing the symbols
  4541. @code{foo} and @code{bar} can be entered. It is very important to write
  4542. the whitespace before and after the dot, because otherwise the Scheme
  4543. parser would not be able to figure out where to split the tokens.
  4544. @lisp
  4545. (1 . 2)
  4546. (foo . bar)
  4547. @end lisp
  4548. But beware, if you want to try out these examples, you have to
  4549. @dfn{quote} the expressions. More information about quotation is
  4550. available in the section @ref{Expression Syntax}. The correct way
  4551. to try these examples is as follows.
  4552. @lisp
  4553. '(1 . 2)
  4554. @result{}
  4555. (1 . 2)
  4556. '(foo . bar)
  4557. @result{}
  4558. (foo . bar)
  4559. @end lisp
  4560. A new pair is made by calling the procedure @code{cons} with two
  4561. arguments. Then the argument values are stored into a newly allocated
  4562. pair, and the pair is returned. The name @code{cons} stands for
  4563. "construct". Use the procedure @code{pair?} to test whether a
  4564. given Scheme object is a pair or not.
  4565. @rnindex cons
  4566. @deffn {Scheme Procedure} cons x y
  4567. @deffnx {C Function} scm_cons (x, y)
  4568. Return a newly allocated pair whose car is @var{x} and whose
  4569. cdr is @var{y}. The pair is guaranteed to be different (in the
  4570. sense of @code{eq?}) from every previously existing object.
  4571. @end deffn
  4572. @rnindex pair?
  4573. @deffn {Scheme Procedure} pair? x
  4574. @deffnx {C Function} scm_pair_p (x)
  4575. Return @code{#t} if @var{x} is a pair; otherwise return
  4576. @code{#f}.
  4577. @end deffn
  4578. @deftypefn {C Function} int scm_is_pair (SCM x)
  4579. Return 1 when @var{x} is a pair; otherwise return 0.
  4580. @end deftypefn
  4581. The two parts of a pair are traditionally called @dfn{car} and
  4582. @dfn{cdr}. They can be retrieved with procedures of the same name
  4583. (@code{car} and @code{cdr}), and can be modified with the procedures
  4584. @code{set-car!} and @code{set-cdr!}.
  4585. Since a very common operation in Scheme programs is to access the car of
  4586. a car of a pair, or the car of the cdr of a pair, etc., the procedures
  4587. called @code{caar}, @code{cadr} and so on are also predefined. However,
  4588. using these procedures is often detrimental to readability, and
  4589. error-prone. Thus, accessing the contents of a list is usually better
  4590. achieved using pattern matching techniques (@pxref{Pattern Matching}).
  4591. @rnindex car
  4592. @rnindex cdr
  4593. @deffn {Scheme Procedure} car pair
  4594. @deffnx {Scheme Procedure} cdr pair
  4595. @deffnx {C Function} scm_car (pair)
  4596. @deffnx {C Function} scm_cdr (pair)
  4597. Return the car or the cdr of @var{pair}, respectively.
  4598. @end deffn
  4599. @deftypefn {C Macro} SCM SCM_CAR (SCM pair)
  4600. @deftypefnx {C Macro} SCM SCM_CDR (SCM pair)
  4601. These two macros are the fastest way to access the car or cdr of a
  4602. pair; they can be thought of as compiling into a single memory
  4603. reference.
  4604. These macros do no checking at all. The argument @var{pair} must be a
  4605. valid pair.
  4606. @end deftypefn
  4607. @deffn {Scheme Procedure} cddr pair
  4608. @deffnx {Scheme Procedure} cdar pair
  4609. @deffnx {Scheme Procedure} cadr pair
  4610. @deffnx {Scheme Procedure} caar pair
  4611. @deffnx {Scheme Procedure} cdddr pair
  4612. @deffnx {Scheme Procedure} cddar pair
  4613. @deffnx {Scheme Procedure} cdadr pair
  4614. @deffnx {Scheme Procedure} cdaar pair
  4615. @deffnx {Scheme Procedure} caddr pair
  4616. @deffnx {Scheme Procedure} cadar pair
  4617. @deffnx {Scheme Procedure} caadr pair
  4618. @deffnx {Scheme Procedure} caaar pair
  4619. @deffnx {Scheme Procedure} cddddr pair
  4620. @deffnx {Scheme Procedure} cdddar pair
  4621. @deffnx {Scheme Procedure} cddadr pair
  4622. @deffnx {Scheme Procedure} cddaar pair
  4623. @deffnx {Scheme Procedure} cdaddr pair
  4624. @deffnx {Scheme Procedure} cdadar pair
  4625. @deffnx {Scheme Procedure} cdaadr pair
  4626. @deffnx {Scheme Procedure} cdaaar pair
  4627. @deffnx {Scheme Procedure} cadddr pair
  4628. @deffnx {Scheme Procedure} caddar pair
  4629. @deffnx {Scheme Procedure} cadadr pair
  4630. @deffnx {Scheme Procedure} cadaar pair
  4631. @deffnx {Scheme Procedure} caaddr pair
  4632. @deffnx {Scheme Procedure} caadar pair
  4633. @deffnx {Scheme Procedure} caaadr pair
  4634. @deffnx {Scheme Procedure} caaaar pair
  4635. @deffnx {C Function} scm_cddr (pair)
  4636. @deffnx {C Function} scm_cdar (pair)
  4637. @deffnx {C Function} scm_cadr (pair)
  4638. @deffnx {C Function} scm_caar (pair)
  4639. @deffnx {C Function} scm_cdddr (pair)
  4640. @deffnx {C Function} scm_cddar (pair)
  4641. @deffnx {C Function} scm_cdadr (pair)
  4642. @deffnx {C Function} scm_cdaar (pair)
  4643. @deffnx {C Function} scm_caddr (pair)
  4644. @deffnx {C Function} scm_cadar (pair)
  4645. @deffnx {C Function} scm_caadr (pair)
  4646. @deffnx {C Function} scm_caaar (pair)
  4647. @deffnx {C Function} scm_cddddr (pair)
  4648. @deffnx {C Function} scm_cdddar (pair)
  4649. @deffnx {C Function} scm_cddadr (pair)
  4650. @deffnx {C Function} scm_cddaar (pair)
  4651. @deffnx {C Function} scm_cdaddr (pair)
  4652. @deffnx {C Function} scm_cdadar (pair)
  4653. @deffnx {C Function} scm_cdaadr (pair)
  4654. @deffnx {C Function} scm_cdaaar (pair)
  4655. @deffnx {C Function} scm_cadddr (pair)
  4656. @deffnx {C Function} scm_caddar (pair)
  4657. @deffnx {C Function} scm_cadadr (pair)
  4658. @deffnx {C Function} scm_cadaar (pair)
  4659. @deffnx {C Function} scm_caaddr (pair)
  4660. @deffnx {C Function} scm_caadar (pair)
  4661. @deffnx {C Function} scm_caaadr (pair)
  4662. @deffnx {C Function} scm_caaaar (pair)
  4663. These procedures are compositions of @code{car} and @code{cdr}, where
  4664. for example @code{caddr} could be defined by
  4665. @lisp
  4666. (define caddr (lambda (x) (car (cdr (cdr x)))))
  4667. @end lisp
  4668. @code{cadr}, @code{caddr} and @code{cadddr} pick out the second, third
  4669. or fourth elements of a list, respectively. SRFI-1 provides the same
  4670. under the names @code{second}, @code{third} and @code{fourth}
  4671. (@pxref{SRFI-1 Selectors}).
  4672. @end deffn
  4673. @rnindex set-car!
  4674. @deffn {Scheme Procedure} set-car! pair value
  4675. @deffnx {C Function} scm_set_car_x (pair, value)
  4676. Stores @var{value} in the car field of @var{pair}. The value returned
  4677. by @code{set-car!} is unspecified.
  4678. @end deffn
  4679. @rnindex set-cdr!
  4680. @deffn {Scheme Procedure} set-cdr! pair value
  4681. @deffnx {C Function} scm_set_cdr_x (pair, value)
  4682. Stores @var{value} in the cdr field of @var{pair}. The value returned
  4683. by @code{set-cdr!} is unspecified.
  4684. @end deffn
  4685. @node Lists
  4686. @subsection Lists
  4687. @tpindex Lists
  4688. A very important data type in Scheme---as well as in all other Lisp
  4689. dialects---is the data type @dfn{list}.@footnote{Strictly speaking,
  4690. Scheme does not have a real datatype @dfn{list}. Lists are made up of
  4691. @dfn{chained pairs}, and only exist by definition---a list is a chain
  4692. of pairs which looks like a list.}
  4693. This is the short definition of what a list is:
  4694. @itemize @bullet
  4695. @item
  4696. Either the empty list @code{()},
  4697. @item
  4698. or a pair which has a list in its cdr.
  4699. @end itemize
  4700. @c FIXME::martin: Describe the pair chaining in more detail.
  4701. @c FIXME::martin: What is a proper, what an improper list?
  4702. @c What is a circular list?
  4703. @c FIXME::martin: Maybe steal some graphics from the Elisp reference
  4704. @c manual?
  4705. @menu
  4706. * List Syntax:: Writing literal lists.
  4707. * List Predicates:: Testing lists.
  4708. * List Constructors:: Creating new lists.
  4709. * List Selection:: Selecting from lists, getting their length.
  4710. * Append/Reverse:: Appending and reversing lists.
  4711. * List Modification:: Modifying existing lists.
  4712. * List Searching:: Searching for list elements
  4713. * List Mapping:: Applying procedures to lists.
  4714. @end menu
  4715. @node List Syntax
  4716. @subsubsection List Read Syntax
  4717. The syntax for lists is an opening parentheses, then all the elements of
  4718. the list (separated by whitespace) and finally a closing
  4719. parentheses.@footnote{Note that there is no separation character between
  4720. the list elements, like a comma or a semicolon.}.
  4721. @lisp
  4722. (1 2 3) ; @r{a list of the numbers 1, 2 and 3}
  4723. ("foo" bar 3.1415) ; @r{a string, a symbol and a real number}
  4724. () ; @r{the empty list}
  4725. @end lisp
  4726. The last example needs a bit more explanation. A list with no elements,
  4727. called the @dfn{empty list}, is special in some ways. It is used for
  4728. terminating lists by storing it into the cdr of the last pair that makes
  4729. up a list. An example will clear that up:
  4730. @lisp
  4731. (car '(1))
  4732. @result{}
  4733. 1
  4734. (cdr '(1))
  4735. @result{}
  4736. ()
  4737. @end lisp
  4738. This example also shows that lists have to be quoted when written
  4739. (@pxref{Expression Syntax}), because they would otherwise be
  4740. mistakenly taken as procedure applications (@pxref{Simple
  4741. Invocation}).
  4742. @node List Predicates
  4743. @subsubsection List Predicates
  4744. Often it is useful to test whether a given Scheme object is a list or
  4745. not. List-processing procedures could use this information to test
  4746. whether their input is valid, or they could do different things
  4747. depending on the datatype of their arguments.
  4748. @rnindex list?
  4749. @deffn {Scheme Procedure} list? x
  4750. @deffnx {C Function} scm_list_p (x)
  4751. Return @code{#t} if @var{x} is a proper list, else @code{#f}.
  4752. @end deffn
  4753. The predicate @code{null?} is often used in list-processing code to
  4754. tell whether a given list has run out of elements. That is, a loop
  4755. somehow deals with the elements of a list until the list satisfies
  4756. @code{null?}. Then, the algorithm terminates.
  4757. @rnindex null?
  4758. @deffn {Scheme Procedure} null? x
  4759. @deffnx {C Function} scm_null_p (x)
  4760. Return @code{#t} if @var{x} is the empty list, else @code{#f}.
  4761. @end deffn
  4762. @deftypefn {C Function} int scm_is_null (SCM x)
  4763. Return 1 when @var{x} is the empty list; otherwise return 0.
  4764. @end deftypefn
  4765. @node List Constructors
  4766. @subsubsection List Constructors
  4767. This section describes the procedures for constructing new lists.
  4768. @code{list} simply returns a list where the elements are the arguments,
  4769. @code{cons*} is similar, but the last argument is stored in the cdr of
  4770. the last pair of the list.
  4771. @c C Function scm_list(rest) used to be documented here, but it's a
  4772. @c no-op since it does nothing but return the list the caller must
  4773. @c have already created.
  4774. @c
  4775. @deffn {Scheme Procedure} list elem @dots{}
  4776. @deffnx {C Function} scm_list_1 (elem1)
  4777. @deffnx {C Function} scm_list_2 (elem1, elem2)
  4778. @deffnx {C Function} scm_list_3 (elem1, elem2, elem3)
  4779. @deffnx {C Function} scm_list_4 (elem1, elem2, elem3, elem4)
  4780. @deffnx {C Function} scm_list_5 (elem1, elem2, elem3, elem4, elem5)
  4781. @deffnx {C Function} scm_list_n (elem1, @dots{}, elemN, @nicode{SCM_UNDEFINED})
  4782. @rnindex list
  4783. Return a new list containing elements @var{elem} @enddots{}.
  4784. @code{scm_list_n} takes a variable number of arguments, terminated by
  4785. the special @code{SCM_UNDEFINED}. That final @code{SCM_UNDEFINED} is
  4786. not included in the list. None of @var{elem} @dots{} can
  4787. themselves be @code{SCM_UNDEFINED}, or @code{scm_list_n} will
  4788. terminate at that point.
  4789. @end deffn
  4790. @c C Function scm_cons_star(arg1,rest) used to be documented here,
  4791. @c but it's not really a useful interface, since it expects the
  4792. @c caller to have already consed up all but the first argument
  4793. @c already.
  4794. @c
  4795. @deffn {Scheme Procedure} cons* arg1 arg2 @dots{}
  4796. Like @code{list}, but the last arg provides the tail of the
  4797. constructed list, returning @code{(cons @var{arg1} (cons
  4798. @var{arg2} (cons @dots{} @var{argn})))}. Requires at least one
  4799. argument. If given one argument, that argument is returned as
  4800. result. This function is called @code{list*} in some other
  4801. Schemes and in Common LISP.
  4802. @end deffn
  4803. @deffn {Scheme Procedure} list-copy lst
  4804. @deffnx {C Function} scm_list_copy (lst)
  4805. Return a (newly-created) copy of @var{lst}.
  4806. @end deffn
  4807. @deffn {Scheme Procedure} make-list n [init]
  4808. Create a list containing of @var{n} elements, where each element is
  4809. initialized to @var{init}. @var{init} defaults to the empty list
  4810. @code{()} if not given.
  4811. @end deffn
  4812. Note that @code{list-copy} only makes a copy of the pairs which make up
  4813. the spine of the lists. The list elements are not copied, which means
  4814. that modifying the elements of the new list also modifies the elements
  4815. of the old list. On the other hand, applying procedures like
  4816. @code{set-cdr!} or @code{delv!} to the new list will not alter the old
  4817. list. If you also need to copy the list elements (making a deep copy),
  4818. use the procedure @code{copy-tree} from @code{(ice-9 copy-tree)}
  4819. (@pxref{Copying}).
  4820. @node List Selection
  4821. @subsubsection List Selection
  4822. These procedures are used to get some information about a list, or to
  4823. retrieve one or more elements of a list.
  4824. @rnindex length
  4825. @deffn {Scheme Procedure} length lst
  4826. @deffnx {C Function} scm_length (lst)
  4827. Return the number of elements in list @var{lst}.
  4828. @end deffn
  4829. @deffn {Scheme Procedure} last-pair lst
  4830. @deffnx {C Function} scm_last_pair (lst)
  4831. Return the last pair in @var{lst}, signaling an error if
  4832. @var{lst} is circular.
  4833. @end deffn
  4834. @rnindex list-ref
  4835. @deffn {Scheme Procedure} list-ref list k
  4836. @deffnx {C Function} scm_list_ref (list, k)
  4837. Return the @var{k}th element from @var{list}.
  4838. @end deffn
  4839. @rnindex list-tail
  4840. @deffn {Scheme Procedure} list-tail lst k
  4841. @deffnx {Scheme Procedure} list-cdr-ref lst k
  4842. @deffnx {C Function} scm_list_tail (lst, k)
  4843. Return the "tail" of @var{lst} beginning with its @var{k}th element.
  4844. The first element of the list is considered to be element 0.
  4845. @code{list-tail} and @code{list-cdr-ref} are identical. It may help to
  4846. think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,
  4847. or returning the results of cdring @var{k} times down @var{lst}.
  4848. @end deffn
  4849. @deffn {Scheme Procedure} list-head lst k
  4850. @deffnx {C Function} scm_list_head (lst, k)
  4851. Copy the first @var{k} elements from @var{lst} into a new list, and
  4852. return it.
  4853. @end deffn
  4854. @node Append/Reverse
  4855. @subsubsection Append and Reverse
  4856. @code{append} and @code{append!} are used to concatenate two or more
  4857. lists in order to form a new list. @code{reverse} and @code{reverse!}
  4858. return lists with the same elements as their arguments, but in reverse
  4859. order. The procedure variants with an @code{!} directly modify the
  4860. pairs which form the list, whereas the other procedures create new
  4861. pairs. This is why you should be careful when using the side-effecting
  4862. variants.
  4863. @rnindex append
  4864. @deffn {Scheme Procedure} append lst @dots{} obj
  4865. @deffnx {Scheme Procedure} append
  4866. @deffnx {Scheme Procedure} append! lst @dots{} obj
  4867. @deffnx {Scheme Procedure} append!
  4868. @deffnx {C Function} scm_append (lstlst)
  4869. @deffnx {C Function} scm_append_x (lstlst)
  4870. Return a list comprising all the elements of lists @var{lst} @dots{}
  4871. @var{obj}. If called with no arguments, return the empty list.
  4872. @lisp
  4873. (append '(x) '(y)) @result{} (x y)
  4874. (append '(a) '(b c d)) @result{} (a b c d)
  4875. (append '(a (b)) '((c))) @result{} (a (b) (c))
  4876. @end lisp
  4877. The last argument @var{obj} may actually be any object; an improper
  4878. list results if the last argument is not a proper list.
  4879. @lisp
  4880. (append '(a b) '(c . d)) @result{} (a b c . d)
  4881. (append '() 'a) @result{} a
  4882. @end lisp
  4883. @code{append} doesn't modify the given lists, but the return may share
  4884. structure with the final @var{obj}. @code{append!} is permitted, but
  4885. not required, to modify the given lists to form its return.
  4886. For @code{scm_append} and @code{scm_append_x}, @var{lstlst} is a list
  4887. of the list operands @var{lst} @dots{} @var{obj}. That @var{lstlst}
  4888. itself is not modified or used in the return.
  4889. @end deffn
  4890. @rnindex reverse
  4891. @deffn {Scheme Procedure} reverse lst
  4892. @deffnx {Scheme Procedure} reverse! lst [newtail]
  4893. @deffnx {C Function} scm_reverse (lst)
  4894. @deffnx {C Function} scm_reverse_x (lst, newtail)
  4895. Return a list comprising the elements of @var{lst}, in reverse order.
  4896. @code{reverse} constructs a new list. @code{reverse!} is permitted, but
  4897. not required, to modify @var{lst} in constructing its return.
  4898. For @code{reverse!}, the optional @var{newtail} is appended to the
  4899. result. @var{newtail} isn't reversed, it simply becomes the list
  4900. tail. For @code{scm_reverse_x}, the @var{newtail} parameter is
  4901. mandatory, but can be @code{SCM_EOL} if no further tail is required.
  4902. @end deffn
  4903. @node List Modification
  4904. @subsubsection List Modification
  4905. The following procedures modify an existing list, either by changing
  4906. elements of the list, or by changing the list structure itself.
  4907. @deffn {Scheme Procedure} list-set! list k val
  4908. @deffnx {C Function} scm_list_set_x (list, k, val)
  4909. Set the @var{k}th element of @var{list} to @var{val}.
  4910. @end deffn
  4911. @deffn {Scheme Procedure} list-cdr-set! list k val
  4912. @deffnx {C Function} scm_list_cdr_set_x (list, k, val)
  4913. Set the @var{k}th cdr of @var{list} to @var{val}.
  4914. @end deffn
  4915. @deffn {Scheme Procedure} delq item lst
  4916. @deffnx {C Function} scm_delq (item, lst)
  4917. Return a newly-created copy of @var{lst} with elements
  4918. @code{eq?} to @var{item} removed. This procedure mirrors
  4919. @code{memq}: @code{delq} compares elements of @var{lst} against
  4920. @var{item} with @code{eq?}.
  4921. @end deffn
  4922. @deffn {Scheme Procedure} delv item lst
  4923. @deffnx {C Function} scm_delv (item, lst)
  4924. Return a newly-created copy of @var{lst} with elements
  4925. @code{eqv?} to @var{item} removed. This procedure mirrors
  4926. @code{memv}: @code{delv} compares elements of @var{lst} against
  4927. @var{item} with @code{eqv?}.
  4928. @end deffn
  4929. @deffn {Scheme Procedure} delete item lst
  4930. @deffnx {C Function} scm_delete (item, lst)
  4931. Return a newly-created copy of @var{lst} with elements
  4932. @code{equal?} to @var{item} removed. This procedure mirrors
  4933. @code{member}: @code{delete} compares elements of @var{lst}
  4934. against @var{item} with @code{equal?}.
  4935. See also SRFI-1 which has an extended @code{delete} (@ref{SRFI-1
  4936. Deleting}), and also an @code{lset-difference} which can delete
  4937. multiple @var{item}s in one call (@ref{SRFI-1 Set Operations}).
  4938. @end deffn
  4939. @deffn {Scheme Procedure} delq! item lst
  4940. @deffnx {Scheme Procedure} delv! item lst
  4941. @deffnx {Scheme Procedure} delete! item lst
  4942. @deffnx {C Function} scm_delq_x (item, lst)
  4943. @deffnx {C Function} scm_delv_x (item, lst)
  4944. @deffnx {C Function} scm_delete_x (item, lst)
  4945. These procedures are destructive versions of @code{delq}, @code{delv}
  4946. and @code{delete}: they modify the pointers in the existing @var{lst}
  4947. rather than creating a new list. Caveat evaluator: Like other
  4948. destructive list functions, these functions cannot modify the binding of
  4949. @var{lst}, and so cannot be used to delete the first element of
  4950. @var{lst} destructively.
  4951. @end deffn
  4952. @deffn {Scheme Procedure} delq1! item lst
  4953. @deffnx {C Function} scm_delq1_x (item, lst)
  4954. Like @code{delq!}, but only deletes the first occurrence of
  4955. @var{item} from @var{lst}. Tests for equality using
  4956. @code{eq?}. See also @code{delv1!} and @code{delete1!}.
  4957. @end deffn
  4958. @deffn {Scheme Procedure} delv1! item lst
  4959. @deffnx {C Function} scm_delv1_x (item, lst)
  4960. Like @code{delv!}, but only deletes the first occurrence of
  4961. @var{item} from @var{lst}. Tests for equality using
  4962. @code{eqv?}. See also @code{delq1!} and @code{delete1!}.
  4963. @end deffn
  4964. @deffn {Scheme Procedure} delete1! item lst
  4965. @deffnx {C Function} scm_delete1_x (item, lst)
  4966. Like @code{delete!}, but only deletes the first occurrence of
  4967. @var{item} from @var{lst}. Tests for equality using
  4968. @code{equal?}. See also @code{delq1!} and @code{delv1!}.
  4969. @end deffn
  4970. @deffn {Scheme Procedure} filter pred lst
  4971. @deffnx {Scheme Procedure} filter! pred lst
  4972. Return a list containing all elements from @var{lst} which satisfy the
  4973. predicate @var{pred}. The elements in the result list have the same
  4974. order as in @var{lst}. The order in which @var{pred} is applied to
  4975. the list elements is not specified.
  4976. @code{filter} does not change @var{lst}, but the result may share a
  4977. tail with it. @code{filter!} may modify @var{lst} to construct its
  4978. return.
  4979. @end deffn
  4980. @node List Searching
  4981. @subsubsection List Searching
  4982. The following procedures search lists for particular elements. They use
  4983. different comparison predicates for comparing list elements with the
  4984. object to be searched. When they fail, they return @code{#f}, otherwise
  4985. they return the sublist whose car is equal to the search object, where
  4986. equality depends on the equality predicate used.
  4987. @rnindex memq
  4988. @deffn {Scheme Procedure} memq x lst
  4989. @deffnx {C Function} scm_memq (x, lst)
  4990. Return the first sublist of @var{lst} whose car is @code{eq?}
  4991. to @var{x} where the sublists of @var{lst} are the non-empty
  4992. lists returned by @code{(list-tail @var{lst} @var{k})} for
  4993. @var{k} less than the length of @var{lst}. If @var{x} does not
  4994. occur in @var{lst}, then @code{#f} (not the empty list) is
  4995. returned.
  4996. @end deffn
  4997. @rnindex memv
  4998. @deffn {Scheme Procedure} memv x lst
  4999. @deffnx {C Function} scm_memv (x, lst)
  5000. Return the first sublist of @var{lst} whose car is @code{eqv?}
  5001. to @var{x} where the sublists of @var{lst} are the non-empty
  5002. lists returned by @code{(list-tail @var{lst} @var{k})} for
  5003. @var{k} less than the length of @var{lst}. If @var{x} does not
  5004. occur in @var{lst}, then @code{#f} (not the empty list) is
  5005. returned.
  5006. @end deffn
  5007. @rnindex member
  5008. @deffn {Scheme Procedure} member x lst
  5009. @deffnx {C Function} scm_member (x, lst)
  5010. Return the first sublist of @var{lst} whose car is
  5011. @code{equal?} to @var{x} where the sublists of @var{lst} are
  5012. the non-empty lists returned by @code{(list-tail @var{lst}
  5013. @var{k})} for @var{k} less than the length of @var{lst}. If
  5014. @var{x} does not occur in @var{lst}, then @code{#f} (not the
  5015. empty list) is returned.
  5016. See also SRFI-1 which has an extended @code{member} function
  5017. (@ref{SRFI-1 Searching}).
  5018. @end deffn
  5019. @node List Mapping
  5020. @subsubsection List Mapping
  5021. List processing is very convenient in Scheme because the process of
  5022. iterating over the elements of a list can be highly abstracted. The
  5023. procedures in this section are the most basic iterating procedures for
  5024. lists. They take a procedure and one or more lists as arguments, and
  5025. apply the procedure to each element of the list. They differ in their
  5026. return value.
  5027. @rnindex map
  5028. @c begin (texi-doc-string "guile" "map")
  5029. @deffn {Scheme Procedure} map proc arg1 arg2 @dots{}
  5030. @deffnx {Scheme Procedure} map-in-order proc arg1 arg2 @dots{}
  5031. @deffnx {C Function} scm_map (proc, arg1, args)
  5032. Apply @var{proc} to each element of the list @var{arg1} (if only two
  5033. arguments are given), or to the corresponding elements of the argument
  5034. lists (if more than two arguments are given). The result(s) of the
  5035. procedure applications are saved and returned in a list. For
  5036. @code{map}, the order of procedure applications is not specified,
  5037. @code{map-in-order} applies the procedure from left to right to the list
  5038. elements.
  5039. @end deffn
  5040. @rnindex for-each
  5041. @c begin (texi-doc-string "guile" "for-each")
  5042. @deffn {Scheme Procedure} for-each proc arg1 arg2 @dots{}
  5043. Like @code{map}, but the procedure is always applied from left to right,
  5044. and the result(s) of the procedure applications are thrown away. The
  5045. return value is not specified.
  5046. @end deffn
  5047. See also SRFI-1 which extends these functions to take lists of unequal
  5048. lengths (@ref{SRFI-1 Fold and Map}).
  5049. @node Vectors
  5050. @subsection Vectors
  5051. @tpindex Vectors
  5052. Vectors are sequences of Scheme objects. Unlike lists, the length of a
  5053. vector, once the vector is created, cannot be changed. The advantage of
  5054. vectors over lists is that the time required to access one element of a vector
  5055. given its @dfn{position} (synonymous with @dfn{index}), a zero-origin number,
  5056. is constant, whereas lists have an access time linear to the position of the
  5057. accessed element in the list.
  5058. Vectors can contain any kind of Scheme object; it is even possible to
  5059. have different types of objects in the same vector. For vectors
  5060. containing vectors, you may wish to use @ref{Arrays} instead.
  5061. Note, too, that vectors are a special case of one dimensional
  5062. non-uniform arrays and that array procedures operate happily on vectors.
  5063. Also see @ref{SRFI-43}, @ref{R6RS Support}, or @ref{R7RS Support}, for
  5064. more comprehensive vector libraries.
  5065. @menu
  5066. * Vector Syntax:: Read syntax for vectors.
  5067. * Vector Creation:: Dynamic vector creation and validation.
  5068. * Vector Accessors:: Accessing and modifying vector contents.
  5069. * Vector Accessing from C:: Ways to work with vectors from C.
  5070. * Uniform Numeric Vectors:: Vectors of unboxed numeric values.
  5071. @end menu
  5072. @node Vector Syntax
  5073. @subsubsection Read Syntax for Vectors
  5074. Vectors can literally be entered in source code, just like strings,
  5075. characters or some of the other data types. The read syntax for vectors
  5076. is as follows: A sharp sign (@code{#}), followed by an opening
  5077. parentheses, all elements of the vector in their respective read syntax,
  5078. and finally a closing parentheses. Like strings, vectors do not have to
  5079. be quoted.
  5080. The following are examples of the read syntax for vectors; where the
  5081. first vector only contains numbers and the second three different object
  5082. types: a string, a symbol and a number in hexadecimal notation.
  5083. @lisp
  5084. #(1 2 3)
  5085. #("Hello" foo #xdeadbeef)
  5086. @end lisp
  5087. @node Vector Creation
  5088. @subsubsection Dynamic Vector Creation and Validation
  5089. Instead of creating a vector implicitly by using the read syntax just
  5090. described, you can create a vector dynamically by calling one of the
  5091. @code{vector} and @code{list->vector} primitives with the list of Scheme
  5092. values that you want to place into a vector. The size of the vector
  5093. thus created is determined implicitly by the number of arguments given.
  5094. @rnindex vector
  5095. @rnindex list->vector
  5096. @deffn {Scheme Procedure} vector arg @dots{}
  5097. @deffnx {Scheme Procedure} list->vector l
  5098. @deffnx {C Function} scm_vector (l)
  5099. Return a newly allocated vector composed of the
  5100. given arguments. Analogous to @code{list}.
  5101. @lisp
  5102. (vector 'a 'b 'c) @result{} #(a b c)
  5103. @end lisp
  5104. @end deffn
  5105. The inverse operation is @code{vector->list}:
  5106. @rnindex vector->list
  5107. @deffn {Scheme Procedure} vector->list v
  5108. @deffnx {C Function} scm_vector_to_list (v)
  5109. Return a newly allocated list composed of the elements of @var{v}.
  5110. @lisp
  5111. (vector->list #(dah dah didah)) @result{} (dah dah didah)
  5112. (list->vector '(dididit dah)) @result{} #(dididit dah)
  5113. @end lisp
  5114. @end deffn
  5115. To allocate a vector with an explicitly specified size, use
  5116. @code{make-vector}. With this primitive you can also specify an initial
  5117. value for the vector elements (the same value for all elements, that
  5118. is):
  5119. @rnindex make-vector
  5120. @deffn {Scheme Procedure} make-vector len [fill]
  5121. @deffnx {C Function} scm_make_vector (len, fill)
  5122. Return a newly allocated vector of @var{len} elements. If a
  5123. second argument is given, then each position is initialized to
  5124. @var{fill}. Otherwise the initial contents of each position is
  5125. unspecified.
  5126. @end deffn
  5127. @deftypefn {C Function} SCM scm_c_make_vector (size_t k, SCM fill)
  5128. Like @code{scm_make_vector}, but the length is given as a @code{size_t}.
  5129. @end deftypefn
  5130. To check whether an arbitrary Scheme value @emph{is} a vector, use the
  5131. @code{vector?} primitive:
  5132. @rnindex vector?
  5133. @deffn {Scheme Procedure} vector? obj
  5134. @deffnx {C Function} scm_vector_p (obj)
  5135. Return @code{#t} if @var{obj} is a vector, otherwise return
  5136. @code{#f}.
  5137. @end deffn
  5138. @deftypefn {C Function} int scm_is_vector (SCM obj)
  5139. Return non-zero when @var{obj} is a vector, otherwise return
  5140. @code{zero}.
  5141. @end deftypefn
  5142. @node Vector Accessors
  5143. @subsubsection Accessing and Modifying Vector Contents
  5144. @code{vector-length} and @code{vector-ref} return information about a
  5145. given vector, respectively its size and the elements that are contained
  5146. in the vector.
  5147. @rnindex vector-length
  5148. @deffn {Scheme Procedure} vector-length vector
  5149. @deffnx {C Function} scm_vector_length (vector)
  5150. Return the number of elements in @var{vector} as an exact integer.
  5151. @end deffn
  5152. @deftypefn {C Function} size_t scm_c_vector_length (SCM vec)
  5153. Return the number of elements in @var{vec} as a @code{size_t}.
  5154. @end deftypefn
  5155. @rnindex vector-ref
  5156. @deffn {Scheme Procedure} vector-ref vec k
  5157. @deffnx {C Function} scm_vector_ref (vec, k)
  5158. Return the contents of position @var{k} of @var{vec}.
  5159. @var{k} must be a valid index of @var{vec}.
  5160. @lisp
  5161. (vector-ref #(1 1 2 3 5 8 13 21) 5) @result{} 8
  5162. (vector-ref #(1 1 2 3 5 8 13 21)
  5163. (let ((i (round (* 2 (acos -1)))))
  5164. (if (inexact? i)
  5165. (inexact->exact i)
  5166. i))) @result{} 13
  5167. @end lisp
  5168. @end deffn
  5169. @anchor{x-scm_c_vector_ref}
  5170. @deftypefn {C Function} SCM scm_c_vector_ref (SCM vec, size_t k)
  5171. Return the contents of position @var{k} (a @code{size_t}) of
  5172. @var{vec}.
  5173. @end deftypefn
  5174. A vector created by one of the dynamic vector constructor procedures
  5175. (@pxref{Vector Creation}) can be modified using the following
  5176. procedures.
  5177. @emph{NOTE:} According to R5RS, it is an error to use any of these
  5178. procedures on a literally read vector, because such vectors should be
  5179. considered as constants. Currently, however, Guile does not detect this
  5180. error.
  5181. @rnindex vector-set!
  5182. @deffn {Scheme Procedure} vector-set! vec k obj
  5183. @deffnx {C Function} scm_vector_set_x (vec, k, obj)
  5184. Store @var{obj} in position @var{k} of @var{vec}.
  5185. @var{k} must be a valid index of @var{vec}.
  5186. The value returned by @samp{vector-set!} is unspecified.
  5187. @lisp
  5188. (let ((vec (vector 0 '(2 2 2 2) "Anna")))
  5189. (vector-set! vec 1 '("Sue" "Sue"))
  5190. vec) @result{} #(0 ("Sue" "Sue") "Anna")
  5191. @end lisp
  5192. @end deffn
  5193. @anchor{x-scm_c_vector_set_x}
  5194. @deftypefn {C Function} void scm_c_vector_set_x (SCM vec, size_t k, SCM obj)
  5195. Store @var{obj} in position @var{k} (a @code{size_t}) of @var{vec}.
  5196. @end deftypefn
  5197. @rnindex vector-fill!
  5198. @anchor{x-vector-fill!}
  5199. @deffn {Scheme Procedure} vector-fill! vec fill [start [end]]
  5200. @deffnx {C Function} scm_vector_fill_x (vec, fill)
  5201. Store @var{fill} in every position of @var{vec} in the range
  5202. [@var{start} ... @var{end}). @var{start} defaults to 0 and @var{end}
  5203. defaults to the length of @var{vec}.
  5204. The value returned by @code{vector-fill!} is unspecified.
  5205. @end deffn
  5206. @rnindex vector-copy
  5207. @anchor{x-vector-copy}
  5208. @deffn {Scheme Procedure} vector-copy vec [start [end]]
  5209. @deffnx {C Function} scm_vector_copy (vec)
  5210. Returns a freshly allocated vector containing the elements of @var{vec}
  5211. in the range [@var{start} ... @var{end}). @var{start} defaults to 0 and
  5212. @var{end} defaults to the length of @var{vec}.
  5213. @end deffn
  5214. @rnindex vector-copy!
  5215. @anchor{x-vector-copy!}
  5216. @deffn {Scheme Procedure} vector-copy! dst at src [start [end]]
  5217. Copy the block of elements from vector @var{src} in the range
  5218. [@var{start} ... @var{end}) into vector @var{dst}, starting at position
  5219. @var{at}. @var{at} and @var{start} default to 0 and @var{end} defaults
  5220. to the length of @var{src}.
  5221. It is an error for @var{dst} to have a length less than @var{at} +
  5222. (@var{end} - @var{start}).
  5223. The order in which elements are copied is unspecified, except that if the
  5224. source and destination overlap, copying takes place as if the source is
  5225. first copied into a temporary vector and then into the destination.
  5226. The value returned by @code{vector-copy!} is unspecified.
  5227. @end deffn
  5228. @deffn {Scheme Procedure} vector-move-left! vec1 start1 end1 vec2 start2
  5229. @deffnx {C Function} scm_vector_move_left_x (vec1, start1, end1, vec2, start2)
  5230. Copy elements from @var{vec1}, positions @var{start1} to @var{end1},
  5231. to @var{vec2} starting at position @var{start2}. @var{start1} and
  5232. @var{start2} are inclusive indices; @var{end1} is exclusive.
  5233. @code{vector-move-left!} copies elements in leftmost order.
  5234. Therefore, in the case where @var{vec1} and @var{vec2} refer to the
  5235. same vector, @code{vector-move-left!} is usually appropriate when
  5236. @var{start1} is greater than @var{start2}.
  5237. The value returned by @code{vector-move-left!} is unspecified.
  5238. @end deffn
  5239. @deffn {Scheme Procedure} vector-move-right! vec1 start1 end1 vec2 start2
  5240. @deffnx {C Function} scm_vector_move_right_x (vec1, start1, end1, vec2, start2)
  5241. Copy elements from @var{vec1}, positions @var{start1} to @var{end1},
  5242. to @var{vec2} starting at position @var{start2}. @var{start1} and
  5243. @var{start2} are inclusive indices; @var{end1} is exclusive.
  5244. @code{vector-move-right!} copies elements in rightmost order.
  5245. Therefore, in the case where @var{vec1} and @var{vec2} refer to the
  5246. same vector, @code{vector-move-right!} is usually appropriate when
  5247. @var{start1} is less than @var{start2}.
  5248. The value returned by @code{vector-move-right!} is unspecified.
  5249. @end deffn
  5250. @node Vector Accessing from C
  5251. @subsubsection Vector Accessing from C
  5252. A vector can be read and modified from C with the functions
  5253. @ref{x-scm_c_vector_ref,@code{scm_c_vector_ref}} and
  5254. @ref{x-scm_c_vector_set_x,@code{scm_c_vector_set_x}}. In addition to
  5255. these functions, there are two other ways to access vectors from C that
  5256. might be more efficient in certain situations: you can use the unsafe
  5257. @emph{vector macros}; or you can use the general framework for accessing
  5258. all kinds of arrays (@pxref{Accessing Arrays from C}), which is more
  5259. verbose, but can deal efficiently with all kinds of vectors (and
  5260. arrays). For arrays of rank 1 whose backing store is a vector, you can
  5261. use the @code{scm_vector_elements} and
  5262. @code{scm_vector_writable_elements} functions as shortcuts.
  5263. @deftypefn {C Macro} size_t SCM_SIMPLE_VECTOR_LENGTH (SCM vec)
  5264. Evaluates to the length of the vector @var{vec}. No type
  5265. checking is done.
  5266. @end deftypefn
  5267. @deftypefn {C Macro} SCM SCM_SIMPLE_VECTOR_REF (SCM vec, size_t idx)
  5268. Evaluates to the element at position @var{idx} in the vector @var{vec}.
  5269. No type or range checking is done.
  5270. @end deftypefn
  5271. @deftypefn {C Macro} void SCM_SIMPLE_VECTOR_SET (SCM vec, size_t idx, SCM val)
  5272. Sets the element at position @var{idx} in the vector @var{vec} to
  5273. @var{val}. No type or range checking is done.
  5274. @end deftypefn
  5275. @deftypefn {C Function} {const SCM *} scm_vector_elements (SCM array, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  5276. Acquire a @ref{Accessing Arrays from C,handle} for @var{array} and
  5277. return a read-only pointer to its elements. @var{array} must be either
  5278. a vector, or an array of rank 1 whose backing store is a vector;
  5279. otherwise an error is signaled. The handle must eventually be released
  5280. with @ref{x-scm_array_handle_release,@code{scm_array_handle_release}}.
  5281. The variables pointed to by @var{lenp} and @var{incp} are filled with
  5282. the number of elements of the array and the increment (number of
  5283. elements) between successive elements, respectively. Successive
  5284. elements of @var{array} need not be contiguous in their underlying
  5285. ``root vector'' returned here; hence the increment is not necessarily
  5286. equal to 1 and may well be negative too (@pxref{Shared Arrays}).
  5287. The following example shows the typical way to use this function. It
  5288. creates a list of all elements of @var{array} (in reverse order).
  5289. @example
  5290. scm_t_array_handle handle;
  5291. size_t i, len;
  5292. ssize_t inc;
  5293. const SCM *elt;
  5294. SCM list;
  5295. elt = scm_vector_elements (array, &handle, &len, &inc);
  5296. list = SCM_EOL;
  5297. for (i = 0; i < len; i++, elt += inc)
  5298. list = scm_cons (*elt, list);
  5299. scm_array_handle_release (&handle);
  5300. @end example
  5301. @end deftypefn
  5302. @deftypefn {C Function} {SCM *} scm_vector_writable_elements (SCM array, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
  5303. Like @code{scm_vector_elements} but the pointer can be used to modify
  5304. the array.
  5305. The following example shows the typical way to use this function. It
  5306. fills an array with @code{#t}.
  5307. @example
  5308. scm_t_array_handle handle;
  5309. size_t i, len;
  5310. ssize_t inc;
  5311. SCM *elt;
  5312. elt = scm_vector_writable_elements (array, &handle, &len, &inc);
  5313. for (i = 0; i < len; i++, elt += inc)
  5314. *elt = SCM_BOOL_T;
  5315. scm_array_handle_release (&handle);
  5316. @end example
  5317. @end deftypefn
  5318. @node Uniform Numeric Vectors
  5319. @subsubsection Uniform Numeric Vectors
  5320. A uniform numeric vector is a vector whose elements are all of a single
  5321. numeric type. Guile offers uniform numeric vectors for signed and
  5322. unsigned 8-bit, 16-bit, 32-bit, and 64-bit integers, two sizes of
  5323. floating point values, and complex floating-point numbers of these two
  5324. sizes. @xref{SRFI-4}, for more information.
  5325. For many purposes, bytevectors work just as well as uniform vectors, and have
  5326. the advantage that they integrate well with binary input and output.
  5327. @xref{Bytevectors}, for more information on bytevectors.
  5328. @node Bit Vectors
  5329. @subsection Bit Vectors
  5330. @noindent
  5331. Bit vectors are zero-origin, one-dimensional arrays of booleans. They
  5332. are displayed as a sequence of @code{0}s and @code{1}s prefixed by
  5333. @code{#*}, e.g.,
  5334. @example
  5335. (make-bitvector 8 #f) @result{}
  5336. #*00000000
  5337. @end example
  5338. Bit vectors are the special case of one dimensional bit arrays, and can
  5339. thus be used with the array procedures, @xref{Arrays}.
  5340. @deffn {Scheme Procedure} bitvector? obj
  5341. Return @code{#t} when @var{obj} is a bitvector, else
  5342. return @code{#f}.
  5343. @end deffn
  5344. @deffn {Scheme Procedure} make-bitvector len [fill]
  5345. Create a new bitvector of length @var{len} and
  5346. optionally initialize all elements to @var{fill}.
  5347. @end deffn
  5348. @deffn {Scheme Procedure} bitvector bit @dots{}
  5349. Create a new bitvector with the arguments as elements.
  5350. @end deffn
  5351. @deffn {Scheme Procedure} bitvector-length vec
  5352. Return the length of the bitvector @var{vec}.
  5353. @end deffn
  5354. @deffn {Scheme Procedure} bitvector-bit-set? vec idx
  5355. @deffnx {Scheme Procedure} bitvector-bit-clear? vec idx
  5356. Return @code{#t} if the bit at index @var{idx} of the bitvector
  5357. @var{vec} is set (for @code{bitvector-bit-set?}) or clear (for
  5358. @code{bitvector-bit-clear?}).
  5359. @end deffn
  5360. @deffn {Scheme Procedure} bitvector-set-bit! vec idx
  5361. @deffnx {Scheme Procedure} bitvector-clear-bit! vec idx
  5362. Set (for @code{bitvector-set-bit!}) or clear (for
  5363. @code{bitvector-clear-bit!}) the bit at index @var{idx} of the bitvector
  5364. @var{vec}.
  5365. @end deffn
  5366. @deffn {Scheme Procedure} bitvector-set-all-bits! vec
  5367. @deffnx {Scheme Procedure} bitvector-clear-all-bits! vec
  5368. @deffnx {Scheme Procedure} bitvector-flip-all-bits! vec
  5369. Set, clear, or flip all bits of @var{vec}.
  5370. @end deffn
  5371. @deffn {Scheme Procedure} list->bitvector list
  5372. @deffnx {C Function} scm_list_to_bitvector (list)
  5373. Return a new bitvector initialized with the elements
  5374. of @var{list}.
  5375. @end deffn
  5376. @deffn {Scheme Procedure} bitvector->list vec
  5377. @deffnx {C Function} scm_bitvector_to_list (vec)
  5378. Return a new list initialized with the elements
  5379. of the bitvector @var{vec}.
  5380. @end deffn
  5381. @deffn {Scheme Procedure} bitvector-copy bitvector [start [end]]
  5382. @deffnx {C Function} scm_bitvector_copy (bitvector, start, end)
  5383. Returns a freshly allocated bitvector containing the elements of @var{bitvector}
  5384. in the range [@var{start} ... @var{end}). @var{start} defaults to 0 and
  5385. @var{end} defaults to the length of @var{bitvector}.
  5386. @end deffn
  5387. @deffn {Scheme Procedure} bitvector-count bitvector
  5388. Return a count of how many entries in @var{bitvector} are set.
  5389. @example
  5390. (bitvector-count #*000111000) @result{} 3
  5391. @end example
  5392. @end deffn
  5393. @deffn {Scheme Procedure} bitvector-count-bits bitvector bits
  5394. Return a count of how many entries in @var{bitvector} are set, with the
  5395. bitvector @var{bits} selecting the entries to consider. @var{bitvector}
  5396. must be at least as long as @var{bits}.
  5397. For example,
  5398. @example
  5399. (bitvector-count-bits #*01110111 #*11001101) @result{} 3
  5400. @end example
  5401. @end deffn
  5402. @deffn {Scheme Procedure} bitvector-position bitvector bool start
  5403. @deffnx {C Function} scm_bitvector_position (bitvector, bool, start)
  5404. Return the index of the first occurrence of @var{bool} in
  5405. @var{bitvector}, starting from @var{start}. If there is no @var{bool}
  5406. entry between @var{start} and the end of @var{bitvector}, then return
  5407. @code{#f}. For example,
  5408. @example
  5409. (bitvector-position #*000101 #t 0) @result{} 3
  5410. (bitvector-position #*0001111 #f 3) @result{} #f
  5411. @end example
  5412. @end deffn
  5413. @deffn {Scheme Procedure} bitvector-set-bits! bitvector bits
  5414. Set entries of @var{bitvector} to @code{#t}, with @var{bits} selecting
  5415. the bits to set. The return value is unspecified. @var{bitvector} must
  5416. be at least as long as @var{bits}.
  5417. @example
  5418. (define bv (bitvector-copy #*11000010))
  5419. (bitvector-set-bits! bv #*10010001)
  5420. bv
  5421. @result{} #*11010011
  5422. @end example
  5423. @end deffn
  5424. @deffn {Scheme Procedure} bitvector-clear-bits! bitvector bits
  5425. Set entries of @var{bitvector} to @code{#f}, with @var{bits} selecting
  5426. the bits to clear. The return value is unspecified. @var{bitvector}
  5427. must be at least as long as @var{bits}.
  5428. @example
  5429. (define bv (bitvector-copy #*11000010))
  5430. (bitvector-clear-bits! bv #*10010001)
  5431. bv
  5432. @result{} #*01000010
  5433. @end example
  5434. @end deffn
  5435. @deftypefn {C Function} int scm_is_bitvector (SCM obj)
  5436. @deftypefnx {C Function} SCM scm_c_make_bitvector (size_t len, SCM fill)
  5437. @deftypefnx {C Function} int scm_bitvector_bit_is_set (SCM vec, size_t idx)
  5438. @deftypefnx {C Function} int scm_bitvector_bit_is_clear (SCM vec, size_t idx)
  5439. @deftypefnx {C Function} void scm_c_bitvector_set_bit_x (SCM vec, size_t idx)
  5440. @deftypefnx {C Function} void scm_c_bitvector_clear_bit_x (SCM vec, size_t idx)
  5441. @deftypefnx {C Function} void scm_c_bitvector_set_bits_x (SCM vec, SCM bits)
  5442. @deftypefnx {C Function} void scm_c_bitvector_clear_bits_x (SCM vec, SCM bits)
  5443. @deftypefnx {C Function} void scm_c_bitvector_set_all_bits_x (SCM vec)
  5444. @deftypefnx {C Function} void scm_c_bitvector_clear_all_bits_x (SCM vec)
  5445. @deftypefnx {C Function} void scm_c_bitvector_flip_all_bits_x (SCM vec)
  5446. @deftypefnx {C Function} size_t scm_c_bitvector_length (SCM bitvector)
  5447. @deftypefnx {C Function} size_t scm_c_bitvector_count (SCM bitvector)
  5448. @deftypefnx {C Function} size_t scm_c_bitvector_count_bits (SCM bitvector, SCM bits)
  5449. C API for the corresponding Scheme bitvector interfaces.
  5450. @end deftypefn
  5451. @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)
  5452. Like @code{scm_vector_elements} (@pxref{Vector Accessing from C}), but
  5453. for bitvectors. The variable pointed to by @var{offp} is set to the
  5454. value returned by @code{scm_array_handle_bit_elements_offset}. See
  5455. @code{scm_array_handle_bit_elements} for how to use the returned
  5456. pointer and the offset.
  5457. @end deftypefn
  5458. @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)
  5459. Like @code{scm_bitvector_elements}, but the pointer is good for reading
  5460. and writing.
  5461. @end deftypefn
  5462. @node Bytevectors
  5463. @subsection Bytevectors
  5464. @cindex bytevector
  5465. @cindex R6RS
  5466. A @dfn{bytevector} is a raw byte string. The @code{(rnrs bytevectors)}
  5467. module provides the programming interface specified by the
  5468. @uref{http://www.r6rs.org/, Revised^6 Report on the Algorithmic Language
  5469. Scheme (R6RS)}. It contains procedures to manipulate bytevectors and
  5470. interpret their contents in a number of ways: as signed or unsigned
  5471. integer of various sizes and endianness, as IEEE-754 floating point
  5472. numbers, or as strings. It is a useful tool to encode and decode binary
  5473. data. The @ref{R7RS Support,R7RS} offers its own set of bytevector
  5474. procedures (@pxref{Bytevector Procedures in R7RS}).
  5475. The R6RS (Section 4.3.4) specifies an external representation for
  5476. bytevectors, whereby the octets (integers in the range 0--255) contained
  5477. in the bytevector are represented as a list prefixed by @code{#vu8}:
  5478. @lisp
  5479. #vu8(1 53 204)
  5480. @end lisp
  5481. denotes a 3-byte bytevector containing the octets 1, 53, and 204. Like
  5482. string literals, booleans, etc., bytevectors are ``self-quoting'', i.e.,
  5483. they do not need to be quoted:
  5484. @lisp
  5485. #vu8(1 53 204)
  5486. @result{} #vu8(1 53 204)
  5487. @end lisp
  5488. Bytevectors can be used with the binary input/output primitives
  5489. (@pxref{Binary I/O}).
  5490. @menu
  5491. * Bytevector Endianness:: Dealing with byte order.
  5492. * Bytevector Manipulation:: Creating, copying, manipulating bytevectors.
  5493. * Bytevectors as Integers:: Interpreting bytes as integers.
  5494. * Bytevectors and Integer Lists:: Converting to/from an integer list.
  5495. * Bytevectors as Floats:: Interpreting bytes as real numbers.
  5496. * Bytevectors as Strings:: Interpreting bytes as Unicode strings.
  5497. * Bytevectors as Arrays:: Guile extension to the bytevector API.
  5498. * Bytevectors as Uniform Vectors:: Bytevectors and SRFI-4.
  5499. * Bytevector Procedures in R7RS:: R7RS interface for bytevectors.
  5500. * Bytevector Slices:: Aliases for parts of a bytevector.
  5501. @end menu
  5502. @node Bytevector Endianness
  5503. @subsubsection Endianness
  5504. @cindex endianness
  5505. @cindex byte order
  5506. @cindex word order
  5507. Some of the following procedures take an @var{endianness} parameter.
  5508. The @dfn{endianness} is defined as the order of bytes in multi-byte
  5509. numbers: numbers encoded in @dfn{big endian} have their most
  5510. significant bytes written first, whereas numbers encoded in
  5511. @dfn{little endian} have their least significant bytes
  5512. first@footnote{Big-endian and little-endian are the most common
  5513. ``endiannesses'', but others do exist. For instance, the GNU MP
  5514. library allows @dfn{word order} to be specified independently of
  5515. @dfn{byte order} (@pxref{Integer Import and Export,,, gmp, The GNU
  5516. Multiple Precision Arithmetic Library Manual}).}.
  5517. Little-endian is the native endianness of the IA32 architecture and
  5518. its derivatives, while big-endian is native to SPARC and PowerPC,
  5519. among others. The @code{native-endianness} procedure returns the
  5520. native endianness of the machine it runs on.
  5521. @deffn {Scheme Procedure} native-endianness
  5522. @deffnx {C Function} scm_native_endianness ()
  5523. Return a value denoting the native endianness of the host machine.
  5524. @end deffn
  5525. @deffn {Scheme Macro} endianness symbol
  5526. Return an object denoting the endianness specified by @var{symbol}. If
  5527. @var{symbol} is neither @code{big} nor @code{little} then an error is
  5528. raised at expand-time.
  5529. @end deffn
  5530. @defvr {C Variable} scm_endianness_big
  5531. @defvrx {C Variable} scm_endianness_little
  5532. The objects denoting big- and little-endianness, respectively.
  5533. @end defvr
  5534. @node Bytevector Manipulation
  5535. @subsubsection Manipulating Bytevectors
  5536. Bytevectors can be created, copied, and analyzed with the following
  5537. procedures and C functions.
  5538. @anchor{x-make-bytevector}
  5539. @deffn {Scheme Procedure} make-bytevector len [fill]
  5540. @deffnx {C Function} scm_make_bytevector (len, fill)
  5541. @deffnx {C Function} scm_c_make_bytevector (size_t len)
  5542. Return a new bytevector of @var{len} bytes. Optionally, if @var{fill}
  5543. is given, fill it with @var{fill}; @var{fill} must be in the range
  5544. [-128,255].
  5545. @end deffn
  5546. @anchor{x-bytevector?}
  5547. @deffn {Scheme Procedure} bytevector? obj
  5548. @deffnx {C Function} scm_bytevector_p (obj)
  5549. Return true if @var{obj} is a bytevector.
  5550. @end deffn
  5551. @deftypefn {C Function} int scm_is_bytevector (SCM obj)
  5552. Equivalent to @code{scm_is_true (scm_bytevector_p (obj))}.
  5553. @end deftypefn
  5554. @anchor{x-bytevector-length}
  5555. @deffn {Scheme Procedure} bytevector-length bv
  5556. @deffnx {C Function} scm_bytevector_length (bv)
  5557. Return the length in bytes of bytevector @var{bv}.
  5558. @end deffn
  5559. @deftypefn {C Function} size_t scm_c_bytevector_length (SCM bv)
  5560. Likewise, return the length in bytes of bytevector @var{bv}.
  5561. @end deftypefn
  5562. @deffn {Scheme Procedure} bytevector=? bv1 bv2
  5563. @deffnx {C Function} scm_bytevector_eq_p (bv1, bv2)
  5564. Return @code{#t} if @var{bv1} equals @var{bv2}---i.e., if they have the same
  5565. length and contents.
  5566. @end deffn
  5567. @deffn {Scheme Procedure} bytevector-fill! bv fill [start [end]]
  5568. @deffnx {C Function} scm_bytevector_fill_x (bv, fill)
  5569. Fill positions [@var{start} ... @var{end}) of bytevector @var{bv} with
  5570. byte @var{fill}. @var{start} defaults to 0 and @var{end} defaults to the
  5571. length of @var{bv}.@footnote{R6RS only defines @code{(bytevector-fill! bv
  5572. fill)}. Arguments @var{start} and @var{end} are a Guile extension
  5573. (cf. @ref{x-vector-fill!,@code{vector-fill!}},
  5574. @ref{x-string-fill!,@code{string-fill!}}).}
  5575. @end deffn
  5576. @anchor{x-r6:bytevector-copy!}
  5577. @deffn {Scheme Procedure} bytevector-copy! source source-start target target-start len
  5578. @deffnx {C Function} scm_bytevector_copy_x (source, source_start, target, target_start, len)
  5579. Copy @var{len} bytes from @var{source} into @var{target}, starting
  5580. reading from @var{source-start} (an index index within @var{source})
  5581. and writing at @var{target-start}.
  5582. It is permitted for the @var{source} and @var{target} regions to
  5583. overlap. In that case, copying takes place as if the source is first
  5584. copied into a temporary bytevector and then into the destination.
  5585. @end deffn
  5586. @anchor{x-r6:bytevector-copy}
  5587. @deffn {Scheme Procedure} bytevector-copy bv
  5588. @deffnx {C Function} scm_bytevector_copy (bv)
  5589. Return a newly allocated copy of @var{bv}.
  5590. @end deffn
  5591. @deftypefn {C Function} scm_t_uint8 scm_c_bytevector_ref (SCM bv, size_t index)
  5592. Return the byte at @var{index} in bytevector @var{bv}.
  5593. @end deftypefn
  5594. @deftypefn {C Function} void scm_c_bytevector_set_x (SCM bv, size_t index, scm_t_uint8 value)
  5595. Set the byte at @var{index} in @var{bv} to @var{value}.
  5596. @end deftypefn
  5597. Low-level C macros are available. They do not perform any
  5598. type-checking; as such they should be used with care.
  5599. @deftypefn {C Macro} size_t SCM_BYTEVECTOR_LENGTH (bv)
  5600. Return the length in bytes of bytevector @var{bv}.
  5601. @end deftypefn
  5602. @deftypefn {C Macro} {signed char *} SCM_BYTEVECTOR_CONTENTS (bv)
  5603. Return a pointer to the contents of bytevector @var{bv}.
  5604. @end deftypefn
  5605. @node Bytevectors as Integers
  5606. @subsubsection Interpreting Bytevector Contents as Integers
  5607. The contents of a bytevector can be interpreted as a sequence of
  5608. integers of any given size, sign, and endianness.
  5609. @lisp
  5610. (let ((bv (make-bytevector 4)))
  5611. (bytevector-u8-set! bv 0 #x12)
  5612. (bytevector-u8-set! bv 1 #x34)
  5613. (bytevector-u8-set! bv 2 #x56)
  5614. (bytevector-u8-set! bv 3 #x78)
  5615. (map (lambda (number)
  5616. (number->string number 16))
  5617. (list (bytevector-u8-ref bv 0)
  5618. (bytevector-u16-ref bv 0 (endianness big))
  5619. (bytevector-u32-ref bv 0 (endianness little)))))
  5620. @result{} ("12" "1234" "78563412")
  5621. @end lisp
  5622. The most generic procedures to interpret bytevector contents as integers
  5623. are described below.
  5624. @deffn {Scheme Procedure} bytevector-uint-ref bv index endianness size
  5625. @deffnx {C Function} scm_bytevector_uint_ref (bv, index, endianness, size)
  5626. Return the @var{size}-byte long unsigned integer at index @var{index} in
  5627. @var{bv}, decoded according to @var{endianness}.
  5628. @end deffn
  5629. @deffn {Scheme Procedure} bytevector-sint-ref bv index endianness size
  5630. @deffnx {C Function} scm_bytevector_sint_ref (bv, index, endianness, size)
  5631. Return the @var{size}-byte long signed integer at index @var{index} in
  5632. @var{bv}, decoded according to @var{endianness}.
  5633. @end deffn
  5634. @deffn {Scheme Procedure} bytevector-uint-set! bv index value endianness size
  5635. @deffnx {C Function} scm_bytevector_uint_set_x (bv, index, value, endianness, size)
  5636. Set the @var{size}-byte long unsigned integer at @var{index} to
  5637. @var{value}, encoded according to @var{endianness}.
  5638. @end deffn
  5639. @deffn {Scheme Procedure} bytevector-sint-set! bv index value endianness size
  5640. @deffnx {C Function} scm_bytevector_sint_set_x (bv, index, value, endianness, size)
  5641. Set the @var{size}-byte long signed integer at @var{index} to
  5642. @var{value}, encoded according to @var{endianness}.
  5643. @end deffn
  5644. The following procedures are similar to the ones above, but specialized
  5645. to a given integer size:
  5646. @anchor{x-bytevector-u8-ref}
  5647. @deffn {Scheme Procedure} bytevector-u8-ref bv index
  5648. @deffnx {Scheme Procedure} bytevector-s8-ref bv index
  5649. @deffnx {Scheme Procedure} bytevector-u16-ref bv index endianness
  5650. @deffnx {Scheme Procedure} bytevector-s16-ref bv index endianness
  5651. @deffnx {Scheme Procedure} bytevector-u32-ref bv index endianness
  5652. @deffnx {Scheme Procedure} bytevector-s32-ref bv index endianness
  5653. @deffnx {Scheme Procedure} bytevector-u64-ref bv index endianness
  5654. @deffnx {Scheme Procedure} bytevector-s64-ref bv index endianness
  5655. @deffnx {C Function} scm_bytevector_u8_ref (bv, index)
  5656. @deffnx {C Function} scm_bytevector_s8_ref (bv, index)
  5657. @deffnx {C Function} scm_bytevector_u16_ref (bv, index, endianness)
  5658. @deffnx {C Function} scm_bytevector_s16_ref (bv, index, endianness)
  5659. @deffnx {C Function} scm_bytevector_u32_ref (bv, index, endianness)
  5660. @deffnx {C Function} scm_bytevector_s32_ref (bv, index, endianness)
  5661. @deffnx {C Function} scm_bytevector_u64_ref (bv, index, endianness)
  5662. @deffnx {C Function} scm_bytevector_s64_ref (bv, index, endianness)
  5663. Return the unsigned @var{n}-bit (signed) integer (where @var{n} is 8,
  5664. 16, 32 or 64) from @var{bv} at @var{index}, decoded according to
  5665. @var{endianness}.
  5666. @end deffn
  5667. @anchor{x-bytevector-u8-set!}
  5668. @deffn {Scheme Procedure} bytevector-u8-set! bv index value
  5669. @deffnx {Scheme Procedure} bytevector-s8-set! bv index value
  5670. @deffnx {Scheme Procedure} bytevector-u16-set! bv index value endianness
  5671. @deffnx {Scheme Procedure} bytevector-s16-set! bv index value endianness
  5672. @deffnx {Scheme Procedure} bytevector-u32-set! bv index value endianness
  5673. @deffnx {Scheme Procedure} bytevector-s32-set! bv index value endianness
  5674. @deffnx {Scheme Procedure} bytevector-u64-set! bv index value endianness
  5675. @deffnx {Scheme Procedure} bytevector-s64-set! bv index value endianness
  5676. @deffnx {C Function} scm_bytevector_u8_set_x (bv, index, value)
  5677. @deffnx {C Function} scm_bytevector_s8_set_x (bv, index, value)
  5678. @deffnx {C Function} scm_bytevector_u16_set_x (bv, index, value, endianness)
  5679. @deffnx {C Function} scm_bytevector_s16_set_x (bv, index, value, endianness)
  5680. @deffnx {C Function} scm_bytevector_u32_set_x (bv, index, value, endianness)
  5681. @deffnx {C Function} scm_bytevector_s32_set_x (bv, index, value, endianness)
  5682. @deffnx {C Function} scm_bytevector_u64_set_x (bv, index, value, endianness)
  5683. @deffnx {C Function} scm_bytevector_s64_set_x (bv, index, value, endianness)
  5684. Store @var{value} as an @var{n}-bit (signed) integer (where @var{n} is
  5685. 8, 16, 32 or 64) in @var{bv} at @var{index}, encoded according to
  5686. @var{endianness}.
  5687. @end deffn
  5688. Finally, a variant specialized for the host's endianness is available
  5689. for each of these functions (with the exception of the @code{u8} and
  5690. @code{s8} accessors, as endianness is about byte order and there is only
  5691. 1 byte):
  5692. @deffn {Scheme Procedure} bytevector-u16-native-ref bv index
  5693. @deffnx {Scheme Procedure} bytevector-s16-native-ref bv index
  5694. @deffnx {Scheme Procedure} bytevector-u32-native-ref bv index
  5695. @deffnx {Scheme Procedure} bytevector-s32-native-ref bv index
  5696. @deffnx {Scheme Procedure} bytevector-u64-native-ref bv index
  5697. @deffnx {Scheme Procedure} bytevector-s64-native-ref bv index
  5698. @deffnx {C Function} scm_bytevector_u16_native_ref (bv, index)
  5699. @deffnx {C Function} scm_bytevector_s16_native_ref (bv, index)
  5700. @deffnx {C Function} scm_bytevector_u32_native_ref (bv, index)
  5701. @deffnx {C Function} scm_bytevector_s32_native_ref (bv, index)
  5702. @deffnx {C Function} scm_bytevector_u64_native_ref (bv, index)
  5703. @deffnx {C Function} scm_bytevector_s64_native_ref (bv, index)
  5704. Return the unsigned @var{n}-bit (signed) integer (where @var{n} is 8,
  5705. 16, 32 or 64) from @var{bv} at @var{index}, decoded according to the
  5706. host's native endianness.
  5707. @end deffn
  5708. @deffn {Scheme Procedure} bytevector-u16-native-set! bv index value
  5709. @deffnx {Scheme Procedure} bytevector-s16-native-set! bv index value
  5710. @deffnx {Scheme Procedure} bytevector-u32-native-set! bv index value
  5711. @deffnx {Scheme Procedure} bytevector-s32-native-set! bv index value
  5712. @deffnx {Scheme Procedure} bytevector-u64-native-set! bv index value
  5713. @deffnx {Scheme Procedure} bytevector-s64-native-set! bv index value
  5714. @deffnx {C Function} scm_bytevector_u16_native_set_x (bv, index, value)
  5715. @deffnx {C Function} scm_bytevector_s16_native_set_x (bv, index, value)
  5716. @deffnx {C Function} scm_bytevector_u32_native_set_x (bv, index, value)
  5717. @deffnx {C Function} scm_bytevector_s32_native_set_x (bv, index, value)
  5718. @deffnx {C Function} scm_bytevector_u64_native_set_x (bv, index, value)
  5719. @deffnx {C Function} scm_bytevector_s64_native_set_x (bv, index, value)
  5720. Store @var{value} as an @var{n}-bit (signed) integer (where @var{n} is
  5721. 8, 16, 32 or 64) in @var{bv} at @var{index}, encoded according to the
  5722. host's native endianness.
  5723. @end deffn
  5724. @node Bytevectors and Integer Lists
  5725. @subsubsection Converting Bytevectors to/from Integer Lists
  5726. Bytevector contents can readily be converted to/from lists of signed or
  5727. unsigned integers:
  5728. @lisp
  5729. (bytevector->sint-list (u8-list->bytevector (make-list 4 255))
  5730. (endianness little) 2)
  5731. @result{} (-1 -1)
  5732. @end lisp
  5733. @deffn {Scheme Procedure} bytevector->u8-list bv
  5734. @deffnx {C Function} scm_bytevector_to_u8_list (bv)
  5735. Return a newly allocated list of unsigned 8-bit integers from the
  5736. contents of @var{bv}.
  5737. @end deffn
  5738. @anchor{x-u8-list->bytevector}
  5739. @deffn {Scheme Procedure} u8-list->bytevector lst
  5740. @deffnx {C Function} scm_u8_list_to_bytevector (lst)
  5741. Return a newly allocated bytevector consisting of the unsigned 8-bit
  5742. integers listed in @var{lst}.
  5743. @end deffn
  5744. @deffn {Scheme Procedure} bytevector->uint-list bv endianness size
  5745. @deffnx {C Function} scm_bytevector_to_uint_list (bv, endianness, size)
  5746. Return a list of unsigned integers of @var{size} bytes representing the
  5747. contents of @var{bv}, decoded according to @var{endianness}.
  5748. @end deffn
  5749. @deffn {Scheme Procedure} bytevector->sint-list bv endianness size
  5750. @deffnx {C Function} scm_bytevector_to_sint_list (bv, endianness, size)
  5751. Return a list of signed integers of @var{size} bytes representing the
  5752. contents of @var{bv}, decoded according to @var{endianness}.
  5753. @end deffn
  5754. @deffn {Scheme Procedure} uint-list->bytevector lst endianness size
  5755. @deffnx {C Function} scm_uint_list_to_bytevector (lst, endianness, size)
  5756. Return a new bytevector containing the unsigned integers listed in
  5757. @var{lst} and encoded on @var{size} bytes according to @var{endianness}.
  5758. @end deffn
  5759. @deffn {Scheme Procedure} sint-list->bytevector lst endianness size
  5760. @deffnx {C Function} scm_sint_list_to_bytevector (lst, endianness, size)
  5761. Return a new bytevector containing the signed integers listed in
  5762. @var{lst} and encoded on @var{size} bytes according to @var{endianness}.
  5763. @end deffn
  5764. @node Bytevectors as Floats
  5765. @subsubsection Interpreting Bytevector Contents as Floating Point Numbers
  5766. @cindex IEEE-754 floating point numbers
  5767. Bytevector contents can also be accessed as IEEE-754 single- or
  5768. double-precision floating point numbers (respectively 32 and 64-bit
  5769. long) using the procedures described here.
  5770. @deffn {Scheme Procedure} bytevector-ieee-single-ref bv index endianness
  5771. @deffnx {Scheme Procedure} bytevector-ieee-double-ref bv index endianness
  5772. @deffnx {C Function} scm_bytevector_ieee_single_ref (bv, index, endianness)
  5773. @deffnx {C Function} scm_bytevector_ieee_double_ref (bv, index, endianness)
  5774. Return the IEEE-754 single-precision floating point number from @var{bv}
  5775. at @var{index} according to @var{endianness}.
  5776. @end deffn
  5777. @deffn {Scheme Procedure} bytevector-ieee-single-set! bv index value endianness
  5778. @deffnx {Scheme Procedure} bytevector-ieee-double-set! bv index value endianness
  5779. @deffnx {C Function} scm_bytevector_ieee_single_set_x (bv, index, value, endianness)
  5780. @deffnx {C Function} scm_bytevector_ieee_double_set_x (bv, index, value, endianness)
  5781. Store real number @var{value} in @var{bv} at @var{index} according to
  5782. @var{endianness}.
  5783. @end deffn
  5784. Specialized procedures are also available:
  5785. @deffn {Scheme Procedure} bytevector-ieee-single-native-ref bv index
  5786. @deffnx {Scheme Procedure} bytevector-ieee-double-native-ref bv index
  5787. @deffnx {C Function} scm_bytevector_ieee_single_native_ref (bv, index)
  5788. @deffnx {C Function} scm_bytevector_ieee_double_native_ref (bv, index)
  5789. Return the IEEE-754 single-precision floating point number from @var{bv}
  5790. at @var{index} according to the host's native endianness.
  5791. @end deffn
  5792. @deffn {Scheme Procedure} bytevector-ieee-single-native-set! bv index value
  5793. @deffnx {Scheme Procedure} bytevector-ieee-double-native-set! bv index value
  5794. @deffnx {C Function} scm_bytevector_ieee_single_native_set_x (bv, index, value)
  5795. @deffnx {C Function} scm_bytevector_ieee_double_native_set_x (bv, index, value)
  5796. Store real number @var{value} in @var{bv} at @var{index} according to
  5797. the host's native endianness.
  5798. @end deffn
  5799. @node Bytevectors as Strings
  5800. @subsubsection Interpreting Bytevector Contents as Unicode Strings
  5801. @cindex Unicode string encoding
  5802. Bytevector contents can also be interpreted as Unicode strings encoded
  5803. in one of the most commonly available encoding formats.
  5804. @xref{Representing Strings as Bytes}, for a more generic interface.
  5805. @lisp
  5806. (utf8->string (u8-list->bytevector '(99 97 102 101)))
  5807. @result{} "cafe"
  5808. (string->utf8 "caf@'e") ;; SMALL LATIN LETTER E WITH ACUTE ACCENT
  5809. @result{} #vu8(99 97 102 195 169)
  5810. @end lisp
  5811. @deftypefn {Scheme Procedure} {} string-utf8-length str
  5812. @deftypefnx {C function} SCM scm_string_utf8_length (str)
  5813. @deftypefnx {C function} size_t scm_c_string_utf8_length (str)
  5814. Return the number of bytes in the UTF-8 representation of @var{str}.
  5815. @end deftypefn
  5816. @deffn {Scheme Procedure} string->utf8 str
  5817. @deffnx {Scheme Procedure} string->utf16 str [endianness]
  5818. @deffnx {Scheme Procedure} string->utf32 str [endianness]
  5819. @deffnx {C Function} scm_string_to_utf8 (str)
  5820. @deffnx {C Function} scm_string_to_utf16 (str, endianness)
  5821. @deffnx {C Function} scm_string_to_utf32 (str, endianness)
  5822. Return a newly allocated bytevector that contains the UTF-8, UTF-16, or
  5823. UTF-32 (aka. UCS-4) encoding of @var{str}. For UTF-16 and UTF-32,
  5824. @var{endianness} should be the symbol @code{big} or @code{little}; when omitted,
  5825. it defaults to big endian.
  5826. @end deffn
  5827. @deffn {Scheme Procedure} utf8->string utf
  5828. @deffnx {Scheme Procedure} utf16->string utf [endianness]
  5829. @deffnx {Scheme Procedure} utf32->string utf [endianness]
  5830. @deffnx {C Function} scm_utf8_to_string (utf)
  5831. @deffnx {C Function} scm_utf16_to_string (utf, endianness)
  5832. @deffnx {C Function} scm_utf32_to_string (utf, endianness)
  5833. Return a newly allocated string that contains from the UTF-8-, UTF-16-,
  5834. or UTF-32-decoded contents of bytevector @var{utf}. For UTF-16 and UTF-32,
  5835. @var{endianness} should be the symbol @code{big} or @code{little}; when omitted,
  5836. it defaults to big endian.
  5837. @end deffn
  5838. @node Bytevectors as Arrays
  5839. @subsubsection Accessing Bytevectors with the Array API
  5840. As an extension to the R6RS, Guile allows bytevectors to be manipulated
  5841. with the @dfn{array} procedures (@pxref{Arrays}). When using these
  5842. APIs, bytes are accessed one at a time as 8-bit unsigned integers:
  5843. @example
  5844. (define bv #vu8(0 1 2 3))
  5845. (array? bv)
  5846. @result{} #t
  5847. (array-rank bv)
  5848. @result{} 1
  5849. (array-ref bv 2)
  5850. @result{} 2
  5851. ;; Note the different argument order on array-set!.
  5852. (array-set! bv 77 2)
  5853. (array-ref bv 2)
  5854. @result{} 77
  5855. (array-type bv)
  5856. @result{} vu8
  5857. @end example
  5858. @node Bytevectors as Uniform Vectors
  5859. @subsubsection Accessing Bytevectors with the SRFI-4 API
  5860. Bytevectors may also be accessed with the SRFI-4 API. @xref{SRFI-4 and
  5861. Bytevectors}, for more information.
  5862. @node Bytevector Procedures in R7RS
  5863. @subsubsection Bytevector Procedures in R7RS
  5864. The @ref{R7RS Support,R7RS} (Section 6.9) defines a set of
  5865. bytevector manipulation procedures, accessible with
  5866. @example
  5867. (use-modules (scheme base))
  5868. @end example
  5869. Of these, @ref{x-make-bytevector,@code{make-bytevector}},
  5870. @ref{x-bytevector?,@code{bytevector?}},
  5871. @ref{x-bytevector-length,@code{bytevector-length}},
  5872. @ref{x-bytevector-u8-ref,@code{bytevector-u8-ref}} and
  5873. @ref{x-bytevector-u8-set!,@code{bytevector-u8-set!}} have the same
  5874. definition as in R6RS. The procedures listed below either have a
  5875. different definition in R7RS and R6RS, or are not defined in R6RS.
  5876. @deffn {Scheme Procedure} bytevector arg @dots{}
  5877. Return a newly allocated bytevector composed of the given arguments.
  5878. Analogous to @code{list}.
  5879. @lisp
  5880. (bytevector 2 3 4) @result{} #vu8(2 3 4)
  5881. @end lisp
  5882. See also @ref{x-u8-list->bytevector,@code{u8-list->bytevector}}.
  5883. @end deffn
  5884. @anchor{x-r7:bytevector-copy}
  5885. @deffn {Scheme Procedure} bytevector-copy bv [start [end]]
  5886. Returns a newly allocated bytevector containing the elements of @var{bv}
  5887. in the range [@var{start} ... @var{end}). @var{start} defaults to 0 and
  5888. @var{end} defaults to the length of @var{bv}.
  5889. @lisp
  5890. (define bv #vu8(0 1 2 3 4 5))
  5891. (bytevector-copy bv) @result{} #vu8(0 1 2 3 4 5)
  5892. (bytevector-copy bv 2) @result{} #vu8(2 3 4 5)
  5893. (bytevector-copy bv 2 4) @result{} #vu8(2 3)
  5894. @end lisp
  5895. See also @ref{x-r6:bytevector-copy,the R6RS version}.
  5896. @end deffn
  5897. @anchor{x-r7:bytevector-copy!}
  5898. @deffn {Scheme Procedure} bytevector-copy! dst at src [start [end]]
  5899. Copy the block of elements from bytevector @var{src} in the range
  5900. [@var{start} ... @var{end}) into bytevector @var{dst}, starting at
  5901. position @var{at}. @var{start} defaults to 0 and @var{end} defaults to
  5902. the length of @var{src}. It is an error for @var{dst}
  5903. to have a length less than @var{at} + (@var{end} - @var{start}).
  5904. See also @ref{x-r6:bytevector-copy!,the R6RS version}. With
  5905. @lisp
  5906. (use-modules ((rnrs bytevectors) #:prefix r6:)
  5907. ((scheme base) #:prefix r7:))
  5908. @end lisp
  5909. the following calls are equivalent:
  5910. @lisp
  5911. (r6:bytevector-copy! source source-start target target-start len)
  5912. (r7:bytevector-copy! target target-start source source-start (+ source-start len))
  5913. @end lisp
  5914. @end deffn
  5915. @rnindex bytevector-append
  5916. @deffn {Scheme Procedure} bytevector-append arg @dots{}
  5917. Return a newly allocated bytevector whose characters form the
  5918. concatenation of the given bytevectors @var{arg} @enddots{}
  5919. @lisp
  5920. (bytevector-append #vu8(0 1 2) #vu8(3 4 5))
  5921. @result{} #vu8(0 1 2 3 4 5)
  5922. @end lisp
  5923. @end deffn
  5924. @node Bytevector Slices
  5925. @subsubsection Bytevector Slices
  5926. @cindex subset, of a bytevector
  5927. @cindex slice, of a bytevector
  5928. @cindex slice, of a uniform vector
  5929. As an extension to the R6RS specification, the @code{(rnrs bytevectors
  5930. gnu)} module provides the @code{bytevector-slice} procedure, which
  5931. returns a bytevector aliasing part of an existing bytevector.
  5932. @deffn {Scheme Procedure} bytevector-slice @var{bv} @var{offset} [@var{size}]
  5933. @deffnx {C Function} scm_bytevector_slice (@var{bv}, @var{offset}, @var{size})
  5934. Return the slice of @var{bv} starting at @var{offset} and counting
  5935. @var{size} bytes. When @var{size} is omitted, the slice covers all
  5936. of @var{bv} starting from @var{offset}. The returned slice shares
  5937. storage with @var{bv}: changes to the slice are visible in @var{bv}
  5938. and vice-versa.
  5939. When @var{bv} is actually a SRFI-4 uniform vector, its element
  5940. type is preserved unless @var{offset} and @var{size} are not aligned
  5941. on its element type size.
  5942. @end deffn
  5943. Here is an example showing how to use it:
  5944. @lisp
  5945. (use-modules (rnrs bytevectors)
  5946. (rnrs bytevectors gnu))
  5947. (define bv (u8-list->bytevector (iota 10)))
  5948. (define slice (bytevector-slice bv 2 3))
  5949. slice
  5950. @result{} #vu8(2 3 4)
  5951. (bytevector-u8-set! slice 0 77)
  5952. slice
  5953. @result{} #vu8(77 3 4)
  5954. bv
  5955. @result{} #vu8(0 1 77 3 4 5 6 7 8 9)
  5956. @end lisp
  5957. @node Arrays
  5958. @subsection Arrays
  5959. @tpindex Arrays
  5960. @dfn{Arrays} are a collection of cells organized into an arbitrary
  5961. number of dimensions. Each cell can be accessed in constant time by
  5962. supplying an index for each dimension.
  5963. In the current implementation, an array uses a vector of some kind for
  5964. the actual storage of its elements. Any kind of vector will do, so you
  5965. can have arrays of uniform numeric values, arrays of characters, arrays
  5966. of bits, and of course, arrays of arbitrary Scheme values. For example,
  5967. arrays with an underlying @code{c64vector} might be nice for digital
  5968. signal processing, while arrays made from a @code{u8vector} might be
  5969. used to hold gray-scale images.
  5970. The number of dimensions of an array is called its @dfn{rank}. Thus,
  5971. a matrix is an array of rank 2, while a vector has rank 1. When
  5972. accessing an array element, you have to specify one exact integer for
  5973. each dimension. These integers are called the @dfn{indices} of the
  5974. element. An array specifies the allowed range of indices for each
  5975. dimension via an inclusive lower and upper bound. These bounds can
  5976. well be negative, but the upper bound must be greater than or equal to
  5977. the lower bound minus one. When all lower bounds of an array are
  5978. zero, it is called a @dfn{zero-origin} array.
  5979. Arrays can be of rank 0, which could be interpreted as a scalar.
  5980. Thus, a zero-rank array can store exactly one object and the list of
  5981. indices of this element is the empty list.
  5982. Arrays contain zero elements when one of their dimensions has a zero
  5983. length. These empty arrays maintain information about their shape: a
  5984. matrix with zero columns and 3 rows is different from a matrix with 3
  5985. columns and zero rows, which again is different from a vector of
  5986. length zero.
  5987. The array procedures are all polymorphic, treating strings, uniform
  5988. numeric vectors, bytevectors, bit vectors and ordinary vectors as one
  5989. dimensional arrays.
  5990. @menu
  5991. * Array Syntax::
  5992. * Array Procedures::
  5993. * Shared Arrays::
  5994. * Arrays as arrays of arrays::
  5995. * Accessing Arrays from C::
  5996. @end menu
  5997. @node Array Syntax
  5998. @subsubsection Array Syntax
  5999. An array is displayed as @code{#} followed by its rank, followed by a
  6000. tag that describes the underlying vector, optionally followed by
  6001. information about its shape, and finally followed by the cells,
  6002. organized into dimensions using parentheses.
  6003. In more words, the array tag is of the form
  6004. @example
  6005. #<rank><vectag><@@lower><:len><@@lower><:len>...
  6006. @end example
  6007. where @code{<rank>} is a positive integer in decimal giving the rank of
  6008. the array. It is omitted when the rank is 1 and the array is non-shared
  6009. and has zero-origin (see below). For shared arrays and for a non-zero
  6010. origin, the rank is always printed even when it is 1 to distinguish
  6011. them from ordinary vectors.
  6012. The @code{<vectag>} part is the tag for a uniform numeric vector, like
  6013. @code{u8}, @code{s16}, etc, @code{b} for bitvectors, or @code{a} for
  6014. strings. It is empty for ordinary vectors.
  6015. The @code{<@@lower>} part is a @samp{@@} character followed by a signed
  6016. integer in decimal giving the lower bound of a dimension. There is one
  6017. @code{<@@lower>} for each dimension. When all lower bounds are zero,
  6018. all @code{<@@lower>} parts are omitted.
  6019. The @code{<:len>} part is a @samp{:} character followed by an unsigned
  6020. integer in decimal giving the length of a dimension. Like for the lower
  6021. bounds, there is one @code{<:len>} for each dimension, and the
  6022. @code{<:len>} part always follows the @code{<@@lower>} part for a
  6023. dimension. Lengths are only then printed when they can't be deduced
  6024. from the nested lists of elements of the array literal, which can happen
  6025. when at least one length is zero.
  6026. As a special case, an array of rank 0 is printed as
  6027. @code{#0<vectag>(<scalar>)}, where @code{<scalar>} is the result of
  6028. printing the single element of the array.
  6029. Thus,
  6030. @table @code
  6031. @item #(1 2 3)
  6032. is an ordinary array of rank 1 with lower bound 0 in dimension 0.
  6033. (I.e., a regular vector.)
  6034. @item #@@2(1 2 3)
  6035. is an ordinary array of rank 1 with lower bound 2 in dimension 0.
  6036. @item #2((1 2 3) (4 5 6))
  6037. is a non-uniform array of rank 2; a 2@cross{}3 matrix with index ranges 0..1
  6038. and 0..2.
  6039. @item #u8(0 1 2)
  6040. is a uniform u8 array of rank 1.
  6041. @item #2u32@@2@@3((1 2) (2 3))
  6042. is a uniform u32 array of rank 2 with index ranges 2..3 and 3..4.
  6043. @item #2()
  6044. is a two-dimensional array with index ranges 0..-1 and 0..-1, i.e.@:
  6045. both dimensions have length zero.
  6046. @item #2:0:2()
  6047. is a two-dimensional array with index ranges 0..-1 and 0..1, i.e.@: the
  6048. first dimension has length zero, but the second has length 2.
  6049. @item #0(12)
  6050. is a rank-zero array with contents 12.
  6051. @end table
  6052. In addition, bytevectors are also arrays, but use a different syntax
  6053. (@pxref{Bytevectors}):
  6054. @table @code
  6055. @item #vu8(1 2 3)
  6056. is a 3-byte long bytevector, with contents 1, 2, 3.
  6057. @end table
  6058. @node Array Procedures
  6059. @subsubsection Array Procedures
  6060. When an array is created, the range of each dimension must be
  6061. specified, e.g., to create a 2@cross{}3 array with a zero-based index:
  6062. @example
  6063. (make-array 'ho 2 3) @result{} #2((ho ho ho) (ho ho ho))
  6064. @end example
  6065. The range of each dimension can also be given explicitly, e.g., another
  6066. way to create the same array:
  6067. @example
  6068. (make-array 'ho '(0 1) '(0 2)) @result{} #2((ho ho ho) (ho ho ho))
  6069. @end example
  6070. The following procedures can be used with arrays (or vectors). An
  6071. argument shown as @var{idx}@dots{} means one parameter for each
  6072. dimension in the array. A @var{idxlist} argument means a list of such
  6073. values, one for each dimension.
  6074. @deffn {Scheme Procedure} array? obj
  6075. @deffnx {C Function} scm_array_p (obj, unused)
  6076. Return @code{#t} if the @var{obj} is an array, and @code{#f} if
  6077. not.
  6078. The second argument to scm_array_p is there for historical reasons,
  6079. but it is not used. You should always pass @code{SCM_UNDEFINED} as
  6080. its value.
  6081. @end deffn
  6082. @deffn {Scheme Procedure} typed-array? obj type
  6083. @deffnx {C Function} scm_typed_array_p (obj, type)
  6084. Return @code{#t} if the @var{obj} is an array of type @var{type}, and
  6085. @code{#f} if not.
  6086. @end deffn
  6087. @deftypefn {C Function} int scm_is_array (SCM obj)
  6088. Return @code{1} if the @var{obj} is an array and @code{0} if not.
  6089. @end deftypefn
  6090. @deftypefn {C Function} int scm_is_typed_array (SCM obj, SCM type)
  6091. Return @code{0} if the @var{obj} is an array of type @var{type}, and
  6092. @code{1} if not.
  6093. @end deftypefn
  6094. @deffn {Scheme Procedure} make-array fill bound @dots{}
  6095. @deffnx {C Function} scm_make_array (fill, bounds)
  6096. Equivalent to @code{(make-typed-array #t @var{fill} @var{bound} ...)}.
  6097. @end deffn
  6098. @deffn {Scheme Procedure} make-typed-array type fill bound @dots{}
  6099. @deffnx {C Function} scm_make_typed_array (type, fill, bounds)
  6100. Create and return an array that has as many dimensions as there are
  6101. @var{bound}s and (maybe) fill it with @var{fill}.
  6102. The underlying storage vector is created according to @var{type},
  6103. which must be a symbol whose name is the `vectag' of the array as
  6104. explained above, or @code{#t} for ordinary, non-specialized arrays.
  6105. For example, using the symbol @code{f64} for @var{type} will create an
  6106. array that uses a @code{f64vector} for storing its elements, and
  6107. @code{a} will use a string.
  6108. When @var{fill} is not the special @emph{unspecified} value, the new
  6109. array is filled with @var{fill}. Otherwise, the initial contents of
  6110. the array is unspecified. The special @emph{unspecified} value is
  6111. stored in the variable @code{*unspecified*} so that for example
  6112. @code{(make-typed-array 'u32 *unspecified* 4)} creates a uninitialized
  6113. @code{u32} vector of length 4.
  6114. Each @var{bound} may be a positive non-zero integer @var{n}, in which
  6115. case the index for that dimension can range from 0 through @var{n}-1; or
  6116. an explicit index range specifier in the form @code{(LOWER UPPER)},
  6117. where both @var{lower} and @var{upper} are integers, possibly less than
  6118. zero, and possibly the same number (however, @var{lower} cannot be
  6119. greater than @var{upper}).
  6120. @end deffn
  6121. @deffn {Scheme Procedure} list->array dimspec list
  6122. Equivalent to @code{(list->typed-array #t @var{dimspec}
  6123. @var{list})}.
  6124. @end deffn
  6125. @deffn {Scheme Procedure} list->typed-array type dimspec list
  6126. @deffnx {C Function} scm_list_to_typed_array (type, dimspec, list)
  6127. Return an array of the type indicated by @var{type} with elements the
  6128. same as those of @var{list}.
  6129. The argument @var{dimspec} determines the number of dimensions of the
  6130. array and their lower bounds. When @var{dimspec} is an exact integer,
  6131. it gives the number of dimensions directly and all lower bounds are
  6132. zero. When it is a list of exact integers, then each element is the
  6133. lower index bound of a dimension, and there will be as many dimensions
  6134. as elements in the list.
  6135. @end deffn
  6136. @deffn {Scheme Procedure} array-type array
  6137. @deffnx {C Function} scm_array_type (array)
  6138. Return the type of @var{array}. This is the `vectag' used for
  6139. printing @var{array} (or @code{#t} for ordinary arrays) and can be
  6140. used with @code{make-typed-array} to create an array of the same kind
  6141. as @var{array}.
  6142. @end deffn
  6143. @deffn {Scheme Procedure} array-ref array idx @dots{}
  6144. @deffnx {C Function} scm_array_ref (array, idxlist)
  6145. Return the element at @code{(idx @dots{})} in @var{array}.
  6146. @example
  6147. (define a (make-array 999 '(1 2) '(3 4)))
  6148. (array-ref a 2 4) @result{} 999
  6149. @end example
  6150. @end deffn
  6151. @deffn {Scheme Procedure} array-in-bounds? array idx @dots{}
  6152. @deffnx {C Function} scm_array_in_bounds_p (array, idxlist)
  6153. Return @code{#t} if the given indices would be acceptable to
  6154. @code{array-ref}.
  6155. @example
  6156. (define a (make-array #f '(1 2) '(3 4)))
  6157. (array-in-bounds? a 2 3) @result{} #t
  6158. (array-in-bounds? a 0 0) @result{} #f
  6159. @end example
  6160. @end deffn
  6161. @deffn {Scheme Procedure} array-set! array obj idx @dots{}
  6162. @deffnx {C Function} scm_array_set_x (array, obj, idxlist)
  6163. Set the element at @code{(idx @dots{})} in @var{array} to @var{obj}.
  6164. The return value is unspecified.
  6165. @example
  6166. (define a (make-array #f '(0 1) '(0 1)))
  6167. (array-set! a #t 1 1)
  6168. a @result{} #2((#f #f) (#f #t))
  6169. @end example
  6170. @end deffn
  6171. @deffn {Scheme Procedure} array-shape array
  6172. @deffnx {Scheme Procedure} array-dimensions array
  6173. @deffnx {C Function} scm_array_dimensions (array)
  6174. Return a list of the bounds for each dimension of @var{array}.
  6175. @code{array-shape} gives @code{(@var{lower} @var{upper})} for each
  6176. dimension. @code{array-dimensions} instead returns just
  6177. @math{@var{upper}+1} for dimensions with a 0 lower bound. Both are
  6178. suitable as input to @code{make-array}.
  6179. For example,
  6180. @example
  6181. (define a (make-array 'foo '(-1 3) 5))
  6182. (array-shape a) @result{} ((-1 3) (0 4))
  6183. (array-dimensions a) @result{} ((-1 3) 5)
  6184. @end example
  6185. @end deffn
  6186. @deffn {Scheme Procedure} array-length array
  6187. @deffnx {C Function} scm_array_length (array)
  6188. @deffnx {C Function} size_t scm_c_array_length (array)
  6189. Return the length of an array: its first dimension. It is an error to
  6190. ask for the length of an array of rank 0.
  6191. @end deffn
  6192. @deffn {Scheme Procedure} array-rank array
  6193. @deffnx {C Function} scm_array_rank (array)
  6194. Return the rank of @var{array}.
  6195. @end deffn
  6196. @deftypefn {C Function} size_t scm_c_array_rank (SCM array)
  6197. Return the rank of @var{array} as a @code{size_t}.
  6198. @end deftypefn
  6199. @deffn {Scheme Procedure} array->list array
  6200. @deffnx {C Function} scm_array_to_list (array)
  6201. Return a list consisting of all the elements, in order, of
  6202. @var{array}.
  6203. @end deffn
  6204. @c FIXME: Describe how the order affects the copying (it matters for
  6205. @c shared arrays with the same underlying root vector, presumably).
  6206. @c
  6207. @deffn {Scheme Procedure} array-copy! src dst
  6208. @deffnx {Scheme Procedure} array-copy-in-order! src dst
  6209. @deffnx {C Function} scm_array_copy_x (src, dst)
  6210. Copy every element from vector or array @var{src} to the corresponding
  6211. element of @var{dst}. @var{dst} must have the same rank as @var{src},
  6212. and be at least as large in each dimension. The return value is
  6213. unspecified.
  6214. @end deffn
  6215. @deffn {Scheme Procedure} array-fill! array fill
  6216. @deffnx {C Function} scm_array_fill_x (array, fill)
  6217. Store @var{fill} in every element of @var{array}. The value returned
  6218. is unspecified.
  6219. @end deffn
  6220. @c begin (texi-doc-string "guile" "array-equal?")
  6221. @deffn {Scheme Procedure} array-equal? array @dots{}
  6222. Return @code{#t} if all arguments are arrays with the same shape, the
  6223. same type, and have corresponding elements which are either
  6224. @code{equal?} or @code{array-equal?}. This function differs from
  6225. @code{equal?} (@pxref{Equality}) in that all arguments must be arrays.
  6226. @end deffn
  6227. @c FIXME: array-for-each doesn't say what happens if the sources have
  6228. @c different index ranges. The code currently iterates over the
  6229. @c indices of the first and expects the others to cover those. That
  6230. @c at least vaguely matches array-map!, but is it meant to be a
  6231. @c documented feature?
  6232. @deffn {Scheme Procedure} array-map! dst proc src @dots{}
  6233. @deffnx {Scheme Procedure} array-map-in-order! dst proc src @dots{}
  6234. @deffnx {C Function} scm_array_map_x (dst, proc, srclist)
  6235. Set each element of the @var{dst} array to values obtained from calls to
  6236. @var{proc}. The list of @var{src} arguments may be empty. The value
  6237. returned is unspecified.
  6238. Each call is @code{(@var{proc} @var{elem} @dots{})}, where each
  6239. @var{elem} is from the corresponding @var{src} array, at the
  6240. @var{dst} index. @code{array-map-in-order!} makes the calls in
  6241. row-major order, @code{array-map!} makes them in an unspecified order.
  6242. The @var{src} arrays must have the same number of dimensions as
  6243. @var{dst}, and must have a range for each dimension which covers the
  6244. range in @var{dst}. This ensures all @var{dst} indices are valid in
  6245. each @var{src}.
  6246. @end deffn
  6247. @deffn {Scheme Procedure} array-for-each proc src1 src2 @dots{}
  6248. @deffnx {C Function} scm_array_for_each (proc, src1, srclist)
  6249. Apply @var{proc} to each tuple of elements of @var{src1} @var{src2}
  6250. @dots{}, in row-major order. The value returned is unspecified.
  6251. @end deffn
  6252. @deffn {Scheme Procedure} array-index-map! dst proc
  6253. @deffnx {C Function} scm_array_index_map_x (dst, proc)
  6254. Set each element of the @var{dst} array to values returned by calls to
  6255. @var{proc}. The value returned is unspecified.
  6256. Each call is @code{(@var{proc} @var{i1} @dots{} @var{iN})}, where
  6257. @var{i1}@dots{}@var{iN} is the destination index, one parameter for
  6258. each dimension. The order in which the calls are made is unspecified.
  6259. For example, to create a @m{4\times4, 4x4} matrix representing a
  6260. cyclic group,
  6261. @tex
  6262. \advance\leftskip by 2\lispnarrowing {
  6263. $\left(\matrix{%
  6264. 0 & 1 & 2 & 3 \cr
  6265. 1 & 2 & 3 & 0 \cr
  6266. 2 & 3 & 0 & 1 \cr
  6267. 3 & 0 & 1 & 2 \cr
  6268. }\right)$} \par
  6269. @end tex
  6270. @ifnottex
  6271. @example
  6272. / 0 1 2 3 \
  6273. | 1 2 3 0 |
  6274. | 2 3 0 1 |
  6275. \ 3 0 1 2 /
  6276. @end example
  6277. @end ifnottex
  6278. @example
  6279. (define a (make-array #f 4 4))
  6280. (array-index-map! a (lambda (i j)
  6281. (modulo (+ i j) 4)))
  6282. @end example
  6283. @end deffn
  6284. An additional array function is available in the module
  6285. @code{(ice-9 arrays)}. It can be used with:
  6286. @example
  6287. (use-modules (ice-9 arrays))
  6288. @end example
  6289. @deffn {Scheme Procedure} array-copy src
  6290. Return a new array with the same elements, type and shape as
  6291. @var{src}. However, the array increments may be different from those of
  6292. @var{src}. In the current implementation, the returned array will be in
  6293. row-major order, but that might change in the future. Use
  6294. @code{array-copy!} on an array of known order if that is a concern.
  6295. @end deffn
  6296. @node Shared Arrays
  6297. @subsubsection Shared Arrays
  6298. @deffn {Scheme Procedure} make-shared-array oldarray mapfunc bound @dots{}
  6299. @deffnx {C Function} scm_make_shared_array (oldarray, mapfunc, boundlist)
  6300. Return a new array which shares the storage of @var{oldarray}.
  6301. Changes made through either affect the same underlying storage. The
  6302. @var{bound} @dots{} arguments are the shape of the new array, the same
  6303. as @code{make-array} (@pxref{Array Procedures}).
  6304. @var{mapfunc} translates coordinates from the new array to the
  6305. @var{oldarray}. It's called as @code{(@var{mapfunc} newidx1 @dots{})}
  6306. with one parameter for each dimension of the new array, and should
  6307. return a list of indices for @var{oldarray}, one for each dimension of
  6308. @var{oldarray}.
  6309. @var{mapfunc} must be affine linear, meaning that each @var{oldarray}
  6310. index must be formed by adding integer multiples (possibly negative)
  6311. of some or all of @var{newidx1} etc, plus a possible integer offset.
  6312. The multiples and offset must be the same in each call.
  6313. @sp 1
  6314. One good use for a shared array is to restrict the range of some
  6315. dimensions, so as to apply say @code{array-for-each} or
  6316. @code{array-fill!} to only part of an array. The plain @code{list}
  6317. function can be used for @var{mapfunc} in this case, making no changes
  6318. to the index values. For example,
  6319. @example
  6320. (make-shared-array #2((a b c) (d e f) (g h i)) list 3 2)
  6321. @result{} #2((a b) (d e) (g h))
  6322. @end example
  6323. The new array can have fewer dimensions than @var{oldarray}, for
  6324. example to take a column from an array.
  6325. @example
  6326. (make-shared-array #2((a b c) (d e f) (g h i))
  6327. (lambda (i) (list i 2))
  6328. '(0 2))
  6329. @result{} #1(c f i)
  6330. @end example
  6331. A diagonal can be taken by using the single new array index for both
  6332. row and column in the old array. For example,
  6333. @example
  6334. (make-shared-array #2((a b c) (d e f) (g h i))
  6335. (lambda (i) (list i i))
  6336. '(0 2))
  6337. @result{} #1(a e i)
  6338. @end example
  6339. Dimensions can be increased by for instance considering portions of a
  6340. one dimensional array as rows in a two dimensional array.
  6341. (@code{array-contents} below can do the opposite, flattening an
  6342. array.)
  6343. @example
  6344. (make-shared-array #1(a b c d e f g h i j k l)
  6345. (lambda (i j) (list (+ (* i 3) j)))
  6346. 4 3)
  6347. @result{} #2((a b c) (d e f) (g h i) (j k l))
  6348. @end example
  6349. By negating an index the order that elements appear can be reversed.
  6350. The following just reverses the column order,
  6351. @example
  6352. (make-shared-array #2((a b c) (d e f) (g h i))
  6353. (lambda (i j) (list i (- 2 j)))
  6354. 3 3)
  6355. @result{} #2((c b a) (f e d) (i h g))
  6356. @end example
  6357. A fixed offset on indexes allows for instance a change from a 0 based
  6358. to a 1 based array,
  6359. @example
  6360. (define x #2((a b c) (d e f) (g h i)))
  6361. (define y (make-shared-array x
  6362. (lambda (i j) (list (1- i) (1- j)))
  6363. '(1 3) '(1 3)))
  6364. (array-ref x 0 0) @result{} a
  6365. (array-ref y 1 1) @result{} a
  6366. @end example
  6367. A multiple on an index allows every Nth element of an array to be
  6368. taken. The following is every third element,
  6369. @example
  6370. (make-shared-array #1(a b c d e f g h i j k l)
  6371. (lambda (i) (list (* i 3)))
  6372. 4)
  6373. @result{} #1(a d g j)
  6374. @end example
  6375. The above examples can be combined to make weird and wonderful
  6376. selections from an array, but it's important to note that because
  6377. @var{mapfunc} must be affine linear, arbitrary permutations are not
  6378. possible.
  6379. In the current implementation, @var{mapfunc} is not called for every
  6380. access to the new array but only on some sample points to establish a
  6381. base and stride for new array indices in @var{oldarray} data. A few
  6382. sample points are enough because @var{mapfunc} is linear.
  6383. @end deffn
  6384. @deffn {Scheme Procedure} shared-array-increments array
  6385. @deffnx {C Function} scm_shared_array_increments (array)
  6386. For each dimension, return the distance between elements in the root vector.
  6387. @end deffn
  6388. @deffn {Scheme Procedure} shared-array-offset array
  6389. @deffnx {C Function} scm_shared_array_offset (array)
  6390. Return the root vector index of the first element in the array.
  6391. @end deffn
  6392. @deffn {Scheme Procedure} shared-array-root array
  6393. @deffnx {C Function} scm_shared_array_root (array)
  6394. Return the root vector of a shared array.
  6395. @end deffn
  6396. @deffn {Scheme Procedure} array-contents array [strict]
  6397. @deffnx {C Function} scm_array_contents (array, strict)
  6398. If @var{array} may be @dfn{unrolled} into a one dimensional shared array
  6399. without changing their order (last subscript changing fastest), then
  6400. @code{array-contents} returns that shared array, otherwise it returns
  6401. @code{#f}. All arrays made by @code{make-array} and
  6402. @code{make-typed-array} may be unrolled, some arrays made by
  6403. @code{make-shared-array} may not be.
  6404. If the optional argument @var{strict} is provided, a shared array will
  6405. be returned only if its elements are stored internally contiguous in
  6406. memory.
  6407. @end deffn
  6408. @deffn {Scheme Procedure} transpose-array array dim1 dim2 @dots{}
  6409. @deffnx {C Function} scm_transpose_array (array, dimlist)
  6410. Return an array sharing contents with @var{array}, but with
  6411. dimensions arranged in a different order. There must be one
  6412. @var{dim} argument for each dimension of @var{array}.
  6413. @var{dim1}, @var{dim2}, @dots{} should be integers between 0
  6414. and the rank of the array to be returned. Each integer in that
  6415. range must appear at least once in the argument list.
  6416. The values of @var{dim1}, @var{dim2}, @dots{} correspond to
  6417. dimensions in the array to be returned, and their positions in the
  6418. argument list to dimensions of @var{array}. Several @var{dim}s
  6419. may have the same value, in which case the returned array will
  6420. have smaller rank than @var{array}.
  6421. @lisp
  6422. (transpose-array '#2((a b) (c d)) 1 0) @result{} #2((a c) (b d))
  6423. (transpose-array '#2((a b) (c d)) 0 0) @result{} #1(a d)
  6424. (transpose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 1 0) @result{}
  6425. #2((a 4) (b 5) (c 6))
  6426. @end lisp
  6427. @end deffn
  6428. @node Arrays as arrays of arrays
  6429. @subsubsection Arrays as arrays of arrays
  6430. @cindex array cell
  6431. One can see an array of rank @math{n} (an
  6432. @math{n}-array) as an array of lower rank where the elements are
  6433. themselves arrays (`cells').
  6434. @cindex array frame
  6435. @cindex frame rank
  6436. We speak of the first @math{n-k} dimensions of the array as the
  6437. @math{n-k}-`frame' of the array, while the last @math{k} dimensions are
  6438. the dimensions of the @math{k}-`cells'. For example, a 3-array can be
  6439. seen as a 2-array of vectors (1-arrays) or as a 1-array of matrices
  6440. (2-arrays). In each case, the vectors or matrices are the 1-cells or
  6441. 2-cells of the array. This terminology originates in the J language.
  6442. @cindex array slice
  6443. @cindex prefix slice
  6444. The more vague concept of a `slice' refers to a subset of the array
  6445. where some indices are fixed and others are left free. As a Guile data
  6446. object, a cell is the same as a `prefix slice' (the first @math{n-k}
  6447. indices into the original array are fixed), except that a 0-cell is not
  6448. a shared array of the original array, but a 0-slice (where all the
  6449. indices into the original array are fixed) is.
  6450. @cindex enclosed array
  6451. Before @w{version 2.0}, Guile had a feature called `enclosed arrays' to
  6452. create special `array of arrays' objects. The functions in this section
  6453. do not need special types; instead, the frame rank is given in the
  6454. arguments, either implicitly (through the number of indices) or
  6455. explicitly.
  6456. @deffn {Scheme Procedure} array-cell-ref array idx @dots{}
  6457. @deffnx {C Function} scm_array_cell_ref (array, idxlist)
  6458. If the length of @var{idxlist} equals the rank @math{n} of @var{array},
  6459. return the element at @code{(idx @dots{})}, just like @code{(array-ref
  6460. array idx @dots{})}. If, however, the length @math{k} of @var{idxlist}
  6461. is smaller than @math{n}, then return the @math{(n-k)}-cell of
  6462. @var{array} given by @var{idxlist}, as a shared array.
  6463. For example:
  6464. @lisp
  6465. (array-cell-ref #2((a b) (c d)) 0) @result{} #1(a b)
  6466. (array-cell-ref #2((a b) (c d)) 1) @result{} #1(c d)
  6467. (array-cell-ref #2((a b) (c d)) 1 1) @result{} d
  6468. (array-cell-ref #2((a b) (c d))) @result{} #2((a b) (c d))
  6469. @end lisp
  6470. @code{(apply array-cell-ref array indices)} is equivalent to
  6471. @lisp
  6472. (let ((len (length indices)))
  6473. (if (= (array-rank a) len)
  6474. (apply array-ref a indices)
  6475. (apply make-shared-array a
  6476. (lambda t (append indices t))
  6477. (drop (array-dimensions a) len))))
  6478. @end lisp
  6479. @end deffn
  6480. @deffn {Scheme Procedure} array-slice array idx @dots{}
  6481. @deffnx {C Function} scm_array_slice (array, idxlist)
  6482. Like @code{(array-cell-ref array idx @dots{})}, but return a 0-rank
  6483. shared array into @var{ARRAY} if the length of @var{idxlist} matches the
  6484. rank of @var{array}. This can be useful when using @var{ARRAY} as a
  6485. place to write to.
  6486. Compare:
  6487. @lisp
  6488. (array-cell-ref #2((a b) (c d)) 1 1) @result{} d
  6489. (array-slice #2((a b) (c d)) 1 1) @result{} #0(d)
  6490. (define a (make-array 'a 2 2))
  6491. (array-fill! (array-slice a 1 1) 'b)
  6492. a @result{} #2((a a) (a b)).
  6493. (array-fill! (array-cell-ref a 1 1) 'b) @result{} error: not an array
  6494. @end lisp
  6495. @code{(apply array-slice array indices)} is equivalent to
  6496. @lisp
  6497. (apply make-shared-array a
  6498. (lambda t (append indices t))
  6499. (drop (array-dimensions a) (length indices)))
  6500. @end lisp
  6501. @end deffn
  6502. @deffn {Scheme Procedure} array-cell-set! array x idx @dots{}
  6503. @deffnx {C Function} scm_array_cell_set_x (array, x, idxlist)
  6504. If the length of @var{idxlist} equals the rank @math{n} of
  6505. @var{array}, set the element at @code{(idx @dots{})} of @var{array} to
  6506. @var{x}, just like @code{(array-set! array x idx @dots{})}. If,
  6507. however, the length @math{k} of @var{idxlist} is smaller than
  6508. @math{n}, then copy the @math{(n-k)}-rank array @var{x}
  6509. into the @math{(n-k)}-cell of @var{array} given by
  6510. @var{idxlist}. In this case, the last @math{(n-k)} dimensions of
  6511. @var{array} and the dimensions of @var{x} must match exactly.
  6512. This function returns the modified @var{array}.
  6513. For example:
  6514. @lisp
  6515. (array-cell-set! (make-array 'a 2 2) 'b 1 1)
  6516. @result{} #2((a a) (a b))
  6517. (array-cell-set! (make-array 'a 2 2) #(x y) 1)
  6518. @result{} #2((a a) (x y))
  6519. @end lisp
  6520. Note that @code{array-cell-set!} expects elements, not arrays, when
  6521. the destination has rank 0. Use @code{array-slice} for the opposite
  6522. behavior.
  6523. @lisp
  6524. (array-cell-set! (make-array 'a 2 2) #0(b) 1 1)
  6525. @result{} #2((a a) (a #0(b)))
  6526. (let ((a (make-array 'a 2 2)))
  6527. (array-copy! #0(b) (array-slice a 1 1)) a)
  6528. @result{} #2((a a) (a b))
  6529. @end lisp
  6530. @code{(apply array-cell-set! array x indices)} is equivalent to
  6531. @lisp
  6532. (let ((len (length indices)))
  6533. (if (= (array-rank array) len)
  6534. (apply array-set! array x indices)
  6535. (array-copy! x (apply array-cell-ref array indices)))
  6536. array)
  6537. @end lisp
  6538. @end deffn
  6539. @deffn {Scheme Procedure} array-slice-for-each frame-rank op x @dots{}
  6540. @deffnx {C Function} scm_array_slice_for_each (array, frame_rank, op, xlist)
  6541. Each @var{x} must be an array of rank ≥ @var{frame-rank}, and
  6542. the first @var{frame-rank} dimensions of each @var{x} must all be the
  6543. same. @var{array-slice-for-each} calls @var{op} with each set of
  6544. (rank(@var{x}) - @var{frame-rank})-cells from @var{x}, in unspecified order.
  6545. @var{array-slice-for-each} allows you to loop over cells of any rank
  6546. without having to carry an index list or construct shared arrays
  6547. manually. The slices passed to @var{op} are always shared arrays of
  6548. @var{X}, even if they are of rank 0, so it is possible to write to them.
  6549. This function returns an unspecified value.
  6550. For example, to sort each row of rank-2 array @code{a}:
  6551. @lisp
  6552. (array-slice-for-each 1 (lambda (x) (sort! x <)) a)
  6553. @end lisp
  6554. As another example, let @code{a} be a rank-2 array where each row is a
  6555. 2-element vector @math{(x,y)}. Let's compute the arguments of these
  6556. vectors and store them in rank-1 array @code{b}.
  6557. @lisp
  6558. (array-slice-for-each 1
  6559. (lambda (a b)
  6560. (array-set! b (atan (array-ref a 1) (array-ref a 0))))
  6561. a b)
  6562. @end lisp
  6563. @code{(apply array-slice-for-each frame-rank op x)} is equivalent to
  6564. @lisp
  6565. (let ((frame (take (array-dimensions (car x)) frank)))
  6566. (unless (every (lambda (x)
  6567. (equal? frame (take (array-dimensions x) frank)))
  6568. (cdr x))
  6569. (error))
  6570. (array-index-map!
  6571. (apply make-shared-array (make-array #t) (const '()) frame)
  6572. (lambda i (apply op (map (lambda (x) (apply array-slice x i)) x)))))
  6573. @end lisp
  6574. @end deffn
  6575. @deffn {Scheme Procedure} array-slice-for-each-in-order frame-rank op x @dots{}
  6576. @deffnx {C Function} scm_array_slice_for_each_in_order (array, frame_rank, op, xlist)
  6577. Same as @code{array-slice-for-each}, but the arguments are traversed
  6578. sequentially and in row-major order.
  6579. @end deffn
  6580. @node Accessing Arrays from C
  6581. @subsubsection Accessing Arrays from C
  6582. For interworking with external C code, Guile provides an API to allow C
  6583. code to access the elements of a Scheme array. In particular, for
  6584. uniform numeric arrays, the API exposes the underlying uniform data as a
  6585. C array of numbers of the relevant type.
  6586. While pointers to the elements of an array are in use, the array itself
  6587. must be protected so that the pointer remains valid. Such a protected
  6588. array is said to be @dfn{reserved}. A reserved array can be read but
  6589. modifications to it that would cause the pointer to its elements to
  6590. become invalid are prevented. When you attempt such a modification, an
  6591. error is signaled.
  6592. (This is similar to locking the array while it is in use, but without
  6593. the danger of a deadlock. In a multi-threaded program, you will need
  6594. additional synchronization to avoid modifying reserved arrays.)
  6595. You must take care to always unreserve an array after reserving it,
  6596. even in the presence of non-local exits. If a non-local exit can
  6597. happen between these two calls, you should install a dynwind context
  6598. that releases the array when it is left (@pxref{Dynamic Wind}).
  6599. In addition, array reserving and unreserving must be properly
  6600. paired. For instance, when reserving two or more arrays in a certain
  6601. order, you need to unreserve them in the opposite order.
  6602. Once you have reserved an array and have retrieved the pointer to its
  6603. elements, you must figure out the layout of the elements in memory.
  6604. Guile allows slices to be taken out of arrays without actually making a
  6605. copy, such as making an alias for the diagonal of a matrix that can be
  6606. treated as a vector. Arrays that result from such an operation are not
  6607. stored contiguously in memory and when working with their elements
  6608. directly, you need to take this into account.
  6609. The layout of array elements in memory can be defined via a
  6610. @emph{mapping function} that computes a scalar position from a vector of
  6611. indices. The scalar position then is the offset of the element with the
  6612. given indices from the start of the storage block of the array.
  6613. In Guile, this mapping function is restricted to be @dfn{affine}: all
  6614. mapping functions of Guile arrays can be written as @code{p = b +
  6615. c[0]*i[0] + c[1]*i[1] + ... + c[n-1]*i[n-1]} where @code{i[k]} is the
  6616. @nicode{k}th index and @code{n} is the rank of the array. For
  6617. example, a matrix of size 3x3 would have @code{b == 0}, @code{c[0] ==
  6618. 3} and @code{c[1] == 1}. When you transpose this matrix (with
  6619. @code{transpose-array}, say), you will get an array whose mapping
  6620. function has @code{b == 0}, @code{c[0] == 1} and @code{c[1] == 3}.
  6621. The function @code{scm_array_handle_dims} gives you (indirect) access to
  6622. the coefficients @code{c[k]}.
  6623. @c XXX
  6624. Note that there are no functions for accessing the elements of a
  6625. character array yet. Once the string implementation of Guile has been
  6626. changed to use Unicode, we will provide them.
  6627. @deftp {C Type} scm_t_array_handle
  6628. This is a structure type that holds all information necessary to manage
  6629. the reservation of arrays as explained above. Structures of this type
  6630. must be allocated on the stack and must only be accessed by the
  6631. functions listed below.
  6632. @end deftp
  6633. @deftypefn {C Function} void scm_array_get_handle (SCM array, scm_t_array_handle *handle)
  6634. Reserve @var{array}, which must be an array, and prepare @var{handle} to
  6635. be used with the functions below. You must eventually call
  6636. @code{scm_array_handle_release} on @var{handle}, and do this in a
  6637. properly nested fashion, as explained above. The structure pointed to
  6638. by @var{handle} does not need to be initialized before calling this
  6639. function.
  6640. @end deftypefn
  6641. @anchor{x-scm_array_handle_release}
  6642. @deftypefn {C Function} void scm_array_handle_release (scm_t_array_handle *handle)
  6643. End the array reservation represented by @var{handle}. After a call to
  6644. this function, @var{handle} might be used for another reservation.
  6645. @end deftypefn
  6646. @deftypefn {C Function} size_t scm_array_handle_rank (scm_t_array_handle *handle)
  6647. Return the rank of the array represented by @var{handle}.
  6648. @end deftypefn
  6649. @deftp {C Type} scm_t_array_dim
  6650. This structure type holds information about the layout of one dimension
  6651. of an array. It includes the following fields:
  6652. @table @code
  6653. @item ssize_t lbnd
  6654. @itemx ssize_t ubnd
  6655. The lower and upper bounds (both inclusive) of the permissible index
  6656. range for the given dimension. Both values can be negative, but
  6657. @var{lbnd} is always less than or equal to @var{ubnd}.
  6658. @item ssize_t inc
  6659. The distance from one element of this dimension to the next. Note, too,
  6660. that this can be negative.
  6661. @end table
  6662. @end deftp
  6663. @deftypefn {C Function} {const scm_t_array_dim *} scm_array_handle_dims (scm_t_array_handle *handle)
  6664. Return a pointer to a C vector of information about the dimensions of
  6665. the array represented by @var{handle}. This pointer is valid as long as
  6666. the array remains reserved. As explained above, the
  6667. @code{scm_t_array_dim} structures returned by this function can be used
  6668. calculate the position of an element in the storage block of the array
  6669. from its indices.
  6670. This position can then be used as an index into the C array pointer
  6671. returned by the various @code{scm_array_handle_<foo>_elements}
  6672. functions, or with @code{scm_array_handle_ref} and
  6673. @code{scm_array_handle_set}.
  6674. Here is how one can compute the position @var{pos} of an element given
  6675. its indices in the vector @var{indices}:
  6676. @example
  6677. ssize_t indices[RANK];
  6678. scm_t_array_dim *dims;
  6679. ssize_t pos;
  6680. size_t i;
  6681. pos = 0;
  6682. for (i = 0; i < RANK; i++)
  6683. @{
  6684. if (indices[i] < dims[i].lbnd || indices[i] > dims[i].ubnd)
  6685. out_of_range ();
  6686. pos += (indices[i] - dims[i].lbnd) * dims[i].inc;
  6687. @}
  6688. @end example
  6689. @end deftypefn
  6690. @deftypefn {C Function} ssize_t scm_array_handle_pos (scm_t_array_handle *handle, SCM indices)
  6691. Compute the position corresponding to @var{indices}, a list of
  6692. indices. The position is computed as described above for
  6693. @code{scm_array_handle_dims}. The number of the indices and their
  6694. range is checked and an appropriate error is signaled for invalid
  6695. indices.
  6696. @end deftypefn
  6697. @deftypefn {C Function} SCM scm_array_handle_ref (scm_t_array_handle *handle, ssize_t pos)
  6698. Return the element at position @var{pos} in the storage block of the
  6699. array represented by @var{handle}. Any kind of array is acceptable. No
  6700. range checking is done on @var{pos}.
  6701. @end deftypefn
  6702. @deftypefn {C Function} void scm_array_handle_set (scm_t_array_handle *handle, ssize_t pos, SCM val)
  6703. Set the element at position @var{pos} in the storage block of the array
  6704. represented by @var{handle} to @var{val}. Any kind of array is
  6705. acceptable. No range checking is done on @var{pos}. An error is
  6706. signaled when the array can not store @var{val}.
  6707. @end deftypefn
  6708. @deftypefn {C Function} {const SCM *} scm_array_handle_elements (scm_t_array_handle *handle)
  6709. Return a pointer to the elements of a ordinary array of general Scheme
  6710. values (i.e., a non-uniform array) for reading. This pointer is valid
  6711. as long as the array remains reserved.
  6712. @end deftypefn
  6713. @deftypefn {C Function} {SCM *} scm_array_handle_writable_elements (scm_t_array_handle *handle)
  6714. Like @code{scm_array_handle_elements}, but the pointer is good for
  6715. reading and writing.
  6716. @end deftypefn
  6717. @deftypefn {C Function} {const void *} scm_array_handle_uniform_elements (scm_t_array_handle *handle)
  6718. Return a pointer to the elements of a uniform numeric array for reading.
  6719. This pointer is valid as long as the array remains reserved. The size
  6720. of each element is given by @code{scm_array_handle_uniform_element_size}.
  6721. @end deftypefn
  6722. @deftypefn {C Function} {void *} scm_array_handle_uniform_writable_elements (scm_t_array_handle *handle)
  6723. Like @code{scm_array_handle_uniform_elements}, but the pointer is good
  6724. reading and writing.
  6725. @end deftypefn
  6726. @deftypefn {C Function} size_t scm_array_handle_uniform_element_size (scm_t_array_handle *handle)
  6727. Return the size of one element of the uniform numeric array represented
  6728. by @var{handle}.
  6729. @end deftypefn
  6730. @deftypefn {C Function} {const scm_t_uint8 *} scm_array_handle_u8_elements (scm_t_array_handle *handle)
  6731. @deftypefnx {C Function} {const scm_t_int8 *} scm_array_handle_s8_elements (scm_t_array_handle *handle)
  6732. @deftypefnx {C Function} {const scm_t_uint16 *} scm_array_handle_u16_elements (scm_t_array_handle *handle)
  6733. @deftypefnx {C Function} {const scm_t_int16 *} scm_array_handle_s16_elements (scm_t_array_handle *handle)
  6734. @deftypefnx {C Function} {const scm_t_uint32 *} scm_array_handle_u32_elements (scm_t_array_handle *handle)
  6735. @deftypefnx {C Function} {const scm_t_int32 *} scm_array_handle_s32_elements (scm_t_array_handle *handle)
  6736. @deftypefnx {C Function} {const scm_t_uint64 *} scm_array_handle_u64_elements (scm_t_array_handle *handle)
  6737. @deftypefnx {C Function} {const scm_t_int64 *} scm_array_handle_s64_elements (scm_t_array_handle *handle)
  6738. @deftypefnx {C Function} {const float *} scm_array_handle_f32_elements (scm_t_array_handle *handle)
  6739. @deftypefnx {C Function} {const double *} scm_array_handle_f64_elements (scm_t_array_handle *handle)
  6740. @deftypefnx {C Function} {const float *} scm_array_handle_c32_elements (scm_t_array_handle *handle)
  6741. @deftypefnx {C Function} {const double *} scm_array_handle_c64_elements (scm_t_array_handle *handle)
  6742. Return a pointer to the elements of a uniform numeric array of the
  6743. indicated kind for reading. This pointer is valid as long as the array
  6744. remains reserved.
  6745. The pointers for @code{c32} and @code{c64} uniform numeric arrays point
  6746. to pairs of floating point numbers. The even index holds the real part,
  6747. the odd index the imaginary part of the complex number.
  6748. @end deftypefn
  6749. @deftypefn {C Function} {scm_t_uint8 *} scm_array_handle_u8_writable_elements (scm_t_array_handle *handle)
  6750. @deftypefnx {C Function} {scm_t_int8 *} scm_array_handle_s8_writable_elements (scm_t_array_handle *handle)
  6751. @deftypefnx {C Function} {scm_t_uint16 *} scm_array_handle_u16_writable_elements (scm_t_array_handle *handle)
  6752. @deftypefnx {C Function} {scm_t_int16 *} scm_array_handle_s16_writable_elements (scm_t_array_handle *handle)
  6753. @deftypefnx {C Function} {scm_t_uint32 *} scm_array_handle_u32_writable_elements (scm_t_array_handle *handle)
  6754. @deftypefnx {C Function} {scm_t_int32 *} scm_array_handle_s32_writable_elements (scm_t_array_handle *handle)
  6755. @deftypefnx {C Function} {scm_t_uint64 *} scm_array_handle_u64_writable_elements (scm_t_array_handle *handle)
  6756. @deftypefnx {C Function} {scm_t_int64 *} scm_array_handle_s64_writable_elements (scm_t_array_handle *handle)
  6757. @deftypefnx {C Function} {float *} scm_array_handle_f32_writable_elements (scm_t_array_handle *handle)
  6758. @deftypefnx {C Function} {double *} scm_array_handle_f64_writable_elements (scm_t_array_handle *handle)
  6759. @deftypefnx {C Function} {float *} scm_array_handle_c32_writable_elements (scm_t_array_handle *handle)
  6760. @deftypefnx {C Function} {double *} scm_array_handle_c64_writable_elements (scm_t_array_handle *handle)
  6761. Like @code{scm_array_handle_<kind>_elements}, but the pointer is good
  6762. for reading and writing.
  6763. @end deftypefn
  6764. @deftypefn {C Function} {const scm_t_uint32 *} scm_array_handle_bit_elements (scm_t_array_handle *handle)
  6765. Return a pointer to the words that store the bits of the represented
  6766. array, which must be a bit array.
  6767. Unlike other arrays, bit arrays have an additional offset that must be
  6768. figured into index calculations. That offset is returned by
  6769. @code{scm_array_handle_bit_elements_offset}.
  6770. To find a certain bit you first need to calculate its position as
  6771. explained above for @code{scm_array_handle_dims} and then add the
  6772. offset. This gives the absolute position of the bit, which is always a
  6773. non-negative integer.
  6774. Each word of the bit array storage block contains exactly 32 bits, with
  6775. the least significant bit in that word having the lowest absolute
  6776. position number. The next word contains the next 32 bits.
  6777. Thus, the following code can be used to access a bit whose position
  6778. according to @code{scm_array_handle_dims} is given in @var{pos}:
  6779. @example
  6780. SCM bit_array;
  6781. scm_t_array_handle handle;
  6782. scm_t_uint32 *bits;
  6783. ssize_t pos;
  6784. size_t abs_pos;
  6785. size_t word_pos, mask;
  6786. scm_array_get_handle (&bit_array, &handle);
  6787. bits = scm_array_handle_bit_elements (&handle);
  6788. pos = ...
  6789. abs_pos = pos + scm_array_handle_bit_elements_offset (&handle);
  6790. word_pos = abs_pos / 32;
  6791. mask = 1L << (abs_pos % 32);
  6792. if (bits[word_pos] & mask)
  6793. /* bit is set. */
  6794. scm_array_handle_release (&handle);
  6795. @end example
  6796. @end deftypefn
  6797. @deftypefn {C Function} {scm_t_uint32 *} scm_array_handle_bit_writable_elements (scm_t_array_handle *handle)
  6798. Like @code{scm_array_handle_bit_elements} but the pointer is good for
  6799. reading and writing. You must take care not to modify bits outside of
  6800. the allowed index range of the array, even for contiguous arrays.
  6801. @end deftypefn
  6802. @node VLists
  6803. @subsection VLists
  6804. @cindex vlist
  6805. The @code{(ice-9 vlist)} module provides an implementation of the @dfn{VList}
  6806. data structure designed by Phil Bagwell in 2002. VLists are immutable lists,
  6807. which can contain any Scheme object. They improve on standard Scheme linked
  6808. lists in several areas:
  6809. @itemize
  6810. @item
  6811. Random access has typically constant-time complexity.
  6812. @item
  6813. Computing the length of a VList has time complexity logarithmic in the number of
  6814. elements.
  6815. @item
  6816. VLists use less storage space than standard lists.
  6817. @item
  6818. VList elements are stored in contiguous regions, which improves memory locality
  6819. and leads to more efficient use of hardware caches.
  6820. @end itemize
  6821. The idea behind VLists is to store vlist elements in increasingly large
  6822. contiguous blocks (implemented as vectors here). These blocks are linked to one
  6823. another using a pointer to the next block and an offset within that block. The
  6824. size of these blocks form a geometric series with ratio
  6825. @code{block-growth-factor} (2 by default).
  6826. The VList structure also serves as the basis for the @dfn{VList-based hash
  6827. lists} or ``vhashes'', an immutable dictionary type (@pxref{VHashes}).
  6828. However, the current implementation in @code{(ice-9 vlist)} has several
  6829. noteworthy shortcomings:
  6830. @itemize
  6831. @item
  6832. It is @emph{not} thread-safe. Although operations on vlists are all
  6833. @dfn{referentially transparent} (i.e., purely functional), adding elements to a
  6834. vlist with @code{vlist-cons} mutates part of its internal structure, which makes
  6835. it non-thread-safe. This could be fixed, but it would slow down
  6836. @code{vlist-cons}.
  6837. @item
  6838. @code{vlist-cons} always allocates at least as much memory as @code{cons}.
  6839. Again, Phil Bagwell describes how to fix it, but that would require tuning the
  6840. garbage collector in a way that may not be generally beneficial.
  6841. @item
  6842. @code{vlist-cons} is a Scheme procedure compiled to bytecode, and it does not
  6843. compete with the straightforward C implementation of @code{cons}, and with the
  6844. fact that the VM has a special @code{cons} instruction.
  6845. @end itemize
  6846. We hope to address these in the future.
  6847. The programming interface exported by @code{(ice-9 vlist)} is defined below.
  6848. Most of it is the same as SRFI-1 with an added @code{vlist-} prefix to function
  6849. names.
  6850. @deffn {Scheme Procedure} vlist? obj
  6851. Return true if @var{obj} is a VList.
  6852. @end deffn
  6853. @defvr {Scheme Variable} vlist-null
  6854. The empty VList. Note that it's possible to create an empty VList not
  6855. @code{eq?} to @code{vlist-null}; thus, callers should always use
  6856. @code{vlist-null?} when testing whether a VList is empty.
  6857. @end defvr
  6858. @deffn {Scheme Procedure} vlist-null? vlist
  6859. Return true if @var{vlist} is empty.
  6860. @end deffn
  6861. @deffn {Scheme Procedure} vlist-cons item vlist
  6862. Return a new vlist with @var{item} as its head and @var{vlist} as its tail.
  6863. @end deffn
  6864. @deffn {Scheme Procedure} vlist-head vlist
  6865. Return the head of @var{vlist}.
  6866. @end deffn
  6867. @deffn {Scheme Procedure} vlist-tail vlist
  6868. Return the tail of @var{vlist}.
  6869. @end deffn
  6870. @defvr {Scheme Variable} block-growth-factor
  6871. A fluid that defines the growth factor of VList blocks, 2 by default.
  6872. @end defvr
  6873. The functions below provide the usual set of higher-level list operations.
  6874. @deffn {Scheme Procedure} vlist-fold proc init vlist
  6875. @deffnx {Scheme Procedure} vlist-fold-right proc init vlist
  6876. Fold over @var{vlist}, calling @var{proc} for each element, as for SRFI-1
  6877. @code{fold} and @code{fold-right} (@pxref{SRFI-1, @code{fold}}).
  6878. @end deffn
  6879. @deffn {Scheme Procedure} vlist-ref vlist index
  6880. Return the element at index @var{index} in @var{vlist}. This is typically a
  6881. constant-time operation.
  6882. @end deffn
  6883. @deffn {Scheme Procedure} vlist-length vlist
  6884. Return the length of @var{vlist}. This is typically logarithmic in the number
  6885. of elements in @var{vlist}.
  6886. @end deffn
  6887. @deffn {Scheme Procedure} vlist-reverse vlist
  6888. Return a new @var{vlist} whose content are those of @var{vlist} in reverse
  6889. order.
  6890. @end deffn
  6891. @deffn {Scheme Procedure} vlist-map proc vlist
  6892. Map @var{proc} over the elements of @var{vlist} and return a new vlist.
  6893. @end deffn
  6894. @deffn {Scheme Procedure} vlist-for-each proc vlist
  6895. Call @var{proc} on each element of @var{vlist}. The result is unspecified.
  6896. @end deffn
  6897. @deffn {Scheme Procedure} vlist-drop vlist count
  6898. Return a new vlist that does not contain the @var{count} first elements of
  6899. @var{vlist}. This is typically a constant-time operation.
  6900. @end deffn
  6901. @deffn {Scheme Procedure} vlist-take vlist count
  6902. Return a new vlist that contains only the @var{count} first elements of
  6903. @var{vlist}.
  6904. @end deffn
  6905. @deffn {Scheme Procedure} vlist-filter pred vlist
  6906. Return a new vlist containing all the elements from @var{vlist} that satisfy
  6907. @var{pred}.
  6908. @end deffn
  6909. @deffn {Scheme Procedure} vlist-delete x vlist [equal?]
  6910. Return a new vlist corresponding to @var{vlist} without the elements
  6911. @var{equal?} to @var{x}.
  6912. @end deffn
  6913. @deffn {Scheme Procedure} vlist-unfold p f g seed [tail-gen]
  6914. @deffnx {Scheme Procedure} vlist-unfold-right p f g seed [tail]
  6915. Return a new vlist, as for SRFI-1 @code{unfold} and @code{unfold-right}
  6916. (@pxref{SRFI-1, @code{unfold}}).
  6917. @end deffn
  6918. @deffn {Scheme Procedure} vlist-append vlist @dots{}
  6919. Append the given vlists and return the resulting vlist.
  6920. @end deffn
  6921. @deffn {Scheme Procedure} list->vlist lst
  6922. Return a new vlist whose contents correspond to @var{lst}.
  6923. @end deffn
  6924. @deffn {Scheme Procedure} vlist->list vlist
  6925. Return a new list whose contents match those of @var{vlist}.
  6926. @end deffn
  6927. @node Record Overview
  6928. @subsection Record Overview
  6929. @cindex record
  6930. @cindex structure
  6931. @dfn{Records}, also called @dfn{structures}, are Scheme's primary
  6932. mechanism to define new disjoint types. A @dfn{record type} defines a
  6933. list of @dfn{fields} that instances of the type consist of. This is like
  6934. C's @code{struct}.
  6935. Historically, Guile has offered several different ways to define record
  6936. types and to create records, offering different features, and making
  6937. different trade-offs. Over the years, each ``standard'' has also come
  6938. with its own new record interface, leading to a maze of record APIs.
  6939. At the highest level is SRFI-9, a high-level record interface
  6940. implemented by most Scheme implementations (@pxref{SRFI-9 Records}). It
  6941. defines a simple and efficient syntactic abstraction of record types and
  6942. their associated type predicate, fields, and field accessors. SRFI-9 is
  6943. suitable for most uses, and this is the recommended way to create record
  6944. types in Guile. Similar high-level record APIs include SRFI-35
  6945. (@pxref{SRFI-35}) and R6RS records (@pxref{rnrs records syntactic}).
  6946. Then comes Guile's historical ``records'' API (@pxref{Records}). Record
  6947. types defined this way are first-class objects. Introspection
  6948. facilities are available, allowing users to query the list of fields or
  6949. the value of a specific field at run-time, without prior knowledge of
  6950. the type.
  6951. Finally, the common denominator of these interfaces is Guile's
  6952. @dfn{structure} API (@pxref{Structures}). Guile's structures are the
  6953. low-level building block for all other record APIs. Application writers
  6954. will normally not need to use it.
  6955. Records created with these APIs may all be pattern-matched using Guile's
  6956. standard pattern matcher (@pxref{Pattern Matching}).
  6957. @node SRFI-9 Records
  6958. @subsection SRFI-9 Records
  6959. @cindex SRFI-9
  6960. @cindex record
  6961. SRFI-9 standardizes a syntax for defining new record types and creating
  6962. predicate, constructor, and field getter and setter functions. In Guile
  6963. this is the recommended option to create new record types (@pxref{Record
  6964. Overview}). It can be used with:
  6965. @example
  6966. (use-modules (srfi srfi-9))
  6967. @end example
  6968. @deffn {Scheme Syntax} define-record-type type @* (constructor fieldname @dots{}) @* predicate @* (fieldname accessor [modifier]) @dots{}
  6969. @sp 1
  6970. Create a new record type, and make various @code{define}s for using
  6971. it. This syntax can only occur at the top-level, not nested within
  6972. some other form.
  6973. @var{type} is bound to the record type, which is as per the return
  6974. from the core @code{make-record-type}. @var{type} also provides the
  6975. name for the record, as per @code{record-type-name}.
  6976. @var{constructor} is bound to a function to be called as
  6977. @code{(@var{constructor} fieldval @dots{})} to create a new record of
  6978. this type. The arguments are initial values for the fields, one
  6979. argument for each field, in the order they appear in the
  6980. @code{define-record-type} form.
  6981. The @var{fieldname}s provide the names for the record fields, as per
  6982. the core @code{record-type-fields} etc, and are referred to in the
  6983. subsequent accessor/modifier forms.
  6984. @var{predicate} is bound to a function to be called as
  6985. @code{(@var{predicate} obj)}. It returns @code{#t} or @code{#f}
  6986. according to whether @var{obj} is a record of this type.
  6987. Each @var{accessor} is bound to a function to be called
  6988. @code{(@var{accessor} record)} to retrieve the respective field from a
  6989. @var{record}. Similarly each @var{modifier} is bound to a function to
  6990. be called @code{(@var{modifier} record val)} to set the respective
  6991. field in a @var{record}.
  6992. @end deffn
  6993. @noindent
  6994. An example will illustrate typical usage,
  6995. @example
  6996. (define-record-type <employee>
  6997. (make-employee name age salary)
  6998. employee?
  6999. (name employee-name)
  7000. (age employee-age set-employee-age!)
  7001. (salary employee-salary set-employee-salary!))
  7002. @end example
  7003. This creates a new employee data type, with name, age and salary
  7004. fields. Accessor functions are created for each field, but no
  7005. modifier function for the name (the intention in this example being
  7006. that it's established only when an employee object is created). These
  7007. can all then be used as for example,
  7008. @example
  7009. <employee> @result{} #<record-type <employee>>
  7010. (define fred (make-employee "Fred" 45 20000.00))
  7011. (employee? fred) @result{} #t
  7012. (employee-age fred) @result{} 45
  7013. (set-employee-salary! fred 25000.00) ;; pay rise
  7014. @end example
  7015. The functions created by @code{define-record-type} are ordinary
  7016. top-level @code{define}s. They can be redefined or @code{set!} as
  7017. desired, exported from a module, etc.
  7018. @unnumberedsubsubsec Non-toplevel Record Definitions
  7019. The SRFI-9 specification explicitly disallows record definitions in a
  7020. non-toplevel context, such as inside @code{lambda} body or inside a
  7021. @var{let} block. However, Guile's implementation does not enforce that
  7022. restriction.
  7023. @unnumberedsubsubsec Custom Printers
  7024. You may use @code{set-record-type-printer!} to customize the default printing
  7025. behavior of records. This is a Guile extension and is not part of SRFI-9. It
  7026. is located in the @nicode{(srfi srfi-9 gnu)} module.
  7027. @deffn {Scheme Syntax} set-record-type-printer! type proc
  7028. Where @var{type} corresponds to the first argument of @code{define-record-type},
  7029. and @var{proc} is a procedure accepting two arguments, the record to print, and
  7030. an output port.
  7031. @end deffn
  7032. @noindent
  7033. This example prints the employee's name in brackets, for instance @code{[Fred]}.
  7034. @example
  7035. (set-record-type-printer! <employee>
  7036. (lambda (record port)
  7037. (write-char #\[ port)
  7038. (display (employee-name record) port)
  7039. (write-char #\] port)))
  7040. @end example
  7041. @unnumberedsubsubsec Functional ``Setters''
  7042. @cindex functional setters
  7043. When writing code in a functional style, it is desirable to never alter
  7044. the contents of records. For such code, a simple way to return new
  7045. record instances based on existing ones is highly desirable.
  7046. The @code{(srfi srfi-9 gnu)} module extends SRFI-9 with facilities to
  7047. return new record instances based on existing ones, only with one or
  7048. more field values changed---@dfn{functional setters}. First, the
  7049. @code{define-immutable-record-type} works like
  7050. @code{define-record-type}, except that fields are immutable and setters
  7051. are defined as functional setters.
  7052. @deffn {Scheme Syntax} define-immutable-record-type type @* (constructor fieldname @dots{}) @* predicate @* (fieldname accessor [modifier]) @dots{}
  7053. Define @var{type} as a new record type, like @code{define-record-type}.
  7054. However, the record type is made @emph{immutable} (records may not be
  7055. mutated, even with @code{struct-set!}), and any @var{modifier} is
  7056. defined to be a functional setter---a procedure that returns a new
  7057. record instance with the specified field changed, and leaves the
  7058. original unchanged (see example below.)
  7059. @end deffn
  7060. @noindent
  7061. In addition, the generic @code{set-field} and @code{set-fields} macros
  7062. may be applied to any SRFI-9 record.
  7063. @deffn {Scheme Syntax} set-field record (field sub-fields ...) value
  7064. Return a new record of @var{record}'s type whose fields are equal to
  7065. the corresponding fields of @var{record} except for the one specified by
  7066. @var{field}.
  7067. @var{field} must be the name of the getter corresponding to the field of
  7068. @var{record} being ``set''. Subsequent @var{sub-fields} must be record
  7069. getters designating sub-fields within that field value to be set (see
  7070. example below.)
  7071. @end deffn
  7072. @deffn {Scheme Syntax} set-fields record ((field sub-fields ...) value) ...
  7073. Like @code{set-field}, but can be used to set more than one field at a
  7074. time. This expands to code that is more efficient than a series of
  7075. single @code{set-field} calls.
  7076. @end deffn
  7077. To illustrate the use of functional setters, let's assume these two
  7078. record type definitions:
  7079. @example
  7080. (define-record-type <address>
  7081. (address street city country)
  7082. address?
  7083. (street address-street)
  7084. (city address-city)
  7085. (country address-country))
  7086. (define-immutable-record-type <person>
  7087. (person age email address)
  7088. person?
  7089. (age person-age set-person-age)
  7090. (email person-email set-person-email)
  7091. (address person-address set-person-address))
  7092. @end example
  7093. @noindent
  7094. First, note that the @code{<person>} record type definition introduces
  7095. named functional setters. These may be used like this:
  7096. @example
  7097. (define fsf-address
  7098. (address "Franklin Street" "Boston" "USA"))
  7099. (define rms
  7100. (person 30 "rms@@gnu.org" fsf-address))
  7101. (and (equal? (set-person-age rms 60)
  7102. (person 60 "rms@@gnu.org" fsf-address))
  7103. (= (person-age rms) 30))
  7104. @result{} #t
  7105. @end example
  7106. @noindent
  7107. Here, the original @code{<person>} record, to which @var{rms} is bound,
  7108. is left unchanged.
  7109. Now, suppose we want to change both the street and age of @var{rms}.
  7110. This can be achieved using @code{set-fields}:
  7111. @example
  7112. (set-fields rms
  7113. ((person-age) 60)
  7114. ((person-address address-street) "Temple Place"))
  7115. @result{} #<<person> age: 60 email: "rms@@gnu.org"
  7116. address: #<<address> street: "Temple Place" city: "Boston" country: "USA">>
  7117. @end example
  7118. @noindent
  7119. Notice how the above changed two fields of @var{rms}, including the
  7120. @code{street} field of its @code{address} field, in a concise way. Also
  7121. note that @code{set-fields} works equally well for types defined with
  7122. just @code{define-record-type}.
  7123. @node Records
  7124. @subsection Records
  7125. A @dfn{record type} is a first class object representing a user-defined
  7126. data type. A @dfn{record} is an instance of a record type.
  7127. Note that in many ways, this interface is too low-level for every-day
  7128. use. Most uses of records are better served by SRFI-9 records.
  7129. @xref{SRFI-9 Records}.
  7130. @deffn {Scheme Procedure} record? obj
  7131. Return @code{#t} if @var{obj} is a record of any type and @code{#f}
  7132. otherwise.
  7133. Note that @code{record?} may be true of any Scheme value; there is no
  7134. promise that records are disjoint with other Scheme types.
  7135. @end deffn
  7136. @deffn {Scheme Procedure} make-record-type type-name field-names [print] @
  7137. [#:parent=@code{#f}] [#:uid=@code{#f}] @
  7138. [#:extensible?=@code{#f}] [#:opaque?=@code{#f}] @
  7139. [#:allow-duplicate-field-names?=@code{#t}]
  7140. Create and return a new @dfn{record-type descriptor}.
  7141. @var{type-name} is a string naming the type. Currently it's only used
  7142. in the printed representation of records, and in diagnostics.
  7143. @var{field-names} is a list of elements of the form @code{(immutable
  7144. @var{name})}, @code{(mutable @var{name})}, or @var{name}, where
  7145. @var{name} are symbols naming the fields of a record of the type.
  7146. Duplicates are not allowed among these symbols, unless
  7147. @var{allow-duplicate-field-names?} is true.
  7148. @example
  7149. (make-record-type "employee" '(name age salary))
  7150. @end example
  7151. The optional @var{print} argument is a function used by
  7152. @code{display}, @code{write}, etc, for printing a record of the new
  7153. type. It's called as @code{(@var{print} record port)} and should look
  7154. at @var{record} and write to @var{port}.
  7155. Pass the @code{#:parent} keyword to derive a record type from a
  7156. supertype. A derived record type has the fields from its parent type,
  7157. followed by fields declared in the @code{make-record-type} call. Record
  7158. predicates and field accessors for instance of a parent type will also
  7159. work on any instance of a subtype.
  7160. @cindex extensible record types
  7161. @cindex record types, extensible
  7162. Allowing record subtyping has a small amount of overhead. To avoid this
  7163. overhead, prevent extensibility by passing @code{#:extensible? #f}.
  7164. By default, record types in Guile are not extensible.
  7165. @cindex prefab record types
  7166. @cindex record types, prefab
  7167. @cindex record types, nongenerative
  7168. Generally speaking, calling @code{make-record-type} returns a fresh
  7169. record type; it @emph{generates} new record types. However sometimes
  7170. you only want to define a record type if one hasn't been defined
  7171. already. For a @emph{nongenerative} record type definition, pass a
  7172. symbol as the @code{#:uid} keyword parameter. If a record with the
  7173. given @var{uid} was already defined, it will be returned instead. The
  7174. type name, fields, parent (if any), and so on for the previously-defined
  7175. type must be compatible.
  7176. @cindex record types, opaque
  7177. R6RS defines a notion of ``opaque'' record types. Given an instance of
  7178. an opaque record type, one cannot obtain a run-time representation of
  7179. the record type. @xref{rnrs records procedural}, for full details. The
  7180. @code{#:opaque?} flag is used by Guile's R6RS layer to record this
  7181. information. The default is determined by whether the parent type, if
  7182. any, was opaque.
  7183. Fields are mutable by default, meaning that @code{record-modifier} will
  7184. return a procedure that can update a record in place. Specifying a
  7185. field using the form @code{(immutable @var{name})} instead marks a field
  7186. as immutable.
  7187. @end deffn
  7188. @deffn {Scheme Procedure} record-constructor rtd
  7189. Return a procedure for constructing new members of the type represented
  7190. by @var{rtd}. The result will be a procedure accepting exactly as many
  7191. arguments as there are fields in the record type.
  7192. @end deffn
  7193. @deffn {Scheme Procedure} record-predicate rtd
  7194. Return a procedure for testing membership in the type represented by
  7195. @var{rtd}. The returned procedure accepts exactly one argument and
  7196. returns a true value if the argument is a member of the indicated record
  7197. type; it returns a false value otherwise.
  7198. @end deffn
  7199. @deffn {Scheme Procedure} record-accessor rtd field-name
  7200. Return a procedure for reading the value of a particular field of a
  7201. member of the type represented by @var{rtd}. The returned procedure
  7202. accepts exactly one argument which must be a record of the appropriate
  7203. type; it returns the current value of the field named by the symbol
  7204. @var{field-name} in that record.
  7205. If @var{field-name} is a symbol, it must be a member of the list of
  7206. field-names in the call to @code{make-record-type} that created the type
  7207. represented by @var{rtd}. If multiple fields in @var{rtd} have the same
  7208. name, @code{record-accessor} returns the first one.
  7209. If @var{field-name} is an integer, it should be an index into
  7210. @code{(record-type-fields @var{rtd})}. This allows accessing fields
  7211. with duplicate names.
  7212. @end deffn
  7213. @deffn {Scheme Procedure} record-modifier rtd field-name
  7214. Return a procedure for writing the value of a particular field of a
  7215. member of the type represented by @var{rtd}. The returned procedure
  7216. accepts exactly two arguments: first, a record of the appropriate type,
  7217. and second, an arbitrary Scheme value; it modifies the field named by
  7218. the symbol @var{field-name} in that record to contain the given value.
  7219. The returned value of the modifier procedure is unspecified. The symbol
  7220. @var{field-name} is a field name or a field index, as in
  7221. @code{record-modifier}.
  7222. @end deffn
  7223. @deffn {Scheme Procedure} record-type-descriptor record
  7224. Return a record-type descriptor representing the type of the given
  7225. record. That is, for example, if the returned descriptor were passed to
  7226. @code{record-predicate}, the resulting predicate would return a true
  7227. value when passed the given record. Note that it is not necessarily the
  7228. case that the returned descriptor is the one that was passed to
  7229. @code{record-constructor} in the call that created the constructor
  7230. procedure that created the given record.
  7231. @end deffn
  7232. @deffn {Scheme Procedure} record-type-name rtd
  7233. Return the type-name associated with the type represented by rtd. The
  7234. returned value is @code{eqv?} to the @var{type-name} argument given in
  7235. the call to @code{make-record-type} that created the type represented by
  7236. @var{rtd}.
  7237. @end deffn
  7238. @deffn {Scheme Procedure} record-type-fields rtd
  7239. Return a list of the symbols naming the fields in members of the type
  7240. represented by @var{rtd}. The returned value is @code{equal?} to the
  7241. field-names argument given in the call to @code{make-record-type} that
  7242. created the type represented by @var{rtd}.
  7243. @end deffn
  7244. @node Structures
  7245. @subsection Structures
  7246. @tpindex Structures
  7247. A @dfn{structure} is a first class data type which holds Scheme values
  7248. or C words in fields numbered 0 upwards. A @dfn{vtable} is a structure
  7249. that represents a structure type, giving field types and permissions,
  7250. and an optional print function for @code{write} etc.
  7251. Structures are lower level than records (@pxref{Records}). Usually,
  7252. when you need to represent structured data, you just want to use
  7253. records. But sometimes you need to implement new kinds of structured
  7254. data abstractions, and for that purpose structures are useful. Indeed,
  7255. records in Guile are implemented with structures.
  7256. @menu
  7257. * Vtables::
  7258. * Structure Basics::
  7259. * Vtable Contents::
  7260. * Meta-Vtables::
  7261. * Vtable Example::
  7262. @end menu
  7263. @node Vtables
  7264. @subsubsection Vtables
  7265. A vtable is a structure type, specifying its layout, and other
  7266. information. A vtable is actually itself a structure, but there's no
  7267. need to worry about that initially (@pxref{Vtable Contents}.)
  7268. @deffn {Scheme Procedure} make-vtable fields [print]
  7269. Create a new vtable.
  7270. @var{fields} is a string describing the fields in the structures to be
  7271. created. Each field is represented by two characters, a type letter
  7272. and a permissions letter, for example @code{"pw"}. The types are as
  7273. follows.
  7274. @itemize @bullet{}
  7275. @item
  7276. @code{p} -- a Scheme value. ``p'' stands for ``protected'' meaning
  7277. it's protected against garbage collection.
  7278. @item
  7279. @code{u} -- an arbitrary word of data (an @code{scm_t_bits}). At the
  7280. Scheme level it's read and written as an unsigned integer. ``u'' stands
  7281. for ``unboxed'', as it's stored as a raw value without additional type
  7282. annotations.
  7283. @end itemize
  7284. It used to be that the second letter for each field was a permission
  7285. code, such as @code{w} for writable or @code{r} for read-only. However
  7286. over time structs have become more of a raw low-level facility; access
  7287. control is better implemented as a layer on top. After all,
  7288. @code{struct-set!} is a cross-cutting operator that can bypass
  7289. abstractions made by higher-level record facilities; it's not generally
  7290. safe (in the sense of abstraction-preserving) to expose
  7291. @code{struct-set!} to ``untrusted'' code, even if the fields happen to
  7292. be writable. Additionally, permission checks added overhead to every
  7293. structure access in a way that couldn't be optimized out, hampering the
  7294. ability of structs to act as a low-level building block. For all of
  7295. these reasons, all fields in Guile structs are now writable; attempting
  7296. to make a read-only field will now issue a deprecation warning, and the
  7297. field will be writable regardless.
  7298. @example
  7299. (make-vtable "pw") ;; one scheme field
  7300. (make-vtable "pwuwuw") ;; one scheme and two unboxed fields
  7301. @end example
  7302. The optional @var{print} argument is a function called by
  7303. @code{display} and @code{write} (etc) to give a printed representation
  7304. of a structure created from this vtable. It's called
  7305. @code{(@var{print} struct port)} and should look at @var{struct} and
  7306. write to @var{port}. The default print merely gives a form like
  7307. @samp{#<struct ADDR:ADDR>} with a pair of machine addresses.
  7308. The following print function for example shows the two fields of its
  7309. structure.
  7310. @example
  7311. (make-vtable "pwpw"
  7312. (lambda (struct port)
  7313. (format port "#<~a and ~a>"
  7314. (struct-ref struct 0)
  7315. (struct-ref struct 1))))
  7316. @end example
  7317. @end deffn
  7318. @node Structure Basics
  7319. @subsubsection Structure Basics
  7320. This section describes the basic procedures for working with structures.
  7321. @code{make-struct/no-tail} creates a structure, and @code{struct-ref}
  7322. and @code{struct-set!} access its fields.
  7323. @deffn {Scheme Procedure} make-struct/no-tail vtable init @dots{}
  7324. Create a new structure, with layout per the given @var{vtable}
  7325. (@pxref{Vtables}).
  7326. The optional @var{init}@dots{} arguments are initial values for the
  7327. fields of the structure. This is the only way to
  7328. put values in read-only fields. If there are fewer @var{init}
  7329. arguments than fields then the defaults are @code{#f} for a Scheme
  7330. field (type @code{p}) or 0 for an unboxed field (type @code{u}).
  7331. The name is a bit strange, we admit. The reason for it is that Guile
  7332. used to have a @code{make-struct} that took an additional argument;
  7333. while we deprecate that old interface, @code{make-struct/no-tail} is the
  7334. new name for this functionality.
  7335. For example,
  7336. @example
  7337. (define v (make-vtable "pwpwpw"))
  7338. (define s (make-struct/no-tail v 123 "abc" 456))
  7339. (struct-ref s 0) @result{} 123
  7340. (struct-ref s 1) @result{} "abc"
  7341. @end example
  7342. @end deffn
  7343. @deftypefn {C Function} SCM scm_make_struct (SCM vtable, SCM tail_size, SCM init_list)
  7344. @deftypefnx {C Function} SCM scm_c_make_struct (SCM vtable, SCM tail_size, SCM init, ...)
  7345. @deftypefnx {C Function} SCM scm_c_make_structv (SCM vtable, SCM tail_size, size_t n_inits, scm_t_bits init[])
  7346. There are a few ways to make structures from C. @code{scm_make_struct}
  7347. takes a list, @code{scm_c_make_struct} takes variable arguments
  7348. terminated with SCM_UNDEFINED, and @code{scm_c_make_structv} takes a
  7349. packed array.
  7350. For all of these, @var{tail_size} should be zero (as a SCM value).
  7351. @end deftypefn
  7352. @deffn {Scheme Procedure} struct? obj
  7353. @deffnx {C Function} scm_struct_p (obj)
  7354. Return @code{#t} if @var{obj} is a structure, or @code{#f} if not.
  7355. @end deffn
  7356. @deffn {Scheme Procedure} struct-ref struct n
  7357. @deffnx {C Function} scm_struct_ref (struct, n)
  7358. Return the contents of field number @var{n} in @var{struct}. The
  7359. first field is number 0.
  7360. An error is thrown if @var{n} is out of range.
  7361. @end deffn
  7362. @deffn {Scheme Procedure} struct-set! struct n value
  7363. @deffnx {C Function} scm_struct_set_x (struct, n, value)
  7364. Set field number @var{n} in @var{struct} to @var{value}. The first
  7365. field is number 0.
  7366. An error is thrown if @var{n} is out of range, or if the field cannot
  7367. be written because it's @code{r} read-only.
  7368. @end deffn
  7369. Unboxed fields (those with type @code{u}) need to be accessed with
  7370. special procedures.
  7371. @deffn {Scheme Procedure} struct-ref/unboxed struct n
  7372. @deffnx {Scheme Procedure} struct-set!/unboxed struct n value
  7373. @deffnx {C Function} scm_struct_ref_unboxed (struct, n)
  7374. @deffnx {C Function} scm_struct_set_x_unboxed (struct, n, value)
  7375. Like @code{struct-ref} and @code{struct-set!}, except that these may
  7376. only be used on unboxed fields. @code{struct-ref/unboxed} will always
  7377. return a positive integer. Likewise, @code{struct-set!/unboxed} takes
  7378. an unsigned integer as the @var{value} argument, and will signal an
  7379. error otherwise.
  7380. @end deffn
  7381. @deffn {Scheme Procedure} struct-vtable struct
  7382. @deffnx {C Function} scm_struct_vtable (struct)
  7383. Return the vtable that describes @var{struct}.
  7384. The vtable is effectively the type of the structure. See @ref{Vtable
  7385. Contents}, for more on vtables.
  7386. @end deffn
  7387. @node Vtable Contents
  7388. @subsubsection Vtable Contents
  7389. A vtable is itself a structure. It has a specific set of fields
  7390. describing various aspects of its @dfn{instances}: the structures
  7391. created from a vtable. Some of the fields are internal to Guile, some
  7392. of them are part of the public interface, and there may be additional
  7393. fields added on by the user.
  7394. Every vtable has a field for the layout of their instances, a field for
  7395. the procedure used to print its instances, and a field for the name of
  7396. the vtable itself. Access to the layout and printer is exposed directly
  7397. via field indexes. Access to the vtable name is exposed via accessor
  7398. procedures.
  7399. @defvr {Scheme Variable} vtable-index-layout
  7400. @defvrx {C Macro} scm_vtable_index_layout
  7401. The field number of the layout specification in a vtable. The layout
  7402. specification is a symbol like @code{pwpw} formed from the fields
  7403. string passed to @code{make-vtable}, or created by
  7404. @code{make-struct-layout} (@pxref{Meta-Vtables}).
  7405. @example
  7406. (define v (make-vtable "pwpw" 0))
  7407. (struct-ref v vtable-index-layout) @result{} pwpw
  7408. @end example
  7409. This field is read-only, since the layout of structures using a vtable
  7410. cannot be changed.
  7411. @end defvr
  7412. @defvr {Scheme Variable} vtable-index-printer
  7413. @defvrx {C Macro} scm_vtable_index_printer
  7414. The field number of the printer function. This field contains @code{#f}
  7415. if the default print function should be used.
  7416. @example
  7417. (define (my-print-func struct port)
  7418. ...)
  7419. (define v (make-vtable "pwpw" my-print-func))
  7420. (struct-ref v vtable-index-printer) @result{} my-print-func
  7421. @end example
  7422. This field is writable, allowing the print function to be changed
  7423. dynamically.
  7424. @end defvr
  7425. @deffn {Scheme Procedure} struct-vtable-name vtable
  7426. @deffnx {Scheme Procedure} set-struct-vtable-name! vtable name
  7427. @deffnx {C Function} scm_struct_vtable_name (vtable)
  7428. @deffnx {C Function} scm_set_struct_vtable_name_x (vtable, name)
  7429. Get or set the name of @var{vtable}. @var{name} is a symbol and is
  7430. used in the default print function when printing structures created
  7431. from @var{vtable}.
  7432. @example
  7433. (define v (make-vtable "pw"))
  7434. (set-struct-vtable-name! v 'my-name)
  7435. (define s (make-struct v 0))
  7436. (display s) @print{} #<my-name b7ab3ae0:b7ab3730>
  7437. @end example
  7438. @end deffn
  7439. @node Meta-Vtables
  7440. @subsubsection Meta-Vtables
  7441. As a structure, a vtable also has a vtable, which is also a structure.
  7442. Structures, their vtables, the vtables of the vtables, and so on form a
  7443. tree of structures. Making a new structure adds a leaf to the tree, and
  7444. if that structure is a vtable, it may be used to create other leaves.
  7445. If you traverse up the tree of vtables, via calling
  7446. @code{struct-vtable}, eventually you reach a root which is the vtable of
  7447. itself:
  7448. @example
  7449. scheme@@(guile-user)> (current-module)
  7450. $1 = #<directory (guile-user) 221b090>
  7451. scheme@@(guile-user)> (struct-vtable $1)
  7452. $2 = #<record-type module>
  7453. scheme@@(guile-user)> (struct-vtable $2)
  7454. $3 = #<<standard-vtable> 12c30a0>
  7455. scheme@@(guile-user)> (struct-vtable $3)
  7456. $4 = #<<standard-vtable> 12c3fa0>
  7457. scheme@@(guile-user)> (struct-vtable $4)
  7458. $5 = #<<standard-vtable> 12c3fa0>
  7459. scheme@@(guile-user)> <standard-vtable>
  7460. $6 = #<<standard-vtable> 12c3fa0>
  7461. @end example
  7462. In this example, we can say that @code{$1} is an instance of @code{$2},
  7463. @code{$2} is an instance of @code{$3}, @code{$3} is an instance of
  7464. @code{$4}, and @code{$4}, strangely enough, is an instance of itself.
  7465. The value bound to @code{$4} in this console session also bound to
  7466. @code{<standard-vtable>} in the default environment.
  7467. @defvr {Scheme Variable} <standard-vtable>
  7468. A meta-vtable, useful for making new vtables.
  7469. @end defvr
  7470. All of these values are structures. All but @code{$1} are vtables. As
  7471. @code{$2} is an instance of @code{$3}, and @code{$3} is a vtable, we can
  7472. say that @code{$3} is a @dfn{meta-vtable}: a vtable that can create
  7473. vtables.
  7474. With this definition, we can specify more precisely what a vtable is: a
  7475. vtable is a structure made from a meta-vtable. Making a structure from
  7476. a meta-vtable runs some special checks to ensure that the first field of
  7477. the structure is a valid layout. Additionally, if these checks see that
  7478. the layout of the child vtable contains all the required fields of a
  7479. vtable, in the correct order, then the child vtable will also be a
  7480. meta-table, inheriting a magical bit from the parent.
  7481. @deffn {Scheme Procedure} struct-vtable? obj
  7482. @deffnx {C Function} scm_struct_vtable_p (obj)
  7483. Return @code{#t} if @var{obj} is a vtable structure: an instance of a
  7484. meta-vtable.
  7485. @end deffn
  7486. @code{<standard-vtable>} is a root of the vtable tree. (Normally there
  7487. is only one root in a given Guile process, but due to some legacy
  7488. interfaces there may be more than one.)
  7489. The set of required fields of a vtable is the set of fields in the
  7490. @code{<standard-vtable>}, and is bound to @code{standard-vtable-fields}
  7491. in the default environment. It is possible to create a meta-vtable that
  7492. with additional fields in its layout, which can be used to create
  7493. vtables with additional data:
  7494. @example
  7495. scheme@@(guile-user)> (struct-ref $3 vtable-index-layout)
  7496. $6 = pwuhuhpwphuhuhpwpwpw
  7497. scheme@@(guile-user)> (struct-ref $4 vtable-index-layout)
  7498. $7 = pwuhuhpwphuhuh
  7499. scheme@@(guile-user)> standard-vtable-fields
  7500. $8 = "pwuhuhpwphuhuh"
  7501. scheme@@(guile-user)> (struct-ref $2 vtable-offset-user)
  7502. $9 = module
  7503. @end example
  7504. In this continuation of our earlier example, @code{$2} is a vtable that
  7505. has extra fields, because its vtable, @code{$3}, was made from a
  7506. meta-vtable with an extended layout. @code{vtable-offset-user} is a
  7507. convenient definition that indicates the number of fields in
  7508. @code{standard-vtable-fields}.
  7509. @defvr {Scheme Variable} standard-vtable-fields
  7510. A string containing the ordered set of fields that a vtable must have.
  7511. @end defvr
  7512. @defvr {Scheme Variable} vtable-offset-user
  7513. The first index in a vtable that is available for a user.
  7514. @end defvr
  7515. @deffn {Scheme Procedure} make-struct-layout fields
  7516. @deffnx {C Function} scm_make_struct_layout (fields)
  7517. Return a structure layout symbol, from a @var{fields} string.
  7518. @var{fields} is as described under @code{make-vtable}
  7519. (@pxref{Vtables}). An invalid @var{fields} string is an error.
  7520. @end deffn
  7521. With these definitions, one can define @code{make-vtable} in this way:
  7522. @example
  7523. (define* (make-vtable fields #:optional printer)
  7524. (make-struct/no-tail <standard-vtable>
  7525. (make-struct-layout fields)
  7526. printer))
  7527. @end example
  7528. @node Vtable Example
  7529. @subsubsection Vtable Example
  7530. Let us bring these points together with an example. Consider a simple
  7531. object system with single inheritance. Objects will be normal
  7532. structures, and classes will be vtables with three extra class fields:
  7533. the name of the class, the parent class, and the list of fields.
  7534. So, first we need a meta-vtable that allocates instances with these
  7535. extra class fields.
  7536. @example
  7537. (define <class>
  7538. (make-vtable
  7539. (string-append standard-vtable-fields "pwpwpw")
  7540. (lambda (x port)
  7541. (format port "<<class> ~a>" (class-name x)))))
  7542. (define (class? x)
  7543. (and (struct? x)
  7544. (eq? (struct-vtable x) <class>)))
  7545. @end example
  7546. To make a structure with a specific meta-vtable, we will use
  7547. @code{make-struct/no-tail}, passing it the computed instance layout and
  7548. printer, as with @code{make-vtable}, and additionally the extra three
  7549. class fields.
  7550. @example
  7551. (define (make-class name parent fields)
  7552. (let* ((fields (compute-fields parent fields))
  7553. (layout (compute-layout fields)))
  7554. (make-struct/no-tail <class>
  7555. layout
  7556. (lambda (x port)
  7557. (print-instance x port))
  7558. name
  7559. parent
  7560. fields)))
  7561. @end example
  7562. Instances will store their associated data in slots in the structure: as
  7563. many slots as there are fields. The @code{compute-layout} procedure
  7564. below can compute a layout, and @code{field-index} returns the slot
  7565. corresponding to a field.
  7566. @example
  7567. (define-syntax-rule (define-accessor name n)
  7568. (define (name obj)
  7569. (struct-ref obj n)))
  7570. ;; Accessors for classes
  7571. (define-accessor class-name (+ vtable-offset-user 0))
  7572. (define-accessor class-parent (+ vtable-offset-user 1))
  7573. (define-accessor class-fields (+ vtable-offset-user 2))
  7574. (define (compute-fields parent fields)
  7575. (if parent
  7576. (append (class-fields parent) fields)
  7577. fields))
  7578. (define (compute-layout fields)
  7579. (make-struct-layout
  7580. (string-concatenate (make-list (length fields) "pw"))))
  7581. (define (field-index class field)
  7582. (list-index (class-fields class) field))
  7583. (define (print-instance x port)
  7584. (format port "<~a" (class-name (struct-vtable x)))
  7585. (for-each (lambda (field idx)
  7586. (format port " ~a: ~a" field (struct-ref x idx)))
  7587. (class-fields (struct-vtable x))
  7588. (iota (length (class-fields (struct-vtable x)))))
  7589. (format port ">"))
  7590. @end example
  7591. So, at this point we can actually make a few classes:
  7592. @example
  7593. (define-syntax-rule (define-class name parent field ...)
  7594. (define name (make-class 'name parent '(field ...))))
  7595. (define-class <surface> #f
  7596. width height)
  7597. (define-class <window> <surface>
  7598. x y)
  7599. @end example
  7600. And finally, make an instance:
  7601. @example
  7602. (make-struct/no-tail <window> 400 300 10 20)
  7603. @result{} <<window> width: 400 height: 300 x: 10 y: 20>
  7604. @end example
  7605. And that's that. Note that there are many possible optimizations and
  7606. feature enhancements that can be made to this object system, and the
  7607. included GOOPS system does make most of them. For more simple use
  7608. cases, the records facility is usually sufficient. But sometimes you
  7609. need to make new kinds of data abstractions, and for that purpose,
  7610. structs are here.
  7611. @node Dictionary Types
  7612. @subsection Dictionary Types
  7613. A @dfn{dictionary} object is a data structure used to index
  7614. information in a user-defined way. In standard Scheme, the main
  7615. aggregate data types are lists and vectors. Lists are not really
  7616. indexed at all, and vectors are indexed only by number
  7617. (e.g.@: @code{(vector-ref foo 5)}). Often you will find it useful
  7618. to index your data on some other type; for example, in a library
  7619. catalog you might want to look up a book by the name of its
  7620. author. Dictionaries are used to help you organize information in
  7621. such a way.
  7622. An @dfn{association list} (or @dfn{alist} for short) is a list of
  7623. key-value pairs. Each pair represents a single quantity or
  7624. object; the @code{car} of the pair is a key which is used to
  7625. identify the object, and the @code{cdr} is the object's value.
  7626. A @dfn{hash table} also permits you to index objects with
  7627. arbitrary keys, but in a way that makes looking up any one object
  7628. extremely fast. A well-designed hash system makes hash table
  7629. lookups almost as fast as conventional array or vector references.
  7630. Alists are popular among Lisp programmers because they use only
  7631. the language's primitive operations (lists, @dfn{car}, @dfn{cdr}
  7632. and the equality primitives). No changes to the language core are
  7633. necessary. Therefore, with Scheme's built-in list manipulation
  7634. facilities, it is very convenient to handle data stored in an
  7635. association list. Also, alists are highly portable and can be
  7636. easily implemented on even the most minimal Lisp systems.
  7637. However, alists are inefficient, especially for storing large
  7638. quantities of data. Because we want Guile to be useful for large
  7639. software systems as well as small ones, Guile provides a rich set
  7640. of tools for using either association lists or hash tables.
  7641. @node Association Lists
  7642. @subsection Association Lists
  7643. @tpindex Association Lists
  7644. @tpindex Alist
  7645. @cindex association List
  7646. @cindex alist
  7647. @cindex database
  7648. An association list is a conventional data structure that is often used
  7649. to implement simple key-value databases. It consists of a list of
  7650. entries in which each entry is a pair. The @dfn{key} of each entry is
  7651. the @code{car} of the pair and the @dfn{value} of each entry is the
  7652. @code{cdr}.
  7653. @example
  7654. ASSOCIATION LIST ::= '( (KEY1 . VALUE1)
  7655. (KEY2 . VALUE2)
  7656. (KEY3 . VALUE3)
  7657. @dots{}
  7658. )
  7659. @end example
  7660. @noindent
  7661. Association lists are also known, for short, as @dfn{alists}.
  7662. The structure of an association list is just one example of the infinite
  7663. number of possible structures that can be built using pairs and lists.
  7664. As such, the keys and values in an association list can be manipulated
  7665. using the general list structure procedures @code{cons}, @code{car},
  7666. @code{cdr}, @code{set-car!}, @code{set-cdr!} and so on. However,
  7667. because association lists are so useful, Guile also provides specific
  7668. procedures for manipulating them.
  7669. @menu
  7670. * Alist Key Equality::
  7671. * Adding or Setting Alist Entries::
  7672. * Retrieving Alist Entries::
  7673. * Removing Alist Entries::
  7674. * Sloppy Alist Functions::
  7675. * Alist Example::
  7676. @end menu
  7677. @node Alist Key Equality
  7678. @subsubsection Alist Key Equality
  7679. All of Guile's dedicated association list procedures, apart from
  7680. @code{acons}, come in three flavors, depending on the level of equality
  7681. that is required to decide whether an existing key in the association
  7682. list is the same as the key that the procedure call uses to identify the
  7683. required entry.
  7684. @itemize @bullet
  7685. @item
  7686. Procedures with @dfn{assq} in their name use @code{eq?} to determine key
  7687. equality.
  7688. @item
  7689. Procedures with @dfn{assv} in their name use @code{eqv?} to determine
  7690. key equality.
  7691. @item
  7692. Procedures with @dfn{assoc} in their name use @code{equal?} to
  7693. determine key equality.
  7694. @end itemize
  7695. @code{acons} is an exception because it is used to build association
  7696. lists which do not require their entries' keys to be unique.
  7697. @node Adding or Setting Alist Entries
  7698. @subsubsection Adding or Setting Alist Entries
  7699. @code{acons} adds a new entry to an association list and returns the
  7700. combined association list. The combined alist is formed by consing the
  7701. new entry onto the head of the alist specified in the @code{acons}
  7702. procedure call. So the specified alist is not modified, but its
  7703. contents become shared with the tail of the combined alist that
  7704. @code{acons} returns.
  7705. In the most common usage of @code{acons}, a variable holding the
  7706. original association list is updated with the combined alist:
  7707. @example
  7708. (set! address-list (acons name address address-list))
  7709. @end example
  7710. In such cases, it doesn't matter that the old and new values of
  7711. @code{address-list} share some of their contents, since the old value is
  7712. usually no longer independently accessible.
  7713. Note that @code{acons} adds the specified new entry regardless of
  7714. whether the alist may already contain entries with keys that are, in
  7715. some sense, the same as that of the new entry. Thus @code{acons} is
  7716. ideal for building alists where there is no concept of key uniqueness.
  7717. @example
  7718. (set! task-list (acons 3 "pay gas bill" '()))
  7719. task-list
  7720. @result{}
  7721. ((3 . "pay gas bill"))
  7722. (set! task-list (acons 3 "tidy bedroom" task-list))
  7723. task-list
  7724. @result{}
  7725. ((3 . "tidy bedroom") (3 . "pay gas bill"))
  7726. @end example
  7727. @code{assq-set!}, @code{assv-set!} and @code{assoc-set!} are used to add
  7728. or replace an entry in an association list where there @emph{is} a
  7729. concept of key uniqueness. If the specified association list already
  7730. contains an entry whose key is the same as that specified in the
  7731. procedure call, the existing entry is replaced by the new one.
  7732. Otherwise, the new entry is consed onto the head of the old association
  7733. list to create the combined alist. In all cases, these procedures
  7734. return the combined alist.
  7735. @code{assq-set!} and friends @emph{may} destructively modify the
  7736. structure of the old association list in such a way that an existing
  7737. variable is correctly updated without having to @code{set!} it to the
  7738. value returned:
  7739. @example
  7740. address-list
  7741. @result{}
  7742. (("mary" . "34 Elm Road") ("james" . "16 Bow Street"))
  7743. (assoc-set! address-list "james" "1a London Road")
  7744. @result{}
  7745. (("mary" . "34 Elm Road") ("james" . "1a London Road"))
  7746. address-list
  7747. @result{}
  7748. (("mary" . "34 Elm Road") ("james" . "1a London Road"))
  7749. @end example
  7750. Or they may not:
  7751. @example
  7752. (assoc-set! address-list "bob" "11 Newington Avenue")
  7753. @result{}
  7754. (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road")
  7755. ("james" . "1a London Road"))
  7756. address-list
  7757. @result{}
  7758. (("mary" . "34 Elm Road") ("james" . "1a London Road"))
  7759. @end example
  7760. The only safe way to update an association list variable when adding or
  7761. replacing an entry like this is to @code{set!} the variable to the
  7762. returned value:
  7763. @example
  7764. (set! address-list
  7765. (assoc-set! address-list "bob" "11 Newington Avenue"))
  7766. address-list
  7767. @result{}
  7768. (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road")
  7769. ("james" . "1a London Road"))
  7770. @end example
  7771. Because of this slight inconvenience, you may find it more convenient to
  7772. use hash tables to store dictionary data. If your application will not
  7773. be modifying the contents of an alist very often, this may not make much
  7774. difference to you.
  7775. If you need to keep the old value of an association list in a form
  7776. independent from the list that results from modification by
  7777. @code{acons}, @code{assq-set!}, @code{assv-set!} or @code{assoc-set!},
  7778. use @code{list-copy} to copy the old association list before modifying
  7779. it.
  7780. @deffn {Scheme Procedure} acons key value alist
  7781. @deffnx {C Function} scm_acons (key, value, alist)
  7782. Add a new key-value pair to @var{alist}. A new pair is
  7783. created whose car is @var{key} and whose cdr is @var{value}, and the
  7784. pair is consed onto @var{alist}, and the new list is returned. This
  7785. function is @emph{not} destructive; @var{alist} is not modified.
  7786. @end deffn
  7787. @deffn {Scheme Procedure} assq-set! alist key val
  7788. @deffnx {Scheme Procedure} assv-set! alist key value
  7789. @deffnx {Scheme Procedure} assoc-set! alist key value
  7790. @deffnx {C Function} scm_assq_set_x (alist, key, val)
  7791. @deffnx {C Function} scm_assv_set_x (alist, key, val)
  7792. @deffnx {C Function} scm_assoc_set_x (alist, key, val)
  7793. Reassociate @var{key} in @var{alist} with @var{value}: find any existing
  7794. @var{alist} entry for @var{key} and associate it with the new
  7795. @var{value}. If @var{alist} does not contain an entry for @var{key},
  7796. add a new one. Return the (possibly new) alist.
  7797. These functions do not attempt to verify the structure of @var{alist},
  7798. and so may cause unusual results if passed an object that is not an
  7799. association list.
  7800. @end deffn
  7801. @node Retrieving Alist Entries
  7802. @subsubsection Retrieving Alist Entries
  7803. @rnindex assq
  7804. @rnindex assv
  7805. @rnindex assoc
  7806. @code{assq}, @code{assv} and @code{assoc} find the entry in an alist
  7807. for a given key, and return the @code{(@var{key} . @var{value})} pair.
  7808. @code{assq-ref}, @code{assv-ref} and @code{assoc-ref} do a similar
  7809. lookup, but return just the @var{value}.
  7810. @deffn {Scheme Procedure} assq key alist
  7811. @deffnx {Scheme Procedure} assv key alist
  7812. @deffnx {Scheme Procedure} assoc key alist
  7813. @deffnx {C Function} scm_assq (key, alist)
  7814. @deffnx {C Function} scm_assv (key, alist)
  7815. @deffnx {C Function} scm_assoc (key, alist)
  7816. Return the first entry in @var{alist} with the given @var{key}. The
  7817. return is the pair @code{(KEY . VALUE)} from @var{alist}. If there's
  7818. no matching entry the return is @code{#f}.
  7819. @code{assq} compares keys with @code{eq?}, @code{assv} uses
  7820. @code{eqv?} and @code{assoc} uses @code{equal?}. See also SRFI-1
  7821. which has an extended @code{assoc} (@ref{SRFI-1 Association Lists}).
  7822. @end deffn
  7823. @deffn {Scheme Procedure} assq-ref alist key
  7824. @deffnx {Scheme Procedure} assv-ref alist key
  7825. @deffnx {Scheme Procedure} assoc-ref alist key
  7826. @deffnx {C Function} scm_assq_ref (alist, key)
  7827. @deffnx {C Function} scm_assv_ref (alist, key)
  7828. @deffnx {C Function} scm_assoc_ref (alist, key)
  7829. Return the value from the first entry in @var{alist} with the given
  7830. @var{key}, or @code{#f} if there's no such entry.
  7831. @code{assq-ref} compares keys with @code{eq?}, @code{assv-ref} uses
  7832. @code{eqv?} and @code{assoc-ref} uses @code{equal?}.
  7833. Notice these functions have the @var{key} argument last, like other
  7834. @code{-ref} functions, but this is opposite to what @code{assq}
  7835. etc above use.
  7836. When the return is @code{#f} it can be either @var{key} not found, or
  7837. an entry which happens to have value @code{#f} in the @code{cdr}. Use
  7838. @code{assq} etc above if you need to differentiate these cases.
  7839. @end deffn
  7840. @node Removing Alist Entries
  7841. @subsubsection Removing Alist Entries
  7842. To remove the element from an association list whose key matches a
  7843. specified key, use @code{assq-remove!}, @code{assv-remove!} or
  7844. @code{assoc-remove!} (depending, as usual, on the level of equality
  7845. required between the key that you specify and the keys in the
  7846. association list).
  7847. As with @code{assq-set!} and friends, the specified alist may or may not
  7848. be modified destructively, and the only safe way to update a variable
  7849. containing the alist is to @code{set!} it to the value that
  7850. @code{assq-remove!} and friends return.
  7851. @example
  7852. address-list
  7853. @result{}
  7854. (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road")
  7855. ("james" . "1a London Road"))
  7856. (set! address-list (assoc-remove! address-list "mary"))
  7857. address-list
  7858. @result{}
  7859. (("bob" . "11 Newington Avenue") ("james" . "1a London Road"))
  7860. @end example
  7861. Note that, when @code{assq/v/oc-remove!} is used to modify an
  7862. association list that has been constructed only using the corresponding
  7863. @code{assq/v/oc-set!}, there can be at most one matching entry in the
  7864. alist, so the question of multiple entries being removed in one go does
  7865. not arise. If @code{assq/v/oc-remove!} is applied to an association
  7866. list that has been constructed using @code{acons}, or an
  7867. @code{assq/v/oc-set!} with a different level of equality, or any mixture
  7868. of these, it removes only the first matching entry from the alist, even
  7869. if the alist might contain further matching entries. For example:
  7870. @example
  7871. (define address-list '())
  7872. (set! address-list (assq-set! address-list "mary" "11 Elm Street"))
  7873. (set! address-list (assq-set! address-list "mary" "57 Pine Drive"))
  7874. address-list
  7875. @result{}
  7876. (("mary" . "57 Pine Drive") ("mary" . "11 Elm Street"))
  7877. (set! address-list (assoc-remove! address-list "mary"))
  7878. address-list
  7879. @result{}
  7880. (("mary" . "11 Elm Street"))
  7881. @end example
  7882. In this example, the two instances of the string "mary" are not the same
  7883. when compared using @code{eq?}, so the two @code{assq-set!} calls add
  7884. two distinct entries to @code{address-list}. When compared using
  7885. @code{equal?}, both "mary"s in @code{address-list} are the same as the
  7886. "mary" in the @code{assoc-remove!} call, but @code{assoc-remove!} stops
  7887. after removing the first matching entry that it finds, and so one of the
  7888. "mary" entries is left in place.
  7889. @deffn {Scheme Procedure} assq-remove! alist key
  7890. @deffnx {Scheme Procedure} assv-remove! alist key
  7891. @deffnx {Scheme Procedure} assoc-remove! alist key
  7892. @deffnx {C Function} scm_assq_remove_x (alist, key)
  7893. @deffnx {C Function} scm_assv_remove_x (alist, key)
  7894. @deffnx {C Function} scm_assoc_remove_x (alist, key)
  7895. Delete the first entry in @var{alist} associated with @var{key}, and return
  7896. the resulting alist.
  7897. @end deffn
  7898. @node Sloppy Alist Functions
  7899. @subsubsection Sloppy Alist Functions
  7900. @code{sloppy-assq}, @code{sloppy-assv} and @code{sloppy-assoc} behave
  7901. like the corresponding non-@code{sloppy-} procedures, except that they
  7902. return @code{#f} when the specified association list is not well-formed,
  7903. where the non-@code{sloppy-} versions would signal an error.
  7904. Specifically, there are two conditions for which the non-@code{sloppy-}
  7905. procedures signal an error, which the @code{sloppy-} procedures handle
  7906. instead by returning @code{#f}. Firstly, if the specified alist as a
  7907. whole is not a proper list:
  7908. @example
  7909. (assoc "mary" '((1 . 2) ("key" . "door") . "open sesame"))
  7910. @result{}
  7911. ERROR: In procedure assoc in expression (assoc "mary" (quote #)):
  7912. ERROR: Wrong type argument in position 2 (expecting
  7913. association list): ((1 . 2) ("key" . "door") . "open sesame")
  7914. (sloppy-assoc "mary" '((1 . 2) ("key" . "door") . "open sesame"))
  7915. @result{}
  7916. #f
  7917. @end example
  7918. @noindent
  7919. Secondly, if one of the entries in the specified alist is not a pair:
  7920. @example
  7921. (assoc 2 '((1 . 1) 2 (3 . 9)))
  7922. @result{}
  7923. ERROR: In procedure assoc in expression (assoc 2 (quote #)):
  7924. ERROR: Wrong type argument in position 2 (expecting
  7925. association list): ((1 . 1) 2 (3 . 9))
  7926. (sloppy-assoc 2 '((1 . 1) 2 (3 . 9)))
  7927. @result{}
  7928. #f
  7929. @end example
  7930. Unless you are explicitly working with badly formed association lists,
  7931. it is much safer to use the non-@code{sloppy-} procedures, because they
  7932. help to highlight coding and data errors that the @code{sloppy-}
  7933. versions would silently cover up.
  7934. @deffn {Scheme Procedure} sloppy-assq key alist
  7935. @deffnx {C Function} scm_sloppy_assq (key, alist)
  7936. Behaves like @code{assq} but does not do any error checking.
  7937. Recommended only for use in Guile internals.
  7938. @end deffn
  7939. @deffn {Scheme Procedure} sloppy-assv key alist
  7940. @deffnx {C Function} scm_sloppy_assv (key, alist)
  7941. Behaves like @code{assv} but does not do any error checking.
  7942. Recommended only for use in Guile internals.
  7943. @end deffn
  7944. @deffn {Scheme Procedure} sloppy-assoc key alist
  7945. @deffnx {C Function} scm_sloppy_assoc (key, alist)
  7946. Behaves like @code{assoc} but does not do any error checking.
  7947. Recommended only for use in Guile internals.
  7948. @end deffn
  7949. @node Alist Example
  7950. @subsubsection Alist Example
  7951. Here is a longer example of how alists may be used in practice.
  7952. @lisp
  7953. (define capitals '(("New York" . "Albany")
  7954. ("Oregon" . "Salem")
  7955. ("Florida" . "Miami")))
  7956. ;; What's the capital of Oregon?
  7957. (assoc "Oregon" capitals) @result{} ("Oregon" . "Salem")
  7958. (assoc-ref capitals "Oregon") @result{} "Salem"
  7959. ;; We left out South Dakota.
  7960. (set! capitals
  7961. (assoc-set! capitals "South Dakota" "Pierre"))
  7962. capitals
  7963. @result{} (("South Dakota" . "Pierre")
  7964. ("New York" . "Albany")
  7965. ("Oregon" . "Salem")
  7966. ("Florida" . "Miami"))
  7967. ;; And we got Florida wrong.
  7968. (set! capitals
  7969. (assoc-set! capitals "Florida" "Tallahassee"))
  7970. capitals
  7971. @result{} (("South Dakota" . "Pierre")
  7972. ("New York" . "Albany")
  7973. ("Oregon" . "Salem")
  7974. ("Florida" . "Tallahassee"))
  7975. ;; After Oregon secedes, we can remove it.
  7976. (set! capitals
  7977. (assoc-remove! capitals "Oregon"))
  7978. capitals
  7979. @result{} (("South Dakota" . "Pierre")
  7980. ("New York" . "Albany")
  7981. ("Florida" . "Tallahassee"))
  7982. @end lisp
  7983. @node VHashes
  7984. @subsection VList-Based Hash Lists or ``VHashes''
  7985. @cindex VList-based hash lists
  7986. @cindex VHash
  7987. The @code{(ice-9 vlist)} module provides an implementation of @dfn{VList-based
  7988. hash lists} (@pxref{VLists}). VList-based hash lists, or @dfn{vhashes}, are an
  7989. immutable dictionary type similar to association lists that maps @dfn{keys} to
  7990. @dfn{values}. However, unlike association lists, accessing a value given its
  7991. key is typically a constant-time operation.
  7992. The VHash programming interface of @code{(ice-9 vlist)} is mostly the same as
  7993. that of association lists found in SRFI-1, with procedure names prefixed by
  7994. @code{vhash-} instead of @code{alist-} (@pxref{SRFI-1 Association Lists}).
  7995. In addition, vhashes can be manipulated using VList operations:
  7996. @example
  7997. (vlist-head (vhash-consq 'a 1 vlist-null))
  7998. @result{} (a . 1)
  7999. (define vh1 (vhash-consq 'b 2 (vhash-consq 'a 1 vlist-null)))
  8000. (define vh2 (vhash-consq 'c 3 (vlist-tail vh1)))
  8001. (vhash-assq 'a vh2)
  8002. @result{} (a . 1)
  8003. (vhash-assq 'b vh2)
  8004. @result{} #f
  8005. (vhash-assq 'c vh2)
  8006. @result{} (c . 3)
  8007. (vlist->list vh2)
  8008. @result{} ((c . 3) (a . 1))
  8009. @end example
  8010. However, keep in mind that procedures that construct new VLists
  8011. (@code{vlist-map}, @code{vlist-filter}, etc.) return raw VLists, not vhashes:
  8012. @example
  8013. (define vh (alist->vhash '((a . 1) (b . 2) (c . 3)) hashq))
  8014. (vhash-assq 'a vh)
  8015. @result{} (a . 1)
  8016. (define vl
  8017. ;; This will create a raw vlist.
  8018. (vlist-filter (lambda (key+value) (odd? (cdr key+value))) vh))
  8019. (vhash-assq 'a vl)
  8020. @result{} ERROR: Wrong type argument in position 2
  8021. (vlist->list vl)
  8022. @result{} ((a . 1) (c . 3))
  8023. @end example
  8024. @deffn {Scheme Procedure} vhash? obj
  8025. Return true if @var{obj} is a vhash.
  8026. @end deffn
  8027. @deffn {Scheme Procedure} vhash-cons key value vhash [hash-proc]
  8028. @deffnx {Scheme Procedure} vhash-consq key value vhash
  8029. @deffnx {Scheme Procedure} vhash-consv key value vhash
  8030. Return a new hash list based on @var{vhash} where @var{key} is associated with
  8031. @var{value}, using @var{hash-proc} to compute the hash of @var{key}.
  8032. @var{vhash} must be either @code{vlist-null} or a vhash returned by a previous
  8033. call to @code{vhash-cons}. @var{hash-proc} defaults to @code{hash} (@pxref{Hash
  8034. Table Reference, @code{hash} procedure}). With @code{vhash-consq}, the
  8035. @code{hashq} hash function is used; with @code{vhash-consv} the @code{hashv}
  8036. hash function is used.
  8037. All @code{vhash-cons} calls made to construct a vhash should use the same
  8038. @var{hash-proc}. Failing to do that, the result is undefined.
  8039. @end deffn
  8040. @deffn {Scheme Procedure} vhash-assoc key vhash [equal? [hash-proc]]
  8041. @deffnx {Scheme Procedure} vhash-assq key vhash
  8042. @deffnx {Scheme Procedure} vhash-assv key vhash
  8043. Return the first key/value pair from @var{vhash} whose key is equal to @var{key}
  8044. according to the @var{equal?} equality predicate (which defaults to
  8045. @code{equal?}), and using @var{hash-proc} (which defaults to @code{hash}) to
  8046. compute the hash of @var{key}. The second form uses @code{eq?} as the equality
  8047. predicate and @code{hashq} as the hash function; the last form uses @code{eqv?}
  8048. and @code{hashv}.
  8049. Note that it is important to consistently use the same hash function for
  8050. @var{hash-proc} as was passed to @code{vhash-cons}. Failing to do that, the
  8051. result is unpredictable.
  8052. @end deffn
  8053. @deffn {Scheme Procedure} vhash-delete key vhash [equal? [hash-proc]]
  8054. @deffnx {Scheme Procedure} vhash-delq key vhash
  8055. @deffnx {Scheme Procedure} vhash-delv key vhash
  8056. Remove all associations from @var{vhash} with @var{key}, comparing keys with
  8057. @var{equal?} (which defaults to @code{equal?}), and computing the hash of
  8058. @var{key} using @var{hash-proc} (which defaults to @code{hash}). The second
  8059. form uses @code{eq?} as the equality predicate and @code{hashq} as the hash
  8060. function; the last one uses @code{eqv?} and @code{hashv}.
  8061. Again the choice of @var{hash-proc} must be consistent with previous calls to
  8062. @code{vhash-cons}.
  8063. @end deffn
  8064. @deffn {Scheme Procedure} vhash-fold proc init vhash
  8065. @deffnx {Scheme Procedure} vhash-fold-right proc init vhash
  8066. Fold over the key/value elements of @var{vhash} in the given direction,
  8067. with each call to @var{proc} having the form @code{(@var{proc} key value
  8068. result)}, where @var{result} is the result of the previous call to
  8069. @var{proc} and @var{init} the value of @var{result} for the first call
  8070. to @var{proc}.
  8071. @end deffn
  8072. @deffn {Scheme Procedure} vhash-fold* proc init key vhash [equal? [hash]]
  8073. @deffnx {Scheme Procedure} vhash-foldq* proc init key vhash
  8074. @deffnx {Scheme Procedure} vhash-foldv* proc init key vhash
  8075. Fold over all the values associated with @var{key} in @var{vhash}, with each
  8076. call to @var{proc} having the form @code{(proc value result)}, where
  8077. @var{result} is the result of the previous call to @var{proc} and @var{init} the
  8078. value of @var{result} for the first call to @var{proc}.
  8079. Keys in @var{vhash} are hashed using @var{hash} are compared using @var{equal?}.
  8080. The second form uses @code{eq?} as the equality predicate and @code{hashq} as
  8081. the hash function; the third one uses @code{eqv?} and @code{hashv}.
  8082. Example:
  8083. @example
  8084. (define vh
  8085. (alist->vhash '((a . 1) (a . 2) (z . 0) (a . 3))))
  8086. (vhash-fold* cons '() 'a vh)
  8087. @result{} (3 2 1)
  8088. (vhash-fold* cons '() 'z vh)
  8089. @result{} (0)
  8090. @end example
  8091. @end deffn
  8092. @deffn {Scheme Procedure} alist->vhash alist [hash-proc]
  8093. Return the vhash corresponding to @var{alist}, an association list, using
  8094. @var{hash-proc} to compute key hashes. When omitted, @var{hash-proc} defaults
  8095. to @code{hash}.
  8096. @end deffn
  8097. @node Hash Tables
  8098. @subsection Hash Tables
  8099. @tpindex Hash Tables
  8100. Hash tables are dictionaries which offer similar functionality as
  8101. association lists: They provide a mapping from keys to values. The
  8102. difference is that association lists need time linear in the size of
  8103. elements when searching for entries, whereas hash tables can normally
  8104. search in constant time. The drawback is that hash tables require a
  8105. little bit more memory, and that you can not use the normal list
  8106. procedures (@pxref{Lists}) for working with them.
  8107. @menu
  8108. * Hash Table Examples:: Demonstration of hash table usage.
  8109. * Hash Table Reference:: Hash table procedure descriptions.
  8110. @end menu
  8111. @node Hash Table Examples
  8112. @subsubsection Hash Table Examples
  8113. For demonstration purposes, this section gives a few usage examples of
  8114. some hash table procedures, together with some explanation what they do.
  8115. First we start by creating a new hash table with 31 slots, and
  8116. populate it with two key/value pairs.
  8117. @lisp
  8118. (define h (make-hash-table 31))
  8119. ;; This is an opaque object
  8120. h
  8121. @result{}
  8122. #<hash-table 0/31>
  8123. ;; Inserting into a hash table can be done with hashq-set!
  8124. (hashq-set! h 'foo "bar")
  8125. @result{}
  8126. "bar"
  8127. (hashq-set! h 'braz "zonk")
  8128. @result{}
  8129. "zonk"
  8130. ;; Or with hash-create-handle!
  8131. (hashq-create-handle! h 'frob #f)
  8132. @result{}
  8133. (frob . #f)
  8134. @end lisp
  8135. You can get the value for a given key with the procedure
  8136. @code{hashq-ref}, but the problem with this procedure is that you
  8137. cannot reliably determine whether a key does exists in the table. The
  8138. reason is that the procedure returns @code{#f} if the key is not in
  8139. the table, but it will return the same value if the key is in the
  8140. table and just happens to have the value @code{#f}, as you can see in
  8141. the following examples.
  8142. @lisp
  8143. (hashq-ref h 'foo)
  8144. @result{}
  8145. "bar"
  8146. (hashq-ref h 'frob)
  8147. @result{}
  8148. #f
  8149. (hashq-ref h 'not-there)
  8150. @result{}
  8151. #f
  8152. @end lisp
  8153. It is often better is to use the procedure @code{hashq-get-handle},
  8154. which makes a distinction between the two cases. Just like @code{assq},
  8155. this procedure returns a key/value-pair on success, and @code{#f} if the
  8156. key is not found.
  8157. @lisp
  8158. (hashq-get-handle h 'foo)
  8159. @result{}
  8160. (foo . "bar")
  8161. (hashq-get-handle h 'not-there)
  8162. @result{}
  8163. #f
  8164. @end lisp
  8165. Interesting results can be computed by using @code{hash-fold} to work
  8166. through each element. This example will count the total number of
  8167. elements:
  8168. @lisp
  8169. (hash-fold (lambda (key value seed) (+ 1 seed)) 0 h)
  8170. @result{}
  8171. 3
  8172. @end lisp
  8173. The same thing can be done with the procedure @code{hash-count}, which
  8174. can also count the number of elements matching a particular predicate.
  8175. For example, count the number of elements with string values:
  8176. @lisp
  8177. (hash-count (lambda (key value) (string? value)) h)
  8178. @result{}
  8179. 2
  8180. @end lisp
  8181. Counting all the elements is a simple task using @code{const}:
  8182. @lisp
  8183. (hash-count (const #t) h)
  8184. @result{}
  8185. 3
  8186. @end lisp
  8187. @node Hash Table Reference
  8188. @subsubsection Hash Table Reference
  8189. @c FIXME: Describe in broad terms what happens for resizing, and what
  8190. @c the initial size means for this.
  8191. Like the association list functions, the hash table functions come in
  8192. several varieties, according to the equality test used for the keys.
  8193. Plain @code{hash-} functions use @code{equal?}, @code{hashq-}
  8194. functions use @code{eq?}, @code{hashv-} functions use @code{eqv?}, and
  8195. the @code{hashx-} functions use an application supplied test.
  8196. A single @code{make-hash-table} creates a hash table suitable for use
  8197. with any set of functions, but it's imperative that just one set is
  8198. then used consistently, or results will be unpredictable.
  8199. Hash tables are implemented as a vector indexed by a hash value formed
  8200. from the key, with an association list of key/value pairs for each
  8201. bucket in case distinct keys hash together. Direct access to the
  8202. pairs in those lists is provided by the @code{-handle-} functions.
  8203. When the number of entries in a hash table goes above a threshold, the
  8204. vector is made larger and the entries are rehashed, to prevent the
  8205. bucket lists from becoming too long and slowing down accesses. When the
  8206. number of entries goes below a threshold, the vector is shrunk to save
  8207. space.
  8208. For the @code{hashx-} ``extended'' routines, an application supplies a
  8209. @var{hash} function producing an integer index like @code{hashq} etc
  8210. below, and an @var{assoc} alist search function like @code{assq} etc
  8211. (@pxref{Retrieving Alist Entries}). Here's an example of such
  8212. functions implementing case-insensitive hashing of string keys,
  8213. @example
  8214. (use-modules (srfi srfi-1)
  8215. (srfi srfi-13))
  8216. (define (my-hash str size)
  8217. (remainder (string-hash-ci str) size))
  8218. (define (my-assoc str alist)
  8219. (find (lambda (pair) (string-ci=? str (car pair))) alist))
  8220. (define my-table (make-hash-table))
  8221. (hashx-set! my-hash my-assoc my-table "foo" 123)
  8222. (hashx-ref my-hash my-assoc my-table "FOO")
  8223. @result{} 123
  8224. @end example
  8225. In a @code{hashx-} @var{hash} function the aim is to spread keys
  8226. across the vector, so bucket lists don't become long. But the actual
  8227. values are arbitrary as long as they're in the range 0 to
  8228. @math{@var{size}-1}. Helpful functions for forming a hash value, in
  8229. addition to @code{hashq} etc below, include @code{symbol-hash}
  8230. (@pxref{Symbol Keys}), @code{string-hash} and @code{string-hash-ci}
  8231. (@pxref{String Comparison}), and @code{char-set-hash}
  8232. (@pxref{Character Set Predicates/Comparison}).
  8233. @sp 1
  8234. @deffn {Scheme Procedure} make-hash-table [size]
  8235. Create a new hash table object, with an optional minimum
  8236. vector @var{size}.
  8237. When @var{size} is given, the table vector will still grow and shrink
  8238. automatically, as described above, but with @var{size} as a minimum.
  8239. If an application knows roughly how many entries the table will hold
  8240. then it can use @var{size} to avoid rehashing when initial entries are
  8241. added.
  8242. @end deffn
  8243. @deffn {Scheme Procedure} alist->hash-table alist
  8244. @deffnx {Scheme Procedure} alist->hashq-table alist
  8245. @deffnx {Scheme Procedure} alist->hashv-table alist
  8246. @deffnx {Scheme Procedure} alist->hashx-table hash assoc alist
  8247. Convert @var{alist} into a hash table. When keys are repeated in
  8248. @var{alist}, the leftmost association takes precedence.
  8249. @example
  8250. (use-modules (ice-9 hash-table))
  8251. (alist->hash-table '((foo . 1) (bar . 2)))
  8252. @end example
  8253. When converting to an extended hash table, custom @var{hash} and
  8254. @var{assoc} procedures must be provided.
  8255. @example
  8256. (alist->hashx-table hash assoc '((foo . 1) (bar . 2)))
  8257. @end example
  8258. @end deffn
  8259. @deffn {Scheme Procedure} hash-table? obj
  8260. @deffnx {C Function} scm_hash_table_p (obj)
  8261. Return @code{#t} if @var{obj} is a abstract hash table object.
  8262. @end deffn
  8263. @deffn {Scheme Procedure} hash-clear! table
  8264. @deffnx {C Function} scm_hash_clear_x (table)
  8265. Remove all items from @var{table} (without triggering a resize).
  8266. @end deffn
  8267. @deffn {Scheme Procedure} hash-ref table key [dflt]
  8268. @deffnx {Scheme Procedure} hashq-ref table key [dflt]
  8269. @deffnx {Scheme Procedure} hashv-ref table key [dflt]
  8270. @deffnx {Scheme Procedure} hashx-ref hash assoc table key [dflt]
  8271. @deffnx {C Function} scm_hash_ref (table, key, dflt)
  8272. @deffnx {C Function} scm_hashq_ref (table, key, dflt)
  8273. @deffnx {C Function} scm_hashv_ref (table, key, dflt)
  8274. @deffnx {C Function} scm_hashx_ref (hash, assoc, table, key, dflt)
  8275. Lookup @var{key} in the given hash @var{table}, and return the
  8276. associated value. If @var{key} is not found, return @var{dflt}, or
  8277. @code{#f} if @var{dflt} is not given.
  8278. @end deffn
  8279. @deffn {Scheme Procedure} hash-set! table key val
  8280. @deffnx {Scheme Procedure} hashq-set! table key val
  8281. @deffnx {Scheme Procedure} hashv-set! table key val
  8282. @deffnx {Scheme Procedure} hashx-set! hash assoc table key val
  8283. @deffnx {C Function} scm_hash_set_x (table, key, val)
  8284. @deffnx {C Function} scm_hashq_set_x (table, key, val)
  8285. @deffnx {C Function} scm_hashv_set_x (table, key, val)
  8286. @deffnx {C Function} scm_hashx_set_x (hash, assoc, table, key, val)
  8287. Associate @var{val} with @var{key} in the given hash @var{table}. If
  8288. @var{key} is already present then it's associated value is changed.
  8289. If it's not present then a new entry is created.
  8290. @end deffn
  8291. @deffn {Scheme Procedure} hash-remove! table key
  8292. @deffnx {Scheme Procedure} hashq-remove! table key
  8293. @deffnx {Scheme Procedure} hashv-remove! table key
  8294. @deffnx {Scheme Procedure} hashx-remove! hash assoc table key
  8295. @deffnx {C Function} scm_hash_remove_x (table, key)
  8296. @deffnx {C Function} scm_hashq_remove_x (table, key)
  8297. @deffnx {C Function} scm_hashv_remove_x (table, key)
  8298. @deffnx {C Function} scm_hashx_remove_x (hash, assoc, table, key)
  8299. Remove any association for @var{key} in the given hash @var{table}.
  8300. If @var{key} is not in @var{table} then nothing is done.
  8301. @end deffn
  8302. @deffn {Scheme Procedure} hash key size
  8303. @deffnx {Scheme Procedure} hashq key size
  8304. @deffnx {Scheme Procedure} hashv key size
  8305. @deffnx {C Function} scm_hash (key, size)
  8306. @deffnx {C Function} scm_hashq (key, size)
  8307. @deffnx {C Function} scm_hashv (key, size)
  8308. Return a hash value for @var{key}. This is a number in the range
  8309. @math{0} to @math{@var{size}-1}, which is suitable for use in a hash
  8310. table of the given @var{size}.
  8311. Note that @code{hashq} and @code{hashv} may use internal addresses of
  8312. objects, so if an object is garbage collected and re-created it can
  8313. have a different hash value, even when the two are notionally
  8314. @code{eq?}. For instance with symbols,
  8315. @example
  8316. (hashq 'something 123) @result{} 19
  8317. (gc)
  8318. (hashq 'something 123) @result{} 62
  8319. @end example
  8320. In normal use this is not a problem, since an object entered into a
  8321. hash table won't be garbage collected until removed. It's only if
  8322. hashing calculations are somehow separated from normal references that
  8323. its lifetime needs to be considered.
  8324. @end deffn
  8325. @deffn {Scheme Procedure} hash-get-handle table key
  8326. @deffnx {Scheme Procedure} hashq-get-handle table key
  8327. @deffnx {Scheme Procedure} hashv-get-handle table key
  8328. @deffnx {Scheme Procedure} hashx-get-handle hash assoc table key
  8329. @deffnx {C Function} scm_hash_get_handle (table, key)
  8330. @deffnx {C Function} scm_hashq_get_handle (table, key)
  8331. @deffnx {C Function} scm_hashv_get_handle (table, key)
  8332. @deffnx {C Function} scm_hashx_get_handle (hash, assoc, table, key)
  8333. Return the @code{(@var{key} . @var{value})} pair for @var{key} in the
  8334. given hash @var{table}, or @code{#f} if @var{key} is not in
  8335. @var{table}.
  8336. @end deffn
  8337. @deffn {Scheme Procedure} hash-create-handle! table key init
  8338. @deffnx {Scheme Procedure} hashq-create-handle! table key init
  8339. @deffnx {Scheme Procedure} hashv-create-handle! table key init
  8340. @deffnx {Scheme Procedure} hashx-create-handle! hash assoc table key init
  8341. @deffnx {C Function} scm_hash_create_handle_x (table, key, init)
  8342. @deffnx {C Function} scm_hashq_create_handle_x (table, key, init)
  8343. @deffnx {C Function} scm_hashv_create_handle_x (table, key, init)
  8344. @deffnx {C Function} scm_hashx_create_handle_x (hash, assoc, table, key, init)
  8345. Return the @code{(@var{key} . @var{value})} pair for @var{key} in the
  8346. given hash @var{table}. If @var{key} is not in @var{table} then
  8347. create an entry for it with @var{init} as the value, and return that
  8348. pair.
  8349. @end deffn
  8350. @deffn {Scheme Procedure} hash-map->list proc table
  8351. @deffnx {Scheme Procedure} hash-for-each proc table
  8352. @deffnx {C Function} scm_hash_map_to_list (proc, table)
  8353. @deffnx {C Function} scm_hash_for_each (proc, table)
  8354. Apply @var{proc} to the entries in the given hash @var{table}. Each
  8355. call is @code{(@var{proc} @var{key} @var{value})}. @code{hash-map->list}
  8356. returns a list of the results from these calls, @code{hash-for-each}
  8357. discards the results and returns an unspecified value.
  8358. Calls are made over the table entries in an unspecified order, and for
  8359. @code{hash-map->list} the order of the values in the returned list is
  8360. unspecified. Results will be unpredictable if @var{table} is modified
  8361. while iterating.
  8362. For example the following returns a new alist comprising all the
  8363. entries from @code{mytable}, in no particular order.
  8364. @example
  8365. (hash-map->list cons mytable)
  8366. @end example
  8367. @end deffn
  8368. @deffn {Scheme Procedure} hash-for-each-handle proc table
  8369. @deffnx {C Function} scm_hash_for_each_handle (proc, table)
  8370. Apply @var{proc} to the entries in the given hash @var{table}. Each
  8371. call is @code{(@var{proc} @var{handle})}, where @var{handle} is a
  8372. @code{(@var{key} . @var{value})} pair. Return an unspecified value.
  8373. @code{hash-for-each-handle} differs from @code{hash-for-each} only in
  8374. the argument list of @var{proc}.
  8375. @end deffn
  8376. @deffn {Scheme Procedure} hash-fold proc init table
  8377. @deffnx {C Function} scm_hash_fold (proc, init, table)
  8378. Accumulate a result by applying @var{proc} to the elements of the
  8379. given hash @var{table}. Each call is @code{(@var{proc} @var{key}
  8380. @var{value} @var{prior-result})}, where @var{key} and @var{value} are
  8381. from the @var{table} and @var{prior-result} is the return from the
  8382. previous @var{proc} call. For the first call, @var{prior-result} is
  8383. the given @var{init} value.
  8384. Calls are made over the table entries in an unspecified order.
  8385. Results will be unpredictable if @var{table} is modified while
  8386. @code{hash-fold} is running.
  8387. For example, the following returns a count of how many keys in
  8388. @code{mytable} are strings.
  8389. @example
  8390. (hash-fold (lambda (key value prior)
  8391. (if (string? key) (1+ prior) prior))
  8392. 0 mytable)
  8393. @end example
  8394. @end deffn
  8395. @deffn {Scheme Procedure} hash-count pred table
  8396. @deffnx {C Function} scm_hash_count (pred, table)
  8397. Return the number of elements in the given hash @var{table} that cause
  8398. @code{(@var{pred} @var{key} @var{value})} to return true. To quickly
  8399. determine the total number of elements, use @code{(const #t)} for
  8400. @var{pred}.
  8401. @end deffn
  8402. @node Other Types
  8403. @subsection Other Types
  8404. Procedures are documented in their own section. @xref{Procedures}.
  8405. Variable objects are documented as part of the description of Guile's
  8406. module system: see @ref{Variables}.
  8407. @xref{Scheduling}, for discussion of threads, mutexes, and so on.
  8408. Ports are described in the section on I/O: see @ref{Input and Output}.
  8409. Regular expressions are described in their own section: see @ref{Regular
  8410. Expressions}.
  8411. There are quite a number of additional data types documented in this
  8412. manual; if you feel a link is missing here, please file a bug.
  8413. @c Local Variables:
  8414. @c TeX-master: "guile.texi"
  8415. @c End: