api-data.texi 175 KB

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