next up previous contents index
Next: Set Operations Up: Higher Order Functions Previous: Searching for an element   Contents   Index


Iterating through all elements of a list: `for-each'

This abstraction traverses through a list executing a certain procdure which is passed the current element of the list as an argument.

This function is an exception to the aforementioned functional programming style, having as its objective to cause side-effects and not to return evaluated expressions.

Such side-effects may be to display data on the screen or to send them to printer. It is obvious that procedures like this, applying the imperative programming style are sometimes needed and important as well.


\begin{lstlisting}[language=Scm]{}
(define for-each
(lambda(procedure lis)
(if...
...()
(procedure (car lis))
(for-each procedure (cdr lis) ))))))
\end{lstlisting}


\begin{lstlisting}[language=Scm]{}
(define l1 (cons seven
(cons eight
(cons ...
...
(cons ten
nil)))))
;
(for-each ndisp! l1)
--> 7
8
9
10
\end{lstlisting}



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