Local Definitions and Scope. Definitions We've seen global definitions: (define answer 42) ;; "42" is RHS (define (f x) (+ 1 x)) Today we'll look at local.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday.
Lilian Blot PART V: FUNCTIONS Core elements Autumn 2013 TPOP 1.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Chapter 5 Functions.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
Honors Compilers Semantic Analysis and Attribute Grammars Mar 5th 2002.
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
Math 015 Section 6.6 Graphing Lines. Objective: To check solutions of an equation in two variables. Question: Is (-3, 7) a solution of y = -2x + 1 ? y.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Local Definitions, Scope, Functional Abstraction, and Polymorphism.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Linearity and Local Linearity. Linear Functions.
CHAPTER ONE Problem Solving and the Object- Oriented Paradigm.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of.
Scope Accessibility of Names. Review We’ve seen that C++ permits a programmer to declare names and then use those names in a manner consistent with their.
More Data Abstraction UW CSE 190p Summer Recap of the Design Exercise You were asked to design a module – a set of related functions. Some of these.
What is the domain of the following relation? (use correct notation) { (1, 3), (4, 5.5), (6, 9), (10, 0) }
Chapter 2 Section 3. Introduction to Functions Goal:Find domain and range, determine if it’s a function, use function notation and evaluate. Definition.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
1 Construction Chapter Key Concepts Be familiar with the system construction process. Understand different types of tests and when to use Understand.
Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.
MATH II – Math I review
Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack 
Program Style Chapter 22 IB103 Week 12 (part 2). Modularity: the ability to reuse code Encapsulation: hide data access directly but may use methods (the.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack 
Loops and Simple Functions CS303E: Elements of Computers and Programming.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
Chapter 12: Programming in the Large By: Suraya Alias 1-1.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Solve Systems of Linear Equations by Substitution Honors Math – Grade 8.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Loops and Simple Functions COSC Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Python Functions Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Purpose of functions Code reuse A function can be used several.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
Lesson 06: Functions Class Participation: Class Chat:
Exam #1 You will have exactly 30 Mins to complete the exam.
Names and Attributes Names are a key programming language feature
6.001 SICP Object Oriented Programming
Dataflow analysis.
Computing Square Roots
Function There are two types of Function User Defined Function
Lesson 06: Functions Class Chat: Attendance: Participation
Bryan Burlingame 13 February 2019
Namespaces How Shall I Name Thee?.
Chapter 9: Value-Returning Functions
Material in the textbook Sections to 1.2.1
Overview of Programming Paradigms
Flow of Control.
Structured Programming
Loops and Simple Functions
What is a programming language?
Namespaces – Local and Global
IST256 : Applications Programming for Information Systems
Brent M. Dingle Texas A&M University Chapter 5 – Section 1
True or False True or False
Chapter 3 Discussion Pages
Chapter 4 Test Review First day
Presentation transcript:

Local Definitions and Scope

Definitions We've seen global definitions: (define answer 42) ;; "42" is RHS (define (f x) (+ 1 x)) Today we'll look at local definitions: (local ((define answer 42) (define (f x) (+ 1 x))) (f answer)) ;; body

Formalities What does the BNF for local definitions look like? ::= … | (local (def + ) exp) Indentation style Semantics (informally)

Why "local"? Avoiding namespace pollution Example: Our insert sort function This is an example of encapsulation How useful is this concept? Reuse name, avoid complex contracts, organize, hiding implementation detail Avoiding repeated work Consider: ;; last-occurrence: ;; x-value, [posn] -> y-value or false

Variables and Scope Recall: (local ((define answer 1 42) (define (f 2 x 3 ) (+ 1 x 4 ))) (f 5 answer 6 )) Variable occurrences: 1-6 Binding (or defining) occurrences: 1,2,3 Use occurrences: 4,5,6 Scopes: 1:(all of local statement), 2:(all of local statement), 3:(+1 x)