utility.texi 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. @node Library utilities
  2. @section Library utilities
  3. Scheme48 provides various miscellaneous library utilities for common
  4. general-purpose tasks.
  5. @subsection Destructuring
  6. @cindex S-expression destructuring
  7. @cindex destructuring S-expressions
  8. @stindex destructuring
  9. The @code{destructuring} structure exports a form for destructuring
  10. S-expressions.
  11. @deffn syntax destructure ((pattern value) @dots{}) body
  12. For each @code{(@var{pattern} @var{value})} pair, binds every name in
  13. @var{pattern} to the corresponding location in the S-expression
  14. @var{value}. For example,
  15. @lisp
  16. (destructure (((x . y) (cons 5 3))
  17. ((#(a b) c) '(#((1 2) 3) (4 5))))
  18. @var{body})@end lisp
  19. @noindent
  20. binds @var{x} to @code{5}, @var{y} to @code{3}, @var{a} to
  21. @code{(1 2)}, @var{b} to @code{3}, and @var{c} to @code{(4 5)}, in
  22. @var{body}.
  23. @end deffn
  24. @subsection Pretty-printing
  25. @cindex pretty-printing
  26. @cindex printing
  27. @stindex pp
  28. The @code{pp} structure exports a simple pretty-printer.
  29. @deffn procedure p object [port] @returns{} unspecified
  30. @deffnx procedure pretty-print object port position @returns{} unspecified
  31. @code{P} is a convenient alias for @code{pretty-print}; it passes 0 for
  32. @var{position} and the value of @code{(current-output-port)} if
  33. @var{port} is not passed. @code{Pretty-print} pretty-prints
  34. @var{object} to @var{port}, using a left margin of @var{position}. For
  35. example:
  36. @lisp
  37. (p '(define (fact n)
  38. (let loop ((p 1) (c 1))
  39. (if (> c n) p (loop (* p c) (+ c 1))))))
  40. @print{} (define (fact n)
  41. @print{} (let loop ((p 1) (c 1))
  42. @print{} (if (> c n)
  43. @print{} p
  44. @print{} (loop (* p c) (+ c 1)))))@end lisp
  45. @end deffn
  46. The pretty-printer is somewhat extensible as well:
  47. @deffn procedure define-indentation name count @returns{} unspecified
  48. Sets the number of subforms to be indented past @var{name} in
  49. pretty-printed output to be @var{count}. For example:
  50. @lisp
  51. (define-indentation 'frobozz 3)
  52. (p '(frobozz (foo bar baz quux zot) (zot quux baz bar foo)
  53. (mumble frotz gargle eek) (froomble zargle hrumph)))
  54. @print{} (frobozz (foo bar baz quux zot)
  55. @print{} (zot quux baz bar foo)
  56. @print{} (mumble frotz gargle eek)
  57. @print{} (froomble zargle hrumph))@end lisp
  58. @end deffn
  59. @subsection Strongly connected graph components
  60. @cindex strongly-connected graph components
  61. @cindex graph algorithms
  62. @stindex strong
  63. The @code{strong} structure exports a routine for finding a list of the
  64. strongly connected components in a graph.
  65. @deffn procedure strongly-connected-components vertices to slot set-slot! @returns{} sorted-strong-vertices
  66. Returns the components of a graph containing vertices from the list
  67. @var{vertices} that are strongly connected, in a reversed topologically
  68. sorted list. @var{To} should be a procedure of one argument, a vertex,
  69. that returns a list of all vertices that have an edge to its argument.
  70. @var{Slot} & @var{set-slot!} should be procedures of one & two
  71. arguments, respectively, that access & modify arbitrary slots used by
  72. the algorithm. The slot for every vertex should initially be @code{#f}
  73. before calling @code{strongly-connected-components}, and the slots are
  74. reverted to @code{#f} before @code{strongly-connected-components}
  75. returns.
  76. @end deffn
  77. @subsection Nondeterminism
  78. @cindex nondeterminism
  79. @cindex backtracking
  80. @cindex @code{amb} operator
  81. @stindex nondeterminism
  82. The @code{nondeterminism} structure provides a simple nondeterministic
  83. ambivalence operator, like McCarthy's @code{AMB}, and a couple utilities
  84. atop it, built with Scheme's @code{call-with-current-continuation}.
  85. @deffn procedure with-nondeterminism thunk @returns{} values
  86. Initializes the nondeterminism system and calls @var{thunk}; this
  87. returns the values @var{thunk} returns after then tearing down what was
  88. set up.
  89. @end deffn
  90. @deffn syntax either option @dots{} @returns{} value
  91. @deffnx syntax one-value exp @returns{} value
  92. @deffnx syntax all-values exp @returns{} list
  93. @code{Either} evaluates to the value of any one of the options. It is
  94. equivalent to McCarthy's @code{AMB}. It may return any number of times.
  95. @code{One-value} returns the only value that @var{exp} could produce; it
  96. will return only once, although it may actually return any number of
  97. values (if @var{exp} contains a call to @code{values}).
  98. @code{All-values} returns a list of all of the single values, not
  99. multiple values, that @var{exp} could nondeterministically evaluate to.
  100. @end deffn
  101. @deffn procedure fail @returns{} does not return
  102. Signals a nondeterministic failure. This is invalid outside of a
  103. @code{with-nondeterminism}-protected dynamic extent.
  104. @end deffn
  105. @subsection Miscellaneous utilities
  106. @stindex big-util
  107. The @code{big-util} structure exports a variety of miscellaneous
  108. utilities.
  109. @deffn procedure concatenate-symbol elt @dots{} @returns{} symbol
  110. Returns a symbol containing the contents of the sequence @var{elt}
  111. @dots{}. Each @var{elt} may be another symbol, a string, or a number.
  112. Numbers are converted to strings in base ten.
  113. @end deffn
  114. @deffn procedure error format-string argument @dots{} @returns{} values (may not return)
  115. @deffnx procedure breakpoint format-string argument @dots{} @returns{} values (may not return)
  116. @code{Error} signals an error whose message is formatted by
  117. @embedref{Common-Lisp-style formatting, @code{format}} with the given
  118. formatting template string and arguments. @code{Breakpoint} signals a
  119. breakpoint with a message similarly constructed and causes the command
  120. processor to push a new @embedref{Command levels,command level}.
  121. @end deffn
  122. @deffn procedure atom? x @returns{} boolean
  123. Returns true if @var{x} is not a pair or false if it is.
  124. @end deffn
  125. @deffn procedure neq? x y @returns{} boolean
  126. @deffnx procedure n= x y @returns{} boolean
  127. Negations of the @code{eq?} and @code{=} predicates.
  128. @end deffn
  129. @deffn procedure identity value @returns{} value
  130. @deffnx procedure no-op value @returns{} value
  131. These simply return their arguments. The difference between them is
  132. that @code{no-op} is guaranteed not to be integrated by the compiler,
  133. whereas @code{identity} may be.
  134. @end deffn
  135. @deffn procedure null-list? object @returns{} boolean
  136. Returns @code{#t} if @var{object} is the null list, returns @code{#f}
  137. if @var{object} is a pair, or signals an error if @var{object} is
  138. neither the null list nor a pair.
  139. @end deffn
  140. @deffn procedure reverse! list @returns{} reversed-list
  141. Returns a list containing the reverse elements of @var{list}. Note
  142. that the original @var{list} is @emph{not} reversed; it becomes
  143. garbage. @code{Reverse!} simply re-uses its structure.
  144. @end deffn
  145. @deffn procedure memq? object list @returns{} boolean
  146. Returns @code{#t} if @var{object} is a member of @var{list}, as
  147. determined by @code{eq?}; or @code{#f} if not.
  148. @end deffn
  149. @deffn procedure first predicate list @returns{} elt or @code{#f}
  150. @deffnx procedure any predicate list @returns{} elt or @code{#f}
  151. @code{First} returns the first element of @var{list} that satisfies
  152. @var{predicate}, or @code{#f} if no element does. @code{Any} returns
  153. an element of @var{list} that satisfies @var{predicate}. Note that
  154. @code{any} may choose any element of the list, whereas @code{first}
  155. explicitly returns the @emph{first} element that satisfies
  156. @var{predicate}.
  157. @end deffn
  158. @deffn procedure any? predicate list @returns{} boolean
  159. @deffnx procedure every? predicate list @returns{} boolean
  160. @code{Any?} returns @code{#t} if any element of @var{list} satisfies
  161. @var{predicate}, or @code{#f} if none do. @code{Every?} returns
  162. @code{#t} if every element of @var{list} satisfies @var{predicate}, or
  163. @code{#f} if there exists an element that does not.
  164. @end deffn
  165. @deffn procedure filter predicate list @returns{} filtered-list
  166. @deffnx procedure filter! predicate list @returns{} filtered-list
  167. These return a list of all elements in @var{list} that satisfy
  168. @var{predicate}. @code{Filter} is not allowed to modify @var{list}'s
  169. structure; @code{filter!} may, however.
  170. @end deffn
  171. @deffn procedure filter-map proc list @returns{} list
  172. This is a combination of @code{filter} and @code{map}. For each
  173. element @var{e} in @var{list}: if @code{(@var{proc} @var{e})} returns a
  174. true value, that true value is collected in the output list.
  175. @code{Filter-map} does not modify @var{list}'s structure.
  176. @end deffn
  177. @deffn procedure remove-duplicates list @returns{} uniquified-list
  178. Returns a unique list of all elements in @var{list}; that is, if there
  179. were any duplicates of any element @var{e} in @var{list}, only a single
  180. @var{e} will occur in the returned list. @code{Remove-duplicates} does
  181. not modify @var{list}'s structure.
  182. @end deffn
  183. @deffn procedure partition-list predicate list @returns{} [satisfied unsatisfied]
  184. @deffnx procedure partition-list! predicate list @returns{} [satisfied unsatisfied]
  185. These return two values: a list of all elements in @var{list} that do
  186. satisfy @var{predicate} and a list of all elements that do not.
  187. @code{Partition-list} is not allowed to modify @var{list}'s structure;
  188. @code{partition-list!} is.
  189. @end deffn
  190. @deffn procedure delq object list @returns{} list
  191. @deffnx procedure delq! object list @returns{} list
  192. These return a list containing all elements of @var{list} except for
  193. @var{object}. @code{Delq} is not allowed to modify @var{list}'s
  194. structure; @code{delq!} is.
  195. @end deffn
  196. @deffn procedure delete predicate list @returns{} list
  197. Returns a list of all elements in @var{list} that do not satisfy
  198. @var{predicate}. Note that, despite the lack of exclamation mark in
  199. the name, this @emph{may} modify @var{list}'s structure.
  200. @end deffn
  201. @deffn procedure string->immutable-string string @returns{} immutable-string
  202. Returns an immutable string with @var{string}'s contents. If
  203. @var{string} is already immutable, it is returned; otherwise, an
  204. immutable copy is returned.
  205. @end deffn
  206. @subsection Multiple value binding
  207. @cindex binding multiple values
  208. @cindex multiple value binding
  209. @stindex receiving
  210. The @code{receiving} structure exports the @code{receive} macro, a
  211. convenient syntax atop R5RS's @code{call-with-values}.
  212. @deffn syntax receive formals producer body
  213. Binds the variables in the lambda parameter list @var{formals} to the
  214. return values of @var{producer} in @var{body}.
  215. @lisp
  216. (receive @var{formals}
  217. @var{producer}
  218. @var{body})
  219. @equiv{}
  220. (call-with-values
  221. (lambda () @var{producer})
  222. (lambda @var{formals} @var{body}))@end lisp
  223. @end deffn
  224. @stindex mvlet
  225. For sequences of multiple value bindings, the @code{mvlet} structure
  226. exports two convenient macros.
  227. @deffn syntax mvlet*
  228. @deffnx syntax mvlet
  229. @code{Mvlet*} is a multiple-value version of @code{let} or a linearly
  230. nested version of @code{receive}:
  231. @lisp
  232. (mvlet* ((@var{formals@sub{0}} @var{producer@sub{0}})
  233. (@var{formals@sub{1}} @var{producer@sub{1}})
  234. @dots{})
  235. @var{body})
  236. @equiv{}
  237. (call-with-values
  238. (lambda () @var{producer@sub{0}})
  239. (lambda @var{formals@sub{0}}
  240. (call-with-values
  241. (lambda () @var{producer@sub{1}})
  242. (lambda @var{formals@sub{1}}
  243. @dots{}@var{body}@dots{}))))@end lisp
  244. @code{Mvlet} is similar, but each @var{producer} is evaluated in an
  245. environment where none of the variables in any of the @var{formals} is
  246. bound, and the order in which each producer expression is evaluated is
  247. unspecified.
  248. @end deffn
  249. @subsection Object dumper
  250. @cindex serialization
  251. @cindex marshalling
  252. @cindex object dumping
  253. @stindex dump/restore
  254. Scheme48 has a rudimentary object dumper and retriever in the structure
  255. @code{dump/restore}. It is not a `real' object dumper in the sense that
  256. it will not handle cycles in object graphs correctly; it simply performs
  257. a recursive descent and will diverge if it reaches a cycle or stop after
  258. a recursive depth parameter.
  259. The types of objects that the dumper supports are: several miscellaneous
  260. constants (@code{()}, @code{#t}, @code{#f}, & the unspecific token),
  261. pairs, vectors, symbols, numbers, strings, characters, and byte vectors.
  262. @deffn procedure dump object char-writer depth @returns{} unspecified
  263. Dumps @var{object} by repeatedly calling @var{char-writer}, which must
  264. be a procedure that accepts exactly one character argument, on the
  265. characters of the serialized representation. If the dumper descends
  266. into the object graph whose root is @var{object} for more than
  267. @var{depth} recursions, an ellipsis token is dumped in the place of the
  268. vertex at @var{depth}.
  269. @end deffn
  270. @deffn procedure restore char-reader @returns{} object
  271. Restores the object whose serialized components are retrieved by
  272. repeatedly calling @var{char-reader}, which must be a procedure that
  273. accepts zero arguments and returns a character.
  274. @end deffn
  275. @subsection Simple time access
  276. @cindex time
  277. @cindex run time
  278. @cindex real time
  279. @stindex time
  280. The @code{time} structure exports a simple facility for accessing time
  281. offsets in two different flavours.
  282. @deffn procedure real-time @returns{} milliseconds
  283. Returns the real time in milliseconds that has passed since some
  284. unspecified moment in time.@footnote{In the current implementation on
  285. Unix, this moment happens to be the first call to @code{real-time}; on
  286. Win32, this is the start of the Scheme process.} Though not suitable
  287. for measurements relative to entities outside the Scheme48 image, the
  288. real time is useful for measuring time differences within the Scheme
  289. image with reasonable precision; for example, thread sleep timing is
  290. implemented with this real time primitive.
  291. @end deffn
  292. @deffn procedure run-time @returns{} ticks
  293. Returns the run time as an integer representing processor clock ticks
  294. since the start of the Scheme48 process. This is much less precise
  295. than the real time, but it is useful for measuring time actually spent
  296. in the Scheme48 process, as opposed to time in general.
  297. @end deffn