Class 6


Review from previous class

Recursive programs
These are useful for certain kinds of tasks, such as those that operate on recursive data structures like lists and trees. Tail recursion makes them run as fast as more common iterative programs, avoid the dreaded "stack overflow" message as well.

Function mapping
Often one wants to transform a list (x_1,...,x_n) to (f(x_1), ..., f(x_n)). Although some functions (eg: sqrt) automatically map over lists, others do not. The built in mapping functions, esp. mapcar, make this easy. Anonymous functions (defined using the lambda form) become important, in order to avoid defining "one-time" functions.


Tools for the Day

Latex for transparencies
I have added an example of a Tex file that can be used to make transparencies. This file requires two additional files. One is my file of standard definitions and abbreviations that I use in most documents, standard.tex . The second is a "style file" which defines the transparency format, seminar.sty. By the way, this Tex example discusses the use of information theory in statistical model selection.

Mathematica
Today, we'll do a bit more. Namely consider how Mathematica maps functions in a style similar to that in Lisp. Mathematica also has anonymous functions comparable in style and application to the "lambda" functions in Lisp.

Status of Projects


Recursion and Searching, with local variables and functions

Newton's method and fixed points.
If we think of Newton's method for finding the zero of the fixed point problem x_1 = x_0 - f(x_0)/f'(x_1), then recursion offers a general way to find the zero implied by this algorithm.

We can use a generic fixed-point program to implement Newton's method. We will implement the fixed-point program in several ways: purely recursive, recursive with a helper function to eliminate redundant computation, and finally using "let" to hold the local variable.

Functions that build functions.
Newton's method is not very elegant since we require as input the full expression x_0 - f(x_0)/f'(x_0). This is hard to do when f itself is very complex (like a nasty likelihood, perhaps with no closed form expression). Numerical derivatives are a handy tools, so lets add one to enhance our implementation of Newton's method.

Local variables, local functions.
More complicated functions have both local variables and local functions. Local functions avoid the tendency to have lots of helper functions lying around. They serve no other purpose than but to help another function, so we'll embed them in the main function, hiding their presence.

Finding the maximum/minimum of a function.
As an example of local variables and local functions (aka, subroutines in Fortran), we will build and compare two methods for finding the max/min of a function. Newton's method is easy. We just have to adapt it to find the zero of the derivative rather than the zero of the function (assuming, of course, that all of these things exist). We'll handle this via two numerical derivatives.

Going without a derivative is harder, but golden section searches are a fast alternative. These converge as fast as is possible to the max of a unimodal function, without needing a derivative.

Lisp script for today's class.
class6.lsp


Next time

We continue with general Lisp programming, sinking into iteration, simple macros, and indexing.