Adding functions such as

append

reverse

range


static List append(List A, List B)

  {

  if( isEmpty(A) )

    return B;

  return cons(first(A),append(rest(A),B));

  }


  append([],B) Þ B;

  append([X|A],B) Þ [X|append(A,B)];

                           Î cons


static List reverse(List L)

  {

  List R = nil;

  while( !isEmpty(L) )

    {

    R = cons(first(L),R);

    L = rest(L);

    }

  return R;

  }

 


range(M,N) ==> [M,M+1,M+2,...,N]

static List range(int M, int N)

  {

  if( M > N)

    return nil;

  return cons(M,range(M+1,N));

  }


  range(M,N) Þ M > N ? [ ];

                  Î guard

  range(M,N) Þ [M | range(M+1,N)];


alt version of range

static List range(int M, int N)

  {

  List R = nil;  //return value

  for(int I=M; I >= M; I--)

    {

    R = cons(I,R);

    }

  return R;

  }

 

To Next Slide To Previous Slide To Contents