This is an old revision of the document!
Higher order management functions
Here we will list functions that serve for higher order of the language management, in other words functions that expand LISP by acting on the language itself, in some sense.
FUNCALL
and APPLY
Both functions apply a function supplied as first argument to parameters, they only differ in the way how the parameters are supplied:
(funcall #'foo a b c) ; parameters are in the same list (apply #'foo (list a b c)) ; parameters are in separate list
Note that since the FUNCALL
and APPLY
are functions, not special operators, the arguments are first evaluated in the FUNCALL/APPLY
function-application form. Then they are supplied to the function object foo
.
The first argument must be function designator, which means either function-object itself, or a symbol which has global function binding:
(funcall #'cons 'a 'b) ; is ok - function object (funcall 'cons 'a 'b) ; is ok - symbol CONS names global function (funcall cons 'a 'b) ; is not ok - CONS evaluated to value, not function
But we can do this
>(setq foo #'cons) ; function object in the value cell of FOO #<function CONS> >(funcall foo 'a 'b) ; symbol FOO then evals to function object (A . B)
Both actually accept any number of parameters. In APPLY
the last argument must be list. The called function will check if it has the right number of paramteres. Here is example with +
, which also accepts any number of parameters:
(funcall #'+ 1 2 3 4) ; these three calls do the same (apply #'+ '(1 2 3 4)) ; single list with four elements (apply #'+ 1 2 '(3 4)) ; the loose atoms are consed to the ending list
MACROEXPAND
and MACROEXPAND-1
Both functions return the expansion of the macro code for the leading operator of the supplied form. MACROEXPAND-1
expands first level of macro expression, MACROEXPAND
expands all levels.
They expect one argument, which can be anything, but it only makes sense to use then on quoted expression which has a macro call in the functor position. Both functions return second value, which is T
when the input was actually macro form and got expanded, otherwise NIL
for non-macro expressions.
>(defmacro add (a b) `(+ ,a ,b)) ; defined simple macro ADD >(macroexpand '(add 1 2)) ; quoted expression with the macro (+ 1 2) ; this is the expansion T ; second value >(defmacro adder (c d) `(add ,c ,d)) ; define new macro which uses the previous ADDER >(macroexpand-1 '(adder 3 4)) (ADD 3 4) ; expands only the first level T >(macroexpand '(adder 3 4)) (+3 4) ; expands also the deeper level macro T >(macroexpand '(add (add 3 4) 2)) (+ (ADD 3 4) 2) ; only expands the leading symbol T >(macroexpand '(+ 3 4)) ; not a macro expression (+ 3 4) NIL ; indicated by the NIL second value
See also the section about macros in chapter 1.
SET
Primitive SET as function, evals both arguments, but first must be symbol. Only accepts 2 arguments. Normally use SETQ, but SET might be used in the rare case where we need variability in the symbol argument.
NAME-PROCESS
KILL
these are unique LabLISP multi-process functions
See also the section 1.2 about multi-process behavior in LabLISP.