misc-modules.texi 50 KB

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