-
Notifications
You must be signed in to change notification settings - Fork 5
/
2.33.scm
executable file
·55 lines (40 loc) · 1.24 KB
/
2.33.scm
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
55
(load "myhelpers.scm")
;Set up
(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))
;Exercise 2.33
(define (map p sequence)
(accumulate (lambda (current accumulated-so-far)
(cons (p current) accumulated-so-far)); This is very similar to the last line of map
nil
sequence))
;Test set up (to test MAP)
;FROM Ex 2.30 -- Using map in other function to SEE IF MAP WORKS!
(define (square-tree-map tree)
(map (lambda (sub-tree)
(if (atom? sub-tree)
(square sub-tree)
(square-tree-map sub-tree)))
tree))
(define l (list 1
(list 2 (list 3 4) 5)
(list 6 7)))
(define answer '(1 (4 (9 16) 25) (36 49)))
;Test
(d 'Map:)
(assert (square-tree-map l) answer)
;Exercise 2.33 (CONTINUED)
(define (append seq1 seq2)
(accumulate cons seq2 seq1))
(d 'Append)
(assert (append (list 1 2) (list 3 4)) '(1 2 3 4))
(define (length sequence)
(accumulate (lambda (bleh accumulated-so-far) ;Bleh is a throw away variable
(inc accumulated-so-far))
0
sequence))
(d 'Length)
(assert (length (list 1 2 3 4)) 4)