Explicit Application of Procedures, and Explicit Evaluation

Slides:



Advertisements
Similar presentations
Functions Robin Burke CSC 358/
Advertisements

Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
LISP primitives on sequences FIRST (or CAR) and REST (or CDR) take lists apart. Consider the list (First day of the semester). * (first '(First day of.
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
CSE S. Tanimoto Macros 1 Defining Macros in Scheme Extensibility: A language is extensible if the language can be extended. New Scheme control structures.
CSE (c) S. Tanimoto, 2002 AI Techniques 1 Where and When Do Symbols Refer to Values and Functions? Scope and Extent of Bindings Bindings Scope Extent.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
Forms Writing your own procedures CS 480/680 – Comparative Languages.
1 Topic #3: Lambda CSE 413, Autumn 2007 Programming Languages.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Functional Programming
CS314 – Section 5 Recitation 10
Functions CSC 358/
Section 15.4, 15.6 plus other materials
Edited by Original material by Eric Grimson
Defining Macros in Lisp
CS 326 Programming Languages, Concepts and Implementation
6.001 Jeopardy.
6.001 SICP Object Oriented Programming
Variables, Environments and Closures
The Environment Model*
CS 326 Programming Languages, Concepts and Implementation
The role of abstractions
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
COP4020 Programming Languages
Env. Model Implementation
Variables, Environments and Closures
The Metacircular Evaluator
Lecture 15: Tables and OOP
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Scheme: Basic Functionality
Lecture 16: Tables and OOP
Bindings, Scope, and Extent
Abstraction and Repetition
The Metacircular Evaluator (Continued)
CSE S. Tanimoto Explicit Function Application
Functional Programming Concepts
Lisp: Using Functions as Data
Lecture 12: Message passing The Environment Model
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
6.001 SICP Variations on a Scheme
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Functional Programming Concepts
Defining Macros in Lisp
Defining Functions with DEFUN
Abstraction and Repetition
6.001 SICP Interpretation Parts of an interpreter
Lisp: Using Functions as Data
Bindings, Scope, and Extent
Functional Programming Concepts
Bindings, Scope, and Extent
Common Lisp II.
More Scheme CS 331.
Defining Macros in Scheme
LISP primitives on sequences
Abstraction and Repetition
Presentation transcript:

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