api-data.texi 205 KB

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