-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.2.1-representing-sequences.rkt
104 lines (82 loc) · 2.39 KB
/
2.2.1-representing-sequences.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
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
102
103
#lang sicp
; exercise 2.17
(define (last-pair xs)
(let ((tail (cdr xs))
(head (car xs)))
(if (null? tail)
(list head)
(last-pair tail))))
(last-pair (list 1 2 3)) ; '(3)
(last-pair (list 1)) ; '(1)
; exercise 2.18
(define (reverse xs)
(define (inner xs acc)
(if (null? xs)
acc
(inner (cdr xs) (cons (car xs) acc))))
(inner xs '()))
(reverse (list 1 2 3 4)) ; '(4 3 2 1)
; exercise 2.19
; from 1.2.2
(define us-coins (list 25 10 5 50 1))
(define uk-coins (list 100 50 20 10 5 2 1 0.5))
(define (first-denomination coin-values)
(car coin-values))
(define (except-first-denomination coin-values)
(cdr coin-values))
(define (no-more? coin-values)
(null? coin-values))
(define (cc amount coin-values)
(cond ((zero? amount) 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 100 us-coins) ; 292
; exercise 2.20
(define (same-parity x . xs)
(define (parity x) (remainder x 2))
(define (accumulate xs)
(cond ((null? xs) '())
((= (parity x) (parity (car xs)))
(cons (car xs) (accumulate (cdr xs))))
(else (accumulate (cdr xs)))))
(cons x (accumulate xs)))
(same-parity 1 2 3 4 5 6 7) ; '(1 3 5 7)
(same-parity 2 3 4 5 6 7) ; '(2 4 6)
; exercise 2.21
(define (map f xs)
(if (null? xs)
'()
(cons (f (car xs))
(map f (cdr xs)))))
(map abs (list -10 2.5 -11.6 17)) ; '(10 2.5 11.6 17)
(define (square x) (* x x))
(define (square-list1 xs)
(if (null? xs)
'()
(cons (square (car xs)) (square-list1 (cdr xs)))))
(square-list1 (list 1 2 -3 5)) ; '(1 4 9 25)
(define (square-list xs)
(map square xs))
(square-list (list 1 2 -3 5)) ; '(1 4 9 25)
; exercise 2.22
; The elements come out reversed in an iterative version because
; cons can only prepend items to a given list. Thus, iterating
; from the beginning of a list yields the first element at the
; end of the result, the second before that, and so on.
; Reversing the order of arguments to cons doesn't work because
; cons has type:
; cons :: T -> [T] -> [T]
; so reversing the order of the arguments builds up a collection
; of nested lists: (((cons 1 . '()) . 2) . 3)
; exercise 2.23
(define (for-each f xs)
(if (null? xs)
true
((lambda ()
(f (car xs))
(for-each f (cdr xs))))))
(for-each (lambda (x) (newline) (display x))
(list 1 2 3 "a"))