Understanding Loops

  /**
   * Determine if the collection generated by the given Traversal
   * contains an element that satisfies the given predicate.
   */
  public <T> boolean contains(Traversal<T> tr, ISelect<T> choice){
    try{
      if (tr.isEmpty())
        return false;
      else    
        if (choice.select(tr.getFirst()))
          return true;
        else
          return contains(tr.getRest(), choice); 
    }
    catch(IllegalUseOfTraversalException e){
      System.out.println("Illegal traversal: " + e.getMessage());
      return false;
    }
  }
  
  /**
   * Count how many elements in the collection generated by the 
   * given Traversal satisfy the given predicate.
   */
  public <T> int countSuch(Traversal<T> tr, ISelect<T> choice){
    try{
      if (tr.isEmpty())                                  
        return 0;
      else    
        if (choice.select(tr.getFirst()))   
          return 1 + countSuch(tr.getRest(), choice); 
        else
          return countSuch(tr.getRest(), choice); 
    }
    catch(IllegalUseOfTraversalException e){
      System.out.println("Illegal traversal: " + e.getMessage());
      return 0;
    }
  }

Last modified: Wednesday, March 19th, 2008 10:51:24am