User Tools

Site Tools


ch2_3_manage

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

At the moment both functions just expand first level of macro expression, so both behave as MACROEXPAND-1 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.

>(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
 
>(defmacro adder (c d) `(add ,c ,d))   ; define new macro which uses the previous
ADDER
 
>(macroexpand-1 '(adder 3 4))          ; expands only the first level
(ADD 3 4)
 
>(macroexpand '(adder 3 4))            ; this should also expand the ADD macro
(ADD 3 4)                              ; but in the present version it is not done
 
>(macroexpand '(add (add 3 4) 2))      ; always just expands the leading symbol
(+ (ADD 3 4) 2)

See also the section about macros in chapter 1.

NAME-PROCESS KILL

these are unique LabLISP multi-process functions

See also the section 1.2 about multi-process behavior in LabLISP.

ch2_3_manage.1618994084.txt.gz · Last modified: 2021/04/21 02:34 by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki