next up previous contents
Next: Extended Numerical Abstractions Up: Basic utility abstractions for Previous: Removing an element from   Contents

Retrieving the n-th element from a list: `nth'

A list can be used like an array in other programming languages by using the abstraction `nth' which extracts an element from a list by using an index pointing to its position.

(define nth
   (lambda (n l)
      (if (equaln n one)
         (car l)
         (nth (sub n one) (cdr l)))))

(define l1 (cons one (cons three (cons four nil))))
(ldisp! (nth two l1))
                             --> 3

Georg P. Loczewski 2004-03-05

Impressum und Datenschutz