Download presentation
Presentation is loading. Please wait.
Published byGerardo Di Matteo Modified over 6 years ago
1
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 S. Tanimoto Explicit Procedure Application
2
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 + > ( ) 6 ; Explicit application of + > (apply + (list 1 2 3)) CSE S. Tanimoto Explicit Procedure Application
3
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 S. Tanimoto Explicit Procedure Application
4
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 S. Tanimoto Explicit Procedure Application
5
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 '( )) ( ) CSE S. Tanimoto Explicit Procedure Application
6
Applying a Procedure to Successive Elements of a List (the MAP way)
; Here’s a simpler way: > (map add1 '( )) ( ) ; OK for functions that take multiple args: > (map cons '(a b c) '(1 2 3)) ((A . 1) (B . 2) (C . 3)) CSE S. Tanimoto Explicit Procedure Application
7
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 S. Tanimoto Explicit Procedure Application
8
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 S. Tanimoto Explicit Procedure Application
9
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 S. Tanimoto Explicit Procedure Application
10
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 S. Tanimoto Explicit Procedure Application
11
Calling MAKE-TOGGLE (continued)
> (define tide-change (make-toggle 'high-tide 'low-tide) ) > (tide-change) low-tide high-tide > (traffic-light) red CSE S. Tanimoto Explicit Procedure Application
12
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 S. Tanimoto Explicit Procedure Application
13
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 S. Tanimoto Explicit Procedure Application
14
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 S. Tanimoto Explicit Procedure Application
15
Calling the Evaluator Explicitly
> (define fruit 'apple) > (define apple 'red) > fruit apple > (eval fruit) red > (eval 'fruit) CSE S. Tanimoto Explicit Procedure Application
16
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 S. Tanimoto Explicit Procedure Application
17
CSE 341 -- S. Tanimoto Explicit Procedure Application
A Caveat with EVAL EVAL does not recognize lexical bindings: > (let ((x 5) (exp2 '(* x))) (print exp2) (newline) (print (eval exp2)) ) Error: Attempt to take the value of the unbound variable 'x'. CSE S. Tanimoto Explicit Procedure Application
18
A Caveat with EVAL (Cont.)
EVAL does recognize global bindings: > (define x 0) > (let ((x 5) (exp2 '(* x))) (print exp2)(newline) (print (eval exp2)) ) (* x) 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 Procedure Application
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.