Contents    Page-10    Prev    Next    Page+10    Index   

Recursive Processing of List

Recursive processing of a list is based on a base case (often an empty list), which usually has a simple answer, and a recursive case, whose answer is based on a recursive call to the same function on the rest of the list.

(defun length (lst)
  (if (null lst)       ; test for base case
      0                ; answer for base case
      (+ 1
         (length (rest lst))) ) )  ; recursive call

public static int length (Cons lst) {
  if ( lst == null )   ; test for base case
     return 0;         ; answer for base case
   else return (1 +
                  length( rest(lst) )); }