EXCEPTIONS, CALCULATOR 10

Similar documents
INTERPRETERS AND TAIL CALLS 9

CALCULATOR Calculator COMPUTER SCIENCE 61A. July 24, 2014

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

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

SCHEME AND CALCULATOR 5b

61A Lecture 26. Monday, October 31

INTERPRETATION AND SCHEME LISTS 12

INTERPRETATION AND SCHEME LISTS 12

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

61A Lecture 2. Friday, August 28, 2015

Public-Service Announcement

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

Chapter 9: Dealing with Errors

Exceptions & a Taste of Declarative Programming in SQL

Getting Started with Python

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

Lecture #12: Quick: Exceptions and SQL

Pairs and Lists. (cons 1 2) 1 2. (cons 2 nil) 2 nil. Not a well-formed list! 1 > (cdr x) 2 > (cons 1 (cons 2 (cons 3 (cons 4 nil)))) ( ) (Demo)

Fundamentals of Programming (Python) Getting Started with Programming

Python for Non-programmers

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

STRUCTURE AND INTERPRETATION COMPUTER PROGRAMS >>> COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 23 Py TODAY REVIEW: INTERPRETATION 7/26/2012 READ PRINT

STRUCTURE AND INTERPRETATION COMPUTER PROGRAMS COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 23 Py TODAY 7/26/2012

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

MEMOIZATION, RECURSIVE DATA, AND SETS

Review 3. Exceptions and Try-Except Blocks

1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19

61A LECTURE 17 ORDERS OF GROWTH, EXCEPTIONS. Steven Tang and Eric Tzeng July 23, 2013

Starting. Read: Chapter 1, Appendix B from textbook.

CS61A Lecture 23 Py. Jom Magrotker UC Berkeley EECS July 26, 2012

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

RLISTS AND THE ENVIRONMENT MODEL 9

Python for C programmers

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

CS61A Lecture 32. Amir Kamil UC Berkeley April 5, 2013

6.001 Notes: Section 15.1

61A Lecture 2. Wednesday, September 4, 2013

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

CS61A Notes Week 13: Interpreters

Functions and Decomposition

Python Essential Reference, Second Edition - Chapter 5: Control Flow Page 1 of 8

Outline. the try-except statement the try-finally statement. exceptions are classes raising exceptions defining exceptions

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

4.2 Variations on a Scheme -- Lazy Evaluation

TAIL RECURSION, SCOPE, AND PROJECT 4 11

T H E I N T E R A C T I V E S H E L L

Exception Handling. Genome 559

How To Think Like A Computer Scientist, chapter 3; chapter 6, sections

What is an Exception? Exception Handling. What is an Exception? What is an Exception? test = [1,2,3] test[3]

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Slide Set 15 (Complete)

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Text Input and Conditionals

Beyond Blocks: Python Session #1

SCHEME INTERPRETER GUIDE 4

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

Python for Informatics

TESTING, DEBUGGING, EXCEPTIONS, ASSERTIONS

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

Lists, loops and decisions

Lecture 21. Programming with Subclasses

Class extension and. Exception handling. Genome 559

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

Iterators & Generators

Jython. secondary. memory

Structure and Interpretation of Computer Programs

61A Lecture 25. Friday, October 28

Decision structures. A more complex decision structure is an if-else-statement: if <condition>: <body1> else: <body2>

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Introduction to: Computers & Programming: Exception Handling

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

CSE : Python Programming

15 150: Principles of Functional Programming. Exceptions

Structure and Interpretation of Computer Programs Spring 2014 Final (with corrections)

CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter

Project 5 - The Meta-Circular Evaluator

CS 111X - Fall Test 1

Control Structures 1 / 17

Documentation for LISP in BASIC

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Part III Appendices 165

MEIN 50010: Python Flow Control

Lecture 18. Classes and Types

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

CS 61A Discussion 8: Scheme. March 23, 2017

TUPLES AND RECURSIVE LISTS 5

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Project 5 - The Meta-Circular Evaluator

Functions CHAPTER 5. FIGURE 1. Concrete syntax for the P 2 subset of Python. (In addition to that of P 1.)

At full speed with Python

Exceptions CS GMU


61A Lecture 3. Friday, September 5

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Transcription:

EXCEPTIONS, CALCULATOR 10 COMPUTER SCIENCE 61A November 5th, 2012 We are beginning to dive into the realm of interpreting computer programs - that is, writing programs that understand programs. In order to do so, we ll have to examine programming languages in-depth. The Calculator language, a subset of Scheme, will be the first of these examples. In today s discussion, we ll be looking at implementing Calculator using regular Python. We ll also take a look at Exceptions, a mechanism for handling unexpected execution - quite common when handling user input. 1 Exceptions Up to this point in the semester, we have assumed that the input to our functions are always correct, and thus have not done any error handling. However, functions can often have large domains, and we want our functions to handle erroneous input gracefully. This is where exceptions come in. Exceptions provide a general mechanism for adding error-handling logic to programs. Raising an exception is a technique for interrupting the normal flow of execution in a program, signaling that some exceptional circumstance has arisen. An exception is an object instance of a class that inherits, either directly or indirectly, from the BaseException class. The following is an example of how to raise an exception: >>> raise Exception( An error occurred ) Traceback (most recent call last): File "<stdin>", line 1, in <module> Exception: An error occurred 1

