format.texi 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. @node Common-Lisp-style formatting
  2. @section Common-Lisp-style formatting
  3. @stindex formats
  4. Scheme48 provides a simple Common-Lisp-style @code{format} facility in
  5. the @code{formats} structure. It does not provide nearly as much
  6. functionality as Common Lisp, however: the considerable complexity of
  7. Common Lisp's @code{format} was deliberately avoided because it was
  8. deemed inconsistent with Scheme48's design goals. Scheme48's
  9. @code{format} is suitable for most simple purposes, anyhow.
  10. @deffn procedure format port control-string argument @dots{} @returns{} unspecified or string
  11. Prints @var{control-string} to @var{port}. If, anywhere in
  12. @var{control-string}, the character @code{~} (tilde) occurs, the
  13. following character determines what to print in the place of the tilde
  14. and following character. Some formatting directives consume arguments
  15. from @var{argument} @dots{}. Formatting directive characters are
  16. case-insensitive. If @var{port} is @code{#t}, the output is printed to
  17. to the value of @code{(current-output-port)}; if @var{port} is false,
  18. the output is collected in a string and returned.
  19. The complete list of formatting directives:
  20. @table @code
  21. @item ~~
  22. Prints a single @code{~} (tilde), and does not consume an argument.
  23. @item ~A
  24. Consumes and prints the first remaining argument with @code{display}.
  25. (`A'ny)
  26. @item ~D
  27. Consumes and prints the first remaining argument as a decimal number
  28. using @code{number->string}. (`D'ecimal)
  29. @item ~S
  30. Consumes and prints the first remaining argument with @code{write}.
  31. (`S'-expression)
  32. @item ~%
  33. Prints a newline with @code{newline}.
  34. @item ~&
  35. Prints a newline with @code{newline}, unless it can be determined that
  36. a newline was immediately previously printed to @var{port}
  37. (@pxref{I/O extensions}).
  38. @item ~?
  39. Recursively formats. The first remaining argument is consumed and must
  40. be another control string; the argument directly thereafter is also
  41. consumed, and it must be a list of arguments corresponding with that
  42. control string. The control string is formatted with those arguments
  43. using @code{format}.
  44. @end table
  45. @code{Format} examples:
  46. @lisp
  47. (format #t "Hello, ~A!~%" "world")
  48. @print{} Hello, world!
  49. @print{}
  50. (format #t "Hello~?~S~%" "~A world" '(#\,) '!)
  51. @print{} Hello, world!
  52. @print{}
  53. (format #f "~A~A ~A." "cH" "uMBLE" "spuZz")
  54. @result{} "cHuMBLE spuZz."
  55. (let ((x 10) (y .1))
  56. (format #t "x: ~D~%~&y: ~D~%~&" x y))
  57. @print{} x: 10
  58. @print{} y: .1@end lisp
  59. @end deffn