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 [2022/03/31 03:34] 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'').
  
-Since version 1.3, it is possible for user to define function with variable number of parameters, using a symbol ''&OPTIONAL'' and ''&REST'' in the list of parameters.  +=== Optional parameters === 
 + 
 +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> <code lisp>
Line 25: Line 27:
 </code>  </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 in this case: +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> <code lisp>
Line 33: Line 35:
 </code> </code>
  
-Eventually, the initializer can contain additional parameter, which would indicate if the user actually supplied the optional argument. +Eventually, the initializer can contain additional parameter, which will indicate, whether the user actually supplied the optional argument. 
  
 <code lisp> <code lisp>
Line 48: Line 50:
 </code> </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.  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. 
ch2_1_defun.1648719297.txt.gz · Last modified: 2022/03/31 03:34 by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki