scheme-concepts.texi 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. @node Guile Scheme concepts
  2. @chapter Guile Scheme concepts
  3. Most Scheme implementations go beyond what is specified in the R4RS
  4. document @footnote{Remember? R4RS is the Revised^4 report on the
  5. Algorithmic Language Scheme}, mostly because R4RS does not give
  6. specifications (or even recommendations) regarding some issues that are
  7. quite important in practical programming.
  8. Here is a list of how Guile implements some of these much-needed Scheme
  9. extensions; other Scheme implementations do so quite similarly.
  10. @menu
  11. * Scheme slang::
  12. * Read-eval-print loops::
  13. * Extra data types::
  14. * Miscellaneous features::
  15. @end menu
  16. @node Scheme slang
  17. @section Scheme slang
  18. @cindex slang
  19. Even if you read some of the nice books on Scheme, or the R4RS report,
  20. you might not find some of the terms frequently used by Scheme hackers,
  21. both in the manual and in the @url{news:comp.lang.scheme} newsgroup.
  22. Here is a glossary of some of the terms that make Scheme beginners and
  23. intermediate users say ``huh?''
  24. @table @strong
  25. @item thunk
  26. @cindex thunk
  27. A Scheme procedure that takes no arguments. In this example,
  28. @code{thunk} and @code{another-thunk} are both thunks:
  29. @lisp
  30. (define (thunk)
  31. (display "Dude, I'm a thunk!")
  32. (newline))
  33. (define another-thunk
  34. (lambda ()
  35. (display "Me too!\n")
  36. (newline)))
  37. @end lisp
  38. @item closure
  39. @cindex closure
  40. A closure is a procedure. However, the term emphasizes the fact that a
  41. Scheme procedure remembers (or @dfn{closes over}) the variables that
  42. were visible when the @code{lambda} expression was
  43. evaluated.
  44. In the example below, we might refer to @code{q} as a closure, because
  45. it has closed over the value of @code{x}:
  46. @lisp
  47. (define p
  48. (lambda (x)
  49. (lambda (y)
  50. (+ x y))))
  51. (define q (p 5.7))
  52. (q 10)
  53. @result{} 15.7
  54. @end lisp
  55. However, strictly speaking, every Scheme procedure is really a closure,
  56. since it closes over the top-level environment.
  57. @item alist
  58. @itemx association list
  59. @item plist
  60. @itemx property list
  61. @end table
  62. @node Read-eval-print loops
  63. @section Read-eval-print loops
  64. @cindex Read-eval-print loop
  65. @cindex REPL
  66. To explicitly mention the Scheme read-eval-print loop (REPL) seems weird
  67. because we are all accustomed to firing up an interpreter and having it
  68. read and execute commands.
  69. But the REPL is not specified in R4RS; rather, it is proposed by the
  70. Scheme Bible @cite{Structure and Interpretation of Computer Programs}
  71. (also known as @emph{SICP}), and implemented in some form in all Scheme
  72. interpreters.
  73. @cindex Structure and Interpretation of Computer Programs
  74. @cindex SICP
  75. [FIXME: Someone needs to tell me what needs to be said about Guile's
  76. REPL.]
  77. @node Extra data types
  78. @section Extra data types
  79. The fundamental Scheme data types specified in R4RS are @emph{numbers}
  80. (both exact and inexact), @emph{characters}, @emph{strings},
  81. @emph{symbols}, @emph{vectors}, @emph{pairs} and @emph{lists} [FIXME: is
  82. this complete?].
  83. Many Scheme interpreters offer more types, and Guile is no exception.
  84. Guile is based on Aubrey Jaffer's SCM interpreter, and thus inherits
  85. @emph{uniform arrays}, [FIXME: any others? How about records?].
  86. On top of that, Guile allows you to add extra types, but that is covered
  87. in @ref{Adding types to Guile}. Here I will simply document all the
  88. extra Scheme types shipped with Guile.
  89. @menu
  90. * Conventional arrays::
  91. * Uniform arrays::
  92. * Bit vectors::
  93. * Complex numbers::
  94. @end menu
  95. @node Conventional arrays
  96. @subsection Conventional arrays
  97. @node Uniform arrays
  98. @subsection Uniform arrays
  99. @cindex arrays - uniform
  100. The motivation for uniform arrays in Scheme is performance. A vector
  101. provides a performance increase over lists when you want a fixed-size
  102. indexable list. But the elements in a vector can be of different types,
  103. and this makes for larger storage requirements and slightly lower
  104. performance.
  105. A uniform array is similar to a vector, but all elements have to be of
  106. the same type.
  107. arrays, uniform arrays, bit vectors:
  108. @deffn procedure array-fill ra fill
  109. @end deffn
  110. @deffn procedure serial-array-copy! src dst
  111. @end deffn
  112. @deffn procedure serial-array-map ra0 proc [lra]
  113. @end deffn
  114. @deffn procedure array-map ra0 proc [lra]
  115. @end deffn
  116. @deffn procedure array-for-each proc ra0 [lra]
  117. @end deffn
  118. @deffn procedure array-index-map! ra proc
  119. @end deffn
  120. @deffn procedure array-copy! src dst
  121. @end deffn
  122. @deffn procedure array-copy! src dst
  123. @end deffn
  124. @deffn procedure array-copy! src dst
  125. @end deffn
  126. @deffn procedure array-copy! src dst
  127. @end deffn
  128. @deffn procedure array-copy! src dst
  129. @end deffn
  130. @deffn procedure array? ra [prot]
  131. @end deffn
  132. @deffn procedure array-rank ra
  133. @end deffn
  134. @deffn procedure array-dimensions ra
  135. @end deffn
  136. @deffn procedure dimensions->uniform-array dims prot fill ...
  137. @end deffn
  138. @deffn procedure make-shared-array ra mapfunc dims ...
  139. @end deffn
  140. @deffn procedure transpose-array arg ...
  141. @end deffn
  142. @deffn procedure enclose-array axes ...
  143. @end deffn
  144. @deffn procedure array-in-bounds? arg ...
  145. @end deffn
  146. @deffn procedure array-ref ra arg ..
  147. @end deffn
  148. @deffn procedure uniform-vector-ref vec pos
  149. @end deffn
  150. @deffn procedure array-set! ra obj arg ...
  151. @end deffn
  152. @deffn procedure uniform-array-set1! ua obj arg
  153. @end deffn
  154. @deffn procedure array-contents ra [strict]
  155. @end deffn
  156. @deffn procedure uniform-array-read! ra [port-or-fd] [start] [end]
  157. @end deffn
  158. @deffn procedure uniform-array-write! ra [port-or-fd] [start] [end]
  159. @end deffn
  160. @deffn procedure bit-count item seq
  161. @end deffn
  162. @deffn procedure bit-position item v k
  163. @end deffn
  164. @deffn procedure bit-set! v kv obj
  165. @end deffn
  166. @deffn procedure bit-count* v kv obj
  167. @end deffn
  168. @deffn procedure bit-invert v
  169. @end deffn
  170. @deffn procedure array->list ra
  171. @end deffn
  172. @deffn procedure list->uniform-array ndim prot list
  173. @end deffn
  174. @deffn procedure array-prototype ra
  175. @end deffn
  176. Unform arrays can be written and read, but @code{read} won't recognize
  177. them unless the optional @code{read-sharp} parameter is supplied,
  178. e.g,
  179. @smalllisp
  180. (read port #t read-sharp)
  181. @end smalllisp
  182. where @code{read-sharp} is the default procedure for parsing extended
  183. sharp notations.
  184. Reading an array is not very efficient at present, since it's implemented
  185. by reading a list and converting the list to an array.
  186. @c FIXME: must use @deftp, but its generation of TeX code is buggy.
  187. @c Must fix it when TeXinfo gets fixed.
  188. @deftp {Scheme type} {uniform array}
  189. @end deftp
  190. @node Bit vectors
  191. @subsection Bit vectors
  192. @node Complex numbers
  193. @subsection Complex numbers
  194. @c FIXME: must use @deftp, but its generation of TeX code is buggy.
  195. @c Must fix it when TeXinfo gets fixed.
  196. @deftp {Scheme type} complex
  197. Standard complex numbers.
  198. @end deftp
  199. @node Miscellaneous features
  200. @section Miscellaneous features
  201. @defun defined? symbol
  202. Returns @code{#t} if a symbol is bound to a value, @code{#f} otherwise.
  203. This kind of procedure is not specified in R4RS because @c FIXME: finish
  204. this thought
  205. @end defun
  206. @defun object-properties OBJ
  207. and so forth
  208. @end defun