CONTROL AND HIGHER ORDER FUNCTIONS 2

Similar documents
CONTROL AND HIGHER ORDER FUNCTIONS 1

C ONTROL AND H IGHER O RDER F UNCTIONS

HIGHER-ORDER FUNCTIONS 2

CONTROL AND ENVIRONMENTS 1

HIGHER ORDER FUNCTIONS 2

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

CONTROL AND HIGHER ORDER FUNCTIONS 1

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

HIGHER ORDER FUNCTIONS AND ENVIRONMENT DIAGRAMS 2

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

61A Lecture 4. Monday, September 9

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

61A Lecture 3. Friday, September 5

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

ENVIRONMENT DIAGRAMS AND RECURSION 2

ENVIRONMENT DIAGRAMS AND RECURSION 2

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

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

61A Lecture 6. Friday, September 7

functional programming in Python, part 2

CS61A Notes Week 3: Environments and Higher Order Functions

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

61A Lecture 2. Wednesday, September 4, 2013

Structure and Interpretation of Computer Programs Fall 2015 Midterm 1

Statements 2. a operator= b a = a operator b

Structure and Interpretation of Computer Programs

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Lecture 7: Primitive Recursion is Turing Computable. Michael Beeson

Midterm Exam 1 Solutions

Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 1

RECURSION: n. SEE RECURSION 3

Structure and Interpretation of Computer Programs

Measuring Efficiency

Organization of Programming Languages CS3200/5200N. Lecture 11

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Test #2 October 8, 2015

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Problem Set 4: Streams and Lazy Evaluation

Conditionals and Recursion. Python Part 4

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Flow Control: Branches and loops

Conditionals: Making Choices

Chapter 15 Functional Programming Languages

Lab 5 - Repetition. September 26, 2018

61A Lecture 2. Friday, August 28, 2015

Lecture #11: Immutable and Mutable Data. Last modified: Sun Feb 19 17:03: CS61A: Lecture #11 1

Control Flow: Loop Statements

Introduction to Functional Programming (Python) John R. Woodward

CIS192 Python Programming. Robert Rand. August 27, 2015

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

CS 1301 CS1 with Robots Summer 2007 Exam 1

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

CS Boolean Statements and Decision Structures. Week 6

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

CS1 Lecture 3 Jan. 18, 2019

CS1 Lecture 5 Jan. 26, 2018

Fundamentals of Programming. Week 1 - Lecture 3: Loops

Structure and Interpretation of Computer Programs

CME 193: Introduction to Scientific Python Lecture 1: Introduction

Midterm 1 Review. Important control structures. Important things to review. Functions Loops Conditionals

CS1 Lecture 3 Jan. 22, 2018

DM536 Introduction to Programming. Peter Schneider-Kamp.

DM536 / DM550 Part 1 Introduction to Programming. Peter Schneider-Kamp.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Python Programming: An Introduction to Computer Science

FP Foundations, Scheme

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

Introduction to Programming II W4260. Lecture 2

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

Intro. Speed V Growth

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

CSCE 110: Programming I

Lecture 02 Making Decisions: Conditional Execution

Programming Languages

A gentle introduction to Matlab

Structure and Interpretation of Computer Programs

Loop structures and booleans

Control Structures 1 / 17

CSSE 120 Introduction to Software Development Practice for Test 1 paper-and-pencil part Page 1 of 6

DM502 Programming A. Peter Schneider-Kamp.

Problem Solving for Intro to Computer Science

DM502 Programming A. Peter Schneider-Kamp.

CSc 520 Principles of Programming Languages

Logical and Function Constructions

Programming to Python

CSM Mock Final Spring 2018

Functionally Modular. Self-Review Questions

Chapter 5: Control Structures

Conditionals & Control Flow

6.1 Evaluate Roots and Rational Exponents

Programming with Python

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

Accelerating Information Technology Innovation

Lecture 21: Functional Programming in Python. List Comprehensions

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

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

Transcription:

CONTROL AND HIGHER ORDER FUNCTIONS 2 COMPUTER SCIENCE 61A September 11, 2014 1 Control Control structures direct the flow of logic in a program. This can mean skipping a portion of code (conditionals) or repeating a portion of code multiple times (iteration). 1.1 Conditional Statements Conditional statements let programs execute different lines of code depending certain conditions. The conditional statement in Python is an if-elif-else block: if <conditional expression>: <suite of statements> elif <conditional expression>: <suite of statements> else: <suite of statements> Some notes: The else and elif statements are optional. You can have any number of elif statements. A conditional expression is a Python expression. whether its value is a true value or a false value. All that matters for control is The code that is executed is the suite that is indented under the first if/elif that has a true conditional expression. If none are true, then the else suite is executed. Once one suite is executed, the rest are skipped. 1

