sicp 2.23

Exercise 2.23.  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

 

 

使用了begin,不知道是不是必须的。

 

(define (for-each apply-function items)
  (if (null? items)
      #t
      (begin (apply-function (car items))
             (for-each apply-function (cdr items)))))

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

 

57

321

88#t

你可能感兴趣的:(SICP)