Explicit Application of Procedures, and Explicit Evaluation Implicit and explicit application of procedures, APPLY. Treating procedures as data and arguments. Repeated application with the mapping procedure MAP. Anonymous function creation with LAMBDA. Closures: Function-oriented encapsulations. Procedures that generate closures. Closures that share data. Evaluation. Explicit evaluation with EVAL. CSE 341 -- S. Tanimoto Explicit Procedure Application
Explicit Application of Procedures In Scheme, procedures are “first class (Scheme) objects” and can be manipulated as data, e.g., passed as arguments to other procedures. They can be explicitly (as well as implicitly) applied. ; Implicit application of + > (+ 1 2 3) 6 ; Explicit application of + > (apply + (list 1 2 3)) CSE 341 -- S. Tanimoto Explicit Procedure Application
Procedures as Arguments A procedure can be passed as an argument and applied to other arguments. ; Example > car #<primitive:CAR> > (define first car) > (let ((list-accessors (list car cdr))) > ((first list-accessors) '(a b c)) ) a CSE 341 -- S. Tanimoto Explicit Procedure Application
Choosing a function to apply > (define (do-command command arglist) (let (f) (if (equal? command '(multiply that)) (set! f *) (set! f +) ) (apply f arglist) ) DO-COMMAND > (do-command '(add them) '(8 13)) 21 > (do-command '(multiply that) '(8 13)) 104 CSE 341 -- S. Tanimoto Explicit Procedure Application
Applying a Procedure to Successive Elements of a List ; Here’s a unary function for a special case ; of addition: > (add1 20) 21 ; Here’s custom-made recursive function: > (define (increment-each lst) (if (null? lst) () (cons (add1 (car lst)) (increment-each (cdr lst)) ) ) ) INCREMENT-EACH > (increment-each '(1 2 3 4 99.9)) (2 3 4 5 100.9) CSE 341 -- S. Tanimoto Explicit Procedure Application
Applying a Procedure to Successive Elements of a List (the MAP way) ; Here’s a simpler way: > (map add1 '(1 2 3 4 99.9)) (2 3 4 5 100.9) ; OK for functions that take multiple args: > (map cons '(a b c) '(1 2 3)) ((A . 1) (B . 2) (C . 3)) CSE 341 -- S. Tanimoto Explicit Procedure Application
Anonymous Functions with LAMBDA ; Creating and using a named procedure: > (define (cube x) (* x x x)) CUBE > (cube 5) 125 ; Creating and using an unnamed procedure: > ((lambda (x) (* x x x)) 5) Benefits of unnamed procedures: -- Can help achieve locality of reference, -- Might help keep the name space “unpolluted” CSE 341 -- S. Tanimoto Explicit Procedure Application
Defining and Using Procedures in Different Ways ; Creating and using a function, stored as the ; Value of a symbol: > (let ((double (lambda (x)(+ x x)))) (double 7) ) 14 ; Creating a procedure, without using ; “defun syntax”: > (define triple (lambda (x)(* 3 x)) ) > (triple 7) 21 CSE 341 -- S. Tanimoto Explicit Procedure Application
CSE 341 -- S. Tanimoto Explicit Procedure Application Closures A closure is a callable procedural object that can use variable bindings in effect when the closure was created. > (define toggle (let ((bit 0)) (lambda () (set! bit (- 1 bit)) bit) ) ) > (toggle) 1 CSE 341 -- S. Tanimoto Explicit Procedure Application
A Procedure that Makes Closures > (define (make-toggle on-value off-value) (let ((bit 0)) (lambda () (set! bit (- 1 bit)) (if (= bit 1) on-value off-value) ) ) ) > (define traffic-light (make-toggle 'green 'red) ) > (traffic-light) red green CSE 341 -- S. Tanimoto Explicit Procedure Application
Calling MAKE-TOGGLE (continued) > (define tide-change (make-toggle 'high-tide 'low-tide) ) > (tide-change) low-tide high-tide > (traffic-light) red CSE 341 -- S. Tanimoto Explicit Procedure Application
Closures that Share Bindings > (define (make-stack) (let ((stack () )) (cons (lambda (item) ; closure for push. (set! stack (cons item stack)) ) (lambda () ; closure for pop. (if (pair? stack) (begin (let ((top (car stack))) (set! stack (cdr stack)) top) ) () ) ) ) ) ) CSE 341 -- S. Tanimoto Explicit Procedure Application
Closures that Share Bindings > (define my-stack-closures (make-stack)) > ((car my-stack-closures) 'apple) > ((car my-stack-closures) 'pear) > ((cdr my-stack-closures)) pear apple () CSE 341 -- S. Tanimoto Explicit Procedure Application
CSE 341 -- S. Tanimoto Explicit Procedure 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 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 the transformer to the form, and then evaluate the result of that and return whatever results from this. IF the value of the first element of FORM, which now must be a list, is a procedure, or is a LAMBDA expression, recursively evaluate the remaining elements of the list as arguments, and then apply the procedure to the evaluated arguments, and return the result. (But give an error if the wrong number of args were given). OTHERWISE issue an error saying that the first element of FORM was not recognized as a procedure. CSE 341 -- S. Tanimoto Explicit Procedure Application
Calling the Evaluator Explicitly > (define fruit 'apple) > (define apple 'red) > fruit apple > (eval fruit) red > (eval 'fruit) CSE 341 -- S. Tanimoto Explicit Procedure Application
Calling the Evaluator Explicitly (Cont.) > (define exp '(+ 3 4 (* 5 6) 7 8)) (+ 3 4 (* 5 6) 7 8) > (eval exp) 52 > (eval (cons '* (cdr exp))) 20160 ; = (* 3 4 (* 5 6) 7 8) CSE 341 -- S. Tanimoto Explicit Procedure Application
CSE 341 -- S. Tanimoto Explicit Procedure Application A Caveat with EVAL EVAL does not recognize lexical bindings: > (let ((x 5) (exp2 '(* 2 3 4 x))) (print exp2) (newline) (print (eval exp2)) ) Error: Attempt to take the value of the unbound variable 'x'. CSE 341 -- S. Tanimoto Explicit Procedure Application
A Caveat with EVAL (Cont.) EVAL does recognize global bindings: > (define x 0) > (let ((x 5) (exp2 '(* 2 3 4 x))) (print exp2)(newline) (print (eval exp2)) ) (* 2 3 4 x) 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. CSE 341 -- S. Tanimoto Explicit Procedure Application