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

Similar documents
Lecture #3: Environments

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

61A Lecture 4. Monday, September 9

ENVIRONMENT DIAGRAMS AND RECURSION 2

Measuring Efficiency

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

ENVIRONMENT DIAGRAMS AND RECURSION 2

Lecture 16: Static Semantics Overview 1

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

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

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 02 Lecture - 45 Memoization

SCHEME AND CALCULATOR 5b

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

Recurrences and Memoization: The Fibonacci Sequence

Multithreaded Algorithms Part 1. Dept. of Computer Science & Eng University of Moratuwa

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

# track total function calls using a global variable global fibcallcounter fibcallcounter +=1

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

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

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

CS 61A Higher Order Functions and Recursion Fall 2018 Discussion 2: June 26, 2018 Solutions. 1 Higher Order Functions. HOFs in Environment Diagrams

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

CS 61A Higher Order Functions and Recursion Fall 2018 Discussion 2: June 26, Higher Order Functions. HOFs in Environment Diagrams

APCS-AB: Java. Recursion in Java December 12, week14 1

TAIL RECURSION, SCOPE, AND PROJECT 4 11

CS1 Recitation. Week 2

Programming Languages. Streams Wrapup, Memoization, Type Systems, and Some Monty Python

CS 4349 Lecture September 13th, 2017

Math 4242 Fibonacci, Memoization lecture 13

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

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

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

An introduction to Scheme

Last week: Breadth-First Search

Lecture 23: Priority Queues, Part 2 10:00 AM, Mar 19, 2018

Structure and Interpretation of Computer Programs

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

DRAWING ENVIRONMENT DIAGRAMS

Lecture 12: Dynamic Programming Part 1 10:00 AM, Feb 21, 2018

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

Algorithm Design and Recursion. Search and Sort Algorithms

Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018

SD314 Outils pour le Big Data

CIS 194: Homework 6. Due Wednesday, 4 March. Fibonacci numbers. It s all about being lazy.

Recursion. Recursion [Bono] 1

Twelve Simple Algorithms to Compute Fibonacci Numbers

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

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

61A Lecture 2. Wednesday, September 4, 2013

Lecture 7: Primitive Recursion is Turing Computable. Michael Beeson

15-122: Principles of Imperative Computation, Fall 2015

CIS 194: Homework 6. Due Monday, February 25. Fibonacci numbers

CSC324 Functional Programming Efficiency Issues, Parameter Lists

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

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

HIGHER-ORDER FUNCTIONS 2

CSCC24 Functional Programming Scheme Part 2

Compiler Construction Lecture 05 A Simple Stack Machine. Lent Term, Lecturer: Timothy G. Griffin. Computer Laboratory University of Cambridge

Homework. Reading: Chapter 17 Homework: All exercises from Chapter 17 Due: 10/27 Correction: Chapter 16 homework is due 10/25

Recursive definition: A definition that is defined in terms of itself. Recursive method: a method that calls itself (directly or indirectly).

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

61A Lecture 2. Friday, August 28, 2015

The Running Time of Programs

Algorithm Analysis. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Generating Functions

Forward recursion. CS 321 Programming Languages. Functions calls and the stack. Functions calls and the stack

Tail Recursion. ;; a recursive program for factorial (define fact (lambda (m) ;; m is non-negative (if (= m 0) 1 (* m (fact (- m 1))))))

CS450 - Structure of Higher Level Languages

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

COMP 161 Lecture Notes 16 Analyzing Search and Sort

RECURSION, RECURSION, (TREE) RECURSION! 3

Putting the fun in functional programming

CSC-140 Assignment 4

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Reading: Chapter 17 Homework: All exercises from Chapter 17 Due: 10/27 Correction: Chapter 16 homework is due 10/25

CS 261 Data Structures. Big-Oh Analysis: A Review

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

CSE : Python Programming

RECURSION, RECURSION, (TREE) RECURSION! 2

CS1 Lecture 15 Feb. 18, 2019

Organization of Programming Languages CS3200/5200N. Lecture 11

142

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

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

Streams. CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004

RECURSION 3. 1 Recursion COMPUTER SCIENCE 61A. June 30, 2016

Structure and Interpretation of Computer Programs

. As x gets really large, the last terms drops off and f(x) ½x

Introduction to Programming I

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

