Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 341 -- S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.

Similar presentations


Presentation on theme: "CSE 341 -- S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit."— Presentation transcript:

1 CSE 341 -- S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit application of functions. APPLY, FUNCALL Treating functions as data and arguments. Repeated application with mapping functions: MAPCAR Anonymous function creation with LAMBDA. Closures: Function-oriented encapsulations. Functions that generate closures. Closures that share data. Evaluation Explicit evaluation with EVAL

2 CSE 341 -- S. Tanimoto Explicit Function Application 2 Explicit Application of Functions In Lisp, functions are “first class (Lisp) objects” and can be manipulated as data, e.g., passed as arguments to other functions. They can be explicitly (as well as implicitly) applied. ; Implicit application of + > (+ 1 2 3) 6 ; Explicit application of + > (apply (function +) (list 1 2 3)) 6

3 CSE 341 -- S. Tanimoto Explicit Function Application 3 Functional Arguments A functional object or functional argument is a representation of a function that can be passed as an argument and applied to its own arguments. ; Example > (function car) # > #’car # These used to be called funargs but are now usually called “closures”.

4 CSE 341 -- S. Tanimoto Explicit Function Application 4 Choosing a function to apply > (defun do-command (command arglist) (let (f) (if (equal command '(multiply that)) (setq f #'*) (setq f #'+) ) (apply f arglist) ) DO-COMMAND > (do-command '(add them) '(8 13)) 21

5 CSE 341 -- S. Tanimoto Explicit Function Application 5 Applying a Function to Successive Elements of a List ; Here’s a unary function with a funny name: > (1+ 20) 21 ; Here’s custom-made recursive function: > (defun increment-each (lst) (if (null lst) nil (cons (1+ (first lst)) (increment-each (rest lst)) ) ) ) INCREMENT-EACH > (increment-each '(1 2 3 4 99.9)) (2 3 4 5 100.9)

6 CSE 341 -- S. Tanimoto Explicit Function Application 6 Applying a Function to Successive Elements of a List (the MAPCAR way) ; Here’s a simpler way: > (mapcar #'1+ '(1 2 3 4 99.9)) (2 3 4 5 100.9) ; OK for functions that take multiple args: > (mapcar #'cons '(a b c) '(1 2 3)) ((A. 1) (B. 2) (C. 3))

7 CSE 341 -- S. Tanimoto Explicit Function Application 7 Anonymous Functions with LAMBDA ; Creating and using a named function: > (defun cube (x) (* x x x)) CUBE > (cube 5) 125 ; Creating and using an unnamed function: > ((lambda (x) (* x x x)) 5) 125 Benefits of unnamed functions: -- Can help achieve locality of reference, -- Might help keep the name space “unpolluted”

8 CSE 341 -- S. Tanimoto Explicit Function Application 8 Defining and Using Functions Without DEFUN ; Creating and using a function, stored as the ; Value of a symbol: > (setq double (function (lambda (x)(+ x x)))) # > (funcall double 7) 14 ; Creating a function, as DEFUN does, but without ; it. > (setf (symbol-function 'triple (function (lambda (x)(* 3 x))) ) # > (triple 7) 21 # is implementation-dependent; e.g., it could be #, etc.

9 CSE 341 -- S. Tanimoto Explicit Function Application 9 Closures A closure is a callable functional object that can use variable bindings in effect when the closure was created. > (setq toggle (let ((bit 0)) #'(lambda () (setq bit (- 1 bit))) ) ) # > (funcall toggle) 1 > (funcall toggle) 0

10 CSE 341 -- S. Tanimoto Explicit Function Application 10 A Function that Makes Closures > (defun make-toggle (on-value off-value) (let ((bit 0)) #'(lambda () (setq bit (- 1 bit)) (if (= bit 1) on-value off-value) ) ) ) MAKE-TOGGLE > (setq traffic-light (make-toggle 'green 'red) ) # > (funcall traffic-light) RED > (funcall traffic-light) GREEN

11 CSE 341 -- S. Tanimoto Explicit Function Application 11 Calling MAKE-TOGGLE (continued) > (setq tide-change (make-toggle 'high-tide 'low-tide) ) # > (funcall tide-change) LOW-TIDE > (funcall tide-change) HIGH-TIDE > (funcall traffic-light) RED > (funcall tide-change) LOW-TIDE

12 CSE 341 -- S. Tanimoto Explicit Function Application 12 Closures that Share Bindings > (defun make-stack () (let ((stack nil)) (cons #'(lambda (item) ; closure for push. (setq stack (cons item stack)) ) #'(lambda () ; closure for pop. (if stack (progn (let ((top (first stack))) (setq stack (rest stack)) top) ) nil) ) ) ) )

13 CSE 341 -- S. Tanimoto Explicit Function Application 13 Closures that Share Bindings > (setq my-stack-closures (make-stack)) (#. # ) > (funcall (car my-stack-closures) 'apple) (APPLE) > (funcall (car my-stack-closures) 'pear) (PEAR APPLE) > (funcall (cdr my-stack-closures)) PEAR

14 CSE 341 -- S. Tanimoto Explicit Function Application 14 Evaluation If FORM is a constant, return FORM itself. If FORM is a non-constant symbol, and this evaluation is taking place within the scope of a lexical binding for it, return the value of the topmost lexical binding for it, and if not in the scope of a lexical binding, return the symbol-value of FORM if any, and if none, issue an unbound variable error. IF FORM is not a list, issue an error. IF the first element of FORM, which now must be a list, is the name of a function, or is a LAMBDA expression, recursively evaluate the remaining elements of the list as arguments, and then apply the function to the evaluated arguments, and return the result. (But give an error if the wrong number of args were given). IF the first element of FORM is the name of a special form, then perform the special evaluation required by that form and return its value. IF the first element of FORM is the name of a macro, apply MACROEXPAND to the form, and then evaluate the result of that and return whatever results from this second evaluation. OTHERWISE issue an error saying that the first element of FORM was not recognized as a function.

15 CSE 341 -- S. Tanimoto Explicit Function Application 15 Calling the Evaluator Explicitly > (setq fruit 'apple) APPLE > (setq apple 'red) RED > fruit APPLE > (eval fruit) RED > (eval 'fruit) APPLE

16 CSE 341 -- S. Tanimoto Explicit Function Application 16 Calling the Evaluator Explicitly (Cont.) > (setq exp '(+ 3 4 (* 5 6) 7 8)) (+ 3 4 (* 5 6) 7 8) > (eval exp) 52 > (eval (cons '* (rest exp))) 20160 ; = (* 3 4 (* 5 6) 7 8)

17 CSE 341 -- S. Tanimoto Explicit Function Application 17 A Caveat with EVAL EVAL does not recognize lexical bindings: > (let ((x 5) (exp2 '(* 2 3 4 x))) (print exp2) (print (eval exp2)) ) Error: Attempt to take the value of the unbound variable 'X'.

18 CSE 341 -- S. Tanimoto Explicit Function Application 18 A Caveat with EVAL (Cont.) EVAL does recognize dynamic bindings: > (setq x 0) 0 > (let ((x 5) (exp2 '(* 2 3 4 x))) (print exp2) (print (eval exp2)) ) 0 The evaluation of (* 2 3 4 X) takes place within the body of EVAL (a built-in function) which is OUTSIDE the lexical scope established by the LET form. But the global value of X is accessible and is used.


Download ppt "CSE 341 -- S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit."

Similar presentations


Ads by Google