I read chapter 3 on and off, and I think scheme is a little getting started. However, if I don't practice for a long time, my mind can't adapt to functional programming. I think 3.17, 3.18, and 3.19 are quite interesting, post your own solutions.
3.17
(define (count-unique-pair X)
(LET (dB (cons 0 0)
(define (count-pair X)
(define (add-unique-pair x db1)
(define (add-record db0 X)
(set-car! Db0 X)
(set-CDR! Db0 (cons 0 0)
X)
(define (find-record x db0)
(if (not (pair? (Car db0)
(add-record db0 X)
(if (EQ? X (CAR db0)
'()
(find-record X (CDR db0)
(find-record x db1 ))
(if (not (pair? X)
0
(if (null? (Add-unique-pair x dB)
0
(+ (count-pair (car x)
(count-pair (cdr x ))
1)
(count-pair X)
(Define XX '(a B ))
(Define Z1 (cons XX ))
(Define Z2 (cons '(a B )))
(Count-unique-pair Z1)
The implementation is ugly, basically procedural programming. We use a dB to store pair that has already been traversed. We can deal with a table with loops, and as long as the pair has already been traversed, the sub-pair is not traversed. Compared with pure functional writing, this method is clear and easy to understand, but there is a memory competition problem and it cannot be run in parallel.
3.18
This question is simplified. You don't need to consider the car ring. It's okay to use the 3.17 storage method.
3.19
This requires a small trick and a space constant, which means that the pair pointer that has already been traversed cannot be stored. So I will change the space by time and use two pointers. The a pointer will step one pair at a time, the B pointer Step 2 pair at a time. If there is a ring, the B pointer will catch up with the pointer again. You can check whether the pointer is equal at each step to see if it has a ring :)