CS10: The Beauty and Joy of Computing - Fall 2015
Lecture 13: Functions as Data
2021
Since this lecture was originally done six years ago, some of the formatting in the code may look a little weird / out of date.
Higher Order Functions
A higher order procedure is a “procedure that takes another procedure as one of its arguments”.
We’ve seen 4 HoFs. Three that operate on lists, and one tester:
Minor trivia note: As mentioned much earlier in the course, in the real world people often use the word “function” to mean any procedure, even if it is not a “pure function”.
Clicker Question
Which of the four HoFs we’ve seen are pure functions?
Reminder, a pure function is one for which:
Introducing Run and Call
Let’s now add two more HoFs to our toolbox:
Run and call are varadic (variable number of arguments).
Both calls to run give the same square.
One Funny Thing
If you provide fewer input values that there are inputs, they are replicated to fill all available slots.
Why Functions? (Why Review)?
Example, calling a function:
To avoid repetitive code, we make one function that can draw any square size.
Another Kind of Repetition
Highly redundant code is usually a sign that there is probably a better way.
How might creating a HoF help?
Creating a Draw Square HoF
Let’s generalize Draw Square into a HoF.
Draw Square HoF Answer
Use “run” to invoke procedures.
What Run Does For Us
Run doesn’t do anything particularly magical.
Building Keep
Run
Combine
How Combine Works (Below the Abstraction Line)
We know that combine takes a two-input function and somehow computes the result on a list.
Here’s how it really works:
Joinswap Clicker Question
What does the following code report?
Associativity
A dyadic function is a function with two inputs.
A dyadic function is said to be “associative” if f(a, f(b, c)) = f(f(a, b), c)).
Associativity and Combine in Snap!
Combine in Snap! is right-associative (works its way from right to left).
Anonymous Functions and Ringification
Clicker Question
What is the value of x after the script below is run?
Ringification
In Snap!, reporters are ALWAYS evaluated before a value is returned.
If we want to prevent the reporter from actually running, we need to ringify it.
Using a Ringified Function
Below are two examples of how ringified functions behave.
When is Ringification Useful?
We’ve already used ringification!
Puzzle
How many inputs does have?
Puzzle
How many inputs does have?
Puzzle (Don’t Discuss)
What will this code do:
Puzzle
How many inputs does have?
Puzzle
How many inputs does have?
There is only one input!
Input!
Puzzle (Discuss)
What will this code do:
Explicit Ringification and Anonymous Functions
One common pattern when using HoFs is to define custom functions that are used as inputs to the HoF. Examples:
Some programmers consider it silly to create such simple blocks, especially if they’re only used in exactly one place.
Explicit Ringification and Anonymous Functions
One common pattern when using HoFs is to define custom functions that are used as inputs to the HoF. Examples:
Some programmers consider it silly to create such simple blocks, especially if they’re only used in exactly one place.
These do exactly the same thing!
Explicit Ringification and Anonymous Functions
The ringified function and joinswap are exactly the same in behavior!
Anonymous function: A function with no name.
Weird HoF Demo
(Time Permitting)
Using HoFs
Courtesy of Dan Garcia (Berkeley’s primary CS10 architect), let’s try out a demo which uses another HoF called compose.
f(x) = x * 10 + 5
f(10) = 105