next up previous contents index
Next: Insertion Sort Up: Examples for recursion Previous: Calculating the faculty of   Contents   Index


Calculating the sum of elements of a list


\begin{lstlisting}[language=Scm]{}
(define sum
(lambda(l)
(if (nullp l)
zero
(add (car l) (sum (cdr l))))))
\end{lstlisting}


\begin{lstlisting}[language=Scm]{}
(define l1 (cons one (cons two (cons three nil))))
;
(ndisp! (sum l1))
--> 6
\end{lstlisting}


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