Blame


1 665c255d 2023-08-04 jrmu ;; 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,
2 665c255d 2023-08-04 jrmu
3 665c255d 2023-08-04 jrmu (define (for-each proc items)
4 665c255d 2023-08-04 jrmu (if (null? items)
5 665c255d 2023-08-04 jrmu #t
6 665c255d 2023-08-04 jrmu (and (proc (car items))
7 665c255d 2023-08-04 jrmu (for-each proc (cdr items)))))
8 665c255d 2023-08-04 jrmu
9 665c255d 2023-08-04 jrmu (define (test-case actual expected)
10 665c255d 2023-08-04 jrmu (load-option 'format)
11 665c255d 2023-08-04 jrmu (newline)
12 665c255d 2023-08-04 jrmu (format #t "Actual: ~A Expected: ~A" actual expected))
13 665c255d 2023-08-04 jrmu
14 665c255d 2023-08-04 jrmu (for-each (lambda (x) (newline) (display x))
15 665c255d 2023-08-04 jrmu (list 57 321 88))
16 665c255d 2023-08-04 jrmu (newline)
17 665c255d 2023-08-04 jrmu (display 57)
18 665c255d 2023-08-04 jrmu (newline)
19 665c255d 2023-08-04 jrmu (display 321)
20 665c255d 2023-08-04 jrmu (newline)
21 665c255d 2023-08-04 jrmu (display 88)
22 665c255d 2023-08-04 jrmu
23 665c255d 2023-08-04 jrmu