misc-modules.texi 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  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, 2009, 2010, 2011
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Pretty Printing
  7. @section Pretty Printing
  8. @c FIXME::martin: Review me!
  9. @cindex pretty printing
  10. The module @code{(ice-9 pretty-print)} provides the procedure
  11. @code{pretty-print}, which provides nicely formatted output of Scheme
  12. objects. This is especially useful for deeply nested or complex data
  13. structures, such as lists and vectors.
  14. The module is loaded by entering the following:
  15. @lisp
  16. (use-modules (ice-9 pretty-print))
  17. @end lisp
  18. This makes the procedure @code{pretty-print} available. As an example
  19. how @code{pretty-print} will format the output, see the following:
  20. @lisp
  21. (pretty-print '(define (foo) (lambda (x)
  22. (cond ((zero? x) #t) ((negative? x) -x) (else
  23. (if (= x 1) 2 (* x x x)))))))
  24. @print{}
  25. (define (foo)
  26. (lambda (x)
  27. (cond ((zero? x) #t)
  28. ((negative? x) -x)
  29. (else (if (= x 1) 2 (* x x x))))))
  30. @end lisp
  31. @deffn {Scheme Procedure} pretty-print obj [port] [keyword-options]
  32. Print the textual representation of the Scheme object @var{obj} to
  33. @var{port}. @var{port} defaults to the current output port, if not
  34. given.
  35. The further @var{keyword-options} are keywords and parameters as
  36. follows,
  37. @table @asis
  38. @item @nicode{#:display?} @var{flag}
  39. If @var{flag} is true then print using @code{display}. The default is
  40. @code{#f} which means use @code{write} style. (@pxref{Writing})
  41. @item @nicode{#:per-line-prefix} @var{string}
  42. Print the given @var{string} as a prefix on each line. The default is
  43. no prefix.
  44. @item @nicode{#:width} @var{columns}
  45. Print within the given @var{columns}. The default is 79.
  46. @end table
  47. @end deffn
  48. @cindex truncated printing
  49. Also exported by the @code{(ice-9 pretty-print)} module is
  50. @code{truncated-print}, a procedure to print Scheme datums, truncating
  51. the output to a certain number of characters. This is useful when you
  52. need to present an arbitrary datum to the user, but you only have one
  53. line in which to do so.
  54. @lisp
  55. (define exp '(a b #(c d e) f . g))
  56. (truncated-print exp #:width 10) (newline)
  57. @print{} (a b . #)
  58. (truncated-print exp #:width 15) (newline)
  59. @print{} (a b # f . g)
  60. (truncated-print exp #:width 18) (newline)
  61. @print{} (a b #(c ...) . #)
  62. (truncated-print exp #:width 20) (newline)
  63. @print{} (a b #(c d e) f . g)
  64. (truncated-print "The quick brown fox" #:width 20) (newline)
  65. @print{} "The quick brown..."
  66. (truncated-print (current-module) #:width 20) (newline)
  67. @print{} #<directory (gui...>
  68. @end lisp
  69. @code{truncated-print} will not output a trailing newline. If an expression does
  70. not fit in the given width, it will be truncated -- possibly
  71. ellipsized@footnote{On Unicode-capable ports, the ellipsis is represented by
  72. character `HORIZONTAL ELLIPSIS' (U+2026), otherwise it is represented by three
  73. dots.}, or in the worst case, displayed as @nicode{#}.
  74. @deffn {Scheme Procedure} truncated-print obj [port] [keyword-options]
  75. Print @var{obj}, truncating the output, if necessary, to make it fit
  76. into @var{width} characters. By default, @var{x} will be printed using
  77. @code{write}, though that behavior can be overridden via the
  78. @var{display?} keyword argument.
  79. The default behaviour is to print depth-first, meaning that the entire
  80. remaining width will be available to each sub-expression of @var{x} --
  81. e.g., if @var{x} is a vector, each member of @var{x}. One can attempt to
  82. ``ration'' the available width, trying to allocate it equally to each
  83. sub-expression, via the @var{breadth-first?} keyword argument.
  84. The further @var{keyword-options} are keywords and parameters as
  85. follows,
  86. @table @asis
  87. @item @nicode{#:display?} @var{flag}
  88. If @var{flag} is true then print using @code{display}. The default is
  89. @code{#f} which means use @code{write} style. (@pxref{Writing})
  90. @item @nicode{#:width} @var{columns}
  91. Print within the given @var{columns}. The default is 79.
  92. @item @nicode{#:breadth-first?} @var{flag}
  93. If @var{flag} is true, then allocate the available width breadth-first
  94. among elements of a compound data structure (list, vector, pair,
  95. etc.). The default is @code{#f} which means that any element is
  96. allowed to consume all of the available width.
  97. @end table
  98. @end deffn
  99. @node Formatted Output
  100. @section Formatted Output
  101. @cindex formatted output
  102. @c For reference, in this section escapes like ~a are given in
  103. @c @nicode, to give code font in TeX etc, but leave them unadorned in
  104. @c Info.
  105. @c
  106. @c The idea is to reduce clutter around what's shown, and avoid any
  107. @c possible confusion over whether the ` ' quotes are part of what
  108. @c should be entered. (In particular for instance of course ' is
  109. @c meaningful in a format string, introducing a char parameter).
  110. The @code{format} function is a powerful way to print numbers, strings
  111. and other objects together with literal text under the control of a
  112. format string. This function is available from
  113. @example
  114. (use-modules (ice-9 format))
  115. @end example
  116. A format string is generally more compact and easier than using just
  117. the standard procedures like @code{display}, @code{write} and
  118. @code{newline}. Parameters in the output string allow various output
  119. styles, and parameters can be taken from the arguments for runtime
  120. flexibility.
  121. @code{format} is similar to the Common Lisp procedure of the same
  122. name, but it's not identical and doesn't have quite all the features
  123. found in Common Lisp.
  124. C programmers will note the similarity between @code{format} and
  125. @code{printf}, though escape sequences are marked with @nicode{~}
  126. instead of @nicode{%}, and are more powerful.
  127. @sp 1
  128. @deffn {Scheme Procedure} format dest fmt [args@dots{}]
  129. Write output specified by the @var{fmt} string to @var{dest}.
  130. @var{dest} can be an output port, @code{#t} for
  131. @code{current-output-port} (@pxref{Default Ports}), or @code{#f} to
  132. return the output as a string.
  133. @var{fmt} can contain literal text to be output, and @nicode{~}
  134. escapes. Each escape has the form
  135. @example
  136. ~ [param [, param@dots{}] [:] [@@] code
  137. @end example
  138. @nicode{code} is a character determining the escape sequence. The
  139. @nicode{:} and @nicode{@@} characters are optional modifiers, one or
  140. both of which change the way various codes operate. Optional
  141. parameters are accepted by some codes too. Parameters have the
  142. following forms,
  143. @table @asis
  144. @item @nicode{[+/-]number}
  145. An integer, with optional @nicode{+} or @nicode{-}.
  146. @item @nicode{'} (apostrophe)
  147. The following character in the format string, for instance @nicode{'z}
  148. for @nicode{z}.
  149. @item @nicode{v}
  150. The next function argument as the parameter. @nicode{v} stands for
  151. ``variable'', a parameter can be calculated at runtime and included in
  152. the arguments. Upper case @nicode{V} can be used too.
  153. @item @nicode{#}
  154. The number of arguments remaining. (See @nicode{~*} below for some
  155. usages.)
  156. @end table
  157. Parameters are separated by commas (@nicode{,}). A parameter can be
  158. left empty to keep its default value when supplying later parameters.
  159. @sp 1
  160. The following escapes are available. The code letters are not
  161. case-sensitive, upper and lower case are the same.
  162. @table @asis
  163. @item @nicode{~a}
  164. @itemx @nicode{~s}
  165. Object output. Parameters: @var{minwidth}, @var{padinc},
  166. @var{minpad}, @var{padchar}.
  167. @nicode{~a} outputs an argument like @code{display}, @nicode{~s}
  168. outputs an argument like @code{write} (@pxref{Writing}).
  169. @example
  170. (format #t "~a" "foo") @print{} foo
  171. (format #t "~s" "foo") @print{} "foo"
  172. @end example
  173. @nicode{~:a} and @nicode{~:s} put objects that don't have an external
  174. representation in quotes like a string.
  175. @example
  176. (format #t "~:a" car) @print{} "#<primitive-procedure car>"
  177. @end example
  178. If the output is less than @var{minwidth} characters (default 0), it's
  179. padded on the right with @var{padchar} (default space). @nicode{~@@a}
  180. and @nicode{~@@s} put the padding on the left instead.
  181. @example
  182. (format #f "~5a" 'abc) @result{} "abc "
  183. (format #f "~5,,,'-@@a" 'abc) @result{} "--abc"
  184. @end example
  185. @var{minpad} is a minimum for the padding then plus a multiple of
  186. @var{padinc}. Ie.@: the padding is @math{@var{minpad} + @var{N} *
  187. @var{padinc}}, where @var{n} is the smallest integer making the total
  188. object plus padding greater than or equal to @var{minwidth}. The
  189. default @var{minpad} is 0 and the default @var{padinc} is 1 (imposing
  190. no minimum or multiple).
  191. @example
  192. (format #f "~5,1,4a" 'abc) @result{} "abc "
  193. @end example
  194. @item @nicode{~c}
  195. Character. Parameter: @var{charnum}.
  196. Output a character. The default is to simply output, as per
  197. @code{write-char} (@pxref{Writing}). @nicode{~@@c} prints in
  198. @code{write} style. @nicode{~:c} prints control characters (ASCII 0
  199. to 31) in @nicode{^X} form.
  200. @example
  201. (format #t "~c" #\z) @print{} z
  202. (format #t "~@@c" #\z) @print{} #\z
  203. (format #t "~:c" #\newline) @print{} ^J
  204. @end example
  205. If the @var{charnum} parameter is given then an argument is not taken
  206. but instead the character is @code{(integer->char @var{charnum})}
  207. (@pxref{Characters}). This can be used for instance to output
  208. characters given by their ASCII code.
  209. @example
  210. (format #t "~65c") @print{} A
  211. @end example
  212. @item @nicode{~d}
  213. @itemx @nicode{~x}
  214. @itemx @nicode{~o}
  215. @itemx @nicode{~b}
  216. Integer. Parameters: @var{minwidth}, @var{padchar}, @var{commachar},
  217. @var{commawidth}.
  218. Output an integer argument as a decimal, hexadecimal, octal or binary
  219. integer (respectively).
  220. @example
  221. (format #t "~d" 123) @print{} 123
  222. @end example
  223. @nicode{~@@d} etc shows a @nicode{+} sign is shown on positive
  224. numbers.
  225. @c FIXME: "+" is not shown on zero, unlike in Common Lisp. Should
  226. @c that be changed in the code, or is it too late and should just be
  227. @c documented that way?
  228. @example
  229. (format #t "~@@b" 12) @print{} +1100
  230. @end example
  231. If the output is less than the @var{minwidth} parameter (default no
  232. minimum), it's padded on the left with the @var{padchar} parameter
  233. (default space).
  234. @example
  235. (format #t "~5,'*d" 12) @print{} ***12
  236. (format #t "~5,'0d" 12) @print{} 00012
  237. (format #t "~3d" 1234) @print{} 1234
  238. @end example
  239. @nicode{~:d} adds commas (or the @var{commachar} parameter) every
  240. three digits (or the @var{commawidth} parameter many).
  241. @example
  242. (format #t "~:d" 1234567) @print{} 1,234,567
  243. (format #t "~10,'*,'/,2:d" 12345) @print{} ***1/23/45
  244. @end example
  245. Hexadecimal @nicode{~x} output is in lower case, but the @nicode{~(}
  246. and @nicode{~)} case conversion directives described below can be used
  247. to get upper case.
  248. @example
  249. (format #t "~x" 65261) @print{} feed
  250. (format #t "~:@@(~x~)" 65261) @print{} FEED
  251. @end example
  252. @item @nicode{~r}
  253. Integer in words, roman numerals, or a specified radix. Parameters:
  254. @var{radix}, @var{minwidth}, @var{padchar}, @var{commachar},
  255. @var{commawidth}.
  256. With no parameters output is in words as a cardinal like ``ten'', or
  257. @nicode{~:r} prints an ordinal like ``tenth''.
  258. @example
  259. (format #t "~r" 9) @print{} nine ;; cardinal
  260. (format #t "~r" -9) @print{} minus nine ;; cardinal
  261. (format #t "~:r" 9) @print{} ninth ;; ordinal
  262. @end example
  263. And also with no parameters, @nicode{~@@r} gives roman numerals and
  264. @nicode{~:@@r} gives old roman numerals. In old roman numerals
  265. there's no ``subtraction'', so 9 is @nicode{VIIII} instead of
  266. @nicode{IX}. In both cases only positive numbers can be output.
  267. @example
  268. (format #t "~@@r" 89) @print{} LXXXIX ;; roman
  269. (format #t "~:@@r" 89) @print{} LXXXVIIII ;; old roman
  270. @end example
  271. When a parameter is given it means numeric output in the specified
  272. @var{radix}. The modifiers and parameters following the radix are the
  273. same as described for @nicode{~d} etc above.
  274. @example
  275. (format #f "~3r" 27) @result{} "1000" ;; base 3
  276. (format #f "~3,5r" 26) @result{} " 222" ;; base 3 width 5
  277. @end example
  278. @item @nicode{~f}
  279. Fixed-point float. Parameters: @var{width}, @var{decimals},
  280. @var{scale}, @var{overflowchar}, @var{padchar}.
  281. Output a number or number string in fixed-point format, ie.@: with a
  282. decimal point.
  283. @example
  284. (format #t "~f" 5) @print{} 5.0
  285. (format #t "~f" "123") @print{} 123.0
  286. (format #t "~f" "1e-1") @print{} 0.1
  287. @end example
  288. @nicode{~@@f} prints a @nicode{+} sign on positive numbers (including
  289. zero).
  290. @example
  291. (format #t "~@@f" 0) @print{} +0.0
  292. @end example
  293. If the output is less than @var{width} characters it's padded on the
  294. left with @var{padchar} (space by default). If the output equals or
  295. exceeds @var{width} then there's no padding. The default for
  296. @var{width} is no padding.
  297. @example
  298. (format #f "~6f" -1.5) @result{} " -1.5"
  299. (format #f "~6,,,,'*f" 23) @result{} "**23.0"
  300. (format #f "~6f" 1234567.0) @result{} "1234567.0"
  301. @end example
  302. @var{decimals} is how many digits to print after the decimal point,
  303. with the value rounded or padded with zeros as necessary. (The
  304. default is to output as many decimals as required.)
  305. @example
  306. (format #t "~1,2f" 3.125) @print{} 3.13
  307. (format #t "~1,2f" 1.5) @print{} 1.50
  308. @end example
  309. @var{scale} is a power of 10 applied to the value, moving the decimal
  310. point that many places. A positive @var{scale} increases the value
  311. shown, a negative decreases it.
  312. @example
  313. (format #t "~,,2f" 1234) @print{} 123400.0
  314. (format #t "~,,-2f" 1234) @print{} 12.34
  315. @end example
  316. If @var{overflowchar} and @var{width} are both given and if the output
  317. would exceed @var{width}, then that many @var{overflowchar}s are
  318. printed instead of the value.
  319. @example
  320. (format #t "~6,,,'xf" 12345) @print{} 12345.
  321. (format #t "~5,,,'xf" 12345) @print{} xxxxx
  322. @end example
  323. @item @nicode{~e}
  324. Exponential float. Parameters: @var{width}, @var{mantdigits},
  325. @var{expdigits}, @var{intdigits}, @var{overflowchar}, @var{padchar},
  326. @var{expchar}.
  327. Output a number or number string in exponential notation.
  328. @example
  329. (format #t "~e" 5000.25) @print{} 5.00025E+3
  330. (format #t "~e" "123.4") @print{} 1.234E+2
  331. (format #t "~e" "1e4") @print{} 1.0E+4
  332. @end example
  333. @nicode{~@@e} prints a @nicode{+} sign on positive numbers (including
  334. zero). (This is for the mantissa, a @nicode{+} or @nicode{-} sign is
  335. always shown on the exponent.)
  336. @example
  337. (format #t "~@@e" 5000.0) @print{} +5.0E+3
  338. @end example
  339. If the output is less than @var{width} characters it's padded on the
  340. left with @var{padchar} (space by default). The default for
  341. @var{width} is to output with no padding.
  342. @example
  343. (format #f "~10e" 1234.0) @result{} " 1.234E+3"
  344. (format #f "~10,,,,,'*e" 0.5) @result{} "****5.0E-1"
  345. @end example
  346. @c FIXME: Describe what happens when the number is bigger than WIDTH.
  347. @c There seems to be a bit of dodginess about this, or some deviation
  348. @c from Common Lisp.
  349. @var{mantdigits} is the number of digits shown in the mantissa after
  350. the decimal point. The value is rounded or trailing zeros are added
  351. as necessary. The default @var{mantdigits} is to show as much as
  352. needed by the value.
  353. @example
  354. (format #f "~,3e" 11111.0) @result{} "1.111E+4"
  355. (format #f "~,8e" 123.0) @result{} "1.23000000E+2"
  356. @end example
  357. @var{expdigits} is the minimum number of digits shown for the
  358. exponent, with leading zeros added if necessary. The default for
  359. @var{expdigits} is to show only as many digits as required. At least
  360. 1 digit is always shown.
  361. @example
  362. (format #f "~,,1e" 1.0e99) @result{} "1.0E+99"
  363. (format #f "~,,6e" 1.0e99) @result{} "1.0E+000099"
  364. @end example
  365. @var{intdigits} (default 1) is the number of digits to show before the
  366. decimal point in the mantissa. @var{intdigits} can be zero, in which
  367. case the integer part is a single @nicode{0}, or it can be negative,
  368. in which case leading zeros are shown after the decimal point.
  369. @c FIXME: When INTDIGITS is 0, Common Lisp format apparently only
  370. @c shows the single 0 digit if it fits in WIDTH. format.scm seems to
  371. @c show it always. Is it meant to?
  372. @example
  373. (format #t "~,,,3e" 12345.0) @print{} 123.45E+2
  374. (format #t "~,,,0e" 12345.0) @print{} 0.12345E+5
  375. (format #t "~,,,-3e" 12345.0) @print{} 0.00012345E+8
  376. @end example
  377. @c FIXME: MANTDIGITS with negative INTDIGITS doesn't match CL spec,
  378. @c believe the spec says it ought to still show mantdigits+1 sig
  379. @c figures, i.e. leading zeros don't count towards MANTDIGITS, but it
  380. @c seems to just treat MANTDIGITS as how many digits after the
  381. @c decimal point.
  382. If @var{overflowchar} is given then @var{width} is a hard limit. If
  383. the output would exceed @var{width} then instead that many
  384. @var{overflowchar}s are printed.
  385. @example
  386. (format #f "~6,,,,'xe" 100.0) @result{} "1.0E+2"
  387. (format #f "~3,,,,'xe" 100.0) @result{} "xxx"
  388. @end example
  389. @var{expchar} is the exponent marker character (default @nicode{E}).
  390. @example
  391. (format #t "~,,,,,,'ee" 100.0) @print{} 1.0e+2
  392. @end example
  393. @item @nicode{~g}
  394. General float. Parameters: @var{width}, @var{mantdigits},
  395. @var{expdigits}, @var{intdigits}, @var{overflowchar}, @var{padchar},
  396. @var{expchar}.
  397. Output a number or number string in either exponential format the same
  398. as @nicode{~e}, or fixed-point format like @nicode{~f} but aligned
  399. where the mantissa would have been and followed by padding where the
  400. exponent would have been.
  401. @c FIXME: The default MANTDIGITS is apparently max(needed,min(n,7))
  402. @c where 10^(n-1)<=abs(x)<=10^n. But the Common Lisp spec seems to
  403. @c ask for "needed" to be without leading or trailing zeros, whereas
  404. @c format.scm seems to include trailing zeros, ending up with it
  405. @c using fixed format for bigger values than it should.
  406. Fixed-point is used when the absolute value is 0.1 or more and it
  407. takes no more space than the mantissa in exponential format, ie.@:
  408. basically up to @var{mantdigits} digits.
  409. @example
  410. (format #f "~12,4,2g" 999.0) @result{} " 999.0 "
  411. (format #f "~12,4,2g" "100000") @result{} " 1.0000E+05"
  412. @end example
  413. The parameters are interpreted as per @nicode{~e} above. When
  414. fixed-point is used, the @var{decimals} parameter to @nicode{~f} is
  415. established from @var{mantdigits}, so as to give a total
  416. @math{@var{mantdigits}+1} figures.
  417. @item @nicode{~$}
  418. Monetary style fixed-point float. Parameters: @var{decimals},
  419. @var{intdigits}, @var{width}, @var{padchar}.
  420. @c For reference, fmtdoc.txi from past versions of slib showed the
  421. @c INTDIGITS parameter as SCALE. That looks like a typo, in the code
  422. @c and in the Common Lisp spec it's a minimum digits for the integer
  423. @c part, it isn't a power of 10 like in ~f.
  424. Output a number or number string in fixed-point format, ie.@: with a
  425. decimal point. @var{decimals} is the number of decimal places to
  426. show, default 2.
  427. @example
  428. (format #t "~$" 5) @print{} 5.00
  429. (format #t "~4$" "2.25") @print{} 2.2500
  430. (format #t "~4$" "1e-2") @print{} 0.0100
  431. @end example
  432. @nicode{~@@$} prints a @nicode{+} sign on positive numbers (including
  433. zero).
  434. @example
  435. (format #t "~@@$" 0) @print{} +0.00
  436. @end example
  437. @var{intdigits} is a minimum number of digits to show in the integer
  438. part of the value (default 1).
  439. @example
  440. (format #t "~,3$" 9.5) @print{} 009.50
  441. (format #t "~,0$" 0.125) @print{} .13
  442. @end example
  443. If the output is less than @var{width} characters (default 0), it's
  444. padded on the left with @var{padchar} (default space). @nicode{~:$}
  445. puts the padding after the sign.
  446. @example
  447. (format #f "~,,8$" -1.5) @result{} " -1.50"
  448. (format #f "~,,8:$" -1.5) @result{} "- 1.50"
  449. (format #f "~,,8,'.:@@$" 3) @result{} "+...3.00"
  450. @end example
  451. Note that floating point for dollar amounts is generally not a good
  452. idea, because a cent @math{0.01} cannot be represented exactly in the
  453. binary floating point Guile uses, which leads to slowly accumulating
  454. rounding errors. Keeping values as cents (or fractions of a cent) in
  455. integers then printing with the scale option in @nicode{~f} may be a
  456. better approach.
  457. @c For reference, fractions don't work with ~$ (or any of the float
  458. @c conversions) currently. If they did work then we could perhaps
  459. @c suggest keeping dollar amounts as rationals, which would of course
  460. @c give exact cents. An integer as cents is probably still a better
  461. @c recommendation though, since it forces one to think about where
  462. @c and when rounding can or should occur.
  463. @item @nicode{~i}
  464. Complex fixed-point float. Parameters: @var{width}, @var{decimals},
  465. @var{scale}, @var{overflowchar}, @var{padchar}.
  466. @c For reference, in Common Lisp ~i is an indent, but slib fmtdoc.txi
  467. @c described it as complex number output, so we keep that.
  468. Output the argument as a complex number, with both real and imaginary
  469. part shown (even if one or both are zero).
  470. The parameters and modifiers are the same as for fixed-point
  471. @nicode{~f} described above. The real and imaginary parts are both
  472. output with the same given parameters and modifiers, except that for
  473. the imaginary part the @nicode{@@} modifier is always enabled, so as
  474. to print a @nicode{+} sign between the real and imaginary parts.
  475. @example
  476. (format #t "~i" 1) @print{} 1.0+0.0i
  477. @end example
  478. @item @nicode{~p}
  479. Plural. No parameters.
  480. Output nothing if the argument is 1, or @samp{s} for any other
  481. value.
  482. @example
  483. (format #t "enter name~p" 1) @print{} enter name
  484. (format #t "enter name~p" 2) @print{} enter names
  485. @end example
  486. @nicode{~@@p} prints @samp{y} for 1 or @samp{ies} otherwise.
  487. @example
  488. (format #t "pupp~@@p" 1) @print{} puppy
  489. (format #t "pupp~@@p" 2) @print{} puppies
  490. @end example
  491. @nicode{~:p} re-uses the preceding argument instead of taking a new
  492. one, which can be convenient when printing some sort of count.
  493. @example
  494. (format #t "~d cat~:p" 9) @print{} 9 cats
  495. (format #t "~d pupp~:@@p" 5) @print{} 5 puppies
  496. @end example
  497. @nicode{~p} is designed for English plurals and there's no attempt to
  498. support other languages. @nicode{~[} conditionals (below) may be able
  499. to help. When using @code{gettext} to translate messages
  500. @code{ngettext} is probably best though
  501. (@pxref{Internationalization}).
  502. @item @nicode{~y}
  503. Structured printing. Parameters: @var{width}.
  504. @nicode{~y} outputs an argument using @code{pretty-print}
  505. (@pxref{Pretty Printing}). The result will be formatted to fit within
  506. @var{width} columns (79 by default), consuming multiple lines if
  507. necessary.
  508. @nicode{~@@y} outputs an argument using @code{truncated-print}
  509. (@pxref{Pretty Printing}). The resulting code will be formatted to fit
  510. within @var{width} columns (79 by default), on a single line. The
  511. output will be truncated if necessary.
  512. @nicode{~:@@y} is like @nicode{~@@y}, except the @var{width} parameter
  513. is interpreted to be the maximum column to which to output. That is to
  514. say, if you are at column 10, and @nicode{~60:@@y} is seen, the datum
  515. will be truncated to 50 columns.
  516. @item @nicode{~?}
  517. @itemx @nicode{~k}
  518. Sub-format. No parameters.
  519. Take a format string argument and a second argument which is a list of
  520. arguments for that string, and output the result.
  521. @example
  522. (format #t "~?" "~d ~d" '(1 2)) @print{} 1 2
  523. @end example
  524. @nicode{~@@?} takes arguments for the sub-format directly rather than
  525. in a list.
  526. @example
  527. (format #t "~@@? ~s" "~d ~d" 1 2 "foo") @print{} 1 2 "foo"
  528. @end example
  529. @nicode{~?} and @nicode{~k} are the same, @nicode{~k} is provided for
  530. T-Scheme compatibility.
  531. @item @nicode{~*}
  532. Argument jumping. Parameter: @var{N}.
  533. Move forward @var{N} arguments (default 1) in the argument list.
  534. @nicode{~:*} moves backwards. (@var{N} cannot be negative.)
  535. @example
  536. (format #f "~d ~2*~d" 1 2 3 4) @result{} "1 4"
  537. (format #f "~d ~:*~d" 6) @result{} "6 6"
  538. @end example
  539. @nicode{~@@*} moves to argument number @var{N}. The first argument is
  540. number 0 (and that's the default for @var{N}).
  541. @example
  542. (format #f "~d~d again ~@@*~d~d" 1 2) @result{} "12 again 12"
  543. (format #f "~d~d~d ~1@@*~d~d" 1 2 3) @result{} "123 23"
  544. @end example
  545. A @nicode{#} move to the end followed by a @nicode{:} modifier move
  546. back can be used for an absolute position relative to the end of the
  547. argument list, a reverse of what the @nicode{@@} modifier does.
  548. @example
  549. (format #t "~#*~2:*~a" 'a 'b 'c 'd) @print{} c
  550. @end example
  551. At the end of the format string the current argument position doesn't
  552. matter, any further arguments are ignored.
  553. @item @nicode{~t}
  554. Advance to a column position. Parameters: @var{colnum}, @var{colinc},
  555. @var{padchar}.
  556. Output @var{padchar} (space by default) to move to the given
  557. @var{colnum} column. The start of the line is column 0, the default
  558. for @var{colnum} is 1.
  559. @example
  560. (format #f "~tX") @result{} " X"
  561. (format #f "~3tX") @result{} " X"
  562. @end example
  563. If the current column is already past @var{colnum}, then the move is
  564. to there plus a multiple of @var{colinc}, ie.@: column
  565. @math{@var{colnum} + @var{N} * @var{colinc}} for the smallest @var{N}
  566. which makes that value greater than or equal to the current column.
  567. The default @var{colinc} is 1 (which means no further move).
  568. @example
  569. (format #f "abcd~2,5,'.tx") @result{} "abcd...x"
  570. @end example
  571. @nicode{~@@t} takes @var{colnum} as an offset from the current column.
  572. @var{colnum} many pad characters are output, then further padding to
  573. make the current column a multiple of @var{colinc}, if it isn't
  574. already so.
  575. @example
  576. (format #f "a~3,5'*@@tx") @result{} "a****x"
  577. @end example
  578. @nicode{~t} is implemented using @code{port-column} (@pxref{Reading}),
  579. so it works even there has been other output before @code{format}.
  580. @item @nicode{~~}
  581. Tilde character. Parameter: @var{n}.
  582. Output a tilde character @nicode{~}, or @var{n} many if a parameter is
  583. given. Normally @nicode{~} introduces an escape sequence, @nicode{~~}
  584. is the way to output a literal tilde.
  585. @item @nicode{~%}
  586. Newline. Parameter: @var{n}.
  587. Output a newline character, or @var{n} many if a parameter is given.
  588. A newline (or a few newlines) can of course be output just by
  589. including them in the format string.
  590. @item @nicode{~&}
  591. Start a new line. Parameter: @var{n}.
  592. Output a newline if not already at the start of a line. With a
  593. parameter, output that many newlines, but with the first only if not
  594. already at the start of a line. So for instance 3 would be a newline
  595. if not already at the start of a line, and 2 further newlines.
  596. @item @nicode{~_}
  597. Space character. Parameter: @var{n}.
  598. @c For reference, in Common Lisp ~_ is a conditional newline, but
  599. @c slib fmtdoc.txi described it as a space, so we keep that.
  600. Output a space character, or @var{n} many if a parameter is given.
  601. With a variable parameter this is one way to insert runtime calculated
  602. padding (@nicode{~t} or the various field widths can do similar
  603. things).
  604. @example
  605. (format #f "~v_foo" 4) @result{} " foo"
  606. @end example
  607. @item @nicode{~/}
  608. Tab character. Parameter: @var{n}.
  609. Output a tab character, or @var{n} many if a parameter is given.
  610. @item @nicode{~|}
  611. Formfeed character. Parameter: @var{n}.
  612. Output a formfeed character, or @var{n} many if a parameter is given.
  613. @item @nicode{~!}
  614. Force output. No parameters.
  615. At the end of output, call @code{force-output} to flush any buffers on
  616. the destination (@pxref{Writing}). @nicode{~!} can occur anywhere in
  617. the format string, but the force is done at the end of output.
  618. When output is to a string (destination @code{#f}), @nicode{~!} does
  619. nothing.
  620. @item @nicode{~newline} (ie.@: newline character)
  621. Continuation line. No parameters.
  622. Skip this newline and any following whitespace in the format string,
  623. ie.@: don't send it to the output. This can be used to break up a
  624. long format string for readability, but not print the extra
  625. whitespace.
  626. @example
  627. (format #f "abc~
  628. ~d def~
  629. ~d" 1 2) @result{} "abc1 def2"
  630. @end example
  631. @nicode{~:newline} skips the newline but leaves any further whitespace
  632. to be printed normally.
  633. @nicode{~@@newline} prints the newline then skips following
  634. whitespace.
  635. @item @nicode{~(} @nicode{~)}
  636. Case conversion. No parameters.
  637. Between @nicode{~(} and @nicode{~)} the case of all output is changed.
  638. The modifiers on @nicode{~(} control the conversion.
  639. @itemize @w{}
  640. @item
  641. @nicode{~(} --- lower case.
  642. @c
  643. @c FIXME: The : and @ modifiers are not yet documented because the
  644. @c code applies string-capitalize and string-capitalize-first to each
  645. @c separate format:out-str call, which has various subtly doubtful
  646. @c effects. And worse they're applied to individual characters,
  647. @c including literal characters in the format string, which has the
  648. @c silly effect of being always an upcase.
  649. @c
  650. @c The Common Lisp spec is apparently for the capitalization to be
  651. @c applied in one hit to the whole of the output between ~( and ~).
  652. @c (This can no doubt be implemented without accumulating all that
  653. @c text, just by keeping a state or the previous char to tell whether
  654. @c within a word.)
  655. @c
  656. @c @item
  657. @c @nicode{:} --- first letter of each word upper case, the rest lower
  658. @c case, as per the @code{string-capitalize} function (@pxref{Alphabetic
  659. @c Case Mapping}).
  660. @c @item
  661. @c @nicode{@@} --- first letter of just the first word upper case, the
  662. @c rest lower case.
  663. @c
  664. @item
  665. @nicode{~:@@(} --- upper case.
  666. @end itemize
  667. For example,
  668. @example
  669. (format #t "~(Hello~)") @print{} hello
  670. (format #t "~:@@(Hello~)") @print{} HELLO
  671. @end example
  672. In the future it's intended the modifiers @nicode{:} and @nicode{@@}
  673. alone will capitalize the first letters of words, as per Common Lisp
  674. @code{format}, but the current implementation of this is flawed and
  675. not recommended for use.
  676. Case conversions do not nest, currently. This might change in the
  677. future, but if it does then it will be to Common Lisp style where the
  678. outermost conversion has priority, overriding inner ones (making those
  679. fairly pointless).
  680. @item @nicode{~@{} @nicode{~@}}
  681. Iteration. Parameter: @var{maxreps} (for @nicode{~@{}).
  682. The format between @nicode{~@{} and @nicode{~@}} is iterated. The
  683. modifiers to @nicode{~@{} determine how arguments are taken. The
  684. default is a list argument with each iteration successively consuming
  685. elements from it. This is a convenient way to output a whole list.
  686. @example
  687. (format #t "~@{~d~@}" '(1 2 3)) @print{} 123
  688. (format #t "~@{~s=~d ~@}" '("x" 1 "y" 2)) @print{} "x"=1 "y"=2
  689. @end example
  690. @nicode{~:@{} takes a single argument which is a list of lists, each
  691. of those contained lists gives the arguments for the iterated format.
  692. @c @print{} on a new line here to avoid overflowing page width in DVI
  693. @example
  694. (format #t "~:@{~dx~d ~@}" '((1 2) (3 4) (5 6)))
  695. @print{} 1x2 3x4 5x6
  696. @end example
  697. @nicode{~@@@{} takes arguments directly, with each iteration
  698. successively consuming arguments.
  699. @example
  700. (format #t "~@@@{~d~@}" 1 2 3) @print{} 123
  701. (format #t "~@@@{~s=~d ~@}" "x" 1 "y" 2) @print{} "x"=1 "y"=2
  702. @end example
  703. @nicode{~:@@@{} takes list arguments, one argument for each iteration,
  704. using that list for the format.
  705. @c @print{} on a new line here to avoid overflowing page width in DVI
  706. @example
  707. (format #t "~:@@@{~dx~d ~@}" '(1 2) '(3 4) '(5 6))
  708. @print{} 1x2 3x4 5x6
  709. @end example
  710. Iterating stops when there are no more arguments or when the
  711. @var{maxreps} parameter to @nicode{~@{} is reached (default no
  712. maximum).
  713. @example
  714. (format #t "~2@{~d~@}" '(1 2 3 4)) @print{} 12
  715. @end example
  716. If the format between @nicode{~@{} and @nicode{~@}} is empty, then a
  717. format string argument is taken (before iteration argument(s)) and
  718. used instead. This allows a sub-format (like @nicode{~?} above) to be
  719. iterated.
  720. @example
  721. (format #t "~@{~@}" "~d" '(1 2 3)) @print{} 123
  722. @end example
  723. @c FIXME: What is the @nicode{:} modifier to ~} meant to do? The
  724. @c Common Lisp spec says it's a minimum of 1 iteration, but the
  725. @c format.scm code seems to merely make it have MAXREPS default to 1.
  726. Iterations can be nested, an inner iteration operates in the same way
  727. as described, but of course on the arguments the outer iteration
  728. provides it. This can be used to work into nested list structures.
  729. For example in the following the inner @nicode{~@{~d~@}x} is applied
  730. to @code{(1 2)} then @code{(3 4 5)} etc.
  731. @example
  732. (format #t "~@{~@{~d~@}x~@}" '((1 2) (3 4 5))) @print{} 12x345x
  733. @end example
  734. See also @nicode{~^} below for escaping from iteration.
  735. @item @nicode{~[} @nicode{~;} @nicode{~]}
  736. Conditional. Parameter: @var{selector}.
  737. A conditional block is delimited by @nicode{~[} and @nicode{~]}, and
  738. @nicode{~;} separates clauses within the block. @nicode{~[} takes an
  739. integer argument and that number clause is used. The first clause is
  740. number 0.
  741. @example
  742. (format #f "~[peach~;banana~;mango~]" 1) @result{} "banana"
  743. @end example
  744. The @var{selector} parameter can be used for the clause number,
  745. instead of taking an argument.
  746. @example
  747. (format #f "~2[peach~;banana~;mango~]") @result{} "mango"
  748. @end example
  749. If the clause number is out of range then nothing is output. Or the
  750. last clause can be @nicode{~:;} to use that for a number out of range.
  751. @example
  752. (format #f "~[banana~;mango~]" 99) @result{} ""
  753. (format #f "~[banana~;mango~:;fruit~]" 99) @result{} "fruit"
  754. @end example
  755. @nicode{~:[} treats the argument as a flag, and expects two clauses.
  756. The first is used if the argument is @code{#f} or the second
  757. otherwise.
  758. @example
  759. (format #f "~:[false~;not false~]" #f) @result{} "false"
  760. (format #f "~:[false~;not false~]" 'abc) @result{} "not false"
  761. (let ((n 3))
  762. (format #t "~d gnu~:[s are~; is~] here" n (= 1 n)))
  763. @print{} 3 gnus are here
  764. @end example
  765. @nicode{~@@[} also treats the argument as a flag, and expects one
  766. clause. If the argument is @code{#f} then no output is produced and
  767. the argument is consumed, otherwise the clause is used and the
  768. argument is not consumed, it's left for the clause. This can be used
  769. for instance to suppress output if @code{#f} means something not
  770. available.
  771. @example
  772. (format #f "~@@[temperature=~d~]" 27) @result{} "temperature=27"
  773. (format #f "~@@[temperature=~d~]" #f) @result{} ""
  774. @end example
  775. @item @nicode{~^}
  776. Escape. Parameters: @var{val1}, @var{val2}, @var{val3}.
  777. Stop formatting if there are no more arguments. This can be used for
  778. instance to have a format string adapt to a variable number of
  779. arguments.
  780. @example
  781. (format #t "~d~^ ~d" 1) @print{} 1
  782. (format #t "~d~^ ~d" 1 2) @print{} 1 2
  783. @end example
  784. Within a @nicode{~@{} @nicode{~@}} iteration, @nicode{~^} stops the
  785. current iteration step if there are no more arguments to that step,
  786. but continuing with possible further steps and the rest of the format.
  787. This can be used for instance to avoid a separator on the last
  788. iteration, or to adapt to variable length argument lists.
  789. @example
  790. (format #f "~@{~d~^/~@} go" '(1 2 3)) @result{} "1/2/3 go"
  791. (format #f "~:@{ ~d~^~d~@} go" '((1) (2 3))) @result{} " 1 23 go"
  792. @end example
  793. @c For reference, format.scm doesn't implement that Common Lisp ~:^
  794. @c modifier which stops the entire iterating of ~:{ or ~@:{.
  795. @c FIXME: Believe the Common Lisp spec is for ~^ within ~[ ~]
  796. @c conditional to terminate the whole format (or iteration step if in
  797. @c an iteration). But format.scm seems to terminate just the
  798. @c conditional form.
  799. @c
  800. @c (format #f "~[abc~^def~;ghi~] blah" 0)
  801. @c @result{} "abc blah" ;; looks wrong
  802. @c FIXME: Believe the Common Lisp spec is for ~^ within ~( ~) to end
  803. @c that case conversion and then also terminate the whole format (or
  804. @c iteration step if in an iteration). But format.scm doesn't seem
  805. @c to do that quite right.
  806. @c
  807. @c (format #f "~d ~^ ~d" 1) @result{} "1 "
  808. @c (format #f "~(~d ~^ ~d~)" 1) @result{} ERROR
  809. Within a @nicode{~?} sub-format, @nicode{~^} operates just on that
  810. sub-format. If it terminates the sub-format then the originating
  811. format will still continue.
  812. @example
  813. (format #t "~? items" "~d~^ ~d" '(1)) @print{} 1 items
  814. (format #t "~? items" "~d~^ ~d" '(1 2)) @print{} 1 2 items
  815. @end example
  816. The parameters to @nicode{~^} (which are numbers) change the condition
  817. used to terminate. For a single parameter, termination is when that
  818. value is zero (notice this makes plain @nicode{~^} equivalent to
  819. @nicode{~#^}). For two parameters, termination is when those two are
  820. equal. For three parameters, termination is when @math{@var{val1}
  821. @le{} @var{val2}} and @math{@var{val2} @le{} @var{val3}}.
  822. @c FIXME: Good examples of these?
  823. @item @nicode{~q}
  824. Inquiry message. Insert a copyright message into the output.
  825. @nicode{~:q} inserts the format implementation version.
  826. @end table
  827. @sp 1
  828. It's an error if there are not enough arguments for the escapes in the
  829. format string, but any excess arguments are ignored.
  830. Iterations @nicode{~@{} @nicode{~@}} and conditionals @nicode{~[}
  831. @nicode{~;} @nicode{~]} can be nested, but must be properly nested,
  832. meaning the inner form must be entirely within the outer form. So
  833. it's not possible, for instance, to try to conditionalize the endpoint
  834. of an iteration.
  835. @example
  836. (format #t "~@{ ~[ ... ~] ~@}" ...) ;; good
  837. (format #t "~@{ ~[ ... ~@} ... ~]" ...) ;; bad
  838. @end example
  839. The same applies to case conversions @nicode{~(} @nicode{~)}, they
  840. must properly nest with respect to iterations and conditionals (though
  841. currently a case conversion cannot nest within another case
  842. conversion).
  843. When a sub-format (@nicode{~?}) is used, that sub-format string must
  844. be self-contained. It cannot for instance give a @nicode{~@{} to
  845. begin an iteration form and have the @nicode{~@}} up in the
  846. originating format, or similar.
  847. @end deffn
  848. @sp 1
  849. Guile contains a @code{format} procedure even when the module
  850. @code{(ice-9 format)} is not loaded. The default @code{format} is
  851. @code{simple-format} (@pxref{Writing}), it doesn't support all escape
  852. sequences documented in this section, and will signal an error if you
  853. try to use one of them. The reason for two versions is that the full
  854. @code{format} is fairly large and requires some time to load.
  855. @code{simple-format} is often adequate too.
  856. @node File Tree Walk
  857. @section File Tree Walk
  858. @cindex file tree walk
  859. The functions in this section traverse a tree of files and
  860. directories, in a fashion similar to the C @code{ftw} and @code{nftw}
  861. routines (@pxref{Working with Directory Trees,,, libc, GNU C Library
  862. Reference Manual}).
  863. @example
  864. (use-modules (ice-9 ftw))
  865. @end example
  866. @sp 1
  867. @defun ftw startname proc ['hash-size n]
  868. Walk the file system tree descending from @var{startname}, calling
  869. @var{proc} for each file and directory.
  870. Hard links and symbolic links are followed. A file or directory is
  871. reported to @var{proc} only once, and skipped if seen again in another
  872. place. One consequence of this is that @code{ftw} is safe against
  873. circularly linked directory structures.
  874. Each @var{proc} call is @code{(@var{proc} filename statinfo flag)} and
  875. it should return @code{#t} to continue, or any other value to stop.
  876. @var{filename} is the item visited, being @var{startname} plus a
  877. further path and the name of the item. @var{statinfo} is the return
  878. from @code{stat} (@pxref{File System}) on @var{filename}. @var{flag}
  879. is one of the following symbols,
  880. @table @code
  881. @item regular
  882. @var{filename} is a file, this includes special files like devices,
  883. named pipes, etc.
  884. @item directory
  885. @var{filename} is a directory.
  886. @item invalid-stat
  887. An error occurred when calling @code{stat}, so nothing is known.
  888. @var{statinfo} is @code{#f} in this case.
  889. @item directory-not-readable
  890. @var{filename} is a directory, but one which cannot be read and hence
  891. won't be recursed into.
  892. @item symlink
  893. @var{filename} is a dangling symbolic link. Symbolic links are
  894. normally followed and their target reported, the link itself is
  895. reported if the target does not exist.
  896. @end table
  897. The return value from @code{ftw} is @code{#t} if it ran to completion,
  898. or otherwise the non-@code{#t} value from @var{proc} which caused the
  899. stop.
  900. Optional argument symbol @code{hash-size} and an integer can be given
  901. to set the size of the hash table used to track items already visited.
  902. (@pxref{Hash Table Reference})
  903. @c Actually, it's probably safe to escape from ftw, just need to
  904. @c check it.
  905. @c
  906. In the current implementation, returning non-@code{#t} from @var{proc}
  907. is the only valid way to terminate @code{ftw}. @var{proc} must not
  908. use @code{throw} or similar to escape.
  909. @end defun
  910. @defun nftw startname proc ['chdir] ['depth] ['hash-size n] ['mount] ['physical]
  911. Walk the file system tree starting at @var{startname}, calling
  912. @var{proc} for each file and directory. @code{nftw} has extra
  913. features over the basic @code{ftw} described above.
  914. Like @code{ftw}, hard links and symbolic links are followed. A file
  915. or directory is reported to @var{proc} only once, and skipped if seen
  916. again in another place. One consequence of this is that @code{nftw}
  917. is safe against circular linked directory structures.
  918. Each @var{proc} call is @code{(@var{proc} filename statinfo flag
  919. base level)} and it should return @code{#t} to continue, or any
  920. other value to stop.
  921. @var{filename} is the item visited, being @var{startname} plus a
  922. further path and the name of the item. @var{statinfo} is the return
  923. from @code{stat} on @var{filename} (@pxref{File System}). @var{base}
  924. is an integer offset into @var{filename} which is where the basename
  925. for this item begins. @var{level} is an integer giving the directory
  926. nesting level, starting from 0 for the contents of @var{startname} (or
  927. that item itself if it's a file). @var{flag} is one of the following
  928. symbols,
  929. @table @code
  930. @item regular
  931. @var{filename} is a file, including special files like devices, named
  932. pipes, etc.
  933. @item directory
  934. @var{filename} is a directory.
  935. @item directory-processed
  936. @var{filename} is a directory, and its contents have all been visited.
  937. This flag is given instead of @code{directory} when the @code{depth}
  938. option below is used.
  939. @item invalid-stat
  940. An error occurred when applying @code{stat} to @var{filename}, so
  941. nothing is known about it. @var{statinfo} is @code{#f} in this case.
  942. @item directory-not-readable
  943. @var{filename} is a directory, but one which cannot be read and hence
  944. won't be recursed into.
  945. @item stale-symlink
  946. @var{filename} is a dangling symbolic link. Links are normally
  947. followed and their target reported, the link itself is reported if its
  948. target does not exist.
  949. @item symlink
  950. When the @code{physical} option described below is used, this
  951. indicates @var{filename} is a symbolic link whose target exists (and
  952. is not being followed).
  953. @end table
  954. The following optional arguments can be given to modify the way
  955. @code{nftw} works. Each is passed as a symbol (and @code{hash-size}
  956. takes a following integer value).
  957. @table @asis
  958. @item @code{chdir}
  959. Change to the directory containing the item before calling @var{proc}.
  960. When @code{nftw} returns the original current directory is restored.
  961. Under this option, generally the @var{base} parameter to each
  962. @var{proc} call should be used to pick out the base part of the
  963. @var{filename}. The @var{filename} is still a path but with a changed
  964. directory it won't be valid (unless the @var{startname} directory was
  965. absolute).
  966. @item @code{depth}
  967. Visit files ``depth first'', meaning @var{proc} is called for the
  968. contents of each directory before it's called for the directory
  969. itself. Normally a directory is reported first, then its contents.
  970. Under this option, the @var{flag} to @var{proc} for a directory is
  971. @code{directory-processed} instead of @code{directory}.
  972. @item @code{hash-size @var{n}}
  973. Set the size of the hash table used to track items already visited.
  974. (@pxref{Hash Table Reference})
  975. @item @code{mount}
  976. Don't cross a mount point, meaning only visit items on the same
  977. file system as @var{startname} (ie.@: the same @code{stat:dev}).
  978. @item @code{physical}
  979. Don't follow symbolic links, instead report them to @var{proc} as
  980. @code{symlink}. Dangling links (those whose target doesn't exist) are
  981. still reported as @code{stale-symlink}.
  982. @end table
  983. The return value from @code{nftw} is @code{#t} if it ran to
  984. completion, or otherwise the non-@code{#t} value from @var{proc} which
  985. caused the stop.
  986. @c For reference, one reason not to escape is that the current
  987. @c directory is not saved and restored with dynamic-wind. Maybe
  988. @c changing that would be enough to allow escaping.
  989. @c
  990. In the current implementation, returning non-@code{#t} from @var{proc}
  991. is the only valid way to terminate @code{ftw}. @var{proc} must not
  992. use @code{throw} or similar to escape.
  993. @end defun
  994. @node Queues
  995. @section Queues
  996. @cindex queues
  997. @tindex Queues
  998. @noindent
  999. The functions in this section are provided by
  1000. @example
  1001. (use-modules (ice-9 q))
  1002. @end example
  1003. This module implements queues holding arbitrary scheme objects and
  1004. designed for efficient first-in / first-out operations.
  1005. @code{make-q} creates a queue, and objects are entered and removed
  1006. with @code{enq!} and @code{deq!}. @code{q-push!} and @code{q-pop!}
  1007. can be used too, treating the front of the queue like a stack.
  1008. @sp 1
  1009. @deffn {Scheme Procedure} make-q
  1010. Return a new queue.
  1011. @end deffn
  1012. @deffn {Scheme Procedure} q? obj
  1013. Return @code{#t} if @var{obj} is a queue, or @code{#f} if not.
  1014. Note that queues are not a distinct class of objects but are
  1015. implemented with cons cells. For that reason certain list structures
  1016. can get @code{#t} from @code{q?}.
  1017. @end deffn
  1018. @deffn {Scheme Procedure} enq! q obj
  1019. Add @var{obj} to the rear of @var{q}, and return @var{q}.
  1020. @end deffn
  1021. @deffn {Scheme Procedure} deq! q
  1022. @deffnx {Scheme Procedure} q-pop! q
  1023. Remove and return the front element from @var{q}. If @var{q} is
  1024. empty, a @code{q-empty} exception is thrown.
  1025. @code{deq!} and @code{q-pop!} are the same operation, the two names
  1026. just let an application match @code{enq!} with @code{deq!}, or
  1027. @code{q-push!} with @code{q-pop!}.
  1028. @end deffn
  1029. @deffn {Scheme Procedure} q-push! q obj
  1030. Add @var{obj} to the front of @var{q}, and return @var{q}.
  1031. @end deffn
  1032. @deffn {Scheme Procedure} q-length q
  1033. Return the number of elements in @var{q}.
  1034. @end deffn
  1035. @deffn {Scheme Procedure} q-empty? q
  1036. Return true if @var{q} is empty.
  1037. @end deffn
  1038. @deffn {Scheme Procedure} q-empty-check q
  1039. Throw a @code{q-empty} exception if @var{q} is empty.
  1040. @end deffn
  1041. @deffn {Scheme Procedure} q-front q
  1042. Return the first element of @var{q} (without removing it). If @var{q}
  1043. is empty, a @code{q-empty} exception is thrown.
  1044. @end deffn
  1045. @deffn {Scheme Procedure} q-rear q
  1046. Return the last element of @var{q} (without removing it). If @var{q}
  1047. is empty, a @code{q-empty} exception is thrown.
  1048. @end deffn
  1049. @deffn {Scheme Procedure} q-remove! q obj
  1050. Remove all occurrences of @var{obj} from @var{q}, and return @var{q}.
  1051. @var{obj} is compared to queue elements using @code{eq?}.
  1052. @end deffn
  1053. @sp 1
  1054. @cindex @code{q-empty}
  1055. The @code{q-empty} exceptions described above are thrown just as
  1056. @code{(throw 'q-empty)}, there's no message etc like an error throw.
  1057. A queue is implemented as a cons cell, the @code{car} containing a
  1058. list of queued elements, and the @code{cdr} being the last cell in
  1059. that list (for ease of enqueuing).
  1060. @example
  1061. (@var{list} . @var{last-cell})
  1062. @end example
  1063. @noindent
  1064. If the queue is empty, @var{list} is the empty list and
  1065. @var{last-cell} is @code{#f}.
  1066. An application can directly access the queue list if desired, for
  1067. instance to search the elements or to insert at a specific point.
  1068. @deffn {Scheme Procedure} sync-q! q
  1069. Recompute the @var{last-cell} field in @var{q}.
  1070. All the operations above maintain @var{last-cell} as described, so
  1071. normally there's no need for @code{sync-q!}. But if an application
  1072. modifies the queue @var{list} then it must either maintain
  1073. @var{last-cell} similarly, or call @code{sync-q!} to recompute it.
  1074. @end deffn
  1075. @node Streams
  1076. @section Streams
  1077. @cindex streams
  1078. A stream represents a sequence of values, each of which is calculated
  1079. only when required. This allows large or even infinite sequences to
  1080. be represented and manipulated with familiar operations like ``car'',
  1081. ``cdr'', ``map'' or ``fold''. In such manipulations only as much as
  1082. needed is actually held in memory at any one time. The functions in
  1083. this section are available from
  1084. @example
  1085. (use-modules (ice-9 streams))
  1086. @end example
  1087. Streams are implemented using promises (@pxref{Delayed Evaluation}),
  1088. which is how the underlying calculation of values is made only when
  1089. needed, and the values then retained so the calculation is not
  1090. repeated.
  1091. @noindent
  1092. Here is a simple example producing a stream of all odd numbers,
  1093. @example
  1094. (define odds (make-stream (lambda (state)
  1095. (cons state (+ state 2)))
  1096. 1))
  1097. (stream-car odds) @result{} 1
  1098. (stream-car (stream-cdr odds)) @result{} 3
  1099. @end example
  1100. @noindent
  1101. @code{stream-map} could be used to derive a stream of odd squares,
  1102. @example
  1103. (define (square n) (* n n))
  1104. (define oddsquares (stream-map square odds))
  1105. @end example
  1106. These are infinite sequences, so it's not possible to convert them to
  1107. a list, but they could be printed (infinitely) with for example
  1108. @example
  1109. (stream-for-each (lambda (n sq)
  1110. (format #t "~a squared is ~a\n" n sq))
  1111. odds oddsquares)
  1112. @print{}
  1113. 1 squared is 1
  1114. 3 squared is 9
  1115. 5 squared is 25
  1116. 7 squared is 49
  1117. @dots{}
  1118. @end example
  1119. @sp 1
  1120. @defun make-stream proc initial-state
  1121. Return a new stream, formed by calling @var{proc} successively.
  1122. Each call is @code{(@var{proc} @var{state})}, it should return a pair,
  1123. the @code{car} being the value for the stream, and the @code{cdr}
  1124. being the new @var{state} for the next call. For the first call
  1125. @var{state} is the given @var{initial-state}. At the end of the
  1126. stream, @var{proc} should return some non-pair object.
  1127. @end defun
  1128. @defun stream-car stream
  1129. Return the first element from @var{stream}. @var{stream} must not be
  1130. empty.
  1131. @end defun
  1132. @defun stream-cdr stream
  1133. Return a stream which is the second and subsequent elements of
  1134. @var{stream}. @var{stream} must not be empty.
  1135. @end defun
  1136. @defun stream-null? stream
  1137. Return true if @var{stream} is empty.
  1138. @end defun
  1139. @defun list->stream list
  1140. @defunx vector->stream vector
  1141. Return a stream with the contents of @var{list} or @var{vector}.
  1142. @var{list} or @var{vector} should not be modified subsequently, since
  1143. it's unspecified whether changes there will be reflected in the stream
  1144. returned.
  1145. @end defun
  1146. @defun port->stream port readproc
  1147. Return a stream which is the values obtained by reading from
  1148. @var{port} using @var{readproc}. Each read call is
  1149. @code{(@var{readproc} @var{port})}, and it should return an EOF object
  1150. (@pxref{Reading}) at the end of input.
  1151. For example a stream of characters from a file,
  1152. @example
  1153. (port->stream (open-input-file "/foo/bar.txt") read-char)
  1154. @end example
  1155. @end defun
  1156. @defun stream->list stream
  1157. Return a list which is the entire contents of @var{stream}.
  1158. @end defun
  1159. @defun stream->reversed-list stream
  1160. Return a list which is the entire contents of @var{stream}, but in
  1161. reverse order.
  1162. @end defun
  1163. @defun stream->list&length stream
  1164. Return two values (@pxref{Multiple Values}), being firstly a list
  1165. which is the entire contents of @var{stream}, and secondly the number
  1166. of elements in that list.
  1167. @end defun
  1168. @defun stream->reversed-list&length stream
  1169. Return two values (@pxref{Multiple Values}) being firstly a list which
  1170. is the entire contents of @var{stream}, but in reverse order, and
  1171. secondly the number of elements in that list.
  1172. @end defun
  1173. @defun stream->vector stream
  1174. Return a vector which is the entire contents of @var{stream}.
  1175. @end defun
  1176. @defun stream-fold proc init stream0 @dots{} streamN
  1177. Apply @var{proc} successively over the elements of the given streams,
  1178. from first to last until the end of the shortest stream is reached.
  1179. Return the result from the last @var{proc} call.
  1180. Each call is @code{(@var{proc} elem0 @dots{} elemN prev)}, where each
  1181. @var{elem} is from the corresponding @var{stream}. @var{prev} is the
  1182. return from the previous @var{proc} call, or the given @var{init} for
  1183. the first call.
  1184. @end defun
  1185. @defun stream-for-each proc stream0 @dots{} streamN
  1186. Call @var{proc} on the elements from the given @var{stream}s. The
  1187. return value is unspecified.
  1188. Each call is @code{(@var{proc} elem0 @dots{} elemN)}, where each
  1189. @var{elem} is from the corresponding @var{stream}.
  1190. @code{stream-for-each} stops when it reaches the end of the shortest
  1191. @var{stream}.
  1192. @end defun
  1193. @defun stream-map proc stream0 @dots{} streamN
  1194. Return a new stream which is the results of applying @var{proc} to the
  1195. elements of the given @var{stream}s.
  1196. Each call is @code{(@var{proc} elem0 @dots{} elemN)}, where each
  1197. @var{elem} is from the corresponding @var{stream}. The new stream
  1198. ends when the end of the shortest given @var{stream} is reached.
  1199. @end defun
  1200. @node Buffered Input
  1201. @section Buffered Input
  1202. @cindex Buffered input
  1203. @cindex Line continuation
  1204. The following functions are provided by
  1205. @example
  1206. (use-modules (ice-9 buffered-input))
  1207. @end example
  1208. A buffered input port allows a reader function to return chunks of
  1209. characters which are to be handed out on reading the port. A notion
  1210. of further input for an application level logical expression is
  1211. maintained too, and passed through to the reader.
  1212. @defun make-buffered-input-port reader
  1213. Create an input port which returns characters obtained from the given
  1214. @var{reader} function. @var{reader} is called (@var{reader} cont),
  1215. and should return a string or an EOF object.
  1216. The new port gives precisely the characters returned by @var{reader},
  1217. nothing is added, so if any newline characters or other separators are
  1218. desired they must come from the reader function.
  1219. The @var{cont} parameter to @var{reader} is @code{#f} for initial
  1220. input, or @code{#t} when continuing an expression. This is an
  1221. application level notion, set with
  1222. @code{set-buffered-input-continuation?!} below. If the user has
  1223. entered a partial expression then it allows @var{reader} for instance
  1224. to give a different prompt to show more is required.
  1225. @end defun
  1226. @defun make-line-buffered-input-port reader
  1227. @cindex Line buffered input
  1228. Create an input port which returns characters obtained from the
  1229. specified @var{reader} function, similar to
  1230. @code{make-buffered-input-port} above, but where @var{reader} is
  1231. expected to be a line-oriented.
  1232. @var{reader} is called (@var{reader} cont), and should return a string
  1233. or an EOF object as above. Each string is a line of input without a
  1234. newline character, the port code inserts a newline after each string.
  1235. @end defun
  1236. @defun set-buffered-input-continuation?! port cont
  1237. Set the input continuation flag for a given buffered input
  1238. @var{port}.
  1239. An application uses this by calling with a @var{cont} flag of
  1240. @code{#f} when beginning to read a new logical expression. For
  1241. example with the Scheme @code{read} function (@pxref{Scheme Read}),
  1242. @example
  1243. (define my-port (make-buffered-input-port my-reader))
  1244. (set-buffered-input-continuation?! my-port #f)
  1245. (let ((obj (read my-port)))
  1246. ...
  1247. @end example
  1248. @end defun
  1249. @c Local Variables:
  1250. @c TeX-master: "guile.texi"
  1251. @c End: