We continued our discussion of higher-order functions:
Recall that functions which take other functions as input are classified as higher-order functions (as are functions that return functions).
We saw higher-order list functions last time. We also saw function composition, given by SML's infix operator o.
Today, we considered mapping and folding over trees (and arbitrary datatypes more generally).
We discussed staging. Staging takes advantage of the nested lambda form of a curried function to move parts of a computation close to the place where the arguments required for the computation appear. This can save computation time in a function that can do some of its work as soon as it sees its first argument, for instance. When called several times with the same first argument and different second arguments, the function can reuse the results of these early partial computations, since they are available in the environment part of the closure associated with the function that expects the second argument.
Lastly, we discussed combinators. Combinators are functions that combine small pieces of code into larger pieces of code. Composition is a combinator. There are many others. We focused today on higher-order functions that lift operations from a type to functions with values in that type. Such combinators may be defined using the pointwise principle: we specify what a particular combination of functions means by writing out explicitly how the combinator evaluates code for a given argument. Currying frequently makes this easy in SML. Once we have the combinator, we can then combine functions without referring explicitly to the arguments of the functions. We simply use the combinator, taking in function values and returning a function value. This approach is called point-free programming.
See again the notes from last time on higher order functions,
particularly for the generalization to trees.
See as well again
the notes discussing generalizations of
fold.
Lecture Slides with every build, use "slideshow" option in your pdf viewer. Lecture Slides compact.