-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exercise 2.23 for-each.rkt
executable file
·54 lines (46 loc) · 1.39 KB
/
Exercise 2.23 for-each.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#lang racket
; 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
; The value returned by the call to for-each (not illustrated above) can be something
; arbitrary, such as true. Give an implementation of for-each.
; SOLUTION
(define (for-each proc items)
(cond
((not (null? items)) (proc (car items)) (for-each proc (cdr items)))
(else
(void)
)
)
)
; Tests
Welcome to DrRacket, version 6.11 [3m].
Language: racket, with debugging; memory limit: 128 MB.
> (for-each (lambda (x) (display (* x x)) (newline)) (list 57 321 88))
3249
103041
7744
> (for-each (lambda (x) (display (* x 2.0)) (newline)) (list 57 321 88))
114.0
642.0
176.0
> (for-each (lambda (x) (display (/ x 2.0)) (newline)) (list 57 321 88))
28.5
160.5
44.0
> (for-each (lambda (x) (newline) (display x)) (list 57 321 88))
57
321
88
> (for-each (lambda (x) (display x) (newline)) (list 57 321 88))
57
321
88
>