DISCUSSION 2: CONTROL AND HIGHER ORDER FUNCTIONS Page 2 Note: in Python, there are a few things that are treated as false values: The boolean False The integer 0 The value None And more... (we will learn about these later in the semester) Python also includes boolean operators and, or, and not. These operators are used to combine and manipulate boolean values. not True evaluates to False, and not False evaluates to True. True and True evaluates to True, but a false value on either side makes it False. False or False evaluates to False, but a true value on either side makes it True. 1.2 Question 1. It s lecture time! However, whether you go depends on certain conditions about timing, seats, and laziness. Write a simple function which lecture that takes in inputs time, seats left, is lazy and prints out your decision. which lecture should print "go to lecture" if time is before 2:00pm, there are seats, and you are not lazy. which lecture should print "go to alt lecture" if time is after 2:00pm or there are no seats, and you are not lazy. which lecture should print "watch videos" if you feel lazy. time is in military format; e.g 2:20pm is 1420. seats left is a non-negative integer. is lazy is a boolean variable. def which_lecture(time, seats_left, is_lazy):

DISCUSSION 2: CONTROL AND HIGHER ORDER FUNCTIONS Page 3 1.3 Iteration Iteration lets a program repeat statements multiple times. A common iterative block of code is the while loop: while <conditional clause>: <body of statements> This block of code states: while the conditional clause is still True, continue executing the indented body of statements. Here is an example: >>> def countdown(x):... while x > 0:... print(x)... x = x - 1... print("blastoff!")... >>> countdown(3) 3 2 1 Blastoff! 1.4 Questions 1. Fill in the is prime function, which returns True if n is a prime number and False otherwise. Hint: use the % operator: x % y returns the remainder of x is divided by y. def is_prime(n):

DISCUSSION 2: CONTROL AND HIGHER ORDER FUNCTIONS Page 4 2. Fill in the choose function, which returns the number of ways to choose k items from n items. Mathematically, choose(n,k) is defined as: n (n 1) (n 2) (n k + 1) k (k 1) (k 2) 2 1 def choose(n, k): Returns the number of ways to choose K items from N items. >>> choose(5, 2) 10 >>> choose(20, 6) 38760

DISCUSSION 2: CONTROL AND HIGHER ORDER FUNCTIONS Page 5 2 Higher Order Functions A function that manipulates other functions is called a higher order function (HOF). A HOF can be a function that takes functions as arguments, returns a function, or both. 2.1 Functions as Argument Values Suppose we want to square or double every natural number from 1 to n and print the result as we go. Fill in the functions square every number and double every number by using the square and double functions we have defined. def square(x): return x * x def square_every_number(n): Prints out the square of every integer from 1 to n. >>> square_every_number(3) 1 4 9 def double(x): return 2 * x def double_every_number(n): Prints out the double of every integer from 1 to n. >>> double_every_number(3) 2 4 6 The only difference between square every number and double every number is the function called before printing (either square or double). Everything else is the same!

DISCUSSION 2: CONTROL AND HIGHER ORDER FUNCTIONS Page 6 It would be nice to have a generalized function (let s call it the every function) that took care of the while loop and the incrementing for us. That way, we could triple every number or cube every number without repeating so much code: def square_every_number(n): every(square, n) def double_every_number(n): every(double, n) def cube(x): return x * x * x def cube_every_number(n): every(cube, n) 2.2 Questions 1. Implement the function every that takes in a function func and a number n, and prints the result of applying that function to each of the first n natural numbers. def every(func, n): Prints out all integers from 1 to n with func applied on them. >>> def square(x):... return x * x >>> every(square, 3) 1 4 9 2. Similarly, implement a function keep, which takes in a function cond and a number n, and only prints a number from 1 to n to the screen if calling cond on that number returns True: def keep(cond, n):

DISCUSSION 2: CONTROL AND HIGHER ORDER FUNCTIONS Page 7 Prints out all integers from 1 to n that return True when called with cond. >>> def is_even(x):... # Even numbers have remainder 0 when divided by 2.... return x % 2 == 0 >>> keep(is_even, 5) 2 4 2.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 Note two things: 1. The return value of the outer function is inner! This is where a function returns a function. 2. In this case, the inner function is defined inside of the outer function. This is a common pattern, but it is not necessary we could have defined inner outside of the outer and still keep the return statement the same. 2.4 Moar Questions 1. 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.

DISCUSSION 2: CONTROL AND HIGHER ORDER FUNCTIONS Page 8 def and_add(f, n): Returns 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 2. The following code has been loaded into the python interpreter: def skipped(f): def g(): return f return g def composed(f, g): def h(x): return f(g(x)) return h def added(f, g): def h(x): return f(x) + g(x) return h def square(x): return x*x def two(x): return 2 What will python output when the following lines are evaluated? >>> composed(square, two)(7) >>> skipped(added(square, two))()(3)

DISCUSSION 2: CONTROL AND HIGHER ORDER FUNCTIONS Page 9 >>> composed(two, square)(2) 3. 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 return x + 3 def f(f, x): return f(f(x+2)) m = f(g, n)

DISCUSSION 2: CONTROL AND HIGHER ORDER FUNCTIONS Page 10 4. Draw the environment diagram for the following code: from operator import add def curry2(h): def f(x): def g(y): return h(x,y) return g return f make_adder = curry2(add) add_three = make_adder(3) five = add_three(2)