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