![](https://www.opentextbooks.org.hk/sites/opentextbooks/files/book_profile/cc_icon/by-sa.png)
Consider the problem of nding the last element in a list. Again we need to interpret what it means to be the last element of (a) the empty list and (b) a non-empty list.
- Last element of the empty list: the empty list has no element; so there is no such thing as the last element for the empty list. How do we represent the non-existence of an object? For now, we use a key word in Java called null. null is a special value in Java that can be assigned to any variable of Object type to signify that the variable is not referencing any Object at all.
- Last element of a non-empty list with rst and rest: it all depends on rest! rest has the capability to tell whether or not rst is the last element of the current list. When rest is empty, then rst is the last element. When rest is not empty, the rst is certainly not the last element. rest has its own st in this case, and it's up to the rest of rest to determine whether or not this new rst is the last element! It's recursion again, isn't it?
To recapitulate, here is how a list can nd its own last element.
- empty list case: return null or throw an exception to signify there is no such element.
- non-empty list case: pass rst to rest and ask rest for help to nd the last element.
How does rest use the rst element of the enclosing list to help nd the last element of the enclosing list?
- empty list case: the rst element of the enclosing list is the last element.
- non-empty list case: recur! Pass its own rst to its rest to help nd the last element.
Here is the code.
/** * Represents the abstract list structure. */ public interface IList { /** * Returns the last element in this IList. */ Object getLast(); /** * Given the first of the preceding list, returns the last element of the preceding list. * @param acc the first of the preceding list. */ Object getLastHelp(Object acc); } |
/** * Represents empty lists. */ public class MTList implements IList { // Singleton Pattern public static final MTList Singleton = new MTList(); private MTList() { } /** * Returns null to signify there is * no last element in the empty list. */ public Object getLast() { return null; } /** * Returns acc, because being the * first element of the preceding * list, it is the last element. */ public Object getLastHelp( Object acc) { return acc; } } |
/** * Represents non-empty lists. */ public class NEList implements IList {
private Object _first; private IList _rest; public NEList(Object f, IList r) { _first = f; _rest = r; } /** * Passes first to rest and asks for * help to find the last element. */ public Object getLast() { return _rest.getLastHelp(_first); } /** * Passes first to rest and asks for * help to find the last element. */ public Object getLastHelp(Object acc) { return _rest.getLastHelp(_first); } } |
The above algorithm to compute the last element of a list is another example of forward accumulation. Note that in the above, getLast is not recursive while getLastHelp is recursive. Also note that for the NEList, the last computation in getLastHelp is a recursive call to getLastHelp on _rest. There is no other computation after the recursive call returns. This kind of recursion is called tail recursion. Tail recursion is important for program performance. A smart compiler can recognize tail recursion and generate code that speeds up the computation by bypassing unnecessary setup code each time a recursive call is made.
- 瀏覽次數:2065