User Tools

Site Tools


ch2_1_defun

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
ch2_1_defun [2021/07/07 09:16] adminch2_1_defun [2022/03/31 04:57] (current) admin
Line 19: Line 19:
 If function body contains more expressions, all are evaluated sequentially (once the function is called), but only the output of the last one is returned from the function (this behavior is due to implicit ''PROGN''). If function body contains more expressions, all are evaluated sequentially (once the function is called), but only the output of the last one is returned from the function (this behavior is due to implicit ''PROGN'').
  
-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. ''+'' or ''LIST''), and it is planned feature for user defined functions too.+=== Optional parameters ===
  
-Functions have lexical closure. In simple terms, it means that when called, the function body expressions are evaluated in the environment, where the function was defined. +Since LabLISP version 1.3, it is possible for user to define function with variable number of parameters, using a symbols ''&OPTIONAL'' and ''&REST'' in the list of parameters.   
 + 
 +<code lisp> 
 +(defun foo (a b &optional c d)  ...  ; arguments A, B must be given, C, D are optional  
 +</code>  
 + 
 +Such definition will create function with minimum 2 and maximum 4 parameters. If the the optional parameters are not supplied, when calling ''FOO'', they will have ''NIL'' value. It is possible to define initial value, if we need the optional parameter having some other value in such case:  
 + 
 +<code lisp> 
 +>(defun adder (a b &optional (c 0))  ; arguments A, B are mandatory, C is optional 
 +   (+ a b c))                        ; C defaults to 0, so the + will work 
 +ADDER                 
 +</code> 
 + 
 +Eventually, the initializer can contain additional parameter, which will indicate, whether the user actually supplied the optional argument.  
 + 
 +<code lisp> 
 +>(defun adder (a b &optional (c 0 d))  ; arguments A, B are mandatory, C is optional 
 +   (if d (print "c supplied") (print "c not supplied")) ; D indicates if C was given 
 +   (+ a b c))                        
 +ADDER                 
 +>(adder 1 2) 
 +"c not supplied" 
 +
 +>(adder 1 2 0) 
 +"c supplied" 
 +
 +</code> 
 + 
 +Functions can as well be defined with unlimited number of parameters with the ''&REST'' symbol. 
 + 
 +<code lisp> 
 +>(defun adder (&rest a)      ; parameter A represent any number of arguments 
 +   (apply #'+ a))            ; A is a list  
 +ADDER 
 +>(adder 1 2 3 4 5) 
 +15 
 +</code>  
 + 
 +The function will receive any number of arguments as list A. 
 + 
 +The mandatory, optional and rest paramters can be combined. Order of the keywords ''&OPTIONAL'' and ''&REST'' must be kept. That means we can have any number of mandatory parameters, then ''&OPTIONAL'' keyword, after any number of optional parameters, with or without initializers, then ''&REST'', which must be followed by exactly one symbol. 
 + 
 +=== Lexical closure === 
 + 
 +Functions have lexical closure. In simple terms, it means that when called, the function body expressions are evaluated in the environment, where the function was defined, and not in the environment where called
  
 <code lisp> <code lisp>
Line 30: Line 75:
 </code> </code>
  
-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 ''LET'', that means they both can access the same A symbol.
  
 <code lisp> <code lisp>
->(add-a 3)            ; calling the new function +>(add-a 3)             ; calling the new function 
-                   ; it adds 3 to the stored A+                     ; it adds 3 to the stored A
  
->(set-new-a 5)       ; now the stored value is changed+>(set-new-a 5)         ; now the stored value is changed
 5 5
  
 >(add-a 3) >(add-a 3)
-                   ; new value A is used (5+3)+                     ; 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
 </code>  </code> 
  
ch2_1_defun.1625670992.txt.gz · Last modified: 2021/07/07 09:16 by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki