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


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.


\begin{lstlisting}[language=Scm]{}
(define nth
(lambda (n l)
(if (equaln n one)
(car l)
(nth (sub n one) (cdr l)))))
\end{lstlisting}

\begin{lstlisting}[language=Scm]{}
(define l1 (cons one (cons three (cons four nil))))
;
(ldisp! (nth two l1))
--> 3
\end{lstlisting}


domain access counter Georg P. Loczewski 2004-03-05