Scope: What’s in a Name? CMSC 11500 Introduction to Computer Programming October 16, 2002.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Advertisements

Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
6.001 SICP SICP – October HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W web page:
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
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.
Local Definitions, Scope, Functional Abstraction, and Polymorphism.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
1 Saves repeated renaming and substitution: explicit substitution is replaced by variable bindings using new data structures (frame, environment). Can.
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
Dr. Philip Cannata 1 Functions and Recursion. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
Analyzing Programs: Order of Growth CMSC Introduction to Computer Programming October 11, 2002.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
CS314 – Section 5 Recitation 9
Additional Scheme examples
Functional Programming
CS 550 Programming Languages Jeremy Johnson
6.001 SICP Object Oriented Programming
The interpreter.
Computing Square Roots
CS 326 Programming Languages, Concepts and Implementation
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Env. Model Implementation
Original material by Eric Grimson
6.001 SICP Data abstractions
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
The Metacircular Evaluator
Lecture 18 Infinite Streams and
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Closure Closure binds a first-class function and a lexical environment together This is a complex topic, so we will build up our understanding of it we.
CSE S. Tanimoto Explicit Function Application
Lecture 26: The Metacircular Evaluator Eval Apply
Explicit Application of Procedures, and Explicit Evaluation
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture #9 מבוא מורחב.
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
6.001 SICP Variations on a Scheme
Lecture 2 מבוא מורחב.
6.001 SICP Interpretation Parts of an interpreter
topics interpreters meta-linguistic abstraction eval and apply
Lecture 2 מבוא מורחב.
Changing Data: (Continued)
Principles of Programming Languages
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002

Roadmap Recap: Procedural Abstraction –Procedures as Parameters: scale Unnamed Procedures: Lambda Scope –Bound and free variables –Local variables: With formal parameters With let Procedures as return values

Procedural Abstraction Goal: single point of control –Avoid copying and modifying code Identify similarities in procedures –E.g. differ only in some function Solution: Abstract, and pass differing procedure(s) as parameter –Example: map Doublelist, squarelist, etc,...

Scalelist (before abstraction) Observation1: Same pattern as squarelist, map Observation2: Defining scale-fn is silly Observation3: Where does scale get its value? (define (scalelist scale alon) (define (scale-fn x) (* x scale)) (cond ((null? alon) ‘()) (else (cons (scale-fn (car alon)) (scalelist (cdr alon)))))

Scalelist (after abstraction) (define (scalelist scale alon) (define (scale-fn x) (* scale x)) (map scale-fn alon))

Unnamed Functions: Lambda Issue: Defining lots of trivial procedures –E.g. add2, double, triple… Solution: unnamed function Can’t be recursive (lambda ( {.. }) exp) E.g. “Add2” (lambda (x) (+ x 2)) Apply like other procedures –((lambda (x) (+ x 2)) 10) -> 12

Redefining with Lambda (define (doublelist alon) (map (lambda (x) (+ x x)) alon)) (define (triplelist alon) (map ???? alon)) (define (scalelist scale alon) (map ???? alon))

(define (sum alist) (cond ((null? alist) 0) (else (+ (car alist) (sum (cdr alist)))) Abstraction with Sum & Product Product? General Pattern?

Observation 3: Value of Scale Lexical scope: –Variable binding: Parameters Binding occurrence: Formal parameter of procedure BINDS to value in procedure call Bound occurrence: Appearances of variable inside procedure def’n get value –“free” variable: Global scope: top-level define (define (scalelist scale alon) (define (scale-fn x) (* scale x)) (map scale-fn alon))

Scope Region where binding gives value –Nearest enclosing block with binding occurrence Renaming: –Consistently rename variable w/in scope Meaning is unchanged (define (square x) (* x x)) (define (double x) (+ x x)) –(define (double y) (+ y y)) No change in meaning; no effect on square..

Scope Examples (define (p1 x y) (+ (* x y) (+ (* 2 x) (+ (* 2 y) 22)))) (define (p2 x) (+ (* 55 x) (+ x 11))) (define (p3 x) (+ (p1 x 0) (+ (p1 x 1) (p2 x))))

Defining Local Variables with Let Local variables: –Store and name partial result –Avoid repeated computation if result reused –Make code more intelligible (let ((x 3) (y 4)) (* x y)) 12

Let example Reverse-and-double ‘((a b) (c d)) ->((b a) (b a) (d c) (d c) ) (define (reverse-and-double alist) (cond ((null? alist) ‘()) (else (cons (reverse (car alist)) (cons (reverse (car alist)) (reverse-and-double (cdr alist))))) Problems?????

Let Example Avoid repeated work (define (reverse-and-double alist) (cond ((null? alist) ‘()) (else (let ((reverse-first (reverse (car alist)) (reverse-rest (reverse-and-double (cdr alist)))) (cons (reverse-first (cons (reverse-first reverse-rest))))

Procedures as First-class Objects Can be: –Named by variables –Can be passed as arguments to functions –Can be included in data structures –Can be returned as the results of functions

Procedures as Results of Functions Class of procedures: –E.g. scale1, scale2, scale3… ---> scale-n (define (scale-n scaler) (lambda (x) (* x scaler))) (define scale3 (scale-n 3)) (scalelist scale3 ‘( )) ----> ( )

Summary Unnamed procedures Scope: –Variable binding Parameters as binding instances Naming local variables with let Returning procedures as results

Next Time Extended Example