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

Slides:



Advertisements
Similar presentations
CS61A Lecture 25 Delayed Sequences Jom Magrotker UC Berkeley EECS July 31, 2012.
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
CS61A Lecture 8 Data Abstraction Tom Magrino and Jon Kotker UC Berkeley EECS June 28, 2012.
 Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains.
CS61A Lecture 4 Higher Order Functions Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012.
CSC 160 Computer Programming for Non-Majors Lecture #3a: Stepper, Words, Sentences Prof. Adam M. Wittenstein
Multiplying, Dividing, and Simplifying Radicals
CS61A Lecture 14 Object-Oriented Programming Jom Magrotker UC Berkeley EECS July 11, 2012.
CS61A Lecture 2 Functions and Applicative Model of Computation Tom Magrino and Jon Kotker UC Berkeley EECS June 19, 2012.
Genetic Programming.
Calculus With Tech I Instructor: Dr. Chekad Sarami.
List Comprehensions. Python’s higher-order functions  Python supports higher-order functions that operate on lists similar to Scheme’s >>> def square(x):
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
Testing CSE 140 University of Washington 1. Testing Programming to analyze data is powerful It’s useless if the results are not correct Correctness is.
CS61A Lecture 21 Scheme Jom Magrotker UC Berkeley EECS July 24, 2012.
CS61A Lecture 15 Object-Oriented Programming, Mutable Data Structures Jom Magrotker UC Berkeley EECS July 12, 2012.
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.
Radical Expressions MATH 018 Combined Algebra S. Rook.
CS61A Lecture 13 Object-Oriented Programming Jom Magrotker UC Berkeley EECS July 10, 2012.
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS Introduction to Computing for the Physical Sciences.
PLAY!!! $100 Order of Operations In Out Tables Summation Notation +/- AND x/÷ Integers Explain and Justify (MUHAHA) $100 $200 $300 $400 $500 $100 Final.
CS61A Lecture 12 Immutable Trees Jom Magrotker UC Berkeley EECS July 9, 2012.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Xin Liu Feb 4, * Use midterm questions for previous 231/217 midterms for practices * ams
Section 2.1 Functions. A relation is a correspondence between two sets. If x and y are two elements in these sets and if a relation exists between x.
Functions Section 1.4. Relation The value of one variable is related to the value of a second variable A correspondence between two sets If x and y are.
Sullivan Algebra & Trigonometry: Section 3.1 Functions Objectives Determine Whether a Relation Represents a Function Find the Value of a Function Find.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CS61A Lecture 23 Py Jom Magrotker UC Berkeley EECS July 26, 2012.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CS61A Lecture 20 Object-Oriented Programming: Implementation Jom Magrotker UC Berkeley EECS July 23, 2012.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Bell Work: Find the equation of the line that passes through the point (2,3) and has a slope of -2/5.
Testing CSE 160 University of Washington 1. Testing Programming to analyze data is powerful It’s useless (or worse!) if the results are not correct Correctness.
CS61A Lecture 9 Immutable Data Structures Jom Magrotker UC Berkeley EECS July 2, 2012.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Spring 2008Programming Development Techniques 1 Topic 5.5 Higher Order Procedures (This goes back and picks up section 1.3 and then sections in Chapter.
Section 3.1 Functions. OBJECTIVE 1 A relation is a correspondence between two sets. If x and y are two elements in these sets and if a relation exists.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
Machine Language Computer languages cannot be directly interpreted by the computer – they are not in binary. All commands need to be translated into binary.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
UC Berkeley EECS Sr Lecturer SOE Dan Garcia Valve (video game makers of Half-Life) believes the future of video games may not be in the input device (ala.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
5. Function (2) and Exercises
Testing UW CSE 160 Winter 2017.
Do Now: Can you input all real numbers into the x variable in the following functions? If not what numbers can x not take on?
Sullivan Algebra & Trigonometry: Section 3.1 Functions
7.3 – Multiplying and Simplifying Radical Expressions
PROGRAMMING IN HASKELL
Testing UW CSE 160 Winter 2016.
Domain, Range, Maximum and Minimum
Section 3.1 Functions Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall.
Section 2.1 Functions Copyright © 2013 Pearson Education, Inc. All rights reserved.
Loops CIS 40 – Introduction to Programming in Python
Structured Programming Taken from notes by Dr. Neil Moore
Introduction to Value-Returning Functions: Generating Random Numbers
functions: argument, return value
X values y values. x values y values Domain Range.
Introduction to the Lab
Section 3.1 Functions.
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Lecture 2 - Names & Functions
Presentation transcript:

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

2 C OMPUTER S CIENCE IN THE N EWS The (Literal) Evolution of Music Scientists from Imperial College London created a computer program powered by Darwinian natural selection. Theory that cultural changes in language, art, and music evolve like living things do, since consumers choose what is “popular”. Program would produce loops of random sounds and analyze opinions of musical consumers. The top loops were then “mated”. “DarwinTunes” (darwintunes.org) has evolved through at least 2513 generations. Source:

3 R EVIEW def foo(n): k = 2 while k < n: if n % k == 0: return False k = k + 1 return True foo(7)? foo(9)? foo(1)? foo is a metasyntactic variable, or a commonly used nonsense name to denote an arbitary or temporary thing. Other examples you may see are bar, garply, and baz. Sometimes, om and nom. Or even herp and derp. This deserves a better name though. Any suggestions? foo is a metasyntactic variable, or a commonly used nonsense name to denote an arbitary or temporary thing. Other examples you may see are bar, garply, and baz. Sometimes, om and nom. Or even herp and derp. This deserves a better name though. Any suggestions? TRIVIA

4 T ODAY Domain and range of functions... and feeding functions to functions

5 D OMAIN AND R ANGE square(n): return n*n Domain is the set of values that the function is defined for. Range (or image) is the set of values that the function returns. Domain of square : All real numbers. Range of square : All non-negative real numbers.

6 D OMAIN AND R ANGE Remember the domain and range of the functions that you are writing and using! Many bugs arise because programmers forget what a function can and cannot work with. The domain and range are especially important when working with higher order functions.

7 P ROBLEM : F INDING S QUARES >>> square_of_0 = 0*0 >>> square_of_1 = 1*1 >>> square_of_2 = 2*2 >>> square_of_3 = 3*3... >>> square_of_65536 = 65536*65536 >>> square_of_65537 = 65537*

8 P ROBLEM : F INDING S QUARES >>> square_of_0 = 0*0 >>> square_of_1 = 1*1 >>> square_of_2 = 2*2 >>> square_of_3 = 3*3... >>> square_of_65536 = 65536*65536 >>> square_of_65537 = 65537* There has to be a better way!

9 P ROBLEM : F INDING S QUARES >>> square_of_0 = 0*0 >>> square_of_1 = 1*1 >>> square_of_2 = 2*2 >>> square_of_3 = 3*3... >>> square_of_65536 = 65536*65536 >>> square_of_65537 = 65537* Can we generalize?

10 F INDING S QUARES : G ENERALIZATION def square(n): return n * n THE SQUARE OF A NUMBER IS THE NUMBER MULTIPLIED BY ITSELF

11 P ROBLEM : S UMS OF S ERIES def sum_of_n_squares(n): ‘‘‘Returns n 2.’’’ sum, k = 0, 1 while k <= n: sum, k = sum + square(k), k + 1 return sum def sum_of_n_cubes(n): ‘‘‘Returns n 3.’’’ sum, k = 0, 1 while k <= n: sum, k = sum + cube(k), k + 1 return sum

12 P ROBLEM : S UMS OF S ERIES from math import sin, sqrt def sum_of_n_sines(n): ‘‘‘Returns sin(1) + sin(2) + sin(3) sin(n).’’’ sum, k = 0, 1 while k <= n: sum, k = sum + sin(k), k + 1 return sum def sum_of_n_sqrts(n): ‘‘‘Returns sqrt(1) + sqrt(2) sqrt(n).’’’ sum, k = 0, 1 while k <= n: sum, k = sum + sqrt(k), k + 1 return sum and so on…

13 P ROBLEM : S UMS OF S ERIES and so on… from math import sin, sqrt def sum_of_n_sines(n): ‘‘‘Returns sin(1) + sin(2) + sin(3) sin(n).’’’ sum, k = 0, 1 while k <= n: sum, k = sum + sin(k), k + 1 return sum def sum_of_n_sqrts(n): ‘‘‘Returns sqrt(1) + sqrt(2) sqrt(n).’’’ sum, k = 0, 1 while k <= n: sum, k = sum + sqrt(k), k + 1 return sum There has to be a better way!

14 P ROBLEM : S UMS OF S ERIES def sum_of_n_squares(n): sum, k = 0, 1 while k <= n: sum = sum + square(k) k = k + 1 return sum def sum_of_n_cubes(n): sum, k = 0, 1 while k <= n: sum = sum + cube(k) k = k + 1 return sum def sum_of_n_sines(n): sum, k = 0, 1 while k <= n: sum = sum + sin(k) k = k + 1 return sum def sum_of_n_sqrts(n): sum, k = 0, 1 while k <= n: sum = sum + sqrt(k) k = k + 1 return sum Can we generalize?

15 A NNOUNCEMENTS HW2 is released, due Tuesday, June 26. Discussions earlier held in 320 Soda will now be held in 310 Soda for the next six weeks. We will move back to 320 Soda for the last two weeks. Groups for studying and midterms will be assembled on Thursday, June 21 in discussion section.

16 E VALUATION OF P RIMITIVE E XPRESSIONS >>> 3 3 >>> x = 5 >>> x 5 >>> ‘CS61A’ ‘CS61A’ evaluates to Statement

17 F UNCTION N AMES >>> square evaluates to square(n): return n*n Python’s representation of square is the name of the function, or “machine”, that squares its input Address where the function is stored in the computer. It is not fixed.

18 F UNCTION N AMES >>> square >>> cube >>> max Function names are primitive expressions that evaluate to the corresponding “machines”

19 U SING P RIMITIVE E XPRESSIONS We have seen primitive expressions used as arguments to functions: >>> square(3) 9 >>> x = 4 >>> square(x) 16 Can we then use function names as arguments?

20

21 P ROBLEM : S UMS OF S ERIES def sum_of_n_squares(n): sum, k = 0, 1 while k <= n: sum = sum + square(k) k = k + 1 return sum def sum_of_n_cubes(n): sum, k = 0, 1 while k <= n: sum = sum + cube(k) k = k + 1 return sum def sum_of_n_sines(n): sum, k = 0, 1 while k <= n: sum = sum + sin(k) k = k + 1 return sum def sum_of_n_sqrts(n): sum, k = 0, 1 while k <= n: sum = sum + sqrt(k) k = k + 1 return sum

22 F UNCTIONS AS A RGUMENTS def summation(n, term): sum, k = 0, 1 while k <= n: sum = sum + term(k) k = k + 1 return sum def sum_of_n_squares(n): return summation(n, square) term(1) + term(2) term(n)

23 F UNCTIONS AS A RGUMENTS def summation(n, term): sum, k = 0, 1 while k <= n: sum = sum + term(k) k = k + 1 return sum def sum_of_n_squares(n): return summation(n, square) square

24 F UNCTIONS AS A RGUMENTS square(n): return n*n 3 9 sum, k = 0, 1 while k <= n: sum = sum + term(k) k = k + 1 return sum summation(n, term): square(n): return n*n 4, 27 summation takes a function as an argument, and is thus a higher order function.

25 H IGHER O RDER F UNCTIONS

26 H IGHER O RDER F UNCTIONS : P RACTICE def summation(n, term): sum, k = 0, 1 while k <= n: sum = sum + term(k) k = k + 1 return sum def sum_of_n_cubes(n): return summation(n, _______________) def sum_of_n_sines(n): return summation(n, _______________) def sum_of_n_positive_ints(n): return summation(n, _______________)

27 H IGHER O RDER F UNCTIONS : P RACTICE def summation(n, term): sum, k = 0, 1 while k <= n: sum = sum + term(k) k = k + 1 return sum def sum_of_n_cubes(n): return summation(n, lambda x: x ** 3) def sum_of_n_sines(n): return summation(n, sin) def sum_of_n_positive_ints(n): return summation(n, lambda x: x)

28 H IGHER O RDER F UNCTIONS Functions that can take other functions as arguments are considered higher order functions. The domains of these functions now include other functions. Functions can be treated as data!

29 H IGHER O RDER F UNCTIONS : A SIDE Why “higher order”? First-order functions only take non-function values, like numbers and strings, as arguments. Second-order functions can take first-order functions as arguments. Third-order functions can take second-order functions as arguments. … and so on.

30 HOF S E LSEWHERE Integral ∫ is a function that takes three arguments: lower limit a, upper limit b, and a function f Integral ∫ is a function that takes three arguments: lower limit a, upper limit b, and a function f

31 HOF S E LSEWHERE Food YOU (K IND OF.) are a function (K IND OF.) Recipe (Function) Ingredients +

32 HOF S E LSEWHERE Basketball NOUN (Data) Playing VERB (Function) Interests: Playing Basketball NOUN (“Gerund”) (Function as data)

33 F UNCTIONS AS A RGUMENTS def summation2(n, term, next): sum, k = 0, 1 while k <= n: sum = sum + term(k) k = next(k) return sum

34 F UNCTIONS AS A RGUMENTS : P RACTICE def summation2(n, term, next): sum, k = 0, 1 while k <= n: sum = sum + term(k) k = next(k) return sum def summation(n, term): return summation2(n, term, _______________)

35 F UNCTIONS AS A RGUMENTS : P RACTICE def summation2(n, term, next): sum, k = 0, 1 while k <= n: sum = sum + term(k) k = next(k) return sum def summation(n, term): return summation2(n, term, lambda x: x + 1)

36 H IGHER O RDER F UNCTIONS : P RACTICE def summation(n, term): sum, k = 0, 1 while k <= n: sum = sum + term(k) k = k + 1 return sum def sum_of_n_even(n): ‘‘‘Returns the sum of the first n even numbers.’’’ return summation(n, ________________) def sum_of_n_odd(n): ‘‘‘Returns the sum of the first n odd numbers.’’’ return summation(n, ________________) def sum_of_n_starting_from_m(m, n): ‘‘‘Returns the sum of the first n numbers starting from m.’’’ return summation(n, ________________)

37 H IGHER O RDER F UNCTIONS : P RACTICE def summation(n, term): sum, k = 0, 1 while k <= n: sum = sum + term(k) k = k + 1 return sum def sum_of_n_even(n): ‘‘‘Returns the sum of the first n even numbers.’’’ return summation(n, lambda x: 2 * x) def sum_of_n_odd(n): ‘‘‘Returns the sum of the first n odd numbers.’’’ return summation(n, lambda x: 2 * x + 1) def sum_of_n_starting_from_m(m, n): ‘‘‘Returns the sum of the first n numbers starting from m.’’’ return summation(n, lambda x: x + m)

38 H IGHER O RDER F UNCTIONS : P RACTICE

39 H IGHER O RDER F UNCTIONS : P RACTICE

40 C ONCLUSION It is important to remember the domain and range of functions! Functions can take functions as arguments. Preview: Functions can also return functions.