Assignment: Changing Data CMSC 11500 Introduction to Computer Programming November 1, 2002.

Slides:



Advertisements
Similar presentations
ALGORITHMS - PART 2 CONDITIONAL BRANCH CONTROL STRUCTURE
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
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
CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
ACSE th Conference The Iconic Programmer Stephen Chen.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Binary Search Trees Section Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.
Advanced Topics in Algorithms and Data Structures 1 An example.
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
Data Structures: Binary Trees CMSC Introduction to Computer Programming October 28, 2002.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
Mutual Recursion: Web pages CMSC Introduction to Computer Programming November 25, 2002.
Objective : Solving systems of linear equations by graphing System of linear equation two or more linear equations How do I solve linear systems of equations?
1 CSC 533: Organization of Programming Languages Spring 2005 Advanced Scheme programming  memory management: structure sharing, garbage collection  structuring.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
Data Abstraction: Sets Binary Search Trees CMSC Introduction to Computer Programming October 30, 2002.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Binary Search Trees (BST)
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
Fall 2008Programming Development Techniques 1 Topic 16 Assignment and Local State Section 3.1.
Bank Account Environment Model Examples Prof. Tony White April 2010.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
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.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
1 10/15/04CS150 Introduction to Computer Science 1 Reading from and Writing to Files Part 2.
CSED101 INTRODUCTION TO COMPUTING FUNCTION ( 함수 ) 유환조 Hwanjo Yu.
Analyzing Programs: Order of Growth CMSC Introduction to Computer Programming October 11, 2002.
CS61A Lecture Colleen Lewis 1. Clicker poll Are you in a two person group for project 4? A)Yes – I have my partner B)No – I plan to work.
Chapter 3 Implementing Classes
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
Fundamental Programming Fundamental Programming Data Processing and Expressions.
Meta-Circular Evaluation CMSC Introduction to Computer Programming November 20, 2002.
Data Structures Red-Black Trees Design and Analysis of Algorithms I.
Operational Semantics of Scheme
Lecture #5 מבוא מורחב.
Data Abstraction: Sets
6.001 SICP Compilation Context: special purpose vs. universal machines
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
CSC 533: Programming Languages Spring 2015
CSC 533: Organization of Programming Languages Spring 2008
Env. Model Implementation
CS21b: Structure and Interpretation
Binary Search Trees.
Nondeterministic Evaluation
Higher-Order Procedures
Lecture #5 מבוא מורחב.
The Metacircular Evaluator
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
3.7 Variable Assignment Recall instance variables in Python:
topics three representations entity relationship diagram object model
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
EECS 111 Review 11/13/2016.
6.001 SICP Variations on a Scheme
6.001 SICP Interpretation Parts of an interpreter
Introduction to Object-Oriented Programming
CSC 533: Organization of Programming Languages Spring 2007
Changing Data: (Continued)
Reading from and Writing to Files Part 2
More Scheme CS 331.
*Lecture based on notes from SICP
Presentation transcript:

Assignment: Changing Data CMSC Introduction to Computer Programming November 1, 2002

Roadmap Recap: Binary Search Trees State: Changing Data over Time –Assignment with set! –Multiple statements with begin –Objects with state Traffic light example Bank account example Summary

Sets: Binary Search Trees Bst is: 1) #f 2) (make-bt val left right) –, where val: number; left, right: bst (define-struct bt (val left right)) INVARIANT: For node n, all vals in (bt-left n) n Define operations on sets with bst’s –Element-of?, Adjoin, Intersection, Union,.. –More efficient – O(log n) – search, adjunction.. –Functions exploit & maintain invariant If true of inputs, must be kept true of output

State of the Data Computationally model objects in world Objects change over time –E.g. traffic light: red, yellow, green –Bank account: balance up/down –Age, temperature, etc,…

Changing State in Scheme So far, value in variable once –Bind values to variables with formal params To change state must change value assigned to variable Assignment: set! –(set! var exp) var: variable name; exp: any scheme expression –(set! x 1) –(set! x (factorial 10))

Evaluating set! Expressions evaluate to values –(+ 3 4) => 7 –(set! x 1) ??? Void - nothing Set! is produces an effect, not a value –Changes value of variable Problem: Need to set! AND return value Solution: Do both! –(begin exp1 …. expn) –Does evaluates all exps; returns value of last –(begin (set! x (+ 3 4)) x) -> 7

Example: Traffic Lights State of traffic light: color –TL-color is ‘green, ‘yellow, or ‘red –; contract: current-color: TL-color (define current-color ‘red) –? (set! current-color 5) –? (set! current-color ‘green)

Changing Traffic Lights First step: What would it change to? Function: –;;contract:next-color: TL-color -> TL-color –;; purpose: To identify next color in sequence (define (next-color color) (cond ((eq? color ‘red) ‘green) ((eq? color ‘yellow) ‘red) ((eq? color ‘green) ‘yellow)))

Really Changing Lights Key: change value of state variable with set! (define (turn-light) (begin (set! current-color (next-color current-color)) current-color))) (turn-light) Return value? Current-color?

Example: Bank Account State of bank account: balance –Varies over time: withdrawal, deposit, check.. (define balance 100) (set! balance (- balance 25)) Goal: encapsulation in function –Test if enough money If there is, reduce balance O.w., report error

Make-Withdraw Contract: –;;make-withdraw: number -> (number -> number) –Note: returns a procedure Purpose: –;; to create a procedure to process withdrawal

Make-withdraw (define (make-withdraw initial-balance) (let ((balance initial-balance)) (lambda (amount) (if (<= amount balance) (begin (set! balance (- balance amount)) balance) (error “Insufficient funds!!))))

Using Make-withdraw (define w1 (make-withdraw 100)) (w1 20) –> 80 (w1 100) –> “Insufficient funds!!” (w1 60) –> 20

Summary State variables –Represent objects: state changes over time Forms: –(set! var expression) –(begin exp1 … expn) Modeling traffic lights and bank accounts