-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.3.rkt
226 lines (195 loc) · 5.92 KB
/
3.3.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#lang sicp
(define (last-pair x)
(if (null? (cdr x)) x (last-pair (cdr x))))
(define (append! x y)
(set-cdr! (last-pair x) y)
x)
;(define x (list 'a 'b))
;(define y (list 'c 'd))
;(define z (append x y))
;z
;(cdr x)
;(define w (append! x y))
;w
;(cdr x)
(define (make-cycle x)
(set-cdr! (last-pair x) x)
x)
(define (mystery x)
(define (loop x y)
(if (null? x)
y
(let ((temp (cdr x)))
(set-cdr! x y)
(loop temp x))))
(loop x '()))
;(define v (list 'a 'b 'c 'd))
;(define w (mystery v))
;Ex 3.16
;(list 'a 'b 'c) ;Returns 3; Actually 3
(define x (cons 'b '()))
;(cons 'a (cons x x));Returns 4; Actually 3
(define x2 (cons 'a 'b))
(define x1 (cons x2 x2))
;(cons x1 x1);Returns 7; Actually 3
(define x4 (cons 'c '()))
(define x3 (cons 'a (cons 'b x4)))
(set-cdr! x4 x3)
(define (count-pairs-wrong x)
(if (not (pair? x))
0
(+ (count-pairs-wrong (car x))
(count-pairs-wrong (cdr x))
1)))
;Ex 3.17
(define (contains list p)
(cond ((null? list) #f)
((eq? (car list) p) #t)
(else (contains (cdr list) p))))
(define (count-pairs x)
(define pairs '())
(define (add p) (set! pairs (cons p pairs)))
(define (traverse node)
(if (or (not (pair? node)) (contains pairs node))
0
(begin (add node)
(+ (traverse (car node))
(traverse (cdr node))
1))))
(traverse x))
;Ex 3.18
;Only cdrs
(define (list-cycles? list)
(define pairs '())
(define (add p) (set! pairs (cons p pairs)))
(define (cdr-it pair)
(if (not (pair? pair)) #f
(if (contains pairs pair) #t
(begin (add pair)
(cdr-it (cdr pair))))))
(cdr-it list))
;Supports car's
(define (list-structure-cycles? list)
(define pairs '())
(define (add p) (set! pairs (cons p pairs)))
(define (has-cycle? pair history)
(if (not (pair? pair)) #f
(if (contains history pair) #t
(let ((car-result (has-cycle? (car pair) (cons pair history))))
(if car-result #t
(has-cycle? (cdr pair) (cons pair history));(add pair)
)))))
(has-cycle? list '()))
(define t1 (cons 'a 'b))
(define t2 (cons t1 t1))
(define x5 '(a b c))
(define y '(d e f))
(set-car! (cdr x5) y)
(set-car! x5 (cdr x5))
(set-cdr! (last-pair y) (cdr y))
;Ex 3.19
;This takes successive cdrs and checks the previous pairs of the original list.
;It uses a counter to only check the previous pairs up to the current one
;It uses the original list so it doesn't create any new lists
(define (list-cycles-o1-space?-wrong list)
;(define pairs '())
;(define (add p) (set! pairs (cons p pairs)))
(define (contains p n)
(define (loop seq i)
(if (>= i n) #f
(if (eq? p seq) #t
(loop (cdr seq) (+ i 1)))))
(loop list 0))
(define (iter pair n)
(if (not (pair? pair)) #f
(if (contains pair n) #t
(iter (cdr pair) (+ n 1)))))
(iter list 0))
;Uses the rabbit and turtle algorithm, george floyd.
;Move the turtle once and the rabbit twice.
(define (list-cycles-o1-space? list)
(define (loop slow fast)
(cond ((not (pair? fast)) #f)
((not (pair? slow)) #f)
((eq? slow fast) #t)
((not (pair? (cdr fast))) #f);Check if there are two pairs to move to
((eq? slow (cdr fast)) #t);This might make the algorithm faster a bit.
(else (loop (cdr slow) (cddr fast)))))
(loop list (cdr list)))
(define (listmaker n) (if (= n 0) '() (cons n (listmaker (- n 1)))))
(define x6 (listmaker 100000))
(set-cdr! (last-pair x6) x6)
(define y1 '(1 2 3 4 5 6 7 8))
(set-cdr! (cdddr (cddddr y1)) (cdddr y1))
(define z '(1))
(set-cdr! z z)
;From CS-61A-Week10 solutions; Berkeley university
(define (cycle? lst)
(define (subq? x list)
(cond ((null? list) #f)
((eq? x list) #t)
(else (subq? x (cdr list)))))
(define (iter lst pairlist pairlist-tail)
(cond ((not (pair? lst))
(set-cdr! pairlist-tail lst)
#f)
((subq? lst pairlist)
(set-cdr! pairlist-tail lst)
#t)
(else
(let ((oldcdr (cdr lst)))
(set-cdr! pairlist-tail lst)
(set-cdr! lst '())
(iter oldcdr pairlist lst) ))))
(cond ((null? lst) #f)
(else (let ((oldcdr (cdr lst)))
(set-cdr! lst '())
(iter oldcdr lst lst)))))
;From http://community.schemewiki.org/?sicp-ex-3.19
(define (cycles? x)
(let ((rev (mystery x)))
(if (eq? x rev) #t
(begin
(mystery rev)
#f))))
;To support arbitrary list structures, we need to.... ???? \>-</
(define (has-cycle? tree)
;; Helpers
(define (iterator value idx)
(cons value idx))
(define (update-iterator it value idx)
(set-car! it value)
(set-cdr! it idx))
(define (iterator-id it)
(cdr it))
(define (iterator-value it)
(car it))
(define (iterator-same-pos? it1 it2)
(eq? (iterator-id it1) (iterator-id it2)))
(define (iterator-eq? it1 it2)
(and (iterator-same-pos? it1 it2)
(eq? (iterator-value it1) (iterator-value it2))))
;; slow-it - tracks each node (1, 2, 3, 4...)
;; fast-it - tracks only even nodes (2, 4...)
(let ((slow-it (iterator tree 0))
(fast-it (iterator '() 0))
(clock-cnt 0))
(define (dfs root)
(if (not (pair? root))
false
(begin
(set! clock-cnt (+ clock-cnt 1))
(if (and (even? clock-cnt)
(iterator-same-pos? slow-it fast-it))
(update-iterator slow-it root clock-cnt))
(if (even? clock-cnt)
(update-iterator fast-it root
(+ (iterator-id fast-it) 1)))
(if (iterator-eq? slow-it fast-it)
true
(or (dfs (car root))
(dfs (cdr root)))))))
(dfs tree)))
;This example breaks above algorithm
(define r-inf (list 'a 'b 'c 'd))
(set-cdr! (cdddr r-inf) r-inf)