ch2_3_manage
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
ch2_3_manage [2021/04/21 01:43] – admin | ch2_3_manage [2024/05/04 11:15] (current) – admin | ||
---|---|---|---|
Line 4: | Line 4: | ||
'' | '' | ||
+ | |||
+ | Both functions apply a function supplied as first argument to parameters, they only differ in the way how the parameters are supplied: | ||
<code lisp> | <code lisp> | ||
- | (funcall #'foo a b c) | + | (funcall #'foo a b c) ; parameters are in the same list |
- | (apply #' | + | (apply #'foo (list a b c)) ; parameters are in separate list |
</ | </ | ||
+ | |||
+ | Note that since the '' | ||
+ | The first argument must be function designator, which means either function-object itself, or a symbol which has global function binding: | ||
+ | |||
+ | <code lisp> | ||
+ | (funcall #'cons 'a ' | ||
+ | |||
+ | (funcall 'cons 'a ' | ||
+ | |||
+ | (funcall cons 'a ' | ||
+ | </ | ||
+ | |||
+ | But we can do this | ||
+ | |||
+ | <code lisp> | ||
+ | >(setq foo #' | ||
+ | #< | ||
+ | |||
+ | > | ||
+ | (A . B) | ||
+ | </ | ||
+ | |||
+ | Both actually accept any number of parameters. In '' | ||
+ | |||
+ | <code lisp> | ||
+ | (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 | ||
+ | </ | ||
+ | |||
+ | '' | ||
+ | |||
+ | These six similar functions apply given function to a list of arguments and produce a list of outputs. First argument must be function object, or symbol representing global function. Then can have any number of arguments, that must be proper lists. Let's have some examples: | ||
+ | |||
+ | <code lisp> | ||
+ | >(defun sqr (a) (* a a)) ; define square function | ||
+ | SQR | ||
+ | >(mapcar #'sqr '(1 2 3 4)) ; map to list of numbers | ||
+ | (1 4 9 16) ; get list of squares | ||
+ | >(mapcar #'+ '(1 2 3) '(10 20 30)) ; can map multiple lists | ||
+ | (11 22 33) | ||
+ | </ | ||
+ | |||
+ | If lists of different leghths are supplied, the output lenght will be given by the shortest input list. The number of supplied lists should match the allowed number of parameters of the applied function. | ||
+ | |||
+ | Mutually, these 6 differ in a way how the input list is treated (2 ways), and how the output list is returned (3 ways). So we have 3 pairs: | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | In each of the pairs the first one passes the individual elements of the argument list. The second one passes the actual cons cells of the argument list: | ||
+ | |||
+ | <code lisp> | ||
+ | >(mapcar 'print '(a b c)) ; MAPCAR | ||
+ | A ; prints individual elements ... | ||
+ | B ; ... which is the side effect of PRINT | ||
+ | C | ||
+ | (A B C) ; returns list of the outputs | ||
+ | > | ||
+ | (A B C) ; prints the remaining list | ||
+ | (B C) | ||
+ | (C) | ||
+ | ((A B C) (B C) (C)) | ||
+ | </ | ||
+ | |||
+ | The '' | ||
'' | '' | ||
+ | Both functions return the expansion of the macro code for the leading operator of the supplied form. '' | ||
+ | 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 '' | ||
+ | |||
+ | <code lisp> | ||
+ | > | ||
+ | ADD | ||
+ | |||
+ | > | ||
+ | (+ 1 2) ; this is the expansion | ||
+ | T ; second value | ||
+ | |||
+ | > | ||
+ | ADDER | ||
+ | |||
+ | > | ||
+ | (ADD 3 4) ; expands only the first level | ||
+ | T | ||
+ | |||
+ | > | ||
+ | (+3 4) ; expands also the deeper level macro | ||
+ | T | ||
+ | |||
+ | > | ||
+ | (+ (ADD 3 4) 2) ; only expands the leading symbol | ||
+ | T | ||
+ | |||
+ | > | ||
+ | (+ 3 4) | ||
+ | NIL ; indicated by the NIL second value | ||
+ | </ | ||
+ | |||
+ | See also [[ch1_6_macros|Macros]]. | ||
+ | |||
+ | '' | ||
+ | |||
+ | Primitive '' | ||
+ | |||
+ | <code lisp> | ||
+ | >(set 'a 12) ; is same as (SETQ a 12) | ||
+ | 12 | ||
+ | >(set (cadr '(a b)) 45) ; first argument evals to symbol B | ||
+ | 45 | ||
+ | >b ; so B was set to 45 | ||
+ | 45 | ||
+ | </ | ||
+ | |||
+ | '' | ||
+ | |||
+ | Some functions return multiple values, e.g. '' | ||
+ | |||
+ | <code lisp> | ||
+ | >(values 1 2) | ||
+ | 1 ; returned as separate values | ||
+ | 2 | ||
+ | > | ||
+ | ; no return value | ||
+ | >(values 3) | ||
+ | 3 ; trivial single value | ||
+ | </ | ||
+ | |||
+ | '' | ||
+ | |||
+ | Cosmetic function for labeling processes as shown on the LabLISP window, accepts one or two arguments. First must be string - the desired name. Second argument is optional integer specifying the process number, if not supplied, the active process is renamed. This function can be only meaningfully used on the active running process, to give information about reason for waiting. Returns the string name. | ||
+ | |||
+ | <code lisp> | ||
+ | .. | ||
+ | (move-motor 100) ; non-blocking call to start moving | ||
+ | (name-process "motor moving" | ||
+ | (wait (motor-moves? | ||
+ | .. | ||
+ | </ | ||
+ | |||
+ | '' | ||
+ | |||
+ | Function kills given process, needs one argument, the process number. Cannot be used on the active running process. Meaningful use is killing process stuck in an infinite loop. Returns '' | ||
+ | |||
+ | <code lisp> | ||
+ | >(wait t) ; infinite loop in process #0 | ||
+ | >(kill 0) ; user entry, process #1 | ||
+ | p# | ||
+ | </ | ||
+ | |||
+ | '' | ||
+ | |||
+ | This function kills the active running process, no arguments, returns '' | ||
+ | |||
+ | The last three functions are unique for LabLISP multi-process mechanics. | ||
- | '' | + | See also the section 1.2 about multi-process behavior in LabLISP. |
- | these are unique LabLISP multi-process functions | ||
ch2_3_manage.1618991006.txt.gz · Last modified: 2021/04/21 01:43 by admin