61A Lecture 2. Wednesday, September 4, 2013

Similar documents
61A Lecture 2. Friday, August 28, 2015

61A Lecture 3. Friday, September 5

! The simplest building blocks of a language. ! Compound elements are built from simpler ones

61A Lecture 4. Monday, September 9

DRAWING ENVIRONMENT DIAGRAMS

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

61A LECTURE 1 FUNCTIONS, VALUES. Steven Tang and Eric Tzeng June 24, 2013

CONTROL AND ENVIRONMENTS 1

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

Lecture #3: Environments

Lecture content. Course goals. Course Introduction. TDDA69 Data and Program Structure Introduction

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

RLISTS AND THE ENVIRONMENT MODEL 9

CS1 Lecture 3 Jan. 18, 2019

CS1 Lecture 3 Jan. 22, 2018

SCHEME AND CALCULATOR 5b

61A Lecture 9. Friday, September 20

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

LECTURE 2. Python Basics

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

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

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

C ONTROL AND H IGHER O RDER F UNCTIONS

CSCA08 Winter 2018 Week 2: Variables & Functions. Marzieh Ahmadzadeh, Brian Harrington University of Toronto Scarborough

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

Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 1

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

CS 61A Control and Environments Spring 2018 Discussion 1: January 24, Control. If statements. Boolean Operators

ENVIRONMENT DIAGRAMS AND RECURSION 2

Environment Diagrams. Administrivia. Agenda Step by Step Environment Diagram Stuff. The Rules. Practice it s on! What are The Rules?

Defining Functions III: Nested Definitions

Midterm Exam 1 Solutions

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

CS61A Notes Week 13: Interpreters

Intro to Python Programming

61A Lecture 6. Friday, September 7

Key Differences Between Python and Java

Introduction to Bioinformatics

Lecture 4: Defining Functions (Ch ) CS 1110 Introduction to Computing Using Python

Statements 2. a operator= b a = a operator b

Introduction and Functions Summer 2018 Discussion 0: June 19, 2018

Recap: Functions as first-class values

MetacircularScheme! (a variant of lisp)

CONTROL AND HIGHER ORDER FUNCTIONS 2

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

S206E Lecture 19, 5/24/2016, Python an overview

Functional Programming. Pure Functional Languages

ENVIRONMENT MODEL: FUNCTIONS, DATA 18

Structure and Interpretation of Computer Programs

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

61A Lecture 19. Wednesday, October 12

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

An introduction to Scheme

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

Lecture #23: Conversion and Type Inference

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

61A Lecture 28. Friday, November 4

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Python in 10 (50) minutes

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

Functional Programming. Pure Functional Languages

CS 314 Principles of Programming Languages

TAIL RECURSION, SCOPE, AND PROJECT 4 11

A Functional Evaluation Model

Chapter 1. Fundamentals of Higher Order Programming

Downloaded from Chapter 2. Functions

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

CS61A Lecture 38. Robert Huang UC Berkeley April 17, 2013

Structure and Interpretation of Computer Programs

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

Introduction to Python (All the Basic Stuff)

Programming Paradigms

CS1 Lecture 4 Jan. 23, 2019

COMP1730/COMP6730 Programming for Scientists. Functions

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

Object Model Comparisons

CS61A Lecture 15. Amir Kamil UC Berkeley February 25, 2013

Functions and abstraction. Ruth Anderson UW CSE 160 Winter 2017

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

Functional programming in LISP

CS1 Lecture 4 Jan. 24, 2018

EXCEPTIONS, CALCULATOR 10

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Beyond Blocks: Python Session #1

CS 31: Intro to Systems C Programming. Kevin Webb Swarthmore College September 13, 2018

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

CS 11 Haskell track: lecture 1

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Programming with Python

Structure and Interpretation of Computer Programs

259 Lecture 25: Simple Programming

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

CS112 Spring 2012 Dr. Kinga Dobolyi. Exam 2. Do not open this exam until you are told. Read these instructions:

CONTROL AND HIGHER ORDER FUNCTIONS 1

Using IDLE for

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

Welcome to CS61A! Last modified: Thu Jan 23 03:58: CS61A: Lecture #1 1

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

Transcription:

61A Lecture 2 Wednesday, September 4, 2013

Names, Assignment, and User-Defined Functions (Demo)

Types of Expressions Primitive expressions: 2 add 'hello' Number or Numeral Name String Call expressions: max ( 2, 3 ) Operator Operand Operand An operand can also be a call expression max(min(pow(3, 5), -4), min(1, -2)) 3

Discussion Question 1 What is the value of the final expression in this sequence? >>> f = min >>> f = max 1 2 >>> g, h = min, max >>> max = g 3 >>> max(f(2, g(h(1, 5), 3)), 4)??? 4 5 4

Environment Diagrams

Environment Diagrams Environment diagrams visualize the interpreter s process. Just executed Import statement Next to execute Assignment statement Name Value Code (left): Statements and expressions Frames (right): Each name is bound to a value Arrows indicate evaluation order Within a frame, a name cannot be repeated (Demo) Example: http://goo.gl/j2w5nl 6

Assignment Statements Just executed Next to execute Just executed Execution rule for assignment statements: 1. Evaluate all expressions to the right of = from left to right. 2. Bind all names to the left of = to the resulting values in the current frame. Example: http://goo.gl/ppn26m 7

Discussion Question 1 Solution (Demo) 3 func min(...) 3 4 f(2, g(h(1, 5), 3)) 3 func max(...) 2 3 g(h(1, 5), 3) func min(...) 5 3 h(1, 5) func max(...) 1 5 Example: http://goo.gl/bglafb 8

Defining Functions

Defining Functions Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions Function signature indicates how many arguments a function takes >>> def <name>(<formal parameters>): return <return expression> Function body defines the computational process expressed by a function Execution procedure for def statements: 1. Create a function with signature <name>(<formal parameters>) 2. Set the body of that function to be everything indented after the first line 3. Bind <name> to that function in the current frame 10

Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): 1. Add a local frame, forming a new environment 2. Bind the function's formal parameters to its arguments in that frame 3. Execute the body of the function in that new environment Built-in function Original name of function called Local frame User-defined function Formal parameter bound to argument Return value (not a binding!) Example: http://goo.gl/gxydcp 11

Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): 1. Add a local frame, forming a new environment 2. Bind the function's formal parameters to its arguments in that frame 3. Execute the body of the function in that new environment A function s signature has all the information needed to create a local frame Example: http://goo.gl/gxydcp 12

Looking Up Names In Environments Every expression is evaluated in the context of an environment. So far, the current environment is either: The global frame alone, or A local frame, followed by the global frame. Most important two things I ll say all day: An environment is a sequence of frames. A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found. E.g., to look up some name in the body of the square function: Look for that name in the local frame. If not found, look for it in the global frame. (Built-in names like max are in the global frame too, but we don t draw them in environment diagrams.) (Demo) 13

The Print Function (Demo)

Pure Functions & Non-Pure Functions Pure Functions just return values -2 Argument abs Return value 2 2, 100 2 Arguments pow 1267650600228229401496703205376 Non-Pure Functions have side effects -2 print None Python displays the output -2 Returns None! A side effect isn't a value; it's anything that happens as a consequence of calling a function Important: The interactive interpreter (>>>) displays the value of an expression, unless it is None 15

Nested Expressions with Print None, None print(...): None Does not get displayed display None None None print(print(1), print(2)) func print(...) None print(1) None print(2) func print(...) 1 func print(...) 2 1 print(...): None 2 print(...): None display 1 display 2 16