Contents    Page-10    Prev    Next    Page+10    Index   

Tail Recursive Merge in Java


public static Cons merjtr (Cons x, Cons y) {
    return nreverse(merjtrb(x, y, null)); }

public static Cons merjtrb (Cons x, Cons y,
                            Cons result) {
 if ( x == null )
   if ( y == null )
      return result;
      else return merjtrb(x, rest(y),
                          cons(first(y), result));
 else if ( ( y == null ) ||
           ((Comparable) first(x))
                      .compareTo(first(y)) < 0 )
           return merjtrb(rest(x), y,
                          cons(first(x), result));
      else return merjtrb(x, rest(y),
                          cons(first(y), result));}