DISCUSSION 10: EXCEPTIONS, CALCULATOR Page 2 Notice how the string An error occurred is an argument to the Exception object being created, and the string is part of what Python prints out in response to the exception being raised. If the exception is raised while with a try statement, then the interpreter will immediately look for an except statement that handles the type of exception being raised. try and except statements allow programs to respond to unexpected arguments and other errors gracefully, rather than terminating entirely. Here s how to structure try and except statements: try: <try suite> except <exception class> as <name>: <except suite> except <exception class> as <name>: <except suite> 1.1 Questions 1. Fill in all the blanks to produce the desired output: >>> try: x = except as : print( handling a, type(e)) x = handling a <class ZeroDivisionError > >>> x 9001 2. Write the function safe square that uses exceptions to print Incorrect argument type when anything other than an int or float class is given as an argument. Otherwise, safe square should multiply the argument by itself. A useful fact is that a TypeError is raised when * is given incorrect arguments. def safe_square(x):

DISCUSSION 10: EXCEPTIONS, CALCULATOR Page 3 3. Predict the output of each of the following lines, assuming safe square is implemented as described in the previous question. >>> safe_square( hello ) >>> safe_square( hello * 5) >>> safe_square( hello * hello ) >>> safe_square(1 * 2.5) >>> safe_square(1/ 0) 2 Calculator For now, our Calculator language will be a Scheme-syntax language that can handle the four basic arithmetic operations. These operations can be nested and can take varying numbers of arguments. Here s a couple examples of Calculator in action: > (+ 2 2) 4 > (- 5) -5 > (* (+ 1 2) (+ 2 3)) 15 Our goal now is to write an interpreter for this Calculator language. The job of an interpreter is, given an expression, evaluate its meaning. So let s talk about expressions. 2.1 Representing Expressions There are two kinds of expressions. A call expression is a Scheme list - the first element is the operator, and each subsequent element is an operand. A primitive expression is an operator symbol or number. When we type a line at the Calculator prompt and hit enter, we ve just sent an expression to the interpreter. To represent Scheme lists in Python, we ll be using Pair objects. The class definition is below. Note the usage of exceptions: class Pair(object):

DISCUSSION 10: EXCEPTIONS, CALCULATOR Page 4 def init (self, first, second): self.first = first self.second = second def len (self): n, second = 1, self.second while isinstance(second, Pair): n += 1 second = second.second if second is not nil: raise TypeError("length attempted on improper list") return n def getitem (self, k): if k < 0: raise IndexError("negative index into list") j, y = 0, self while j < k: if y.second is nil: raise IndexError("list index out of bounds") elif not isinstance(y.second, Pair): raise TypeError("ill-formed list") j, y = j + 1, y.second return y.first def map(self, fn): """Returns a Scheme list after mapping Python function fn over self.""" mapped = fn(self.first) if self.second is nil or isinstance(self.second, Pair): return Pair(mapped, self.second.map(fn)) else: raise TypeError("ill-formed list") def to_py_list(self): """Returns a Python list containing the elements of this Scheme list.""" y, result = self, [ ] while y is not nil: result += [y.first] if not isinstance(y.second, Pair) or y.second is not nil:

DISCUSSION 10: EXCEPTIONS, CALCULATOR Page 5 raise TypeError("ill-formed list") y = y.second return result class nil(object): """The empty list""" def len (self): return 0 def map(self, fn): return self nil = nil() #nil now refers to a single instance of nil class 2.2 Questions 1. Translate the following Python representation of Calculator expressions into the proper Scheme-syntax: >>> Pair( +, Pair(1, Pair(2, Pair(3, Pair(4, nil))))) >>>Pair( +, Pair( 1, Pair(Pair( *, Pair(2, Pair(3, nil))), nil))) 2. Translate the following Calculator expression into calls to the Pair constructor. > (+ 1 2 (- 3 4)) 2.3 Evaluation So what is evaluation? Evaluation discovers the form of an expression and executes a corresponding evaluation rule. Primitive expressions are evaluated directly. Call expressions are evaluated recursively: (1) Evaluate each operand expression, (2) Collect their values as a list of arguments, and (3) Apply the named operator to the argument list. Here s calc eval: def calc_eval(exp): if not isinstance(exp, Pair): #expression is primitive return exp else:

DISCUSSION 10: EXCEPTIONS, CALCULATOR Page 6 operator, operands = exp.first, exp.second args = operands.map(calc_eval).to_py_list() return calc_apply(operator, args) As you can see, all we ve done is follow the rules of evaluation outlined above. If the expression is primitive (i.e. not a Scheme list), simply return it. Else, evaluate the operands and apply the operator to the evaluated operands. How do we apply the operator? We ll use calc apply, with dispatching on the operator name: def calc_apply(operator, args): if operator == + : return sum(args) elif operator == - : if len(args) == 1: return -args[0] else: return sum(args[0], [-args for args in args[1:]]) elif operator == * : return reduce(mul, args, 1) Depending on what the operator is, we can match it to a corresponding Python call. Each conditional clause above handles the application of one operator. Something very important to keep in mind: calc eval deals with expressions, calc apply deals with values. 2.4 Questions 1. Suppose we typed each of the following expressions into the Calculator interpreter. How many calls to calc eval would they each generate? How many calls to calc apply? > (+ 2 4 6 8) > (+ 2 (* 4 (- 6 8))) 2. The - operator will fail if given no arguments. Add error handling to raise an exception when this situation is encountered (the type of exception is unimportant).

DISCUSSION 10: EXCEPTIONS, CALCULATOR Page 7 3. We also want to be able to perform division, as in (/ 4 2). Supplement the existing code to handle this. If division by 0 is attempted, or if there are less than 2 arguments supplied, you should raise an exception (the type of exception is unimportant). 4. Alyssa P. Hacker and Ben Bitdiddle are also tasked with implementing the and operator, as in (and (= 1 2) (< 3 4)). Ben says this is easy: they just have to follow the same process as in implementing * and /. Alyssa is not so sure. Who s right? 5. Now that you ve had a chance to think about it, you decide to try implementing and yourself. You may assume the conditional operators (e.g. <, >, =, etc) have already been implemented for you.