61A Lecture 4. Monday, September 9

Similar documents
61A Lecture 3. Friday, September 5

61A Lecture 2. Wednesday, September 4, 2013

Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 1

ENVIRONMENT DIAGRAMS AND RECURSION 2

61A LECTURE 7 DATA ABSTRACTION. Steven Tang and Eric Tzeng July 3, 2013

61A Lecture 9. Friday, September 20

61A Lecture 6. Friday, September 7

Math 152: Applicable Mathematics and Computing

UNIT 2B An Introduction to Programming. Announcements

61A Lecture 2. Friday, August 28, 2015

CONTROL AND HIGHER ORDER FUNCTIONS 2

CS61A Lecture 4 Higher Order Functions. Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012

61A Lecture 7. Monday, September 15

CONTROL AND HIGHER ORDER FUNCTIONS 1

Lecture #5: Higher-Order Functions. A Simple Recursion. Avoiding Recalculation. Redundant Calculation. Tail Recursion and Repetition

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

61A Lecture 7. Monday, September 16

61A Lecture 6. Monday, February 2

ENVIRONMENT MODEL: FUNCTIONS, DATA 18

CS61A Lecture 3 Higher Order Functions. Jon Kotker and Tom Magrino UC Berkeley EECS June 20, 2012

CS1 Lecture 3 Jan. 18, 2019

6.S189 Homework 2. What to turn in. Exercise 3.1 Defining A Function. Exercise 3.2 Math Module.

CONTROL AND ENVIRONMENTS 1

HIGHER-ORDER FUNCTIONS 2

Measuring Efficiency

Using Variables to Write Pattern Rules

Finding Pi: Applications of Loops, Random Numbers, Booleans CS 8: Introduction to Computer Science, Winter 2018 Lecture #6

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

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

ENVIRONMENT DIAGRAMS AND RECURSION 2

Environment Diagrams and Recursion Fall 2017 Discussion 2: September 6, 2017 Solutions. 1 More Environment Diagrams

CS1 Lecture 15 Feb. 18, 2019

General course information

61A LECTURE 8 SEQUENCES, ITERABLES

61A LECTURE 8 SEQUENCES, ITERABLES. Steven Tang and Eric Tzeng July 8, 2013

CSC 101: Lab #5 Prelab Boolean Logic Practice Due Date: 5:00pm, day after lab session

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

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

61A LECTURE 16 TREES, ORDERS OF GROWTH. Steven Tang and Eric Tzeng July 22, 2013

CS1 Lecture 3 Jan. 22, 2018

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

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

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

CS 4800: Algorithms & Data. Lecture 1 January 10, 2017

Lecture 1. Course Overview Types & Expressions

CS101 Lecture 04: Binary Arithmetic

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013

C ONTROL AND H IGHER O RDER F UNCTIONS

Programming Karel the Robot

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

KOMAR UNIVERSITY OF SCIENCE AND TECHNOLOGY (KUST)

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 21 Scheme TODAY REVIEW: DISPATCH DICTIONARIES DISPATCH DICTIONARIES 7/27/2012

Iteration and For Loops

CS1 Recitation. Week 2

UNIT 5B Binary Search

TAIL RECURSION, SCOPE, AND PROJECT 4 11

CS 2316 Individual Homework 4 Greedy Scheduler (Part I) Due: Wednesday, September 18th, before 11:55 PM Out of 100 points

Lecture 02. The "Moves" and Quiz 0

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

ENGR 105: Introduction to Scientific Computing. Dr. Graham. E. Wabiszewski

functional programming in Python, part 2

Analysis of Algorithms & Big-O. CS16: Introduction to Algorithms & Data Structures Spring 2018

HIGHER ORDER FUNCTIONS AND ENVIRONMENT DIAGRAMS 2

6.001 Notes: Section 4.1

CME 193: Introduction to Scientific Python Lecture 1: Introduction

Programming Languages and Techniques (CIS120)

CMSC 201 Fall 2016 Homework 6 Functions

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

DRAWING ENVIRONMENT DIAGRAMS

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

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

What s next? Are you interested in CompTIA A+ classes?

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

Pre-Calculus Advanced. Chapter 10 Part 2. Period 8

CS 1803 Pair Homework 4 Greedy Scheduler (Part I) Due: Wednesday, September 29th, before 6 PM Out of 100 points

61A Lecture 21. Friday, March 13

Outline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression

Calculus (Math 1A) Lecture 1

Haskell Review COMP360

Hog Contest Rules. Fall 2011 Winners Kaylee Mann Yan Duan & Ziming Li Brian Prike & Zhenghao Qian Parker Schuh & Robert Chatham

Accelerating Information Technology Innovation

CSE : Python Programming

CIS192 Python Programming. Robert Rand. August 27, 2015

