Other List Functions

(list? x ) tests whether x is a proper list, i.e., either () or a pair whose cdr is a proper list.

(list-tail lst k ) returns the sublist of lst obtained by omitting the first k elements. A similar function (nthcdr k lst) is defined in Common Lisp.


(define (list-tail1 lst k)
  (if (<= k 0)
      lst
      (if (pair? lst)
          (list-tail1 (cdr lst) (1- k)) ) ) )

> (list-tail1 '(a b c d) 2) (c d)

(list-ref lst k ) returns the kth element of lst. A similar function (nth k lst) is defined in Common Lisp.


(define (list-ref1 lst k)
  (car (list-tail1 lst k)))

> (list-ref1 '(a b c d) 2) c

Contents    Page-10    Prev    Next    Page+10    Index