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


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.


(define for-each
  (lambda(procedure lis)
    (if (nullp lis) 
         true
         ((lambda()
            (procedure (car lis))
            (for-each procedure (cdr lis) ))))))


(define l1 (cons seven 
                  (cons eight 
                         (cons nine 
                                (cons ten 
                                       nil)))))
;
(for-each ndisp! l1)
                             --> 7
                                 8
                                 9
                                 10



Georg P. Loczewski 2004-03-05


Impressum und Datenschutz