Unit #2: Recursion, Induction, and Loop Invariants

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

CONTROL AND HIGHER ORDER FUNCTIONS 2

Types of Recursive Methods

Writing Parallel Programs; Cost Model.

RECURSION 7. 1 Recursion COMPUTER SCIENCE 61A. October 15, 2012

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

Transcription:

nnouncements: Lecture #5: Higher-Order Functions Make sure that you have registered electronically with our system (not just TeleBERS). ttend a discussion/lab in which you can fit; don t worry about Tele- BERS lab/discussion time once it allows you to register. Concurrent enrollment students should all get in (once fees are paid, that is). Simple Recursion The Fibonacci sequence is defined k, for k = 0, 1 F k = F k 2 + F k 1, for k > 1...which translates easily into Python: """The Nth Fibonacci number, N>=0.""" assert n >= 0 if n <= 1: return n return fib(n-2) + fib(n-1) This definition works, but why is it so slow? Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 1 Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 2 Redundant Calculation Consider the computation of fib(10). This calls fib(9) and fib(8), but then fib(9) calls fib(8) again and both fib(9) and the two calls to fib(8) call fib(7), so that fib(7) is called 3 times. Likewise, fib(6) is called 5 times, fib(7) is called 8 times, and so forth in increasing Fibonacci sequence, interestingly enough. Therefore, the time required (proportional to the number of calls) grows exponentially: s it turns out, fib(n) requires time roughly proportional to Φ N, where the golden ratio Φ = (1 + 5)/2. voiding Recalculation To compute the next Fibonacci number, we need the preceding two. Let s generalize and consider what it takes to compute N more: """ssuming FK1 and FK F[K-1] and F[K] in the Fibonacci sequence numbers and N>=K, return F[N].""" if n == k: return fk return fib2(fk, fk1+fk, k+1, n) if n <= 1: return n return fib2(0, 1, 1, n) Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 3 Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 4 Tail Recursion and Repetition In this last version, whenever fib2 is called recursively, the value of that call is immediately returned. This property is called tail recursion. return fib2(fk, fk1+fk, k+1, n) return fib2(0, 1, 1, n) It is this sort of process that is easily expressed as an iteration. Explicit Iteration In the Python, C, Java, and Fortran communities, it is more usual to be explicit about repetition, rather than using tail recursion. The simplest form is while while Condition: Statements means If condition evaluates to a true value, execute statements and repeat the entire process. Otherwise, do nothing. Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 5 Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 6

Original version, again: Explicit Iteration in fib return fib2(fk, fk1+fk, k+1, n) return fib2(0, 1, 1, n) s an explicit iteration: fk1, fk, k = 0, 1, 1 while n!= k: fk1, fk, k = fk, fk1+fk, k+1 return fk Nested Functions In the last recursive version, fib2 function is an auxiliary function, used only by fib. It makes sense to tuck it away inside fib, like this: def fib2(fk1, fk, k): return fib2( fk, fk1+fk, k+1) return fib2(0, 1, 1) I ve taken the liberty here of removing the parameter n from fib2: it s always the same as the outer n and never changes. But to explain how this works, we ll have to extend the environment model just a bit. Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 7 Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 8 Nested Functions and Environments fk1: 1 fk: 1 k: 2 fk1: 0 fk: 1 k: 1 fib: λ n:... n: 2 fib2: λ fk1, fk, k:... 1 fib(2)...fib2(0,1,1)...fib2(fk, fk1+fk, k+1)...fib2(fk, fk1+fk, k+1) Defining Environments Defining Environments Each function value is attached to the environment frame in which the def statement that created it was evaluated. Since the def for fib was evaluated in the global frame, the resulting function value bound to fib is attached to the global frame. Since the def for fib2 was evaluated in the local frame of an execution of fib, the resulting function value is attached to that local frame. When a user-defined function value is called, the local frame that is created for that call is attached to the defining frame of the function. Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 9 Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 10 Do You Understand the Machinery? (I) def : def : nswer (I) The program prints 0. t the point that f is called, we are in the situation shown below: h: B: g : h λ : λ : body of h λ : body of g λ : So we evaluate f in an environment (B) where it is bound to a function that returns 0. (B: g means that frame B was created to execute a call to g). B f() Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 11 Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 12

