-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurgery
369 lines (297 loc) · 10.8 KB
/
surgery
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
241029 火
List Surgery
Lists in the real world
-------------------------------------------
____________________________
| |
| 1. Groceries |
| 2. Bank |
| 3. Book store |
| 4. Dry cleaning |
| 5. Book store |
| |
| |
| |
----------------------------
Forgot an item?
Copy list to new paper. Insert new item.
____________________________ ____________________________
| | | |
| 1. Groceries | | 1. Groceries |
| 2. Bank | | 2. Bank |
| 3. Book store | | 3. Bakery |
| 4. Dry cleaning | --\ | 4. Book store |
| 5. Book store | --/ | 5. Dry cleaning |
| | | 6. Book store |
| | | |
| | | |
---------------------------- | |
----------------------------
Alternatively, rip list. Splice in new line(s).
Renumber items.
____________________________ ____________________________
| | | |
| 1. Groceries | | 1. Groceries |
| 2. Bank | | 2. Bank |
vvvvvvvvvvvvvvvvvvvvvvvvvvvv |//////////////////////////|
| 3. Bakery |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --\ |//////////////////////////|
| 3. Bakery | --/ | X4. Book store |
vvvvvvvvvvvvvvvvvvvvvvvvvvvv | X5. Dry cleaning |
| X6. Book store |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
| 3. Book store | | |
| 4. Dry cleaning | | |
| 5. Book store | ----------------------------
| |
| |
| |
----------------------------
Lisp Lists
-------------------------------------------
Often easier to perform non-destructive operation:
(let ((errands (list "Groceries" "Bank" "Book store" "Dry cleaning" "Book store")))
(append (subseq errands 0 2) (cons "Bakery" (subseq errands 2))))
("Groceries" "Bank" "Bakery" "Book store" "Dry cleaning" "Book store")
Even easier with our tools:
(let ((errands (list "Groceries" "Bank" "Book store" "Dry cleaning" "Book store")))
(multiple-value-bind (before after) (take-drop 2 errands)
(append before (cons "Bakery" after))))
("Groceries" "Bank" "Bakery" "Book store" "Dry cleaning" "Book store")
Below we examine destructive splicing.
Non-destructive
"Update" a CONS cell
(defvar *c* (cons 1 2))
(1 . 2)
[*|*]
| |
v v
1 2
(defvar *d* *c*) => *D*
(eq *c* *d*) => T
(cons 3 (cdr *c*)) => (3 . 2)
Same CDR, new CONS cell
[*|*]
| |
v v
3 2
*c* => (1 . 2)
(cons (car *c*) 4) => (1 . 4)
Same CAR, new CONS cell
[*|*]
| |
v v
1 4
Destructive modification of CONS cell
- Old school: RPLACA/RPLACD
(rplaca *c* 3) => (3 . 2)
Same CONS cell, new CAR
*c* => (3 . 2)
[*|*]
/x |
/ x v
3 X 2
(rplacd *c* 4) => (3 . 4)
Same CONS cell, new CDR
*c* => (3 . 4)
[*|*]
| x\
v x \
3 X 4
(eq *c* *d*) => T
*d* => (3 . 4)
- Common Lisp: SETF CAR/CDR (or FIRST/REST)
(setf (car *c*) 3)
(setf (cdr *c*) 4)
Changing value of existing CONS is easy.
-------------------------------------------
(substitute :bar :foo '(a b :foo c d :foo e :foo f))
(A B :BAR C D :BAR E :BAR F)
(substitute :bar :foo '(a b :foo c d :foo e :foo f) :count 1)
(A B :BAR C D :FOO E :FOO F)
(substitute :bar :foo '(a b :foo c d :foo e :foo f) :start 4)
(A B :FOO C D :BAR E :BAR F)
(substitute-if :even #'evenp '(0 1 2 3 4 5))
(:EVEN 1 :EVEN 3 :EVEN 5)
(mapcar #'(lambda (elt) (if (evenp elt) (1+ elt) elt)) '(0 1 2 3 4 5))
(1 1 3 3 5 5)
Destructive NSUBSTITUTE/NSUBSTITUTE-IF
(defvar *l* (list 'a 'b :foo 'c 'd :foo 'e :foo 'f))
*l* => (A B :FOO C D :FOO E :FOO F)
(nsubstitute :bar :foo *l*) => (A B :BAR C D :BAR E :BAR F)
*l* => (A B :BAR C D :BAR E :BAR F)
(defvar *l* (list 0 1 2 3 4 5))
*l* => (0 1 2 3 4 5)
(mapl #'(lambda (l) (when (evenp (first l)) (incf (first l)))) *l*) => (1 1 3 3 5 5)
*l* => (1 1 3 3 5 5)
Adding/removing CONS is harder
-------------------------------------------
(defvar *l1* (list 1 2 3 4 5))
(defvar *l2* (rest *l1*))
*l1* => (1 2 3 4 5)
*l2* => (2 3 4 5)
(push :one *l2*) => (:ONE 2 3 4 5)
*l2* => (:ONE 2 3 4 5)
*l1* => (1 2 3 4 5)
[*|*]
| \
v \
:ONE \
\
\
v
[*|*]--->[*|*]--->[*|*]--->[*|*]--->[*|*]--->NIL
| | | | |
v v v v v
1 2 3 4 5
(push :x (rest *l2*)) => (:X 2 3 4 5)
*l1* => (1 2 3 4 5)
*l2* => (:ONE :X 2 3 4 5)
(push :y (cdddr *l1*)) => (:Y 4 5)
*l1* => (1 2 3 :Y 4 5)
*l2* => (:ONE :X 2 3 :Y 4 5)
(macroexpand-1 '(push :x (rest *l2*)))
(LET* ((#:*L2*637 *L2*))
(SB-KERNEL:%RPLACD #:*L2*637 (CONS :X (REST #:*L2*637))))
T
List surgery
-------------------------------------------
- Splice (add CONS)
- At current node
- After current node
- Snip (remove CONS)
- Arbitrary CONS
- Tail of chain
Easy to splice in a new CONS _after_ the one we are currently pointing to.
(setf *l1* (list 1 2 3 4 5))
(setf *l2* (rest *l1*))
*l1* *l2*
| |
v v
[*|*]--->[*|*]--->[*|*]--->[*|*]--->[*|*]--->NIL
| | | | |
v v v v v
1 2 3 4 5
(setf (cdr *l2*) (cons 2.5 (cdr *l2*)))
(2.5 3 4 5)
*l1* *l2* >[*|*]
| | / | \
v v / v \
[*|*]--->[*|*] 2.5 v
| | [*|*]--->[*|*]--->[*|*]--->NIL
v v | | |
1 2 v v v
3 4 5
[*|*]--->[*|*]--->[*|*]--->[*|*]--->[*|*]--->[*|*]--->NIL
| | | | | |
v v v v v v
1 2 2.5 3 4 5
*l1* => (1 2 2.5 3 4 5)
*l2* => (2 2.5 3 4 5)
Trickier when trying to splice _at_ the current CONS. Must overwrite current
CONS so that anything pointing to it sees change:
(setf *l1* (list 1 2 3 4 5))
(setf *l2* (rest *l1*))
*l1* *l2*
| |
v v
[*|*]--->[*|*]--->[*|*]--->[*|*]--->[*|*]--->NIL
| | | | |
v v v v v
1 2 3 4 5
(setf (cdr *l2*) (cons (car *l2*) (cdr *l2*))
(car *l2*) 1.5)
*l1* *l2* >[*|*]
| | / | \
v v / | \
[*|*]--->[*|*] | >[*|*]--->[*|*]--->[*|*]--->NIL
| | | | | |
v v | v v v
1 2 <----+ 3 4 5
*l1* *l2* >[*|*]
| | / | \
v v / | \
[*|*]--->[*|*] | >[*|*]--->[*|*]--->[*|*]--->NIL
| | | | | |
v v v v v v
1 1.5 2 3 4 5
[*|*]--->[*|*]--->[*|*]--->[*|*]--->[*|*]--->[*|*]--->NIL
| | | | | |
v v v v v v
1 1.5 2 3 4 5
*l1* => (1 1.5 2 3 4 5)
*l2* => (1.5 2 3 4 5)
Most snipping is easy:
(setf *l1* (list 1 2 3 4 5))
(setf *l2* (rest *l1*))
*l1* *l2*
| |
v v
[*|*]--->[*|*]--->[*|*]--->[*|*]--->[*|*]--->NIL
| | | | |
v v v v v
1 2 3 4 5
(setf (car *l2*) (cadr *l2*)
(cdr *l2*) (cddr *l2*))
*l1* *l2*
| |
v v
[*|*]--->[*|*]--->[*|*]--->[*|*]--->[*|*]--->NIL
| x\ | | |
v x \ v v v
1 X ----->3 4 5
*l1* *l2* -----------
| | / \
v v / v
[*|*]--->[*|*] [*|*]--->[*|*]--->[*|*]--->NIL
| x\ | | |
v x \ v v v
1 X ----->3 4 5
[*|*]--->[*|*]--->[*|*]--->[*|*]--->NIL
| | | |
v v v v
1 3 4 5
*l1* => (1 3 4 5)
*l2* => (3 4 5)
;;;
;;; Destructively splice OBJ as the Ith element of L.
;;; In other words, the existing Ith and further elements are pushed down by one element.
;;;
(defun nsplice-before (l i obj)
(cond ((null l) (error "Bad index to splice."))
((zerop i) (setf (cdr l) (cons (car l) (cdr l))
(car l) obj))
(t (nsplice-before (cdr l) (1- i) obj))))
;;;
;;; Destructively splice OBJ after Ith element of L.
;;;
(defun nsplice-after (l i obj)
(cond ((null l) (error "Bad index to splice."))
((zerop i) (setf (cdr l) (cons obj (cdr l))))
(t (nsplice-after (cdr l) (1- i) obj))))
;;;
;;; Special case when elt to be removed is final elt. Must set CDR of previous CONS to NIL.
;;;
(defun nsnip (l i)
(cond ((null l) (error "Bad index to snip."))
((and (= i 1) (null (cddr l))) (setf (rest l) nil))
((zerop i) (setf (first l) (second l) ; Copy CAR/CDR of 2nd CONS to this CONS. Thus 1st CONS is copy of 2nd. 2nd CONS becomes redundant (1st CONS circumvents it, points to 3rd CONS.)
(rest l) (cddr l)))
(t (nsnip (rest l) (1- i)))) )
(deftest test-nsplice-before ()
(check
(let ((l (list 10 20 30 40))) (nsplice-before l 2 25) (equal l '(10 20 25 30 40)))
(let ((l (list 10 20 30 40))) (nsplice-before l 0 3) (equal l '(3 10 20 30 40)))
(let ((l (list 80))) (nsplice-before l 0 70) (equal l '(70 80)))) )
(deftest test-nsplice-after ()
(check
(let ((l (list 10 20 30 40))) (nsplice-after l 2 35) (equal l '(10 20 30 35 40)))
(let ((l (list 10 20 30 40))) (nsplice-after l 0 13) (equal l '(10 13 20 30 40)))
(let ((l (list 80))) (nsplice-after l 0 81) (equal l '(80 81)))) )
(deftest test-nsnip ()
(check
(let ((l (copy-list '(a b c d e)))) (nsnip l 0) (equal l '(b c d e)))
(let ((l (copy-list '(a b c d e)))) (nsnip l 1) (equal l '(a c d e)))
(let ((l (copy-list '(a b c d e)))) (nsnip l 2) (equal l '(a b d e)))
(let ((l (copy-list '(a b c d e)))) (nsnip l 3) (equal l '(a b c e)))
(let ((l (copy-list '(a b c d e)))) (nsnip l 4) (equal l '(a b c d)))) )