EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

Similar documents
EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

CS61A Notes Week 1A: Basics, order of evaluation, special forms, recursion

61A Lecture 2. Wednesday, September 4, 2013

CS61A Notes Week 3: Environments and Higher Order Functions

CONTROL AND ENVIRONMENTS 1

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

C ONTROL AND H IGHER O RDER F UNCTIONS

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

61A Lecture 2. Friday, August 28, 2015

61A Lecture 3. Friday, September 5

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

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

SCHEME AND CALCULATOR 5b

ENVIRONMENT DIAGRAMS AND RECURSION 2

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Lecture 4. Defining Functions

CS1 Lecture 3 Jan. 18, 2019

ENVIRONMENT MODEL: FUNCTIONS, DATA 18

CS61A Notes Week 13: Interpreters

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

CS1 Lecture 3 Jan. 22, 2018

Lecture #3: Environments

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

Lecture 4. Defining Functions

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

CSE : Python Programming

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

61A Lecture 4. Monday, September 9

61A Lecture 9. Friday, September 20

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

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

UNIVERSITY of CALIFORNIA at Berkeley Department of Electrical Engineering and Computer Sciences Computer Sciences Division

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

Discussion 4. Data Abstraction and Sequences

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

Computer Science 21b: Structure and Interpretation of Computer Programs (Spring Term, 2004)

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

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

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

CS61A Lecture 1. Amir Kamil UC Berkeley January 23, 2013

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

Lecture 5. Defining Functions

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Python for C programmers

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

Ruby: Introduction, Basics

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

CIS 110: Introduction to Computer Programming

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

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

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

HIGHER ORDER FUNCTIONS 2

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

CPS122 Lecture: From Python to Java

CSC 443: Web Programming

6.001 Notes: Section 15.1

Racket Style Guide Fall 2017

DRAWING ENVIRONMENT DIAGRAMS

CMSC 201 Fall 2018 Lab 04 While Loops

CS 11 python track: lecture 3. n Today: Useful coding idioms

APPM 2460 Matlab Basics

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Lecture 1. Course Overview Types & Expressions

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Lecture 2: SML Basics

CS 1110, LAB 2: ASSIGNMENTS AND STRINGS

Downloaded from Chapter 2. Functions

Programming Project #4: A Logo Interpreter Summer 2005

COSC2031 Software Tools Introduction to C


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

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

CS 241 Data Organization. August 21, 2018

Java Programming. Computer Science 112

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

CMSC 201 Computer Science I for Majors

You must pass the final exam to pass the course.

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

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

Ruby: Introduction, Basics

Object-Oriented Programming

Java Programming. Computer Science 112

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

CS 11 Haskell track: lecture 1

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

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

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

Overview of the Ruby Language. By Ron Haley

Execution order. main()

Lecture 7: Primitive Recursion is Turing Computable. Michael Beeson

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

CS/ENGRD 2110 FALL 2018

PROGRAMMING FUNDAMENTALS

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

CONTROL AND HIGHER ORDER FUNCTIONS 2

Lists, loops and decisions

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

Transcription:

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1 COMPUTER SCIENCE 61A September 4, 2013 0.1 Warmup What Would Python Do? >>> x = 6 >>> def square(x):... return x * x >>> square(x) >>> max(pow(2, 3), square(-5)) - square(4) Expressions describe a computation and evaluate to a value. 1 Expressions 1.1 Primitive Expressions A primitive expression is a single evaluation step: you either look up the value of a name, or take the literal value. For example, numbers, variable names, and strings are all primitive expressions. >>> 2 2 >>> Hello World! Hello World! 1

DISCUSSION 1: EXPRESSIONS, STATEMENTS, AND FUNCTIONS Page 2 1.2 Call Expressions Call expressions are expressions that involve a call to some function. Call expressions are just another type of expression, called a compound expression. A call expression invokes a function, which may or may not accept arguments, and returns the function s return value. Recall the syntax of a function call: Every call expression is required to have a set of parentheses delimiting its comma-separated operands. To evaluate a function call: 1. First evaluate the operator, and then the operands (from left to right). 2. Apply the function (the value of the operator) to the arguments (the values of the operands). If the operands are nested function calls, apply the two steps to the operands. 1.3 Questions 1. Determine the result of evaluating f(4) in the Python interpreter if the following functions are defined: from operator import add def double(x): return x + x def square(y): return y * y def f(z): add(square(double(z)), 1)

