HOFs may be combined to form larger, more powerful functions!
And these can be themselves combined to form even more powerful
functions!
Examples
Choosing the right tool (mini-summary)
Every maps, or does something to every word in the
sentence
First argument is a one-argument transforming function
Keep selects certain elements of a word or sentence
and discards the others
First argument is a one-argument predicate function
Accumulate transforms the entire word or sentence
into a single result
First argument is a two-argument combining function
These are NOT the holy grail
of higher-order functions! These are just three of the ones we have in the book.
We showed how to write new ones:
(count-ifpredicatesentence-or-word)
which counts the number of elements in sentence-or-word
which satisfy the predicate
First argument is a one-argument predicate function
Built on count and keep
(all?predicatesentence-or-word)
which returns whether all the elements of sentence-or-word
satisfy the predicate
Built on count-if and count
First-class functions and first-class
sentences
Data is first-class if:
It can be an argument to a procedure
It can be the return value from a procedure
It can be given a name
It can be part of a data aggregate
In Scheme, words, sentences, booleans and procedures (and
some other types you'll see soon) are all first-class
repeated : A function that
returns a function!
This is the first function that we've seen that returns
a function
(Example with foo to warm up)
(repeated function number)
Repeated takes a function and a number and returns a new
procedure.
The returned procedure is one that invokes the original procedure
repeatedly
Examples
Summary
Today's big ideas: higher-order functions in detail!
Smaller ideas: We now have at our disposal many powerful
tools which we can plug together to solve a problem
Next Time
We'll finish the part of this lecture we didn't get to, and
after that, look at the reason the MacGambit is shaped the way
it is... Lambda!
Puzzle : I've been had!
Punctuate the following to make a correct english sentence(s):
Whereas John had had had Bill had had had had had had
was corrrect
Game : Northcott's Game [Guy89]
There is just one checker of each color on each row of the
checkerboard
The Red checker is always to the right of the black checker
Aside from those constraints, the pieces on the board can
be placed randomly
When it's your turn to move, you slide one of your checkers
any numbers of squares in its own row without jumping over your
opponent's checker and without going off the board.
Last person to be able to move wins.
References
[Guy89] Richard K. Guy "Fair Game", COMAP,
Arlington, MA, 1989.