Explicit Application of Procedures and Explicit Evaluation Implicit

  • Slides: 18
Download presentation
Explicit Application of Procedures, and Explicit Evaluation Implicit and explicit application of procedures, APPLY.

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 1

Explicit Application of Procedures In Scheme, procedures are “first class (Scheme) objects” and can

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)) 6 CSE 341 -- S. Tanimoto Explicit Procedure Application 2

Procedures as Arguments A procedure can be passed as an argument and applied to

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 3

Choosing a function to apply > (define (do-command arglist) (let (f) (if (equal? command

Choosing a function to apply > (define (do-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 4

Applying a Procedure to Successive Elements of a List ; Here’s a unary function

Applying a Procedure to Successive Elements of a List ; Here’s a unary function for a special case ; of addition: > (add 1 20) 21 ; Here’s custom-made recursive function: > (define (increment-each lst) (if (null? lst) () (cons (add 1 (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 5

Applying a Procedure to Successive Elements of a List (the MAP way) ; Here’s

Applying a Procedure to Successive Elements of a List (the MAP way) ; Here’s a simpler way: > (map add 1 '(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 6

Anonymous Functions with LAMBDA ; Creating and using a named procedure: > (define (cube

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) 125 Benefits of unnamed procedures: -- Can help achieve locality of reference, -- Might help keep the name space “unpolluted” CSE 341 -- S. Tanimoto Explicit Procedure Application 7

Defining and Using Procedures in Different Ways ; Creating and using a function, stored

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 8

Closures A closure is a callable procedural object that can use variable bindings in

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 > (toggle) 0 CSE 341 -- S. Tanimoto Explicit Procedure Application 9

A Procedure that Makes Closures > (define (make-toggle on-value off-value) (let ((bit 0)) (lambda

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 > (traffic-light) green CSE 341 -- S. Tanimoto Explicit Procedure Application 10

Calling MAKE-TOGGLE (continued) > (define tide-change (make-toggle 'high-tide 'low-tide) ) > (tide-change) low-tide >

Calling MAKE-TOGGLE (continued) > (define tide-change (make-toggle 'high-tide 'low-tide) ) > (tide-change) low-tide > (tide-change) high-tide > (traffic-light) red > (tide-change) low-tide CSE 341 -- S. Tanimoto Explicit Procedure Application 11

Closures that Share Bindings > (define (make-stack) (let ((stack () )) (cons (lambda (item)

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 12

Closures that Share Bindings > (define my-stack-closures (make-stack)) > ((car my-stack-closures) 'apple) > ((car

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 > ((cdr my-stack-closures)) apple > ((cdr my-stack-closures)) () CSE 341 -- S. Tanimoto Explicit Procedure Application 13

Evaluation • If FORM is a constant, return FORM itself. • If FORM is

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). CSE 341 -- S. Tanimoto 14 • OTHERWISE issue an error saying that the first element of FORM Explicit Procedure Application

Calling the Evaluator Explicitly > (define fruit 'apple) > (define apple 'red) > fruit

Calling the Evaluator Explicitly > (define fruit 'apple) > (define apple 'red) > fruit apple > (eval fruit) red > (eval 'fruit) apple CSE 341 -- S. Tanimoto Explicit Procedure Application 15

Calling the Evaluator Explicitly (Cont. ) > (define exp '(+ 3 4 (* 5

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 16

A Caveat with EVAL does not recognize lexical bindings: > (let ((x 5) (exp

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

A Caveat with EVAL (Cont. ) EVAL does recognize global bindings: > (define x

A Caveat with EVAL (Cont. ) EVAL does recognize global bindings: > (define x 0) 0 > (let ((x 5) (exp 2 '(* 2 3 4 x))) (print exp 2)(newline) (print (eval exp 2)) ) (* 2 3 4 x) 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. CSE 341 -- S. Tanimoto Explicit Procedure Application 18