DISCUSSION 1: EXPRESSIONS, STATEMENTS, AND FUNCTIONS Page 3 2. What is the result of evaluating the following code? from operator import add def square(x): return x * x def so_slow(num): return num num/0 square(so_slow(5)) 3. What will python print? def f(): print( f ) return print def a(): print( a ) return "hello" def b(): print( b ) return "world" f()(a(), b())

DISCUSSION 1: EXPRESSIONS, STATEMENTS, AND FUNCTIONS Page 4 2 Statements A statement in Python is executed by the interpreter to achieve an effect. For example, we can talk about an assignment statement. In an assignment statement, we ask Python to assign a certain value to a variable name. There are also compound statements but we will cover them later! We can assign values to names using the = operator Every assignment will assign the value on the right of the = operator to the name on the left. For example: >>> foo = 5 Binds the value 5 to the name foo. We can also bind values to names using the import statement. >>> from operator import add Will bind the name add to the add function. Function definitions also bind the name of the function to that function. >>> def foo():... return 0 Will bind foo the function defined above Assignments are also not permanent, if two values are bound to the same name only the assignment executed last will remain. >>> add = 5 >>> from operator import add After the above two statements are executed the value 5 will no longer be bound to add but rather the add function from operator will.

DISCUSSION 1: EXPRESSIONS, STATEMENTS, AND FUNCTIONS Page 5 2.1 Questions 1. Determine the result of evaluating f oo(5) in the Python interpreter if the following functions are defined in the order below: >>> def bar(param):... return param >>> bar = 6 >>> def foo(bar):... return bar(bar) 2. Determine the result of evaluating f oo(5) in the Python interpreter if the following functions are defined in the order below: >>> def bar(param):... return param >>> zoo = bar >>> bar = 6 >>> def foo(bar):... return zoo(bar) 3. What would python print? >>> from operator import add >>> def sub(a, b):... sub = add... return a - b >>> add = sub >>> sub = min >>> print(add(2, sub(2, 3)))

DISCUSSION 1: EXPRESSIONS, STATEMENTS, AND FUNCTIONS Page 6 3 Pure and Non-Pure Functions Pure function It only produces a return value (no side effects), and always evaluates to the same result, given the same argument value(s). Non-Pure function It produces side effects, such as printing to the screen. Further in the semester, we will further expand on the notion of a pure function versus a non-pure function. 3.1 One Moar Question 1. What do you think Python will print for the following? om and nom are defined as follows: >>> def om(foo):... return -foo >>> def nom(foo):... print(foo) >>> nom(4) >>> om(-4) >>> save1 = nom(4) >>> save1 + 1 >>> save2 = om(-4) >>> save2 + 1

DISCUSSION 1: EXPRESSIONS, STATEMENTS, AND FUNCTIONS Page 7 4 An Environment Diagram 1. Draw the environment diagram that results from running the following code. n = 7 def f(x): n = 8 return x+1 def g(x): n = 9 return x + 3 def square_result(f, x): return f(x) * f(x + 2) m = square_result(g, n)

DISCUSSION 1: EXPRESSIONS, STATEMENTS, AND FUNCTIONS Page 8 5 Secrets to Success in CS61A CS61A is definitely a challenge, but we all want you to learn and succeed, so here are a few tips that might help: Ask questions. When you encounter something you dont know, ask. That is what we are here for. This is not to say you should raise your hand impulsively; some usage of the brain first is preferred. You are going to see a lot of challenging stuff in this class, and you can always come to us for help. Go to office hours. Office hours give you time with the instructor or TAs by themselves, and you will be able to get some (nearly) one-on-one instruction to clear up confusion. You are not intruding; the instructors and TAs like to teach! Remember that, if you cannot make office hours, you can always make separate appointments with us! Do the readings (on time!). There is a reason why they are assigned. And it is not because we are evil; that is only partially true. Do (or at least attempt seriously) all the homework. We do not give many homework problems, but those we do give are challenging, time-consuming, and rewarding. The fact that homework is graded on effort does not imply that you should ignore it: it will be one of your primary sources of preparation and understanding. Do all the lab exercises. Most of them are simple and take no more than an hour or two. This is a great time to get acquainted with new material. If you do not finish, work on it at home, and come to office hours if you need more guidance! Study in groups. Again, this class is not trivial; you might feel overwhelmed going at it alone. Work with someone, either on homework, on lab, or for midterms, as long as you don t violate the cheating policy! Most importantly, have fun!