Sec. 5.3, Solutions by Jason Brudvik

Problem 1
• • Construct a Java function which will test whether an element occurs in an argument list.

static int member( List L, int N)
  {
    while(!isEmpty(L))
    {
     if(first(L) == N)
     return 1;

     return member(rest(L),N);
    }
   return 0;
  }

Problem 2
• • Construct a Java function which will add an element onto the end of a list, returning a totally new list
(leaving the original intact).

static List atEnd(List L, int N)
  {
    if(isEmpty(L))
    return cons(N, null);   // so that a list is returned

    return cons(first(L),atEnd(rest(L),N));
  }

Problem 7
• • • Construct a Java function which will return, from a sorted list, a sorted list of the same elements with
no duplicates.

static List noDup(List L)
  {
    if(rest(L) == null)
    return cons(first(L), null); // so that a list will be returned

    if(first(L) == second(L))
    return noDup(rest(L));

    else
    return cons(first(L),noDup(rest(L)));
  }