Download presentation
Presentation is loading. Please wait.
1
Lisp: Using Functions as Data
APPLY MAPCAR Anonymous functions using LAMBDA Closures Functions that create closures EVAL CSE (c) S. Tanimoto, Functions as Data
2
Explicit Application of Functions and Functional Arguments
In Lisp, functions can be passed as arguments to other functions. They can then be explicitly applied. ; Implicit application of + > ( ) 6 ; Explicit application of + > (apply (function +) (list 1 2 3)) CSE (c) S. Tanimoto, Functions as Data
3
CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data
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 (c) S. Tanimoto, Functions as Data
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 (c) S. Tanimoto, Functions as Data
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 (c) S. Tanimoto, Functions as Data
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 (c) S. Tanimoto, Functions as Data
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 (c) S. Tanimoto, Functions as Data
8
CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data
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 (c) S. Tanimoto, Functions as Data
9
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 (c) S. Tanimoto, Functions as Data
10
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 (c) S. Tanimoto, Functions as Data
11
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 (c) S. Tanimoto, Functions as Data
12
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 (c) S. Tanimoto, Functions as Data
13
CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data
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 (c) S. Tanimoto, Functions as Data
14
Calling the Evaluator Explicitly
> (setq fruit 'apple) APPLE > (setq apple 'red) RED > fruit > (eval fruit) > (eval 'fruit) CSE (c) S. Tanimoto, Functions as Data
15
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 (c) S. Tanimoto, Functions as Data
16
CSE 415 -- (c) S. Tanimoto, 2004 Functions as Data
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 (c) S. Tanimoto, Functions as Data
17
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 (c) S. Tanimoto, Functions as Data
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.