Do You Understand the Machinery? (II) g = f print() nswer (II) The program prints 0 again: λ : λ : t the time we evaluate f to assign it to g, it has the value indicated by the crossed-out dotted line, so that is the value g gets. The fact that we change f s value later is irrelevant, just as x = 3; y = x; x = 4; print(y) prints 3 evan though x changes: y doesn t remember where its value came from. Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 13 Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 14 Do You Understand the Machinery? (III) nswer (III) This time, the program prints 1. When g is executed, it evaluates the name f. t the time that happens, f s value has been changed (by the third def), and that new value is therefore the one the program uses. def : Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 15 Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 16 Functions s Templates If we think of a function body as a template for a computation, parameters are blanks in that template. For example: def sum_squares(n): k, sum = 0, 0 while k <= N: sum, k = sum+k**2, k+1 return sum is a template for an infinite set of computations that add squares of numbers up to 0, 1, 2, 3,..., in place of the N. Functions on Functions Likewise, function parameters allow us to have templates with slots for computations: def summation(n, f): k, sum = 1, 0 while k <= N: sum, k = sum+f(k), k+1 return sum eneralizes sum squares. We can write sum squares(5) as: def square(x): return x*x summation(5, square) or (if we don t really need a square function elsewhere), we can create the function argument anonymously on the fly: summation(5, lambda x: x*x) Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 17 Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 18

Functions that Produce Functions Functions are first-class values, meaning that we can assign them to variables, pass them to functions, and return them from functions. Example: def add_func(f, g): """Return function that returns f(x)+g(x) for argument x.""" def adder(x): # return f(x) + g(x) # or return lambda x: f(x) + g(x) return adder # Do You Understand the Machinery? (IV) What is printed: (1, infinite loop, or error) and why? def g(x): print(x) def f(f): f(1) f(g) h = add_func(abs, lambda x: -x) >>> print(h(-5)) 10 eneralize the example: def combine_funcs(op, f, g): return lambda x: op(f(x), g(x)) # Now add_func = lambda f, combine funcs(sum, f, g) Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 19 Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 20 nswer (IV) This prints 1. When we reach f(1) inside f, the call expression, and therefore the name f, evaluated in the environment E, where the value of f is the global function bound to E λ! f(1) λ! x: print(x) n side: Notations To introduce environments, I used arrows to indicate connections between boxes to show these relationships graphically. But for serious use, that notation gets cluttered rapidly. lso, the Python Tutor software does not use it, favoring textual labels instead. There is a link to our official rules for building environment diagrams with the Python tutor notation on the class web page: https://inst.eecs.berkeley.edu/ cs61a/sp14/pdfs/environment-diagrams.pdf For these lectures, I ll generally use the more explicit style (with arrows linking frames) to show everything graphically, but we ll use the more compact Python tutor style for homework and tests. Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 21 Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 22 Example of the Notations Consider the following program, which will print 1: def g(f): def : g(f) h: B: g Example, contd: Lecture notation and Python Tutor notation : h λ : λ : body of g λ : body of h λ : lobal frame f f1: h g g h func f() func g(f) func f func f() [parent: f1] f C: f f [parent f1] Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 23 Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 24

Do You Understand the Machinery? (V) What is printed: (0, 1, or error) and why? def : return f() nswer (V) This prints 0. Function values are attached to current environments when they are first created (by lambda or def). ssignments (such as to p) don t themselves create new values, but only copy old ones, so that when p is evaluated, it is equal to k, which is equal to g, which is attached to the global environment. def h(k): p = k return p() print(h(g)) Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 25 Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 26 Observation: Environments Reflect Nesting From what we ve seen so far: Linking of environment frames Nesting of definitions. For example, given def f(x): def g(x): def h(x): print(x)...... The structure of the program tells you that the environment in which print(x) is evaluated will always be a chain of 4 frames: Frame for h = Frame for g = Frame for f = lobal frame. However, when there are multiple local frames for a particular function lying around, environment diagrams can help sort them out. Do You Understand the Machinery? (VI) What is printed: (0, 1, or error) and why? def f(p, k): def : print(k) if k == 0: f(g, 1) p() f(none, 0) Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 27 Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 28 nswer (VI) This prints 0. There are two local frames for f when p() is called. In the first one, k is 0; in the second, it is 1. When p() is called, its value comes from the value of g that was created in the first frame, where k is 0. Last modified: Mon Mar 3 01:54:55 2014 CS61: Lecture #5 29