Fall 2008Programming Development Techniques 1 Topic 16 Assignment and Local State Section 3.1.

Slides:



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

Use Case Model. C-S 5462 Use case model describes what the user expects the system to do –functional requirements may describe only the functionalities.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
1/25 Concurrency and asynchronous computing How do we deal with evaluation when we have a bunch of processors involved?
Imperative Programming. Back to scheme Scheme is a functional language In some cases there is a need to capture objects state E.g. bank account.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Environment Model 3.2, pages
Fall 2008Programming Development Techniques 1 Topic 10 Example: Symbolic Differentiation Section October 2008.
Chapter 6 Horstmann Programs that make decisions: the IF command.
SICP Symbolic data Symbol: a primitive type Generalization Symbolic differentiation.
Eight compound procedures and higher-order procedures.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
6.001 SICP SICP – September Types and HOPs Trevor Darrell 32-D512 Office Hour: W web page:
Chapter 3 Mathematics of Finance Section 3 Future Value of an Annuity; Sinking Funds.
Six compound procedures and higher-order procedures.
3 rd 9 Weeks Benchmark Review Career Preparedness.
6.001 SICP 1/ : Structure and Interpretation of Computer Programs Symbols Example of using symbols Differentiation.
Logarithmic Functions. Objectives To write exponential equations in logarithmic form. To use properties of logarithms to expand and condense logarithmic.
Day 1 Terms of the Day 1. Financial- having to do with money 2. Financial Management- how you manage your money 3. Financial Goal- something you wish.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
Chapter 3, Section 3 ELECTRONIC BANKING.
Assignment: Changing Data CMSC Introduction to Computer Programming November 1, 2002.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
1 CSC 533: Organization of Programming Languages Spring 2005 Advanced Scheme programming  memory management: structure sharing, garbage collection  structuring.
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
Fall 2008Programming Development Techniques 1 Topic 5 Data Abstraction Note: This represents a change in order. We are skipping to chapter 2 without finishing.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Bank Account Environment Model Examples Prof. Tony White April 2010.
Fall 2008Programming Development Techniques 1 Topic 20 Concurrency Section 3.4.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Identification of Classes. Object Oriented Analysis (OOA) OOA is process by which we identify classes that play role in achieving system goals & requirements.
1 Topic #3: Lambda CSE 413, Autumn 2007 Programming Languages.
Operational Semantics of Scheme
Edited by Original material by Eric Grimson
Chapter 3 Mathematics of Finance
CS 326 Programming Languages, Concepts and Implementation
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
Use Case Model.
The role of abstractions
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Organization of Programming Languages
CSC 533: Organization of Programming Languages Spring 2008
Higher-Order Procedures
The Metacircular Evaluator
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
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
Data Mutation Primitive and compound data mutators set! for names
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
6.001 SICP Variations on a Scheme
Mutators for compound data Stack Queue
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture #7 מבוא מורחב.
6.001 SICP Interpretation Parts of an interpreter
Lecture 13: Assignment and the Environment Model (EM)
Introduction to the Lab
topics interpreters meta-linguistic abstraction eval and apply
CSC 533: Organization of Programming Languages Spring 2007
*Lecture based on notes from SICP
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Fall 2008Programming Development Techniques 1 Topic 16 Assignment and Local State Section 3.1

Fall 2008Programming Development Techniques 2 Program Design So far we have learned about basic elements from which programs are made –Primitive procedures and primitive data combined to form compound entities –Abstraction (data) Here turn to organizational principles for overall design of a program Two prominent organizational strategies: Object oriented – program collection of objects whose behaviors may change over time Streams of information that flow in a system

Fall 2008Programming Development Techniques 3 The state of an object Objects have properties that change over time, e.g., snowballs, cars, people The state of an object is the collection of the values of all of its properties at a given moment in time

Fall 2008Programming Development Techniques 4 Representing objects To represent an object, we must represent its state The measurable properties of an object can be represented by local state variables To make the values of local state variables change over time, an assignment operator is needed, but this will make program behavior harder to predict

Fall 2008Programming Development Techniques 5 Bank account example (bank-statement) --> 1000 (withdraw 50) --> 950 (withdraw 50) --> 900 (withdraw 990)  “insufficient funds" (withdraw 50) --> 850 withdraw is not a mathematical function! Notice that different values are returned for the same function at different times

