-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exercise 3.12 append and append!.rkt
120 lines (99 loc) · 2.36 KB
/
Exercise 3.12 append and append!.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
#lang racket
; Exercise 3.12. The following procedure for appending lists was introduced in section 2.2.1:
; (define (append x y)
; (if (null? x)
; y
; (cons (car x) (append (cdr x) y))))
; Append forms a new list by successively consing the elements of x onto y. The procedure append!
; is similar to append, but it is a mutator rather than a constructor. It appends the lists by
; splicing them together, modifying the final pair of x so that its cdr is now y.
; (It is an error to call append! with an empty x.)
; (define (append! x y)
; (set-cdr! (last-pair x) y)
; x)
; Here last-pair is a procedure that returns the last pair in its argument:
; (define (last-pair x)
; (if (null? (cdr x))
; x
; (last-pair (cdr x))))
; Consider the interaction
; (define x (list 'a 'b))
; (define y (list 'c 'd))
; (define z (append x y))
; z
; (a b c d)
; (cdr x)
; <response>
; (define w (append! x y))
; w
; (a b c d)
; (cdr x)
; <response>
; What are the missing <response>s? Draw box-and-pointer diagrams to explain your answer.
; S O L U T I O N
(require rnrs/mutable-pairs-6)
(require compatibility/mlist)
(define (append x y)
(if (null? x)
y
(mcons (mcar x) (append (mcdr x) y))
)
)
(define (append! x y)
(set-mcdr! (last-pair x) y)
x
)
(define (last-pair x)
(if (null? (mcdr x))
x
(last-pair (mcdr x))
)
)
; Test Driver
(define (run-test proc . args)
(define (print-item-list items first-time?)
(cond
((not (pair? items)) (void))
(else
(if (not first-time?)
(display ", ")
(void)
)
(print (car items))
(print-item-list (cdr items) false)
)
)
)
; (display "Running Test: ") (display (cons proc args)) (display " ")
; (newline)
(display "Applying ")
(display proc)
(display " on: ")
(print-item-list args true)
(newline)
(let ((result (apply proc args)))
(display "Result: ")
(display result)
(newline)
(print result)
(newline)
)
(newline)
)
; Tests
; Test Results
Welcome to DrRacket, version 6.11 [3m].
Language: racket, with debugging; memory limit: 512 MB.
> (define x (mlist 'a 'b))
> (define y (mlist 'c 'd))
> (define z (append x y))
> (display z)
{a b c d}
> (display (mcdr x))
{b}
> (define w (append! x y))
> (display w)
{a b c d}
> (display (mcdr x))
{b c d}
>