Adding Machine Run 2

Size: px
Start display at page:

Download "Adding Machine Run 2"

Transcription

1 Calculator Run 1

2 Adding Machine Run 2

3 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

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 4

5 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

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

7 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

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

9 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

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

11 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

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

13 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

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

15 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

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

17 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

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

19 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

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

21 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

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

23 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

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))...)) 24

25 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

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))))) 26

27 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

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

29 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

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

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

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

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

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

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

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

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

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

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

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

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

42 More Calculator Buttons Run 42

43 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

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

45 ... (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

46 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

47 ... (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

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

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

50 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

51 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

52 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 ( )) 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

YOUR NAME PLEASE: *** SOLUTIONS ***

YOUR NAME PLEASE: *** SOLUTIONS *** YOUR NAME PLEASE: *** SOLUTIONS *** Computer Science 201b SAMPLE Exam 1 SOLUTIONS February 15, 2015 Closed book and closed notes. No electronic devices. Show ALL work you want graded on the test itself.

More information

State. Substitution relies on an identifer having a fxed value

State. Substitution relies on an identifer having a fxed value Part 1 1 State Substitution relies on an identifer having a fxed value {let {[x 5]} {let {[f {lambda {y} {+ x y}}]}... {f 1}}} = {let {[f {lambda {y} {+ 5 y}}]}... {f 1}} because x cannot change In Plait,

More information

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

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

How to Design Programs

How to Design Programs How to Design Programs How to (in Racket): represent data variants trees and lists write functions that process the data See also http://www.htdp.org/ 1 Running Example: GUIs Pick a fruit: Apple Banana

More information

Discussion 4. Data Abstraction and Sequences

Discussion 4. Data Abstraction and Sequences Discussion 4 Data Abstraction and Sequences Data Abstraction: The idea of data abstraction is to conceal the representation of some data and to instead reveal a standard interface that is more aligned

More information

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

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams. CS 61A, Fall, 2002, Midterm #2, L. Rowe 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams. a) d) 3 1 2 3 1 2 e) b) 3 c) 1 2 3 1 2 1 2 For each of the following Scheme

More information

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Programming Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

More information

User-defined Functions. Conditional Expressions in Scheme

User-defined Functions. Conditional Expressions in Scheme User-defined Functions The list (lambda (args (body s to a function with (args as its argument list and (body as the function body. No quotes are needed for (args or (body. (lambda (x (+ x 1 s to the increment

More information

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

20-CS Programming Languages Fall Final Exam! Answer all questions Be sure to put your name on the paper in the space provided! 20-CS-4003-001 Programming Languages Fall 2018 Final Exam! Answer all questions Be sure to put your name on the paper in the space provided! Name Signature The code below compiles and runs just fine (worse

More information

CSE413 Midterm. Question Max Points Total 100

CSE413 Midterm. Question Max Points Total 100 CSE413 Midterm 05 November 2007 Name Student ID Answer all questions; show your work. You may use: 1. The Scheme language definition. 2. One 8.5 * 11 piece of paper with handwritten notes Other items,

More information

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

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda Functional abstraction Readings: HtDP, sections 19-24. Language level: Intermediate Student With Lambda different order used in lecture section 24 material introduced much earlier sections 22, 23 not covered

More information

Functional abstraction

Functional abstraction Functional abstraction Readings: HtDP, sections 19-24. Language level: Intermediate Student With Lambda different order used in lecture section 24 material introduced much earlier sections 22, 23 not covered

More information

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

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley PAIRS AND LISTS 6 GEORGE WANG gswang.cs61a@gmail.com Department of Electrical Engineering and Computer Sciences University of California, Berkeley June 29, 2010 1 Pairs 1.1 Overview To represent data types

More information

Documentation for LISP in BASIC

Documentation for LISP in BASIC Documentation for LISP in BASIC The software and the documentation are both Copyright 2008 Arthur Nunes-Harwitt LISP in BASIC is a LISP interpreter for a Scheme-like dialect of LISP, which happens to have

More information

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

Scheme Basics > (butfirst '(help!)) () Scheme Basics > (butfirst '(help!)) () [The butfirst of a *sentence* containing one word is all but that word, i.e., the empty sentence. (BUTFIRST 'HELP!) without the inner parentheses would be butfirst

More information

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

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12 CS 151 Linked Lists, Recursively Implemented 1 2 Linked Lists, Revisited Recall that a linked list is a structure that represents a sequence of elements that are stored non-contiguously in memory. We can

More information

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

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Implementing Procedure Calls

Implementing Procedure Calls 1 / 39 Implementing Procedure Calls February 18 22, 2013 2 / 39 Outline Intro to procedure calls Caller vs. callee Procedure call basics Calling conventions The stack Interacting with the stack Structure

More information

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

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page. MORE SCHEME COMPUTER SCIENCE MENTORS 61A October 30 to November 3, 2017 1 What Would Scheme Print? Solutions begin on the following page. 1. What will Scheme output? Draw box-and-pointer diagrams to help

More information

Scheme as implemented by Racket

Scheme as implemented by Racket Scheme as implemented by Racket (Simple view:) Racket is a version of Scheme. (Full view:) Racket is a platform for implementing and using many languages, and Scheme is one of those that come out of the

More information

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

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4 CS 61A Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions 1 Calculator We are beginning to dive into the realm of interpreting computer programs that is, writing programs that

More information

Lecture 5: Implementing Lists, Version 1

Lecture 5: Implementing Lists, Version 1 CS18 Integrated Introduction to Computer Science Fisler, Nelson Lecture 5: Implementing Lists, Version 1 Contents 1 Implementing Lists 1 2 Methods 2 2.1 isempty...........................................

More information

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

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs Lexical addressing The difference between a interpreter and a compiler is really two points on a spectrum of possible

More information

Stacks, Queues (cont d)

Stacks, Queues (cont d) Stacks, Queues (cont d) CSE 2011 Winter 2007 February 1, 2007 1 The Adapter Pattern Using methods of one class to implement methods of another class Example: using List to implement Stack and Queue 2 1

More information

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th Name: CAS ID (e.g., abc01234@pomona.edu): I encourage you to collaborate. collaborations below. Please record your Each question is worth

More information

6.001 Notes: Section 8.1

6.001 Notes: Section 8.1 6.001 Notes: Section 8.1 Slide 8.1.1 In this lecture we are going to introduce a new data type, specifically to deal with symbols. This may sound a bit odd, but if you step back, you may realize that everything

More information

Module 9: Binary trees

Module 9: Binary trees Module 9: Binary trees Readings: HtDP, Section 14 We will cover the ideas in the text using different examples and different terminology. The readings are still important as an additional source of examples.

More information

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

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017 SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

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

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion Types of recursion Readings: none. In this module: learn to use accumulative recursion learn to recognize generative recursion CS 135 Fall 2018 07: Types of recursion 1 Structural vs. general recursion

More information

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

Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington Programming Languages Thunks, Laziness, Streams, Memoization Adapted from Dan Grossman s PL class, U. of Washington You ve been lied to Everything that looks like a function call in Racket is not necessarily

More information

Functional programming with Common Lisp

Functional programming with Common Lisp Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 81 Expressions and functions

More information

CS450 - Structure of Higher Level Languages

CS450 - Structure of Higher Level Languages Spring 2018 Streams February 24, 2018 Introduction Streams are abstract sequences. They are potentially infinite we will see that their most interesting and powerful uses come in handling infinite sequences.

More information

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

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1 CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1 Goals of this tutorial You should be able to... understand

More information

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

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal) CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal) What is the Metacircular Evaluator? It is the best part

More information

OptiQL: LINQ on Delite

OptiQL: LINQ on Delite OptiQL: LINQ on Delite What is LINQ? Language Integrated Query (LINQ) is a set of language and framework features for writing structured typesafe queries over local object collections and remote data sources.

More information

CS 3 Midterm 1 Review

CS 3 Midterm 1 Review CS 3 Midterm 1 Review 1. Quick Evaluations Indicate what each of the following would return if typed into STK. If you think it would error, then please write ERROR. If you think that it would loop forever,

More information

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

6.001 SICP. Types. Types compound data. Types simple data. Types. Types procedures Today s topics Types of objects and procedures Procedural abstractions Capturing patterns across procedures Higher Order Procedures Types (+ 5 1) ==> 15 (+ "hi 5) ;The object "hi", passed as the first

More information

Project 2: Scheme Interpreter

Project 2: Scheme Interpreter Project 2: Scheme Interpreter CSC 4101, Fall 2017 Due: 12 November 2017 For this project, you will implement a simple Scheme interpreter in C++ or Java. Your interpreter should be able to handle the same

More information

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

Programming Languages. Streams Wrapup, Memoization, Type Systems, and Some Monty Python Programming Languages Streams Wrapup, Memoization, Type Systems, and Some Monty Python Quick Review of Constructing Streams Usually two ways to construct a stream. Method 1: Use a function that takes a(n)

More information

DRAWING ENVIRONMENT DIAGRAMS

DRAWING ENVIRONMENT DIAGRAMS DRAWING ENVIRONMENT DIAGRAMS COMPUTER SCIENCE 61A September 10, 2012 0.1 Background A frame is a location where variable bindings are stored A binding is a connection between a name and a value. The name

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages www.cs.bgu.ac.il/~ppl172 Lesson 6 - Defining a Programming Language Bottom Up Collaboration and Management - Elements of Programming Dana Fisman 1 What we accomplished

More information

CS3 Midterm 2 Summer 2008

CS3 Midterm 2 Summer 2008 CS3 Midterm 2 Summer 2008 Read this page and fill in the left table now. Name: Instructional login (eg, cs3- ab): UCWISE login: Name of the person sitting to your left: Name of the person sitting to your

More information

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

Types of recursion. Readings: none. In this module: a glimpse of non-structural recursion. CS 135 Winter : Types of recursion 1 Types of recursion Readings: none. In this module: a glimpse of non-structural recursion CS 135 Winter 2018 07: Types of recursion 1 Structural vs. general recursion All of the recursion we have done to

More information

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

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones. 6.001, Fall Semester, 2002 Quiz II Sample solutions 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs

More information

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.

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. How to Design Worlds The How to Design Worlds process provides guidance for designing interactive world programs using big-bang. While some elements of the process are tailored to big-bang, the process

More information

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

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

More information

Putting the fun in functional programming

Putting the fun in functional programming CM20167 Topic 4: Map, Lambda, Filter Guy McCusker 1W2.1 Outline 1 Introduction to higher-order functions 2 Map 3 Lambda 4 Filter Guy McCusker (1W2.1 CM20167 Topic 4 2 / 42 Putting the fun in functional

More information

Functional Programming - 2. Higher Order Functions

Functional Programming - 2. Higher Order Functions Functional Programming - 2 Higher Order Functions Map on a list Apply Reductions: foldr, foldl Lexical scoping with let s Functional-11, CS5314, Sp16 BGRyder 1 Higher Order Functions Functions as 1st class

More information

FUNCTIONAL PROGRAMMING (1) PROF. SIMON PARSONS

FUNCTIONAL PROGRAMMING (1) PROF. SIMON PARSONS FUNCTIONAL PROGRAMMING (1) PROF. SIMON PARSONS Imperative programming is concerned with how. Functional or applicative programming is, by contrast, concerned with what. It is based on the mathematics of

More information

Midterm Examination (Sample Solutions), Cmput 325

Midterm Examination (Sample Solutions), Cmput 325 Midterm Examination (Sample Solutions, Cmput 325 [15 marks] Consider the Lisp definitions below (defun test (L S (if (null L S (let ((New (add (cdar L S (test (cdr L New (defun add (A S (if (null S (cons

More information

(Refer Slide Time: 01.26)

(Refer Slide Time: 01.26) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture # 22 Why Sorting? Today we are going to be looking at sorting.

More information

CS115 - Module 9 - filter, map, and friends

CS115 - Module 9 - filter, map, and friends Fall 2017 Reminder: if you have not already, ensure you: Read How to Design Programs, Intermezzo 3 (Section 18); Sections 19-23. Abstraction abstraction, n. 3a.... The process of isolating properties or

More information

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

bindings (review) Topic 18 Environment Model of Evaluation bindings (review) frames (review) frames (review) frames (review) x: 10 y: #f x: 10 Topic 18 Environment Model of Evaluation Section 3.2 bindings (review) a binding is an association between a name and a Scheme value names: variable names, procedure names formal parameters of procedures

More information

Building a system for symbolic differentiation

Building a system for symbolic differentiation Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)

More information

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))

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)) SCHEME 0 COMPUTER SCIENCE 6A July 26, 206 0. Warm Up: Conditional Expressions. What does Scheme print? scm> (if (or #t (/ 0 (/ 0 scm> (if (> 4 3 (+ 2 3 4 (+ 3 4 (* 3 2 scm> ((if (< 4 3 + - 4 00 scm> (if

More information

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

(Provisional) Lecture 22: Rackette Overview, Binary Tree Analysis 10:00 AM, Oct 27, 2017 Integrated Introduction to Computer Science Hughes (Provisional) Lecture 22: Rackette Overview, Binary Tree Analysis 10:00 Contents 1 Announcements 1 2 An OCaml Debugging Tip 1 3 Introduction to Rackette

More information

CS115 - Module 10 - General Trees

CS115 - Module 10 - General Trees Fall 2017 Reminder: if you have not already, ensure you: Read How to Design Programs, Sections 15 and 16. Arithmetic Expressions Recall with binary trees we could represent an expression containing binary

More information

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

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 6: User-Defined Functions I In this chapter, you will: Objectives Learn about standard (predefined) functions and discover

More information

Module 8: Binary trees

Module 8: Binary trees Module 8: Binary trees Readings: HtDP, Section 14 We will cover the ideas in the text using different examples and different terminology. The readings are still important as an additional source of examples.

More information

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

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action. Deferred operations Continuations 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (define the-cons (cons 1 #f)) (set-cdr! the-cons the-cons) (define (run-in-circles l) (+

More information

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

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition Programming as a contract Assertions, pre/postconditions and invariants Assertions: Section 4.2 in Savitch (p. 239) Loop invariants: Section 4.5 in Rosen Specifying what each method does q Specify it in

More information

SCHEME AND CALCULATOR 5b

SCHEME AND CALCULATOR 5b SCHEME AND CALCULATOR 5b COMPUTER SCIENCE 6A July 25, 203 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Random Testing in 321

Random Testing in 321 Random Testing in 321 1 Test Cases So Far Each test relates a particular input to a particular output. (test (bound-ids (with 'x (id 'y) (id 'x))) '(x)) (test (binding-ids (with 'x (id 'y) (id 'x))) '(x))

More information

CSE431 Translation of Computer Languages

CSE431 Translation of Computer Languages CSE431 Translation of Computer Languages Semantic Analysis Doug Shook Control Structure Analysis Three major phases for Java: Type Checking How might this apply to control structures? Reachability Analysis

More information

Racket Style Guide Fall 2017

Racket Style Guide Fall 2017 CS17 Integrated Introduction to Computer Science Hughes Racket Style Guide Fall 2017 Contents 1 Introduction 1 2 Naming 1 3 Formatting 1 4 Equality 3 5 Conditionals 4 5.1 Prefer Cond to If......................................

More information

Trees and Tree Encodings

Trees and Tree Encodings Trees and Tree Encodings January, 08 Introduction: Today, we are going to be looking at a special class of graph theory called trees. These structures are an important discipline in mathematics and have

More information

Peer-To-Peer Network!

Peer-To-Peer Network! Computer Networks! Introduction. I've been asked to create a PowerPoint on two different kinds of computer networks to be able to discuss the strengths and weaknesses of both. In this PowerPoint I will

More information

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

CS61A Notes Week 9: Muta4on (solu4ons) Revenge of the Box- and- pointers CS61A Notes Week 9: Muta4on (solu4ons) Revenge of the Box- and- pointers QUESTION We can also test if procedures are equal?. Consider this: > (define (square x) (* x x)) > (define (sqr x) (* x x)) > (eq?

More information

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

Artificial Intelligence Prof. Deepak Khemani Department of Computer Science and Engineering Indian Institute of Technology, Madras Artificial Intelligence Prof. Deepak Khemani Department of Computer Science and Engineering Indian Institute of Technology, Madras (Refer Slide Time: 00:17) Lecture No - 10 Hill Climbing So, we were looking

More information

University of Massachusetts Lowell

University of Massachusetts Lowell University of Massachusetts Lowell 91.301: Organization of Programming Languages Fall 2002 Quiz 1 Solutions to Sample Problems 2 91.301 Problem 1 What will Scheme print in response to the following statements?

More information

More About Recursive Data Types

More About Recursive Data Types More About Recursive Data Types CS 5010 Program Design Paradigms Bootcamp Lesson 5.5 Mitchell Wand, 2016-2017 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International

More information

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

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011 CMPSCI 187: Programming With Data Structures Lecture 12: Implementing Stacks With Linked Lists 5 October 2011 Implementing Stacks With Linked Lists Overview: The LinkedStack Class from L&C The Fields and

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

Lecture #24: Programming Languages and Programs

Lecture #24: Programming Languages and Programs Lecture #24: Programming Languages and Programs A programming language is a notation for describing computations or processes. These range from low-level notations, such as machine language or simple hardware

More information

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

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt Data abstractions Abstractions and their variations Basic data abstractions Why data abstractions are useful Procedural abstraction Process of procedural abstraction Define formal parameters, capture process

More information

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

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives CS 6A Scheme Fall 208 Discussion 8: October 24, 208 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

More information

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

For this chapter, switch languages in DrRacket to Advanced Student Language. Chapter 30 Mutation For this chapter, switch languages in DrRacket to Advanced Student Language. 30.1 Remembering changes Suppose you wanted to keep track of a grocery shopping list. You could easily define

More information

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

// The next 4 functions return true on success, false on failure Stacks and Queues Queues and stacks are two special list types of particular importance. They can be implemented using any list implementation, but arrays are a more practical solution for these structures

More information

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

61A LECTURE 18 SCHEME. Steven Tang and Eric Tzeng July 24, 2013 61A LECTURE 18 SCHEME Steven Tang and Eric Tzeng July 24, 2013 What s happening today? We re learning a new language! After you know one language (Python), learning your second (Scheme) is much faster

More information

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

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples Introduction to Typed Racket The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples Getting started Find a machine with DrRacket installed (e.g. the

More information

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341 Spring 2017, Final Examination June 8, 2017 CSE341 Spring 2017, Final Examination June 8, 2017 Please do not turn the page until 8:30. Rules: The exam is closed-book, closed-note, etc. except for both sides of one 8.5x11in piece of paper. Please

More information

More loops Ch

More loops Ch More loops Ch 3.3-3.4 Announcements Quiz next week! -Covers up to (and including) HW1 (week 1-3) -Topics: cout/cin, types, scope, if/else, etc. Review: Loops We put a loop around code that we want to run

More information

Lecture Chapter 6 Recursion as a Problem Solving Technique

Lecture Chapter 6 Recursion as a Problem Solving Technique Lecture Chapter 6 Recursion as a Problem Solving Technique Backtracking 1. Select, i.e., guess, a path of steps that could possibly lead to a solution 2. If the path leads to a dead end then retrace steps

More information

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals Welcome! 0 COMP1511 18s1 Programming Fundamentals COMP1511 18s1 Lecture 4 1 More Functions + Loops Andrew Bennett even more functions while loops 2 Before we begin introduce

More information

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

Problem 1. Remove consecutive duplicates (6 points, 11 mintues) Problem 1. Remove consecutive duplicates (6 points, 11 mintues) CS3 Fall 04 Midterm 2 Consider a function remove-conseq-dups that takes a sentence and returns a sentence in which any occurrences of a word

More information

CMSC th Lecture: Graph Theory: Trees.

CMSC th Lecture: Graph Theory: Trees. CMSC 27100 26th Lecture: Graph Theory: Trees. Lecturer: Janos Simon December 2, 2018 1 Trees Definition 1. A tree is an acyclic connected graph. Trees have many nice properties. Theorem 2. The following

More information

Recursion & Iteration

Recursion & Iteration Recursion & Iteration York University Department of Computer Science and Engineering 1 Overview Recursion Examples Iteration Examples Iteration vs. Recursion Example [ref.: Chap 5,6 Wilensky] 2 Recursion

More information

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

Loops in Scheme, II. (early slides assume map/filter) Loops in Scheme, II (early slides assume map/filter) c. Kathi Fisler, 2001 Recap: filter and map filter and map are Scheme s loops filter : (α! boolean) list[α]! list[α] extract list of elts that satisfy

More information

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

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms... First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms.... Q1) What are some of the applications for the tree data structure? Q2) There are 8, 15, 13, and

More information

Functions and Recursion. Dr. Philip Cannata 1

Functions and Recursion. Dr. Philip Cannata 1 Functions and Recursion Dr. Philip Cannata 1 10 High Level Languages This Course Java (Object Oriented) Jython in Java Relation ASP RDF (Horn Clause Deduction, Semantic Web) Dr. Philip Cannata 2 let transformation,

More information

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

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G. Haskell: Lists CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks

More information

6.001 Notes: Section 6.1

6.001 Notes: Section 6.1 6.001 Notes: Section 6.1 Slide 6.1.1 When we first starting talking about Scheme expressions, you may recall we said that (almost) every Scheme expression had three components, a syntax (legal ways of

More information

Solving Your Problem by Generalization

Solving Your Problem by Generalization Solving Your Problem by Generalization CS 5010 Program Design Paradigms Lesson 7.1 Mitchell Wand, 2012-2015 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.

More information

CS2500 Exam 2 Fall 2011

CS2500 Exam 2 Fall 2011 CS2500 Exam 2 Fall 2011 Name: Student Id (last 4 digits): Section (morning, honors or afternoon): Write down the answers in the space provided. You may use the usual primitives and expression forms, including

More information

ITI Introduction to Computing II

ITI Introduction to Computing II index.pdf March 17, 2013 1 ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 17, 2013 Definitions A List is a linear abstract

More information

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

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7))) Homework 1 Due 13 September Handout 2 CSC 131: Fall, 2006 6 September Reading 1. Read Mitchell, Chapter 3. 2. The Scheme Tutorial and the Scheme Quick Reference from the Links web page, as needed for the

More information

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

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses CS 6A Delayed Expressions Fall 07 Discussion 9: November 8, 07 Iterables and Iterators An iterable is any container that can be processed sequentially. Examples include lists, tuples, strings, and dictionaries.

More information

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

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation Concepts Introduced in Chapter 2 A more detailed overview of the compilation process. Parsing Scanning Semantic Analysis Syntax-Directed Translation Intermediate Code Generation Context-Free Grammar A

More information

15100 Fall 2005 Final Project

15100 Fall 2005 Final Project 15100 Fall 2005 Final Project Robby Findler & Jacob Matthews 1 Introduction to Sudoku Sudoku is a logic puzzle set on a nine by nine grid. The goal is to fill in the blank spaces in the puzzle with the

More information

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

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03 CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03 Topics: Threading, Synchronization 1 Threading Suppose we want to create an automated program that hacks into a server. Many encryption

More information