Fall 2008Programming Development Techniques 6 An implementation ; initialize the balance in the account (define balance 1000) ; takes an amount, if there is enough balance in the account to ; cover the amount, then return the new balance (after withdraw ; is made). Otherwise, return "insufficient funds" (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "insufficient funds"))

Fall 2008Programming Development Techniques 7 An implementation ; initialize the balance in the account (define balance 1000) ; takes an amount, if there is enough balance in the account to ; cover the amount, then return the new balance (after withdraw ; is made). Otherwise, return "insufficient funds" (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "insufficient funds"))

Fall 2008Programming Development Techniques 8 An implementation ; initialize the balance in the account (define balance 1000) ; takes an amount, if there is enough balance in the account to ; cover the amount, then return the new balance (after withdraw ; is made). Otherwise, return "insufficient funds" (define (withdraw amount) (if (>= balance amount) (begin set! Is a special form that (set! Balance takes a symbol & an expression. (- balance amount)) set! changes the value of the balance) symbol so that its value is "insufficient funds")) the value of the expression

Fall 2008Programming Development Techniques 9 An implementation ; initialize the balance in the account (define balance 1000) ; takes an amount, if there is enough balance in the account to ; cover the amount, then return the new balance (after withdraw ; is made). Otherwise, return "insufficient funds" (define (withdraw amount) (if (>= balance amount) (begin (set! Balance (- balance amount)) balance) "insufficient funds"))

Fall 2008Programming Development Techniques 10 An implementation ; initialize the balance in the account (define balance 1000) ; takes an amount, if there is enough balance in the account to ; cover the amount, then return the new balance (after withdraw ; is made). Otherwise, return "insufficient funds" (define (withdraw amount) (if (>= balance amount) (begin begin is a special form that (set! Balance simply evaluates a sequence (- balance amount)) of expressions – its value is balance) the value of the last one. "insufficient funds"))

Fall 2008Programming Development Techniques 11 Problems with implementation balance is a global variable Any procedure can change the variable But, it should only be available for change to the withdraw procedure. Can we do that? Can we use the function with more than one account?

Fall 2008Programming Development Techniques 12 Withdrawing from more than one fund ; make-withdraw takes a balance as an argument it returns ; a function that expects an amount to be withdrawn as its ; argument -- resets the balance accordingly. Think of this ; function as establishing a balance in an account – and ; returning a procedure that implements withdraws (define (make-withdraw balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "insufficient funds")))

Fall 2008Programming Development Techniques 13 A history of withdrawals (define checking-withdraw (make-withdraw 100)) (define savings-withdraw (make-withdraw 500)) > (checking-withdraw 90) 10 > (savings-withdraw 300) 200 > (checking-withdraw 5) 5 0 "insufficient funds" > (savings-withdraw 5) 195 > (savings-withdraw 5) 190

Fall 2008Programming Development Techniques 14 More realistic bank accounts ; make-account takes a balance and returns ; a procedure which is able to dispatch ; calls to withdraw or deposit both ; of which takes an amount and either withdraws ; or deposits the amount from the balance (define (make-account balance)

Fall 2008Programming Development Techniques 15 (the two features) ; takes an amount and sets balance to balance minus ; the amount if balance is high enough (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "insufficient funds")) ; takes an amount and increases balance by that amount (define (deposit amount) (set! balance (+ balance amount)) balance)

Fall 2008Programming Development Techniques 16 (the bank teller) ; dispatches either withdraw or deposit as ; appropriate (define (dispatch msg) (cond ((eq? msg 'withdraw) withdraw) ((eq? msg 'deposit) deposit) (else (error "You can't do that here:“ msg)))) dispatch)

Fall 2008Programming Development Techniques 17 Using the new function > (define checking (make-account 100)) > ((checking 'withdraw) 30) 70 > ((checking 'withdraw) 30) 40 > ((checking 'deposit) 400) 440 > ((checking 'withdraw) 30) 410 >

Fall 2008Programming Development Techniques 18 Tale of two bank accounts > (define checking (make-account 100)) > (define savings (make-account 500)) > ((checking 'withdraw) 75) 25 > ((savings 'withdraw) 75) 425 > ((checking 'deposit) 5) 30 > ((savings 'withdraw) 125) 300 > ((savings 'withdraw) 500) "insufficient funds" > ((savings 'close)). reference to undefined identifier: error (define acc1 (make- account 1000))