-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.sch
101 lines (92 loc) · 2.5 KB
/
list.sch
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
(define (last-pair list)
(let ((remaind-list (cdr list)))
(if (null? remaind-list)
(car list)
(last-pair remaind-list))))
(define (reverse list)
(define (recur remaind-list)
(if (null? remaind-list)
'()
(cons (car remaind-list)
(recur (cdr remaind-list)))))
(define (iter remaind-list new-list)
(if (null? remaind-list)
new-list
(iter (cdr remaind-list)
(cons (car remaind-list) new-list))))
;; (iter list '())
(recur list))
(define us-coins (list 50 25 10 5 1))
(define uk-coins (list 100 50 20 10 5 2 1 0.5))
(define (count-change amount coin-list)
(define (no-more? coin-values)
(null? coin-values))
(define (except-first-denomination coin-values)
(cdr coin-values))
(define (first-denomination coin-values)
(car coin-values))
(define (cc amount coin-values)
(cond ((= amount 0) 1)
((or (< amount 0)
(no-more? coin-values))
0)
(else
(+ (cc amount
(except-first-denomination coin-values))
(cc (- amount
(first-denomination coin-values))
coin-values)))))
(cc amount coin-list))
(count-change 28 uk-coins)
(define (filter f list)
(define (recur list)
(if (null? list)
list
(let ((next (car list))
(other (cdr list)))
(if (f next)
(cons next (recur other))
(recur other)))))
(define (iter old new)
(if (null? old)
new
(let ((next (car old))
(other (cdr old)))
(iter other
(if (f next)
(cons next new)
new)))))
;; (iter list '())
(recur list))
(define (same-parity . number-list)
(let* ((first-number (car number-list))
(indicator (remainder first-number 2)))
(filter
(lambda (x)
(= indicator (remainder x 2)))
number-list)))
(define (show-rest . rest)
(newline)
(display rest))
(define (my-map f list)
(define (iter old new)
(if (null? old)
new
(iter (cdr old)
(cons (f (car old))
new))))
(define (recur list)
(if (null? list)
list
(cons (f (car list))
(recur (cdr list)))))
;; (reverse (iter list '()))
(recur list))
(define (for-each f list)
(define (iter other)
(if (null? other)
other
(begin
(f (car other))
(iter (cdr other)))))
(iter list))