Download presentation
Presentation is loading. Please wait.
Published byPeregrine Eaton Modified over 8 years ago
1
CSE 341, S. Tanimoto Lisp 5 - 1 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 + > (+ 1 2 3) 6 ; Explicit application of + > (apply (function +) (list 1 2 3)) 6
2
CSE 341, S. Tanimoto Lisp 5 - 2 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”.
3
CSE 341, S. Tanimoto Lisp 5 - 3 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
4
CSE 341, S. Tanimoto Lisp 5 - 4 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)
5
CSE 341, S. Tanimoto Lisp 5 - 5 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))
6
CSE 341, S. Tanimoto Lisp 5 - 6 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”
7
CSE 341, S. Tanimoto Lisp 5 - 7 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
8
CSE 341, S. Tanimoto Lisp 5 - 8 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
9
CSE 341, S. Tanimoto Lisp 5 - 9 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
10
CSE 341, S. Tanimoto Lisp 5 - 10 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) ) ) ) )
11
CSE 341, S. Tanimoto Lisp 5 - 11 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
12
CSE 341, S. Tanimoto Lisp 5 - 12 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.
13
CSE 341, S. Tanimoto Lisp 5 - 13 Calling the Evaluator Explicitly > (setq fruit 'apple) APPLE > (setq apple 'red) RED > fruit APPLE > (eval fruit) RED > (eval 'fruit) APPLE
14
CSE 341, S. Tanimoto Lisp 5 - 14 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)
15
CSE 341, S. Tanimoto Lisp 5 - 15 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'.
16
CSE 341, S. Tanimoto Lisp 5 - 16 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.