Adding Machine Run 2

Similar documents
YOUR NAME PLEASE: *** SOLUTIONS ***

State. Substitution relies on an identifer having a fxed value

Intro. Scheme Basics. scm> 5 5. scm>

How to Design Programs

Discussion 4. Data Abstraction and Sequences

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

Functional Programming. Pure Functional Programming

User-defined Functions. Conditional Expressions in Scheme

20-CS Programming Languages Fall Final Exam! Answer all questions Be sure to put your name on the paper in the space provided!

CSE413 Midterm. Question Max Points Total 100

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

Documentation for LISP in BASIC

Scheme Basics > (butfirst '(help!)) ()

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Implementing Procedure Calls

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Scheme as implemented by Racket

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

Lecture 5: Implementing Lists, Version 1

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Stacks, Queues (cont d)

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

6.001 Notes: Section 8.1

Module 9: Binary trees

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington

Functional programming with Common Lisp

CS450 - Structure of Higher Level Languages

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

OptiQL: LINQ on Delite

CS 3 Midterm 1 Review

6.001 SICP. Types. Types compound data. Types simple data. Types. Types procedures

Project 2: Scheme Interpreter

Programming Languages. Streams Wrapup, Memoization, Type Systems, and Some Monty Python

DRAWING ENVIRONMENT DIAGRAMS

Principles of Programming Languages

CS3 Midterm 2 Summer 2008

Types of recursion. Readings: none. In this module: a glimpse of non-structural recursion. CS 135 Winter : Types of recursion 1

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Do a domain analysis by hand-drawing three or more pictures of what the world program will look like at different stages when it is running.

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Putting the fun in functional programming

Functional Programming - 2. Higher Order Functions

FUNCTIONAL PROGRAMMING (1) PROF. SIMON PARSONS

Midterm Examination (Sample Solutions), Cmput 325

(Refer Slide Time: 01.26)

CS115 - Module 9 - filter, map, and friends

bindings (review) Topic 18 Environment Model of Evaluation bindings (review) frames (review) frames (review) frames (review) x: 10 y: #f x: 10

Building a system for symbolic differentiation

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

(Provisional) Lecture 22: Rackette Overview, Binary Tree Analysis 10:00 AM, Oct 27, 2017

CS115 - Module 10 - General Trees

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

Module 8: Binary trees

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition

SCHEME AND CALCULATOR 5b

Random Testing in 321

CSE431 Translation of Computer Languages

Racket Style Guide Fall 2017

Trees and Tree Encodings

Peer-To-Peer Network!

CS61A Notes Week 9: Muta4on (solu4ons) Revenge of the Box- and- pointers

Artificial Intelligence Prof. Deepak Khemani Department of Computer Science and Engineering Indian Institute of Technology, Madras

University of Massachusetts Lowell

More About Recursive Data Types

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

Functional Programming. Pure Functional Languages

Lecture #24: Programming Languages and Programs

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

For this chapter, switch languages in DrRacket to Advanced Student Language.

// The next 4 functions return true on success, false on failure

61A LECTURE 18 SCHEME. Steven Tang and Eric Tzeng July 24, 2013

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

CSE341 Spring 2017, Final Examination June 8, 2017

More loops Ch

Lecture Chapter 6 Recursion as a Problem Solving Technique

Welcome! COMP s1. Programming Fundamentals

Problem 1. Remove consecutive duplicates (6 points, 11 mintues)

CMSC th Lecture: Graph Theory: Trees.

Recursion & Iteration

Loops in Scheme, II. (early slides assume map/filter)

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Functions and Recursion. Dr. Philip Cannata 1

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

6.001 Notes: Section 6.1

Solving Your Problem by Generalization

CS2500 Exam 2 Fall 2011

ITI Introduction to Computing II

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

15100 Fall 2005 Final Project

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

Transcription:

Calculator Run 1

Adding Machine Run 2

Simple Adder (define TOTAL 0) (define total-message (make-message (number->string TOTAL))) (define amount-text (make-text "Amount")) (define add-button (make-button "+" (lambda (evt) (add-to-total (string->number (text-contents amount-text)))))) ; add-to-total : num -> true (define (add-to-total amt) (local [(define new-total (+ TOTAL amt))] (draw-message total-message (number->string new-total)))) (create-window (list (list total-message) (list amount-text) (list add-button))) 3

Why the Adder is Unlike A Calculator (define (add-to-total amt) (local [(define new-total (+ TOTAL amt))] (draw-message total-message (number->string new-total)))) Every time we have a new amt, it s added to the same original TOTAL The new total is drawn on the screen, then forgotten 4

Why the Adder is Unlike A Calculator (define (add-to-total amt) (local [(define new-total (+ TOTAL amt))] (draw-message total-message (number->string new-total)))) Every time we have a new amt, it s added to the same original TOTAL The new total is drawn on the screen, then forgotten We need to remember new-total for next time 5

set! In Advanced: changes the value of TOTAL to 17 (set! TOTAL 17) 6

set! In Advanced: changes the value of TOTAL to 17 (set! TOTAL 17) "Constant" definitions are no longer constant the are variable definitions A set! expression is called an assignment The value of TOTAL is the state of the program 7

Evaluating set! (define TOTAL 0) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (add-amt 1) (add-amt 2) 8

Evaluating set! (define TOTAL 0) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (add-amt 1) (add-amt 2) (define TOTAL 0) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (set! TOTAL (+ TOTAL 1)) (add-amt 2) 9

Evaluating set! (define TOTAL 0) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (set! TOTAL (+ TOTAL 1)) (add-amt 2) 10

Evaluating set! (define TOTAL 0) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (set! TOTAL (+ TOTAL 1)) (add-amt 2) (define TOTAL 0) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (set! TOTAL (+ 0 1)) (add-amt 2) 11

Evaluating set! (define TOTAL 0) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (set! TOTAL (+ 0 1)) (add-amt 2) 12

Evaluating set! (define TOTAL 0) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (set! TOTAL (+ 0 1)) (add-amt 2) (define TOTAL 0) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (set! TOTAL 1) (add-amt 2) 13

Evaluating set! (define TOTAL 0) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (set! TOTAL 1) (add-amt 2) 14

Evaluating set! (define TOTAL 0) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (set! TOTAL 1) (add-amt 2) (define TOTAL 1) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (void) (add-amt 2) To evaluate set!, change a definition and produce (void) 15

Evaluating set! (define TOTAL 1) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (void) (add-amt 2) 16

Evaluating set! (define TOTAL 1) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (void) (add-amt 2) (define TOTAL 1) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (void) (set! TOTAL (+ TOTAL 2)) 17

Evaluating set! (define TOTAL 1) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (void) (set! TOTAL (+ TOTAL 2)) 18

Evaluating set! (define TOTAL 1) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (void) (set! TOTAL (+ TOTAL 2)) (define TOTAL 1) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (void) (set! TOTAL (+ 1 2)) 19

Evaluating set! (define TOTAL 1) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (void) (set! TOTAL (+ 1 2)) 20

Evaluating set! (define TOTAL 1) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (void) (set! TOTAL (+ 1 2)) (define TOTAL 1) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (void) (set! TOTAL 3) It s important that a variable name is not replaced by its value until the value is needed 21

Evaluating set! (define TOTAL 1) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (void) (set! TOTAL 3) 22

Evaluating set! (define TOTAL 1) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (void) (set! TOTAL 3) (define TOTAL 3) (define (add-amt amt) (set! TOTAL (+ TOTAL amt))) (void) (void) 23

Repairing the Calculator (define (add-to-total amt) (local [(define new-total (+ TOTAL amt))] ; How do we combine two actions?... (set! TOTAL new-total) (draw-message total-message (number->string new-total))...)) 24

Repairing the Calculator (define (add-to-total amt) (local [(define new-total (+ TOTAL amt))] ; How do we combine two actions?... (set! TOTAL new-total) (draw-message total-message (number->string new-total))...)) For drawing, we used and to combine actions But set! doesn t return a boolean 25

Making the Adder Remember Totals Also new in Advanced: the begin form (define (add-to-total amt) (local [(define new-total (+ TOTAL amt))] (begin (set! TOTAL new-total) (draw-message total-message (number->string new-total))))) 26

Making the Adder Remember Totals Also new in Advanced: the begin form (define (add-to-total amt) (local [(define new-total (+ TOTAL amt))] (begin (set! TOTAL new-total) (draw-message total-message (number->string new-total))))) The begin form Evaluates its first expression Throws away the result Goes away when only one expression is left begin works with any number of expressions Run 27

Evaluating begin (define TOTAL 3) (define (running-total amt) (begin (set! TOTAL (+ TOTAL amt)) TOTAL)) (running-total 10) 28

Evaluating begin (define TOTAL 3) (define (running-total amt) (begin (set! TOTAL (+ TOTAL amt)) TOTAL)) (running-total 10) (define TOTAL 3)... (begin (set! TOTAL (+ TOTAL 10)) TOTAL) 29

(define TOTAL 3)... (begin (set! TOTAL (+ TOTAL 10)) TOTAL) Evaluating begin 30

(define TOTAL 3)... (begin (set! TOTAL (+ TOTAL 10)) TOTAL) (define TOTAL 3)... (begin (set! TOTAL (+ 3 10)) TOTAL) Evaluating begin 31

(define TOTAL 3)... (begin (set! TOTAL (+ 3 10)) TOTAL) Evaluating begin 32

(define TOTAL 3)... (begin (set! TOTAL (+ 3 10)) TOTAL) (define TOTAL 3)... (begin (set! TOTAL 13) TOTAL) Evaluating begin 33

Evaluating begin (define TOTAL 3)... (begin (set! TOTAL 13) TOTAL) 34

Evaluating begin (define TOTAL 3)... (begin (set! TOTAL 13) TOTAL) (define TOTAL 13)... (begin (void) TOTAL) 35

Evaluating begin (define TOTAL 13)... (begin (void) TOTAL) 36

Evaluating begin (define TOTAL 13)... (begin (void) TOTAL) (define TOTAL 13)... (begin TOTAL) 37

Evaluating begin (define TOTAL 13)... (begin TOTAL) 38

Evaluating begin (define TOTAL 13)... (begin TOTAL) (define TOTAL 13)... TOTAL 39

Evaluating begin (define TOTAL 13)... TOTAL 40

Evaluating begin (define TOTAL 13)... TOTAL (define TOTAL 13)... 13 41

More Calculator Buttons Run 42

Implementing More Calculator Buttons... ; op-button : string (num num -> num) -> button (define (op-button label OP) (make-button label (lambda (evt) (change-total OP (string->number (text-contents amount-text)))))) ; change-total : (num num -> num) num -> true (define (change-total OP amt) (local [(define new-total (OP TOTAL amt))] (begin (set! TOTAL new-total) (draw-message total-message (number->string new-total))))) (create-window (list (list total-message) (list amount-text) (list (op-button "+" +) (op-button "-" -) (op-button "*" *) (op-button "/" /)))) 43

The Digit Buttons Run Now two pieces of state: The running total The number we re typing, so far 44

... (define WORKING 0) Implementing Digit Buttons ; digit-button : num -> button (define (digit-button n) (make-button (number->string n) (lambda (evt) (add-digit n)))) ; add-digit : num -> true (define (add-digit n) (begin (set! WORKING (+ n (* WORKING 10))) (draw-message total-message (number->string WORKING)))) ; change-total : (num num -> num) num -> true (define (change-total OP amt) (local [(define new-total (OP TOTAL amt))] (begin (set! TOTAL new-total) (set! WORKING 0) (draw-message total-message (number->string new-total)))))... 45

Infix Operations Run A normal calculator uses infix (algebra-like) order New piece of state: The operation to perform when the number is ready 46

... (define PREV-OP +) Implementing Infix Operations ; op-button : string (num num -> num) -> button (define (op-button label OP) (make-button label (lambda (evt) (begin (change-total PREV-OP WORKING) (set! PREV-OP OP) true))))... (create-window (list (list total-message) (map digit-button (7 8 9)) (map digit-button (4 5 6)) (map digit-button (1 2 3)) (map digit-button (0)) (list (op-button "+" +) (op-button "-" -) (op-button "*" *) (op-button "/" /) (op-button "=" (lambda (tot new) new))))) 47

Multiple Calculators Run How can we keep the calculators from using the same TOTAL? 48

Multiple Calculators Run How can we keep the calculators from using the same TOTAL? Easy use local 49

Implementing Multiple Calculators (define (make-calculator) (local [(define TOTAL 0) (define WORKING 0)...] (create-window (list (list total-message) (map digit-button (7 8 9)) (map digit-button (4 5 6)) (map digit-button (1 2 3)) (map digit-button (0)) (list (op-button "+" +) (op-button "-" -) (op-button "*" *) (op-button "/" /) (op-button "=" (lambda (tot new) new))))))) (make-calculator) (make-calculator) 50

When to use State Use state and set! when a function needs to remember something about previous calls, and you have no control over the callers 51

When NOT to use State The following is a unacceptable use of set! (define REV empty) (define (reverse-list l) (cond [(empty? l) REV] [(cons? l) (begin (set! REV (cons (first l) REV)) (reverse-list (rest l)))])) (reverse-list (1 2 3 4 5)) Recursive calls build on earlier results, but we control all of the recursive calls It produces the wrong result when you call it a second time 52