HIGHER-ORDER FUNCTIONS 2

Similar documents
HIGHER ORDER FUNCTIONS AND ENVIRONMENT DIAGRAMS 2

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

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

C ONTROL AND H IGHER O RDER F UNCTIONS

CONTROL AND HIGHER ORDER FUNCTIONS 1

ENVIRONMENT DIAGRAMS AND RECURSION 2

CONTROL AND HIGHER ORDER FUNCTIONS 1

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

CONTROL AND HIGHER ORDER FUNCTIONS 2

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

CONTROL AND ENVIRONMENTS 1

ENVIRONMENT DIAGRAMS AND RECURSION 2

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

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

61A Lecture 6. Friday, September 7

61A Lecture 4. Monday, September 9

functional programming in Python, part 2

HIGHER ORDER FUNCTIONS 2

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

Programming Languages

Structure and Interpretation of Computer Programs

6.1 Evaluate Roots and Rational Exponents

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Fall 2015 Midterm 1

ENVIRONMENT MODEL: FUNCTIONS, DATA 18

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

Midterm Exam 1 Solutions

RLISTS AND THE ENVIRONMENT MODEL 9

Lecture 16: Static Semantics Overview 1

Exercise 1 ( = 24 points)

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Execution order. main()

CS 61A HKN Review Session. Nathan Zhang, Caleb Wyllie

Scope, Functions, and Storage Management

Encodings. Using the minimal λ-calculus language we get. functions. local binding. booleans. numbers... and recursive functions?

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

Syntax Errors; Static Semantics

DECOMPOSITION, ABSTRACTION, FUNCTIONS

CS 1110 Prelim 1 October 4th, 2012

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

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

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

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

Transformation a shifting or change in shape of a graph

CS150 - Sample Final

Pre-Calculus Mr. Davis

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

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

Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 1

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

All the operations used in the expression are over integers. a takes a pair as argument. (a pair is also a tuple, or more specifically, a 2-tuple)

SAMS Programming - Section C. Lecture 1: Introduction + Basic Building Blocks of Programming

Lecture #12: Immutable and Mutable Data. Last modified: Mon Feb 22 16:33: CS61A: Lecture #12 1

Structure and Interpretation of Computer Programs

Mini-Project 1: The Library of Functions and Piecewise-Defined Functions

Professor Hugh C. Lauer CS-1004 Introduction to Programming for Non-Majors

April 2 to April 4, 2018

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

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

Core Mathematics 3 Functions

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

Structure and Interpretation of Computer Programs

CS1210 Lecture 28 Mar. 27, 2019

Assignment 7: functions and closure conversion

Structure and Interpretation of Computer Programs

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

CS1110. Lecture 6: Function calls

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

CS1110. Lecture 6: Function calls

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

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

Fundamentals of Programming. Lecture 1: Introduction + Basic Building Blocks of Programming

Unit #3: Quadratic Functions Lesson #13: The Almighty Parabola. Day #1

CS Lecture 18: Card Tricks. Announcements. Slides by D. Gries, L. Lee, S. Marschner, W. White

Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model.

Programming Languages

Adding Machine Run 2

CS140 Operating Systems and Systems Programming Final Exam

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

Structure and Interpretation of Computer Programs

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

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

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Structure and Interpretation of Computer Programs

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Chapter 1: Function Sense Activity 1.2 & 3

Continuations and Continuation-Passing Style

MATH 271 Summer 2016 Assignment 4 solutions

PREPARING FOR PRELIM 1

Comp 150 Exam 2 Overview.

MAT 106: Trigonometry Brief Summary of Function Transformations

CS 321 Programming Languages

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

Topics. Introduction to Repetition Structures Often have to write code that performs the same task multiple times. Controlled Loop

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

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

Functional programming in LISP

CS3: Introduction to Symbolic Programming. Lecture 10: Tic-tac-toe Lambda.

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

Transcription:

HIGHER-ORDER FUNCTIONS 2 COMPUTER SCIENCE 61A June 28, 2016 1 Higher-Order Functions A higher order function (HOF) is a function that manipulates other functions by taking in functions as arguments, returning a function, or both. 1.1 Functions as Arguments One way a higher order function can manipulate other functions is by taking functions as input (an argument). Consider this higher order function called negate. def negate(f, x): return -f(x) negate takes in a function f and a number x. It doesn t care what exactly f does, as long as f is a function, takes in a number and returns a number. Its job is simple: call f on x and return the negation of that value. 1.2 Questions 1. Here are some possible functions that can be passed through as f. def square(n): return n * n def double(n): return 2 * n What will the following Python statements display?

DISCUSSION 2: HIGHER-ORDER FUNCTIONS Page 2 >>> negate(square, 5) >>> negate(double, -19) >>> negate(double, negate(square, -4)) 1.3 Functions as Return Values Often, we will need to write a function that returns another function. One way to do this is to define a function inside of a function: def outer(x): def inner(y):... return inner The return value of outer is the function inner. This is a case of a function returning a function. In this example, inner is defined inside of outer. Although this is a common pattern, we can also define inner outside of outer and still use the same return statement. However, note that in this second example (unlike the first example), inner doesn t have access to variables defined within the outer function, like x. def inner(y):... def outer(x): return inner 1.4 Questions 1. Use this definition of outer to fill in what Python would display when the following lines are evaluated. def outer(n): def inner(m): return n - m return inner >>> outer(61) >>> f = outer(10) >>> f(4) >>> outer(5)(4)

DISCUSSION 2: HIGHER-ORDER FUNCTIONS Page 3 2. Write a function and add that takes a function f (such that f is a function of one argument) and a number n as arguments. It should return a function that takes one argument, and does the same thing as the function f, except also adds n to the result. def and_add(f, n): """Return a new function. This new function takes an argument x and returns f(x) + n. >>> def square(x):... return x * x >>> new_square = and_add(square, 3) >>> new_square(4) # 4 * 4 + 3 19 """

DISCUSSION 2: HIGHER-ORDER FUNCTIONS Page 4 1.5 Environment Diagrams 1. Draw the environment diagram for the following code: def curry2(h): def f(x): def g(y): return h(x, y) return g return f make_adder = curry2(lambda x, y: x + y) add_three = make_adder(3) five = add_three(2)

DISCUSSION 2: HIGHER-ORDER FUNCTIONS Page 5 2. 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 def h(): return x + 1 return h def f(f, x): return f(x + n) f = f(g, n) g = (lambda y: y())(f)

DISCUSSION 2: HIGHER-ORDER FUNCTIONS Page 6 3. **The following question is extremely difficult. Something like this would not appear on the exam. Nonetheless, it s a fun problem to try.** Draw the environment diagram for the following code: (Note that using the + operator with two strings results in the second string being appended to the first. For example "C" + "S" concatenates the two strings into one string "CS") y = "y" h = y def y(y): h = "h" if y == h: return y + "i" y = lambda y: y(h) return lambda h: y(h) y = y(y)(y)