api-data.texi 367 KB

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