OOscheme
Interface IListAlgo

All Known Implementing Classes:
GetLength, GetMin, GetMinHelper

public interface IListAlgo

Represents an abstract algorithm to be executed by a host IList. Plays the role of a "visitor" in the design pattern language.

In non OO programming paradigms, an algorithm on lists is expressed as a function or procedure that takes in a list as parameter. In particular, our Comp 210 Scheme class prescribes a 'template' for writing functions that operates on lists as follows.

 (define f (lst)
   (cond
     [(empty? lst) ...]
     [(cons? lst) ...(first lst) ...(f (rest lst)...]))
 
In our OO formulation, an alogrithm on lists is an interface that has two methods, each takes in a list as a parameter: When we want to perform an alogrithm (IListAlgo) on a list (IList), we asks the IList to 'execute' the algorithm with a given input. An IEmptyList knows instrinsically that is empty and calls the emptyCase method of the IListAlgo. Analogously, a non-empty list (INEList) will correctly call the nonEmptyCase method of the IListAlgo.

One need not (and should not) check for the type of a list before applying an algorithm to it. THERE IS NO cond!

Since:
Copyright 2005 - DXN, SBW All rights reserved
Author:
Dung X. Nguyen, Stephen B. Wong

Method Summary
 Object emptyCase(IEmptyList host, Object... inp)
          Operates on IEmptyList, the empty list.
 Object nonEmptyCase(INEList host, Object... inp)
          Operates on INEList, a non-empty list.
 

Method Detail

emptyCase

Object emptyCase(IEmptyList host,
                 Object... inp)
Operates on IEmptyList, the empty list. Since IEmptyList has only one method, execute(), there is not much the host list can do here. So the code for the empty case usually involves: This corresponds to the [(empty? lst) ...] part of the (Comp 210) Scheme function template.

Parameters:
host - the IEmptyList that is executing this algorithm.
inp - a variable number of input parameters that can be used for any purpose.
Returns:
result from calling this method. The type of the result is problem-specific and may be null.

nonEmptyCase

Object nonEmptyCase(INEList host,
                    Object... inp)
Operates on INEList, a non-empty list. The host list now has a first and a rest. So the code here usually involves what host.getFirst() and host.getRest() can do to accomplish the task at hand.

To summarize, the code for the non-empty case usually involves:

This corresponds to the [(cons? lst) ...(first lst) ...(f (rest lst))...] part of the (Comp 210) Scheme function template.

Parameters:
host - the INEList that is executing this algorithm
inp - variable number input parameters that can be used for any purpose.
Returns:
result from calling this method. The type of the result is problem-specific and may be null.