Next: Error recovery Up: Sather Lisp Previous: Predefined functions

Function definition

New functions can be created using the lambda function. lambda expects a parameter list and a sequence of expressions. Three syntactic variants exist:

a) (lambda x expr0 expr1 ... exprm) b) (lambda (par0 par1 .. parn-1) expr0 expr1 ... exprm) c) (lambda (par0 par1 .. parn-1 . x) expr0 expr1 ... exprm)

a) This corresponds to case c) with n = 0. b) The function expects n parameters which are hold in par0 to parn-1. The result of a function invocation is the value of the last expression exprm evaluted using the parameter values (i.e., the parameters are evaluated before the expri's are evaluated). Symbols that are not parameters are considered to be global. c) The function expects at least n parameters. The values of the first n parameters are hold in par0 to parn-1 (i.e., these parameters are evaluated before the expri's are evaluated), the remaining parameter list is hold in x (i.e., the last parameter is not evaluated prior to evaluation of the expri's).

Variant a) and c) can be used to implement functions that accept variable long argument lists or functions that do only partially evaluate their arguments. For instance, quote could be defined by (setq quote (lambda x (car x))). The ReadFiles function definition in the example section below is another application of this feature. Furthermore, variant a) and c) can also be (mis_)used to implement functions with one local variable x. If more than one local variable are required, an auxiliary function must be called that specifies local variables as additional arguments which are not used (but initialized) by the call.

Note: A function which is used several times should be bound to a symbol (for efficiency reasons). This is especially important when using recursion.

Implementation restriction: Currently, local lambda definitions (lambdas within lambdas) do not work correctly. Furthermore, quoting arguments within lambdas is erroneous. Both these problems are related and due to the simplified implementation. Solving these problems correctly requires a redesign of the function evaluation mechanism.

Examples:



(lambda (x) (add x 1))  defines an increment function
(setq inc (lambda (x) (add x 1)))  the value of inc is the increment function
(inc 1)  inc applied to 1 returns the value 2

(setq sum  defines a function sum which recursively
   (lambda (n)  calculates the sum of the first n integers
      (cond
         ((< 0 n) (+ n (sum (- n 1))))  
         (t 0)
      )
   )
)

(sum 100)  5050

(setq list (lambda x x))  returns its arguments as a list without evaluation
(list a b c)  (a b c)

(setq readFiles  extend the readFile function to arbitrary many
   (lambda x  arguments
      (cond
         ((atom x))
         (t (readFile (car x)) (eval (cons 'readFiles (cdr x))))
      )
   )
)



Next: Error recovery Up: Sather Lisp Previous: Predefined functions


borisv@ICSI.Berkeley.EDU
Thu Jul 20 19:01:07 PDT 1995