Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE S. Tanimoto Explicit Function Application

Similar presentations


Presentation on theme: "CSE S. Tanimoto Explicit Function Application"— Presentation transcript:

1 CSE 341 -- S. Tanimoto Explicit Function Application
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 CSE 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 + > ( ) 6 ; Explicit application of + > (apply (function +) (list 1 2 3)) CSE S. Tanimoto Explicit Function Application

3 CSE 341 -- S. Tanimoto Explicit Function Application
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) #<Function CAR> > #’car These used to be called funargs but are now usually called “closures”. CSE 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 CSE 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 '( )) ( ) CSE 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+ '( )) ( ) ; OK for functions that take multiple args: > (mapcar #'cons '(a b c) '(1 2 3)) ((A . 1) (B . 2) (C . 3)) CSE 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) Benefits of unnamed functions: -- Can help achieve locality of reference, -- Might help keep the name space “unpolluted” CSE 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)))) #<Function...> > (funcall double 7) 14 ; Creating a function, as DEFUN does, but without ; it. > (setf (symbol-function 'triple (function (lambda (x)(* 3 x))) ) #<Function...> > (triple 7) 21 #<Function...> is implementation-dependent; e.g., it could be #<Lambda-closure...>, etc. CSE S. Tanimoto Explicit Function Application

9 CSE 341 -- S. Tanimoto Explicit Function Application
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))) ) ) #<Interpreted Closure #x204c8fca> > (funcall toggle) 1 CSE 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) ) #<Interpreted Closure #x204c8fcb> > (funcall traffic-light) RED GREEN CSE S. Tanimoto Explicit Function Application

11 Calling MAKE-TOGGLE (continued)
> (setq tide-change (make-toggle 'high-tide 'low-tide) ) #<Interpreted Closure #x204c8fcc> > (funcall tide-change) LOW-TIDE HIGH-TIDE > (funcall traffic-light) RED CSE 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) ) ) ) ) CSE S. Tanimoto Explicit Function Application

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

14 CSE 341 -- S. Tanimoto Explicit Function Application
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. CSE S. Tanimoto Explicit Function Application

15 Calling the Evaluator Explicitly
> (setq fruit 'apple) APPLE > (setq apple 'red) RED > fruit > (eval fruit) > (eval 'fruit) CSE 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) CSE S. Tanimoto Explicit Function Application

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

18 A Caveat with EVAL (Cont.)
EVAL does recognize dynamic bindings: > (setq x 0) > (let ((x 5) (exp2 '(* x))) (print exp2) (print (eval exp2)) ) The evaluation of (* 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. CSE S. Tanimoto Explicit Function Application


Download ppt "CSE S. Tanimoto Explicit Function Application"

Similar presentations


Ads by Google