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

Slides:



Advertisements
Similar presentations
CS61A Lecture 24 Infinite Sequences
Advertisements

CS61A Lecture 25 Delayed Sequences Jom Magrotker UC Berkeley EECS July 31, 2012.
CS61A Lecture 8 Data Abstraction Tom Magrino and Jon Kotker UC Berkeley EECS June 28, 2012.
Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Lecture 1: Comments, Variables, Assignment. Definitions The formal (human-readable) instructions that we give to the computer is called source code The.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 2.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Functions in Python. The indentation matters… First line with less indentation is considered to be outside of the function definition. Defining Functions.
CS61A Lecture 14 Object-Oriented Programming Jom Magrotker UC Berkeley EECS July 11, 2012.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
CS61A Lecture 2 Functions and Applicative Model of Computation Tom Magrino and Jon Kotker UC Berkeley EECS June 19, 2012.
CS61A Lecture 3 Higher Order Functions Jon Kotker and Tom Magrino UC Berkeley EECS June 20, 2012.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
CS61A Lecture 21 Scheme Jom Magrotker UC Berkeley EECS July 24, 2012.
CS61A Lecture 16 Mutable Data Structures Jom Magrotker UC Berkeley EECS July 16, 2012.
CS61A Lecture 15 Object-Oriented Programming, Mutable Data Structures Jom Magrotker UC Berkeley EECS July 12, 2012.
CPS120 Introduction to Computer Science Iteration (Looping)
CS61A Lecture 22 Interpretation Jom Magrotker UC Berkeley EECS July 25, 2012.
CS61A Lecture 6 Recursion Tom Magrino and Jon Kotker UC Berkeley EECS June 26, 2012.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CS61A Lecture 13 Object-Oriented Programming Jom Magrotker UC Berkeley EECS July 10, 2012.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CSC 107 – Programming For Science. The Week’s Goal.
CS61A Lecture 12 Immutable Trees Jom Magrotker UC Berkeley EECS July 9, 2012.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
David Stotts Computer Science Department UNC Chapel Hill.
CS61A Lecture 23 Py Jom Magrotker UC Berkeley EECS July 26, 2012.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
CS61A Lecture 27 Logic Programming Jom Magrotker UC Berkeley EECS August 2, 2012.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
CS61A Lecture 20 Object-Oriented Programming: Implementation Jom Magrotker UC Berkeley EECS July 23, 2012.
Python Let’s get started!.
CS61A Lecture 9 Immutable Data Structures Jom Magrotker UC Berkeley EECS July 2, 2012.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Dale Roberts Introduction to Java - Input, Program Control and Instantiation Dale Roberts, Lecturer Computer Science, IUPUI
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
CS61A Lecture 10 Immutable Data Structures Jom Magrotker UC Berkeley EECS July 3, 2012.
CS61A Lecture 11 Immutable Trees Jom Magrotker UC Berkeley EECS July 5, 2012.
CS61A Lecture 1 Introduction and Basics Tom Magrino and Jon Kotker UC Berkeley EECS June 18, 2012.
COSC 1223 Computer Science Concepts I Joe Bryan. What is a function? Like a mini-program that performs a specific task Two types: Built-in functions Functions.
Java I--Copyright © Tom Hunter. Chapter 4 Control Structures: Part I.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
CS61A Lecture 29 Parallel Computing Jom Magrotker UC Berkeley EECS August 7, 2012.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Functional Programming in Python Abhishek Dasgupta Indian Institute of Science Education and Research, Kolkata.
Control Flow (Python) Dr. José M. Reyes Álamo.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Python Let’s get started!.
Functions and Procedures
Introduction to Programming Using Python PART 2
Introduction to Decision Structures and Boolean Variables
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Lambda Functions, MapReduce and List Comprehensions
Lecture 2 - Names & Functions
Lecture 6 - Recursion.
Presentation transcript:

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

2 C OMPUTER S CIENCE IN THE N EWS

3 Write the function filtered_count that takes a number, n, and a function, pred*, and prints all the numbers 1 to n for which pred returns True. def filtered_count(n, pred): k = 1 while k <= n: if pred(k): print(k) k = k + 1 R EVIEW * pred stands for predicate, a function that only returns True or False.

