BSD 3 development
[unix-history] / usr / doc / lisp / ch4.n
CommitLineData
133480cf
JF
1.Lc Special\ Functions 4
2.\".ch 4
3.Lf and "[g_arg1 ...]"
4.Re
5the value of the last argument if all arguments evaluate
6to a non nil value, otherwise
7.i and
8returns nil.
9It returns t if there are no arguments.
10.No
11the arguments are evaluated left to right and evaluation will cease
12with the first nil encountered
13.Lf apply "'u_func 'l_args"
14.Re
15the result of applying function u_func to the arguments in the list l_args.
16.Lf arg "['x_numb]"
17.Re
18if x_numb is specified then the x_numb'\fIth\fP argument to
19the enclosing lexpr
20If x_numb is not specified then this returns the number of arguments
21to the enclosing lexpr.
22.No
23it is an error to the interpreter if x_numb is given and out of range.
24.Lf break "[g_val]"
25.Re
26the value of the next return typed in at top level.
27.Se
28the lisp system stops and prints out g_val if given, and then
29goes into a break loop.
30This allows one to interactively debug a program.
31You may leave the break with
32.i return
33or
34.i retbrk .
35.Lf catch "g_exp [s_tag]"
36.Re
37the result of evaluating g_exp or the value thrown during the evaluation
38of g_exp.
39.Se
40this first sets up a `catch frame' on the lisp runtime stack.
41Then it begins to evaluate g_exp.
42If g_exp evaluates normally, its value is returned.
43If, however a
44.i throw
45is done we will catch the value thrown iff
46the tag thrown to is s_tag or if there is no s_tag argument to the catch.
47.No
48Errors are implemented as a special kind of throw.
49A catch with no tag will not catch an error but a catch whose tag is
50the error type will catch that type of error.
51See \(scX.Y for more information.
52.\".pg
53.Lf cond "[l_clause1 ...]"
54.Re
55the last value evaluated in the first clause satisfied.
56If no clauses are satisfied then nil is returned.
57.No
58This is the basic conditional `statement' in lisp.
59The clauses are processed from left to right.
60The first element of a clause is evaluated.
61If it evaluated to a non nil value then that clause is satisfied and
62all following elements of that clause are evaluated.
63The last value computed is returned as the value of the cond.
64If there is just one element in the clause then its value is returned.
65If the first element of a clause evaluates to nil, then the other
66elements of that clause are not evaluated and the system moves to
67the next clause.
68.Lf declare "[g_arg ...]"
69.Re
70nil
71.No
72this is a no-op to the evaluator.
73It has special meaning to the compiler.
74.Lf def "s_name (s_type l_argl g_exp1 ...)"
75.Wh
76s_type is one of lambda, nlambda, macro or lexpr.
77.Re
78s_name
79.Se
80This defines the function s_name to the lisp system.
81If s_type is nlambda or macro then the argument list l_argl must contain
82exactly one non-nil symbol.
83.Lf defun "s_name [s_mtype] ls_argl g_exp1 ... "
84.Wh
85s_mtype is one of fexpr, expr, args or macro.
86.Re
87s_name
88.Se
89This defines the function s_name.
90.No
91this exists for Maclisp compatibility, it is just a macro which
92changes the defun form to the def form.
93An s_mtype of fexpr is converted to nlambda, of args to lexpr
94, and of expr to lambda. Macro is the same.
95If ls_arg1 is a symbol, then the type is assumed to be lexpr and
96ls_arg1 is the symbol which is bound to the number of args when the
97function is entered.
98.Lf do "l_vrbs l_test g_exp1 ..."
99.Re
100the last form in the cdr of l_test evaluated, or a value explicitly given by
101a return evaluated within the do body.
102.No
103This is the basic iteration form for
104.Fr .
105l_vrbs is a list of zero or more var-init-repeat forms.
106A var-init-repeat form looks like:
107.br
108.tl ''(s_name [g_init [g_repeat]])''
109There are three cases depending on what is present in the form.
110If just s_name is present, this means that when the do is entered,
111s_name is lambda-bound to nil and is never modified by the system
112(though the program is certainly free to modify its value).
113If the form is (s_name\ 'g_init) then the only difference is that
114s_name is lambda-bound to the value of g_init instead of nil.
115If g_repeat is also present then s_name is lambda-bound to g_init
116when the loop is entered and after each pass through the do body
117s_name is bound to the value of g_repeat.
118.br
119l_test is either nil or has the form of a cond clause.
120If it is nil then the do body will be evaluated only once and the
121do will return nil.
122Otherwise, before the do body is evaluated the car of l_test is
123evaluated and if the result is non nil this signals an end to
124the looping.
125Then the rest of the forms in l_test are evaluated
126and the value of the last one is returned as the value of the do.
127If the cdr of l_test is nil, then nil is returned -- thus this is not
128exactly like a cond clause.
129.br
130g_exp1 and those forms which follow constitute the do body.
131A do body is like a prog body and thus may have labels and one may
132use the functions go and return.
133.br
134The sequence of evaluations is this:
135.np
136the init forms are evaluated left to right and stored in temporary
137locations.
138.np
139Simultaneously all do variables are lambda bound to the value of
140their init forms or nil.
141.np
142If l_test is non nil then the car is evaluated and if it is non nil
143the rest of the forms in l_test are evaluated and the last value is
144returned as the value
145of the do.
146.np
147The forms in the do body are evaluated left to right.
148.np
149If l_test is nil the do function returns with the value nil.
150.np
151The repeat forms are evaluated and saved in temporary locations.
152.np
153The variables with repeat forms are simultaneoulsy
154bound to the values of those forms.
155.np
156Go to step 3.
157.No
158there is an alternate form of do which can be used when there is
159only one do variable.
160It is described next.
161.\".pg
162.Lf do "s_name g_init g_repeat g_test g_exp1 ..."
163.nr $p 0
164.No
165this is another, less general, form of do.
166It is evaluated by:
167.np
168evaluating g_init
169.np
170lambda binding s_name to value of g_init
171.np
172g_test is evaluated and if it is not nil the do function returns with nil.
173.np
174the do body is evaluated beginning at g_exp1.
175.np
176the repeat form is evaluted and stored in s_name.
177.np
178go to step 3.
179.\".pg
180.Lf err ???
181.Lf errset ???
182.Lf eval "'g_val"
183.Re
184the result of evaluating g_val.
185.No
186The evaluator evaluates g_val in this way:
187.br
188If g_val is a symbol, then the evaluator returns its value.
189If g_val had never been assigned a value, then this causes
190an 'Unbound Variable' error.
191If g_val is of type value, then its value is returned.
192If g_val is a list object then g_val is either a function call or
193array reference.
194Let g_car be the first element of g_val.
195We continually evaluate g_car until we end up with a symbol with
196a non nil function binding
197or a non-symbol.
198Call what we end up with: g_func.
199g_func must be one of three types: list, binary or array.
200If it is a list then the first element of the list, which
201we shall call g_functype, must be either
202lambda, nlambda, macro or lexpr.
203If g_func is a binary, then its discipline, which we shall call
204g_functype, is either lambda, nlambda or macro.
205If g_func is an array then this form is evaluated specially, see
206\(sc X.arrays.
207If g_func is a list or binary, then g_functype will determine how
208the arguments to this function, the cdr of g_val, are processed.
209If g_functype is lambda or lexpr, the arguments are evaluated
210(by calling
211.i eval
212recursively) and stacked.
213If g_functype is nlambda then the argument list is stacked.
214If g_functype is macro then the entire form, g_val is stacked.
215Next the formal valiables are lambda bound.
216The formal variables are the cadr of g_func - if g_functype is
217nlambda, lexpr or macro, there should only be one formal variable.
218The values on the stack are lambda bound to the formal variables
219except in the case of a lexpr, where the number of actual arguments
220is bound to the formal variable.
221After the binding is done, the function is invoked, either by
222jumping to the entry point in the case of a binary or
223by evaluating the list of forms beginning at cddr g_func.
224The result of this function invocation is returned as the value
225of the call to eval.
226.Lf eval-when "l_times g_exp1 ... g_expn"
227.Wh
228l_times is a list containing any combination of compile, eval and load.
229.Re
230nil
231if the symbol eval is not member of l_times,
232else returns the value of g_expn.
233.Se
234If eval is a member of l_times, then the forms g_exp\fIi\fP are
235evaluated.
236.No
237this is used mainly to control when the compiler evaluates forms.
238.\".pg
239.Lf exec
240.Lf funcall "'u_func ['g_arg1 ...]"
241.Re
242the value of applying function u_func to the arguments g_arg\fIi\fP.
243.Lf function "u_func"
244.Re
245the function binding of u_func if it is an symbol with a function binding
246otherwise u_func is returned.
247.Lf getdisc "'t_func"
248.Re
249the discipline of the machine coded function (either lambda, nlambda
250or macro).
251.Lf go "g_labexp"
252.Wh
253g_labexp is either a symbol or an expression.
254.Se
255If g_labexp is an expression, that expression is evaluated and this
256should
257result in a symbol.
258the locus of control moves to just following the label s_lable in the
259current prog body.
260.No
261this is only valid in the context of a prog body.
262The interpreter will allow non-local go's and expressions which
263evaluate to a symbol, but the compiler will not so
264be forewarned.
265.Lf map "'u_func 'l_arg1 ..."
266.Re
267l_arg1
268.No
269The function u_func is applied to successive sublists of the l_arg\fIi\fP.
270All sublists should have the same length.
271.\".pg
272.Lf mapc "'u_func 'l_arg1 ..."
273.Re
274l_arg1.
275.No
276The function u_func is applied to successive elements of the argument
277lists.
278All of the lists should have the same length.
279.Lf mapcan "'u_func 'l_arg1 ..."
280.Re
281nconc applied to the results of the functional evaluations.
282.No
283The function u_func is applied to successive elements of the
284argument lists.
285All sublists should have the same length.
286.Lf mapcar "'u_func 'l_arg1 ..."
287.Re
288a list of the values returned from the functional application.
289.No
290the function u_func is applied to successive elements of the
291argument lists.
292All sublists should have the same length.
293.Lf mapcon "'u_func 'l_arg1 ..."
294.Re
295nconc applied to the results of the functional evaluation.
296.No
297the function u_func is applied to successive sublists of the
298argument lists.
299All sublists should have the same length.
300.Lf maplist "'u_func 'l_arg1 ..."
301.Re
302a list of the results of the functional evaluations.
303.No
304the function u_func is applied to successive sublists of the arguments
305lists.
306All sublists should have the same length.
307.Lf mfunction "entry 's_disc"
308.Re
309a lisp object of type binary composed of entry and s_disc.
310.No
311entry is a pointer to the machine code for a function, and s_disc is the
312discipline (e.g. lambda).
313.\".pg
314.Lf oblist
315.Re
316a list of all symbols on the oblist.
317.Lf or "[g_arg1 ... ]"
318.Re
319the value of the first non nil argument or nil if all arguments
320evaluate to nil.
321.No
322Evaluation proceeds left to right and stops as soon as one of the arguments
323evaluates to a non nil value.
324.Lf prog "l_vrbls g_exp1 ..."
325.Re
326the value explicitly given in a return form
327or else nil if no return is done by the time the last g_exp\fIi\fP is
328evaluated.
329.No
330the local variables are lambda bound to nil then the g_exp\fI\fP
331are evaluated from left to right.
332This is a prog body (obviously) and this means than
333any symbols seen are not evaluated,
334instead they are treated as labels.
335This also means that returns and go's are allowed.
336.Lf prog2 "g_exp1 g_exp2 [g_exp3 ...]"
337.Re
338the value of g_exp2.
339.No
340the forms are evaluated from left to right and the value of g_exp2 is
341returned.
342.Lf progn "g_exp1 [g_exp2 ...]"
343.Re
344the value of the last g_exp\fIi\fP.
345.No
346the forms are evaluated from left to right and the value of the last one
347is returned.
348.Im
349what if there are no forms.
350.Lf progv "'l_locv 'l_initv g_exp1 ..."
351.Wh
352l_locv is a list of symbols and the length of l_initv is the same as
353the length of l_locv.
354.Re
355the value of the last g_exp\fIi\fP evaluated.
356.No
357first the symbols in l_locv are lambda-bound to the values in l_initv.
358Then the g_exp\fIi\fP are evaluated left to right.
359The body of a progv is like the body of a progn, it is
360.i not
361a prog body.
362.\".pg
363.Lf putd "'s_name 'u_func"
364.Re
365this sets the function binding of symbol s_name to u_func.
366.Lf return "['g_val]"
367.Re
368g_val (or nil if g_val is not present) from the enclosing prog body.
369.No
370this form is only valid in the context of a prog body.
371.Lf throw "'g_val [s_tag]"
372.Re
373g_val from the enclosing catch with the tag s_tag or no tag at all, whichever
374is closest.
375.No
376this is used in conjunction with
377.i catch
378to cause a clean jump to an enclosing context.