api-data.texi 366 KB

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