The procedure for-each

The procedure for-each is similar to map. It takes as arguments a procedure and a list of elements. However, rather than forming a list of the results, for-each just applies the procedure to each of the elements in turn, from left to right. The values returned by applying the procedure to the elements are not used at all -- for-each is used with procedures that perform an action, such as printing. For example,

(for-each (lambda (x) (newline) (display x))
          (list 57 321 88))
57
321
88

The value returned by the call to for-each (not illustrated above) can be something arbitrary, such as true. Give an implementation of my-for-each.


There are no comments yet.

Authentication required

You must log in to post a comment.

Login
(define (example-print example-list out)
  (my-for-each (lambda (x) (newline) (display x out)) example-list))

                      
(define op1 (open-output-string))
(example-print (list 1 2 3) op1)
(define display-result (get-output-string op1))
(check-equal? display-result "123")
(close-output-port op1)

(define op2 (open-output-string))
(example-print (list 57 321 88) op2)
(define display-result2 (get-output-string op2))
(check-equal? display-result2 "5732188")
(close-output-port op2)