Writing Functions. Reading: Dawson, Chapter 6. What is a function? Function declaration and parameter passing Return values Objects as parameters

Intro to Contemporary Math

DEC Computer Technology LESSON 6: DATABASES AND WEB SEARCH ENGINES

Seventh Grade Spiraling Review Week 1 of First Six Weeks

MATH 1131Q - Calculus 1.

Introduction to Scientific Computing

Lecture #10: Sequences. Last modified: Mon Feb 22 16:33: CS61A: Lecture #10 1

Unit 2: Functions, Equations, & Graphs of Degree One

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

File I/O in Python CS 8: Introduction to Computer Science Lecture #11

More About WHILE Loops

Programming Languages and Techniques (CIS120)

SCHEME AND CALCULATOR 5b

CS108 Lecture 16: User Defined Classes. Overview/Questions

isinstance and While Loops

CS 101: Computer Programming and Utilization. Abhiram Ranade

Transcription:

61A Lecture 4 Monday, September 9

Announcements Homework 1 due Tuesday 9/10 at 5pm; Late homework is not accepted! Quiz on Wednesday 9/11 released at 1pm, due Thursday 9/12 at 11:59pm Open-computer: You can use the Python interpreter, watch course videos, and read the online text (http://composingprograms.com). No external resources: Please don't search for answers, talk to your classmates, etc. Content Covered: Lectures through last Friday 9/6; Same topics as Homework 1. Project 1 due next Thursday 9/19 at 11:59pm 2

Iteration Example

The Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13 34, 21, 5 34, def fib(n): 55, """Compute the nth Fibonacci number, for n >= 2.""" predecessor, current = 0, 1 k = 2 21 3 1 1 2 8 89, 14 4, # First two Fibonacci numbers 23 3, 37 # Tracks which Fibonacci number is called current 7, 61 0, 13 98 7 while k < n: predecessor, current = current, predecessor + current k = k + 1 return current The next Fibonacci number is the sum of the current one and its predecessor Example: http://goo.gl/vfymhd 4

Discussion Question Complete the following definition by placing an expression in. def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) 10 >>> choose(20, 6) 38760 """ ways = 1 selected = 0 while selected < selection: selected = selected + 1 n (n 1) (n 2)... (n k + 1) k (k 1) (k 2)... 2 1 ways, total = ways *, total // selected total - 1 return ways...... Example: http://goo.gl/38ch3o 5

Default Arguments (Demo)

Designing Functions

Characteristics of Functions def square(x): """Return X * X.""" def choose(n, d): """Return the number of ways to choose D of N items.""" A function's domain is the set of all inputs it might possibly take as arguments. x is a number n and d are positive integers with n greater than or equal to d. A function's range is the set of output values it might possibly return. return value is a positive number return value is a positive integer A pure function's behavior is the relationship it creates between input and output. return value is the square of the input return value is the number of ways to choose d of n items. 8

A Guide to Designing Function Give each function exactly one job. not Don t repeat yourself (DRY). Implement a process just once, but execute it many times. Define functions generally. 9

Generalization

Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r r Area: r 2 3 p 3 1 r 2 2 r 2 Finding common structure allows for shared implementation (Demo) 11

Higher-Order Functions

Generalizing Over Computational Processes The common structure among functions may be a computational process, rather than a number. 5X k = 1 + 2 + 3 + 4 + 5 = 15 k=1 5X k 3 =1 3 +2 3 +3 3 +4 3 +5 3 = 225 k=1 5X k=1 8 (4k 3) (4k 1) = 8 3 + 8 35 + 8 99 + 8 195 + 8 323 =3.04 (Demo) 13

Summation Example def cube(k): return pow(k, 3) def summation(n, term): be bound to a function """Sum the first n terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: Function of a single argument (not called term) A formal parameter that will total, k = total + term(k), k + 1 return total 0 + 1 3 + 2 3 + 3 3 + 4 3 + 5 3 The cube function is passed as an argument value The function bound to term gets called here 14

Functions as Return Values (Demo)

Locally Defined Functions Functions defined within other function bodies are bound to names in a local frame A function that returns a function def make_adder(n): """Return a function that takes one argument k and returns k + n. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return k + n return adder A local def statement Can refer to names in the enclosing function The name add_three is bound to a function 16

Call Expressions as Operator Expressions An expression that evaluates to a function An expression that evaluates to any value Operator Operand 3 make_adder(1) ( 2 ) func adder(k) make_adder(1) func make_adder(n) 1 2 def make_adder(n): def adder(k): return k + n return adder 17

The Purpose of Higher-Order Functions Functions are first-class: Functions can be manipulated as values in our programming language. Higher-order function: A function that takes a function as an argument value or returns a function as a return value Higher-order functions: Express general methods of computation Remove repetition from programs Separate concerns among functions 18

The Game of Hog (Demo)