ch2_1_defun
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
ch2_1_defun [2021/07/07 09:16] – admin | ch2_1_defun [2022/03/31 04:57] (current) – admin | ||
---|---|---|---|
Line 19: | Line 19: | ||
If function body contains more expressions, | If function body contains more expressions, | ||
- | In LabLisp it is not (yet) possible for user to define function with variable number of parameters. But some of the built-in functions support that (e.g. '' | + | === Optional |
- | Functions have lexical closure. In simple terms, it means that when called, the function body expressions are evaluated in the environment, | + | Since LabLISP version 1.3, it is possible for user to define function with variable number of parameters, using a symbols ''& |
+ | |||
+ | <code lisp> | ||
+ | (defun foo (a b & | ||
+ | </ | ||
+ | |||
+ | Such definition will create function with minimum 2 and maximum 4 parameters. If the the optional parameters are not supplied, when calling '' | ||
+ | |||
+ | <code lisp> | ||
+ | >(defun adder (a b & | ||
+ | (+ a b c)) ; C defaults to 0, so the + will work | ||
+ | ADDER | ||
+ | </ | ||
+ | |||
+ | Eventually, the initializer can contain additional parameter, which will indicate, whether the user actually supplied the optional argument. | ||
+ | |||
+ | <code lisp> | ||
+ | >(defun adder (a b & | ||
+ | (if d (print "c supplied" | ||
+ | (+ a b c)) | ||
+ | ADDER | ||
+ | >(adder 1 2) | ||
+ | "c not supplied" | ||
+ | 3 | ||
+ | >(adder 1 2 0) | ||
+ | "c supplied" | ||
+ | 3 | ||
+ | </ | ||
+ | |||
+ | Functions can as well be defined with unlimited number of parameters with the ''& | ||
+ | |||
+ | <code lisp> | ||
+ | >(defun adder (&rest a) ; parameter A represent any number of arguments | ||
+ | | ||
+ | ADDER | ||
+ | >(adder 1 2 3 4 5) | ||
+ | 15 | ||
+ | </ | ||
+ | |||
+ | The function will receive any number of arguments as list A. | ||
+ | |||
+ | The mandatory, optional and rest paramters can be combined. Order of the keywords ''& | ||
+ | |||
+ | === Lexical closure === | ||
+ | |||
+ | Functions have lexical closure. In simple terms, it means that when called, the function body expressions are evaluated in the environment, | ||
<code lisp> | <code lisp> | ||
Line 30: | Line 75: | ||
</ | </ | ||
- | The two functions share the lexical environment LET, that means they both can access the same A symbol. | + | The two functions share the lexical environment |
<code lisp> | <code lisp> | ||
- | >(add-a 3) ; calling the new function | + | >(add-a 3) |
- | 4 ; it adds 3 to the stored A | + | 4 ; it adds 3 to the stored A |
- | > | + | > |
5 | 5 | ||
>(add-a 3) | >(add-a 3) | ||
- | 8 ; new value A is used (5+3) | + | 8 ; new value A is used (5+3) |
+ | |||
+ | >(let ((a 100)) (add-a 3)) ; local variable A=100 is not used by the function ... | ||
+ | 8 ; ... we still use the 5 | ||
</ | </ | ||
ch2_1_defun.1625670992.txt.gz · Last modified: 2021/07/07 09:16 by admin