4 Write the function filtered_count that takes a number, n, and a function, pred*, and prints all the numbers 1 to n for which pred returns True. def filtered_count(n, pred): k = 1 while k <= n: if pred(k): print(k) k = k + 1 R EVIEW * pred stands for predicate, a function that only returns True or False.

5 T ODAY Defining functions inside functions. Returning functions from functions. A Friendly Greek Letter.

6 L OCAL (H ELPER ) F UNCTIONS

7 Locally defined “helper” functions help us avoid repetition!

8 L OCAL (H ELPER ) F UNCTIONS How does this work? def funny_function(a, b, c): def a_funny(b): return (a * b) + (b / a) + (a ** b) return a_funny(b) / a_funny(c) funny_function(2, 4, 6)

9 L OCAL (H ELPER ) F UNCTIONS How does this work? def funny_function(a, b, c): def a_funny(b): return (a * b) + (b / a) + (a ** b) return a_funny(b) / a_funny(c) funny_function(2, 4, 6)

10 L OCAL (H ELPER ) F UNCTIONS How does this work? def funny_function(2, b, c): def a_funny(b): return (2 * b) + (b / 2) + (2 ** b) return a_funny(b) / a_funny(c) funny_function(2, 4, 6)

11 L OCAL (H ELPER ) F UNCTIONS How does this work? def funny_function(2, b, c): def a_funny(b): return (2 * b) + (b / 2) + (2 ** b) return a_funny(b) / a_funny(c) funny_function(2, 4, 6)

12 L OCAL (H ELPER ) F UNCTIONS How does this work? def funny_function(2, 4, 6): def a_funny(b): return (2 * b) + (b / 2) + (2 ** b) return a_funny(4) / a_funny(6) funny_function(2, 4, 6)

13 L OCAL (H ELPER ) F UNCTIONS How does this work? def funny_function(2, 4, 6): def a_funny(b): return (2 * b) + (b / 2) + (2 ** b) return a_funny(4) / a_funny(6) funny_function(2, 4, 6)

14 L OCAL (H ELPER ) F UNCTIONS How does this work? def funny_function(2, 4, 6): def a_funny(b): return (2 * b) + (b / 2) + (2 ** b) return a_funny(4) / a_funny(6) funny_function(2, 4, 6)

15 L OCAL (H ELPER ) F UNCTIONS How does this work? def funny_function(2, 4, 6): def a_funny(4): return (2 * 4) + (4 / 2) + (2 ** 4) return a_funny(4) / a_funny(6) funny_function(2, 4, 6) 26

16 L OCAL (H ELPER ) F UNCTIONS How does this work? def funny_function(2, 4, 6): def a_funny(b): return (2 * b) + (b / 2) + (2 ** b) return a_funny(4) / a_funny(6) funny_function(2, 4, 6) 26

17 L OCAL (H ELPER ) F UNCTIONS How does this work? def funny_function(2, 4, 6): def a_funny(6): return (2 * 6) + (6 / 2) + (2 ** 6) return a_funny(4) / a_funny(6) funny_function(2, 4, 6) 26 79

18 L OCAL (H ELPER ) F UNCTIONS How does this work? def funny_function(2, 4, 6): def a_funny(b): return (2 * b) + (b / 2) + (2 ** b) return a_funny(4) / a_funny(6) funny_function(2, 4, 6) …

19 A NNOUNCEMENTS You will be placed into study/exam groups in discussion section today. – your TA ASAP if you can’t make section so they can assign you a group. Homework 1 is due tomorrow night. Homework 2 is out and due Tuesday night. Project 1 is out and due the night of 6/29.

20 P ROBLEM : M AKING A DDERS def add_1_to_twice(n): return (2*n)+1 def add_2_to_twice(n): return (2*n)+2 def add_3_to_twice(n): return (2*n)+3... def add_65536_to_twice(n): return (2*n) def add_65537_to_twice(n): return (2*n)

21 P ROBLEM : M AKING A DDERS def add_1_to_twice(n): return (2*n)+1 def add_2_to_twice(n): return (2*n)+2 def add_3_to_twice(n): return (2*n)+3... def add_65536_to_twice(n): return (2*n) def add_65537_to_twice(n): return (2*n) There has to be a better way!

22 P ROBLEM : M AKING A DDERS def add_1_to_twice(n): return (2*n)+1 def add_2_to_twice(n): return (2*n)+2 def add_3_to_twice(n): return (2*n)+3... def add_65536_to_twice(n): return (2*n) def add_65537_to_twice(n): return (2*n) Can we generalize?

23 P ROBLEM : M AKING A DDERS

24 P ROBLEM : M AKING A DDERS def make_adder_for(m): def add_m_to_twice(n): return (2*n)+m return add_m_to_twice add_1_to_twice = make_adder_for(1) add_2_to_twice = make_adder_for(2) add_3_to_twice = make_adder_for(3)... add_65536_to_twice = make_adder_for(65536) add_65537_to_twice = make_adder_for(65537)...

25 make_adder_for(m): return add_m_to_twice P ROBLEM : M AKING A DDERS def make_adder_for(m): def add_m_to_twice(n): return (2*n)+m return add_m_to_twice add_m_to_twice(n): return (2 * n) + m 3 add_m_to_twice(n): return (2 * n) + 3

26 I want to make specialized sums for later use. Complete the definition below: def make_summationer(term): def summation(n): k, sum = 1, 0 while k <= n: sum = sum + term(k) k = k + 1 return sum return summation P RACTICE : M AKING S UMMATION... ERS

27 I want to make specialized sums for later use. Complete the definition below: def make_summationer(term): def summation(n): k, sum = 1, 0 while k <= n: sum = sum + term(k) k = k + 1 return sum return summation P RACTICE : M AKING S UMMATION... ERS

28 P RACTICE : M AKING A DDERS FOR S UMMATIONS Say I wanted to get the sum of the numbers 4 through 17. How can I do this in one line using summation and make_adder ? def make_adder(n): def add_n(m): return n + m return add_n def summation(n, term): sum, k = 0, 1 while k <= n: sum = sum + term(k) k = k + 1 return sum >>> summation(14, make_adder(3)) 147 ??!?!?!??!?!

29 P RACTICE : M AKING A DDERS FOR S UMMATIONS Say I wanted to get the sum of the numbers 4 through 17. How can I do this in one line using summation and make_adder ? def make_adder(n): def add_n(m): return n + m return add_n def summation(n, term): sum, k = 0, 1 while k <= n: sum = sum + term(k) k = k + 1 return sum >>> summation(14, make_adder(3)) 147 “k + 3”

30 A NONYMOUS F UNCTIONS What if we don’t want to give a name to our function? Seems silly that we have to come up with a name that we’re only using once in our program (in the return statement). def make_adder(n): def add_n(m): return n + m return add_n Can I do the same thing without providing a name? – Yes!

31 A NONYMOUS F UNCTIONS In Computer Science, we traditionally call anonymous function values “lambda functions.” In Python a lambda function has the form: lambda : For example we could have done: def make_adder(n): return lambda m: n + m λ

32 A NONYMOUS F UNCTIONS Translating between lambdas and defined functions. λ = lambda : def ( ): return

33 A NONYMOUS F UNCTIONS Translating between lambdas and defined functions. λ square = lambda x: x * x def square(x): return x*x

34 A NONYMOUS F UNCTIONS Translating between lambdas and defined functions. λ foo = lambda a, x, b: (a * x) + b def foo(a, x, b): return (a * x) + b

35 A NONYMOUS F UNCTIONS Translating between lambdas and defined functions. λ def make_adder(n): return lambda m: m + n def make_adder(n): def add_n(m): return m + n return add_n

36 A NONYMOUS F UNCTIONS Key points about lambda in Python: – A lambda in Python is an expression, so you can use them anywhere you can use any other expression in Python. – You can only use a single expression in the body of a lambda function. – A lambda function always returns the value of the body expression. – Lambdas should be used sparingly, only for very simple functions. Otherwise the code can quickly become unreadable. λ

37 P RACTICE : A NONYMOUS F UNCTIONS What would the following expression evaluate to? >>> summation(3, lambda x: x + 5) 21

38 P RACTICE : A NONYMOUS F UNCTIONS What would the following expression evaluate to? >>> summation(3, lambda x: x + 5) 21

39 C ONCLUSION Functions can be defined inside other functions! Functions can return functions. Python has lambda functions for creating anonymous functions. Next Time: More practice with HOFs, and practical applications!

40 E XTRAS : M AKING M Y A DDER AND U SING I T T OO ! We can add 3 and 5, using only the values make_adder, 3, and 5. make_adder(3) (5) 5 make_adder(3) 3 8 make_adder add_n* * Where n is equal to 3